WebsiteService.php 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Ad;
  5. use App\Model\Category;
  6. use App\Model\AdPlace;
  7. use App\Model\Department;
  8. use App\Model\District;
  9. use App\Model\FooterCategory;
  10. use App\Model\FooterContent;
  11. use App\Model\WebsiteTemplateInfo;
  12. use App\Model\Link;
  13. use App\Model\LetterOfComplaint;
  14. use App\Model\TemplateClass;
  15. use App\Model\Template;
  16. use App\Model\User;
  17. use App\Model\UserInfo;
  18. use App\Model\WebsiteRole;
  19. use App\Model\WebsiteRoleUser;
  20. use App\Model\Website;
  21. use App\Model\WebsiteColumn;
  22. use Hyperf\DbConnection\Db;
  23. use Hyperf\RpcServer\Annotation\RpcService;
  24. use App\Tools\Result;
  25. use Carbon\Carbon;
  26. use App\Model\WebsiteCategory;
  27. use App\Model\WebsiteGroup;
  28. use App\Model\WebsiteTemplate;
  29. use App\Model\Sector;
  30. use PhpParser\Node\Stmt\Return_;
  31. use function PHPUnit\Framework\isNull;
  32. use Overtrue\Pinyin\Pinyin;
  33. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  34. class WebsiteService implements WebsiteServiceInterface
  35. {
  36. /**
  37. * 获取网站列表
  38. * @param string $keyword
  39. * @param int $page
  40. * @param int $pageSize
  41. * @return array
  42. */
  43. public function getWebsitetList(array $data): array
  44. {
  45. // 构建查询
  46. $query = Website::when($data, function ($query) use ($data) {
  47. if (isset($data['keyword']) && !empty($data['keyword'])) {
  48. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  49. }
  50. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  51. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  52. }
  53. if (isset($data['city_id']) && !empty($data['city_id'])) {
  54. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  55. }
  56. });
  57. // 执行查询并获取总数和结果
  58. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  59. ->leftJoin("district", "district.id", "website.city_id");
  60. $count = $queryWithJoins->count();
  61. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  62. ->limit($data['pageSize'])
  63. ->offset(($data['page'] - 1) * $data['pageSize'])
  64. ->orderBy("website.updated_at", "desc")
  65. ->get();
  66. if (empty($result)) {
  67. return Result::error("没有数据", 0);
  68. }
  69. $data = [
  70. 'rows' => $result->toArray(),
  71. 'count' => $count,
  72. ];
  73. return Result::success($data);
  74. }
  75. /**
  76. * @param array $data
  77. * @return array
  78. */
  79. public function createWebsite(array $data): array
  80. {
  81. var_dump("网站数据:", $data);
  82. $insertData = [
  83. 'website_name' => $data['website_name'],
  84. 'logo' => $data['logo'] ?? '',
  85. 'website_url' => $data['website_url'] ?? '',
  86. // 'city_id'=>$data['city_id']??0,
  87. 'website_column_id' => $data['website_column_id'],
  88. 'title' => $data['title'] ?? '',
  89. 'keywords' => $data['keywords'] ?? '',
  90. 'description' => $data['description'] ?? '',
  91. 'status' => $data['status'] ?? 1,
  92. 'website_column_arr_id' => $data['website_column_arr_id'],
  93. // 'city_arr_id'=>$data['city_arr_id']??[0],
  94. 'template_id' => $data['template_id'] ?? 0,
  95. 'suffix' => $data['suffix'] ?? "",
  96. ];
  97. $result = Website::insertGetId($insertData);
  98. if (empty($result)) {
  99. return Result::error("创建失败", 0);
  100. } else {
  101. return Result::success(["id" => $result]);
  102. }
  103. }
  104. /**
  105. * @param int $id
  106. * @param array $data
  107. * @return array
  108. */
  109. public function updateWebsite(int $id, array $data): array
  110. {
  111. $insertData = [
  112. 'website_name' => $data['website_name'],
  113. 'logo' => $data['logo'] ?? '',
  114. 'website_url' => $data['website_url'] ?? '',
  115. // 'city_id'=>$data['city_id']??0,
  116. 'website_column_id' => $data['website_column_id'],
  117. 'title' => $data['title'] ?? '',
  118. 'keywords' => $data['keywords'] ?? '',
  119. 'description' => $data['description'] ?? '',
  120. // 'status' => $data['status'] ?? 0,
  121. 'website_column_arr_id' => $data['website_column_arr_id'],
  122. // 'city_arr_id'=>$data['city_arr_id']??[0],
  123. 'template_id' => $data['template_id'] ?? 0,
  124. 'suffix' => $data['suffix'] ?? "",
  125. ];
  126. var_dump("=========:", $insertData);
  127. $result = Website::where('id', $id)->update($insertData);
  128. var_dump("更新站点", $result);
  129. if (empty($result)) {
  130. return Result::error("更新失败", 0);
  131. } else {
  132. return Result::success();
  133. }
  134. }
  135. /**
  136. * @param int $id
  137. * @return array
  138. */
  139. public function delWebsite(int $id): array
  140. {
  141. $result = Website::where('id', $id)->delete();
  142. if (empty($result)) {
  143. return Result::error("删除失败", 0);
  144. } else {
  145. return Result::success();
  146. }
  147. }
  148. /**
  149. * @param int $id
  150. * @return array
  151. */
  152. public function getWebsiteInfo(int $id): array
  153. {
  154. $where = [
  155. ['website.id', '=', $id],
  156. ];
  157. $result = Website::where($where)
  158. ->leftJoin("template", "template.id", "website.template_id")
  159. ->select("website.*", "template.template_name", "template.template_img")
  160. ->first();
  161. if (empty($result)) {
  162. return Result::error("数据不存在", 0);
  163. } else {
  164. return Result::success($result->toArray());
  165. }
  166. }
  167. /**
  168. * 查询所有的站点栏目
  169. * @return array
  170. */
  171. public function getWebsiteColumn(array $data): array
  172. {
  173. $result = WebsiteColumn::where($data)->get();
  174. if (empty($result)) {
  175. return Result::error("数据不存在", 0);
  176. } else {
  177. return Result::success($result->toArray());
  178. }
  179. }
  180. /**
  181. * @param string $keyword
  182. * @param int $page
  183. * @param int $pageSize
  184. * @return array
  185. */
  186. public function getWebsiteColumnList(array $data): array
  187. {
  188. $where = [];
  189. if (isset($data['keyword']) && $data['keyword']) {
  190. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  191. }
  192. $result = WebsiteColumn::where($where)
  193. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  194. ->select("website_column.*", "wc.column_name as parent_column_name")
  195. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  196. $count = WebsiteColumn::where($where)->count();
  197. if (empty($result)) {
  198. return Result::error("没有数据", 0);
  199. }
  200. $data = [
  201. 'rows' => $result->toArray(),
  202. 'count' => $count,
  203. ];
  204. return Result::success($data);
  205. }
  206. /**
  207. * @param array $data
  208. * @return array
  209. */
  210. public function createWebsiteColumn(array $data): array
  211. {
  212. $insertData = [
  213. 'column_name' => $data['column_name'],
  214. 'pid' => $data['pid'] ?? '',
  215. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  216. 'sort' => $data['sort'] ?? 0,
  217. 'remark' => $data['remark'] ?? '',
  218. ];
  219. $result = WebsiteColumn::insertGetId($insertData);
  220. if (empty($result)) {
  221. return Result::error("创建失败", 0);
  222. } else {
  223. return Result::success(["id" => $result]);
  224. }
  225. }
  226. /**
  227. * @param int $id
  228. * @param array $data
  229. * @return array
  230. */
  231. public function updateWebsiteColumn(int $id, array $data): array
  232. {
  233. $insertData = [
  234. 'column_name' => $data['column_name'],
  235. 'pid' => $data['pid'] ?? '',
  236. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  237. 'sort' => $data['sort'] ?? 0,
  238. 'remark' => $data['remark'] ?? '',
  239. ];
  240. $result = WebsiteColumn::where('id', $id)->update($insertData);
  241. if (empty($result)) {
  242. return Result::error("更新失败", 0);
  243. } else {
  244. return Result::success();
  245. }
  246. }
  247. /**
  248. * @param int $id
  249. * @return array
  250. */
  251. public function delWebsiteColumn(int $id): array
  252. {
  253. $list = WebsiteColumn::where(['pid' => $id])->get();
  254. if ($list) {
  255. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  256. }
  257. $result = WebsiteColumn::where('id', $id)->delete();
  258. if (empty($result)) {
  259. return Result::error("删除失败", 0);
  260. } else {
  261. return Result::success();
  262. }
  263. }
  264. /**
  265. * @param string $keyword
  266. * @param int $page
  267. * @param int $pageSize
  268. * @return array
  269. */
  270. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  271. {
  272. $where = [
  273. ['role.role_name', 'like', '%' . $keyword . '%'],
  274. ['website_role.website_id', '=', $websiteId],
  275. ];
  276. $result = WebsiteRole::where($where)
  277. ->leftJoin("role", "role.id", "website_role.role_id")
  278. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  279. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  280. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  281. if (empty($result)) {
  282. return Result::error("没有数据", 0);
  283. }
  284. $data = [
  285. 'rows' => $result->toArray(),
  286. 'count' => $count,
  287. ];
  288. return Result::success($data);
  289. }
  290. /**
  291. * @param array $data
  292. * @return array
  293. */
  294. public function createWebsiteRole(array $data): array
  295. {
  296. $insertData = [
  297. 'website_id' => $data['website_id'],
  298. 'role_id' => $data['role_id'] ?? '',
  299. ];
  300. $info = WebsiteRole::where($insertData)->first();
  301. if ($info) {
  302. return Result::error("不能重复添加角色", 0);
  303. }
  304. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  305. $insertData['type'] = $data['type'] ?? '';
  306. $result = WebsiteRole::insertGetId($insertData);
  307. if (empty($result)) {
  308. return Result::error("创建失败", 0);
  309. } else {
  310. return Result::success(["id" => $result]);
  311. }
  312. }
  313. /**
  314. * 暂时用不上
  315. * @param int $id
  316. * @param array $data
  317. * @return array
  318. */
  319. public function updateWebsiteRole(int $id, array $data): array
  320. {
  321. $insertData = [
  322. 'website_id' => $data['website_id'],
  323. 'type' => $data['type'] ?? '',
  324. ];
  325. $result = WebsiteRole::where('id', $id)->update($insertData);
  326. if (empty($result)) {
  327. return Result::error("更新失败", 0);
  328. } else {
  329. return Result::success();
  330. }
  331. }
  332. /**
  333. * @param int $id
  334. * @return array
  335. */
  336. public function delWebsiteRole(int $id): array
  337. {
  338. $result = WebsiteRole::where('id', $id)->delete();
  339. if (empty($result)) {
  340. return Result::error("删除失败", 0);
  341. } else {
  342. return Result::success();
  343. }
  344. }
  345. /**
  346. * @param string $keyword
  347. * @param int $page
  348. * @param int $pageSize
  349. * @return array
  350. */
  351. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  352. {
  353. $where = [
  354. ['website_role_user.website_id', '=', $websiteId],
  355. ['website_role_user.role_id', '=', $roleId],
  356. ];
  357. $count = WebsiteRoleUser::where($where)->count();
  358. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  359. $result = WebsiteRoleUser::where($where)
  360. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  361. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  362. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  363. ->select("u.*", "u.user_name", 'w.website_name', 'r.role_name', 'website_role_user.id as website_role_user_id', 'website_role_user.updated_at as user_update_at')
  364. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  365. if (empty($result)) {
  366. return Result::error("没有数据", 0);
  367. }
  368. $data = [
  369. 'rows' => $result->toArray(),
  370. 'count' => $count,
  371. ];
  372. return Result::success($data);
  373. }
  374. /**
  375. * @param array $data
  376. * @return array
  377. */
  378. public function createWebsiteRoleUser(array $data): array
  379. {
  380. $insertData = [
  381. 'website_id' => $data['website_id'],
  382. 'user_id' => $data['user_id'] ?? '',
  383. ];
  384. $info = WebsiteRoleUser::where($insertData)->first();
  385. if ($info) {
  386. return Result::error("不能重复添加角色用户", 0);
  387. }
  388. $insertData['role_id'] = $data['role_id'] ?? '';
  389. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  390. $insertData['type'] = $data['type'] ?? '';
  391. $result = WebsiteRoleUser::insertGetId($insertData);
  392. if (empty($result)) {
  393. return Result::error("创建失败", 0);
  394. } else {
  395. return Result::success(["id" => $result]);
  396. }
  397. }
  398. /**
  399. * @param int $id
  400. * @param array $data
  401. * @return array
  402. */
  403. public function updateWebsiteRoleUser(int $id, array $data): array
  404. {
  405. $insertData = [
  406. 'website_id' => $data['website_id'],
  407. 'type' => $data['type'] ?? '',
  408. 'type_id' => $data['type_id'] ?? '',
  409. 'role_id' => $data['role_id'] ?? '',
  410. ];
  411. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  412. if (empty($result)) {
  413. return Result::error("更新失败", 0);
  414. } else {
  415. return Result::success();
  416. }
  417. }
  418. /**
  419. * @param int $id
  420. * @return array
  421. */
  422. public function delWebsiteRoleUser(int $id): array
  423. {
  424. $result = WebsiteRoleUser::where('id', $id)->delete();
  425. if (empty($result)) {
  426. return Result::error("删除失败", 0);
  427. } else {
  428. return Result::success();
  429. }
  430. }
  431. /**
  432. * 根据域名获取网站 站点id
  433. * @param array $data
  434. * @return array
  435. */
  436. public function getWebsiteId(array $data): array
  437. {
  438. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  439. if (empty($result)) {
  440. return Result::error("查询站点失败", 0);
  441. } else {
  442. return Result::success($result);
  443. }
  444. }
  445. /**
  446. * 查询网站下面的导航
  447. * @param array $data
  448. * @return array
  449. */
  450. public function getWebsiteCategory(array $data): array
  451. {
  452. $where = [
  453. 'website_id' => $data['website_id'],
  454. ];
  455. // 使用递归查询构建树状结构
  456. $category = WebsiteCategory::where($where)
  457. ->with([
  458. 'children' => function ($query) {
  459. $query->orderBy('sort', 'asc');
  460. },
  461. ])
  462. ->orderBy('sort', 'asc')
  463. ->where('pid', 0) // 仅查询顶级节点
  464. ->get()->all();
  465. if (empty($category)) {
  466. return Result::error("查询站点栏目失败", 0);
  467. } else {
  468. $count = WebsiteCategory::where($where)->count();
  469. $result = [
  470. 'rep' => $category,
  471. 'count' => $count,
  472. ];
  473. return Result::success($result);
  474. }
  475. }
  476. /**
  477. * 查询网站的广告
  478. * @param array $data
  479. * @return array
  480. */
  481. public function getWebsiteAdvertisement(array $data): array
  482. {
  483. $where = [
  484. 'ad_place.status' => 1,
  485. ];
  486. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  487. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  488. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  489. // return Result::success($where);
  490. $result = AdPlace::where($where)
  491. ->leftJoin("ad", function ($join) use ($now) {
  492. $join->on("ad.pid", "=", "ad_place.id")
  493. ->where('ad.status', 1)
  494. ->where('ad.fromtime', '<=', $now)
  495. ->where('ad.totime', '>=', $now);
  496. })
  497. ->select("ad_place.*", 'ad.*', "ad_place.id as ad_place_id", "ad.name as ad_name")
  498. ->get()->all();
  499. if (empty($result)) {
  500. return Result::error("此广告位不存在!", 0);
  501. }
  502. } else {
  503. return Result::error("请选择广告位!", 0);
  504. }
  505. return Result::success($result);
  506. }
  507. /**
  508. *
  509. * @param array $data
  510. * @return array
  511. */
  512. /**网站行政职能搜索 */
  513. public function selectWebsiteDepartment(array $data): array
  514. {
  515. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  516. if (isset($data['keyword']) && !empty($data['keyword'])) {
  517. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  518. if (empty($departments)) {
  519. $result['message'] = "未查询到与此相关职能部门";
  520. } else {
  521. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  522. $m = [
  523. 'department' => $depart,
  524. 'type' => $departments,
  525. 'count' => $count,
  526. ];
  527. }
  528. $result['sele'] = $m;
  529. return Result::success($result['sele']);
  530. }
  531. $result = $depart;
  532. return Result::success($result);
  533. }
  534. /**
  535. * 搜索地区
  536. * @param array $data
  537. * @return array
  538. */
  539. public function selectWebsiteArea(array $data): array
  540. {
  541. // return Result::success($data);
  542. $pid = $data['pid'] ?? 0;
  543. $result = District::where('pid', $pid)->where('status', 1)->orderBy('code')->get();
  544. if (empty($result)) {
  545. return Result::error("未查询到此地区", 0);
  546. } else {
  547. return Result::success($result);
  548. }
  549. }
  550. /**
  551. * 获取栏目
  552. * @param array $data
  553. * @return array
  554. */
  555. public function getWebsiteModelCategory(array $data): array
  556. {
  557. // return Result::success($data);
  558. $website_id = [
  559. 'website_id' => $data['website_id'],
  560. ];
  561. // 初始化 $pid 数组
  562. // $pid = [];
  563. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  564. $pidQuery = WebsiteCategory::where($website_id)
  565. ->where('pid', '!=', 0)
  566. ->orderBy('sort')
  567. ->select('pid','category_id','alias');
  568. $pid = $pidQuery->pluck('pid');
  569. $pid = array_values(array_unique($pid->toArray()));
  570. // 构建查询语句
  571. $query = WebsiteCategory::where($website_id)
  572. ->where('pid', $data['pid'])
  573. ->offset($data['placeid'])
  574. ->limit($data['num'])
  575. ->orderBy('sort');
  576. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  577. if (!empty($pid)) {
  578. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  579. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  580. } else {
  581. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  582. $query->select('website_category.*', DB::raw('0 as children_count'));
  583. }
  584. // 执行查询
  585. $placeid = $data['placeid'] - 1;
  586. $result = $query->offset($placeid)->limit($data['num'])->get();
  587. if (!empty($result)) {
  588. $pidResults = $pidQuery->get();
  589. $pidMap = $pidResults->keyBy('pid');
  590. $result->each(function ($record) use ($pidMap) {
  591. if ($pidMap->has($record->category_id)) {
  592. $pidResult = $pidMap->get($record->category_id);
  593. $record->chilid_id = $pidResult->category_id;
  594. $record->chilid_alias = $pidResult->alias;
  595. }
  596. });
  597. return Result::success($result);
  598. } else {
  599. return Result::error("本网站暂无栏目", 0);
  600. }
  601. }
  602. /**
  603. * 获取友情链接
  604. * @param array $data
  605. * @return array
  606. */
  607. public function selectWebsiteLinks(array $data): array
  608. {
  609. $where = [
  610. 'website_id' => $data['website_id'],
  611. 'status' => 1,
  612. 'type' => $data['type'],
  613. ];
  614. $num = $data['num'];
  615. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  616. if (!empty($result)) {
  617. return Result::success($result);
  618. } else {
  619. return Result::error("本网站暂无此类型友情链接", 0);
  620. }
  621. }
  622. /**
  623. * 网站首页数据统计, 管理员
  624. * @return void
  625. */
  626. public function getAdminIndex(array $data): array
  627. {
  628. var_dump("用户类型:", $data['type_id']);
  629. switch ($data['type_id']) {
  630. case 4:
  631. $result = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM letter_of_complaint WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  632. return Result::success($result);
  633. break;
  634. case 10000:
  635. $res = [];
  636. //网站
  637. $res['website']['count'] = 0;
  638. $res['website']['growth_rate'] = 0;
  639. //资讯
  640. $res['article']['count'] = 0;
  641. $res['article']['growth_rate'] = 0;
  642. //导航池
  643. $res['category']['count'] = 0;
  644. $res['category']['growth_rate'] = 0;
  645. //近一月数据
  646. $res['monthArticle'] = [];
  647. //用户类型
  648. $res['userType'] = [];
  649. $res['website']['count'] = Website::where([])->count();
  650. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  651. $res['category']['count'] = Category::where([])->count();
  652. $res['monthArticle'] = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM article WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  653. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  654. return Result::success($res);
  655. case 1:
  656. $res = [];
  657. //查询已审核文章
  658. $user_id = $data['user_id'];
  659. $article = Article::where('status', 1)
  660. ->where('admin_user_id', $user_id)
  661. ->orderBy('updated_at', 'desc')
  662. ->limit(10)
  663. ->get();
  664. $res['article'] = $article;
  665. return Result::success($res);
  666. }
  667. return [];
  668. }
  669. /**
  670. * 获取模板类型
  671. * @return void
  672. */
  673. public function getTemplateClass(array $data): array
  674. {
  675. $where = [];
  676. if (isset($data['name']) && $data['name']) {
  677. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  678. }
  679. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  680. if (empty($result)) {
  681. return Result::error("没有模板类型", 0);
  682. } else {
  683. return Result::success($result);
  684. }
  685. }
  686. /**
  687. * 添加模板类型
  688. * @param
  689. * @return void
  690. */
  691. public function addTemplateClass(array $data): array
  692. {
  693. $insertData = [
  694. 'name' => $data['name'],
  695. ];
  696. $result = TemplateClass::insertGetId($insertData);
  697. if (empty($result)) {
  698. return Result::error("创建失败", 0);
  699. } else {
  700. return Result::success(["id" => $result]);
  701. }
  702. }
  703. /**
  704. * 更新模板
  705. * @param array $data
  706. * @return array
  707. */
  708. public function upTemplateClass(array $data): array
  709. {
  710. $where = [
  711. 'id' => $data['id'],
  712. ];
  713. $insertData = [
  714. 'name' => $data['name'],
  715. ];
  716. $result = TemplateClass::where($where)->update($insertData);
  717. if (empty($result)) {
  718. return Result::error("更新失败", 0);
  719. } else {
  720. return Result::success();
  721. }
  722. }
  723. /**
  724. * 删除模板
  725. * @param array $data
  726. * @return array
  727. */
  728. public function delTemplateClass(array $data): array
  729. {
  730. $where = [
  731. 'id' => $data['id'],
  732. ];
  733. $result = TemplateClass::where($where)->delete();
  734. if (empty($result)) {
  735. return Result::error("删除失败", 0);
  736. } else {
  737. return Result::success();
  738. }
  739. }
  740. /**
  741. * 获取分类下的模板
  742. * @param array $data
  743. * @return array
  744. */
  745. public function getTemplate(array $data): array
  746. {
  747. $page = $data['page'];
  748. $pageSize = $data['pageSize'];
  749. $where = [];
  750. if (isset($data['template_class_id']) && $data['template_class_id']) {
  751. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  752. }
  753. $result = Template::where($where)
  754. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  755. $count = Template::where($where)->count();
  756. if (empty($result)) {
  757. return Result::error("没有数据", 0);
  758. }
  759. $data = [
  760. 'rows' => $result->toArray(),
  761. 'count' => $count,
  762. ];
  763. return Result::success($data);
  764. }
  765. /**
  766. * 创建模板
  767. * @param
  768. * @return void
  769. */
  770. public function addTemplate(array $data): array
  771. {
  772. $insertData = [
  773. 'template_name' => $data['template_name'],
  774. 'template_img' => json_encode($data['template_img']),
  775. 'template_class_id' => $data['template_class_id'],
  776. ];
  777. $result = Template::insertGetId($insertData);
  778. if (empty($result)) {
  779. return Result::error("创建模板失败", 0);
  780. } else {
  781. return Result::success(["id" => $result]);
  782. }
  783. }
  784. /**
  785. * 更新模板
  786. * @param array $data
  787. * @return array
  788. */
  789. public function upTemplate(array $data): array
  790. {
  791. $where = [
  792. 'id' => $data['id'],
  793. ];
  794. $insertData = [
  795. 'template_name' => $data['template_name'],
  796. 'template_img' => json_encode($data['template_img']),
  797. 'template_class_id' => $data['template_class_id'],
  798. ];
  799. $result = Template::where($where)->update($insertData);
  800. if (empty($result)) {
  801. return Result::error("更新模板失败", 0);
  802. } else {
  803. return Result::success();
  804. }
  805. }
  806. /**
  807. * 删除模板
  808. * @param array $data
  809. * @return array
  810. */
  811. public function delTemplate(array $data): array
  812. {
  813. $where = [
  814. 'id' => $data['id'],
  815. ];
  816. $result = Template::where($where)->delete();
  817. if (empty($result)) {
  818. return Result::error("删除模板失败", 0);
  819. } else {
  820. return Result::success();
  821. }
  822. }
  823. /**
  824. * 搜索网站
  825. * @param array $data
  826. * @return array
  827. */
  828. public function websiteList(array $data): array
  829. {
  830. $where = [];
  831. if (isset($data['keyword']) && !empty($data['keyword'])) {
  832. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  833. }
  834. $result = Website::where($where)->get();
  835. if ($result) {
  836. return Result::success($result);
  837. } else {
  838. return Result::error("没有网站", 0);
  839. }
  840. }
  841. public function addWebsiteCategory(array $data): array
  842. {
  843. $website_id = $data['website_id'];
  844. $category_arr_id = $data['category_arr_id'];
  845. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  846. $categoryListIds = [];
  847. if ($categoryList) {
  848. foreach ($categoryList->toArray() as $val) {
  849. array_push($categoryListIds, $val['id']);
  850. }
  851. }
  852. $arr = [];
  853. if ($categoryListIds) {
  854. foreach ($categoryListIds as $v) {
  855. $ids = $this->getUnderlingUIds(intval($v));
  856. $ids_arr = explode(",", $ids);
  857. array_push($arr, $ids_arr);
  858. }
  859. }
  860. $mergedArray = [];
  861. foreach ($arr as $subarray) {
  862. $mergedArray = array_merge($mergedArray, $subarray);
  863. }
  864. var_dump("所有:", $arr, $mergedArray);
  865. //查询出所有的分类进行分割插入 组装数据
  866. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  867. $categoryListData = $categoryListData->toArray();
  868. $insertData = [];
  869. if ($categoryListData) {
  870. foreach ($categoryListData as $key => $value) {
  871. $insertData[$key]['website_id'] = $website_id;
  872. $insertData[$key]['name'] = $value['name'];
  873. $insertData[$key]['sort'] = $value['sort'];
  874. $insertData[$key]['pid'] = $value['pid'];
  875. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  876. $insertData[$key]['seo_title'] = $value['seo_title'];
  877. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  878. $insertData[$key]['seo_description'] = $value['seo_description'];
  879. $insertData[$key]['alias'] = $value['name'];
  880. $insertData[$key]['category_id'] = $value['id'];
  881. }
  882. }
  883. $result = WebsiteCategory::insert($insertData);
  884. var_dump("插入数据状态:", $result);
  885. if ($result) {
  886. return Result::success($result);
  887. } else {
  888. return Result::error("创建失败", 0);
  889. }
  890. }
  891. /**
  892. * 删除网站导航
  893. * @param array $data
  894. * @return array
  895. */
  896. public function delWebsiteCategory(array $data): array
  897. {
  898. $website_id = $data['website_id'] ?? 0;
  899. $category_id = $data['category_id'] ?? 0;
  900. $ids = $this->getUnderlingUIds(intval($category_id));
  901. $ids_arr = explode(",", $ids);
  902. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  903. if ($result) {
  904. return Result::success($result);
  905. } else {
  906. return Result::error("删除失败", 0);
  907. }
  908. }
  909. /**
  910. * 获取网站导航
  911. * @param array $data
  912. * @return array
  913. */
  914. public function getAdminWebsiteCategory(array $data): array
  915. {
  916. $where = [
  917. 'website_id' => $data['website_id'],
  918. ];
  919. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  920. $list = [];
  921. if ($result) {
  922. foreach ($result->toArray() as $val) {
  923. array_push($list, json_decode($val['category_arr_id']));
  924. }
  925. }
  926. if ($result) {
  927. return Result::success($list);
  928. } else {
  929. return Result::error("查询失败", 0);
  930. }
  931. }
  932. /**
  933. * 更新网站导航
  934. * @param array $data
  935. * @return array
  936. */
  937. public function upWebsiteCategory(array $data): array
  938. {
  939. Db::beginTransaction();
  940. try {
  941. if ($data['category_arr_id']) {
  942. $oldCategoryList = WebsiteCategory::where(['website_id' => $data['website_id']])->get();
  943. if ($oldCategoryList) {
  944. $oldCategoryList = $oldCategoryList->toArray();
  945. $oldCategoryListIds = [];
  946. foreach ($oldCategoryList as $val) {
  947. array_push($oldCategoryListIds, $val['category_id']);
  948. }
  949. $newCategoryListIds = [];
  950. foreach ($data['category_arr_id'] as $v) {
  951. array_push($newCategoryListIds, end($v));
  952. }
  953. $differenceIDS = array_merge(array_diff($oldCategoryListIds, $newCategoryListIds), array_diff($newCategoryListIds, $oldCategoryListIds));
  954. if ($differenceIDS) {
  955. foreach ($differenceIDS as $v) {
  956. WebsiteCategory::where(['website_id' => $data['website_id'], 'category_id' => $v])->delete();
  957. }
  958. }
  959. foreach ($data['category_arr_id'] as $key => $value) {
  960. $info = Category::where(['id' => end($value)])->first();
  961. $info = $info->toArray();
  962. // 假设 $data、$value 和 $info 是已经定义好的变量
  963. $attributes = [
  964. 'website_id' => $data['website_id'],
  965. 'category_id' => end($value),
  966. ];
  967. // 先复制一份要更新或创建的数据
  968. $values = [
  969. 'website_id' => $data['website_id'],
  970. 'name' => $info['name'] ?? '',
  971. 'alias' => $info['name'] ?? '',
  972. 'sort' => $info['sort'] ?? 0,
  973. 'pid' => $info['pid'] ?? 0,
  974. 'pid_arr' => $info['pid_arr'] ?? json_encode([]),
  975. 'seo_title' => $info['seo_title'] ?? "",
  976. 'seo_keywords' => $info['seo_keywords'] ?? "",
  977. 'seo_description' => $info['seo_description'] ?? "",
  978. 'is_url' => $info['is_url'] ?? 0,
  979. 'web_url' => $info['web_url'] ?? '',
  980. 'category_id' => end($value),
  981. 'category_arr_id' => $value ? json_encode($value) : json_encode([])
  982. ];
  983. // 检查数据库中是否已有该记录
  984. $existingRecord = WebsiteCategory::where($attributes)->first();
  985. if ($existingRecord && $existingRecord->name) {
  986. unset($values['name']);
  987. }
  988. if ($existingRecord && $existingRecord->sort >= 0) {
  989. unset($values['sort']);
  990. }
  991. if ($existingRecord && $existingRecord->seo_title) {
  992. unset($values['seo_title']);
  993. }
  994. if ($existingRecord && $existingRecord->seo_keywords) {
  995. unset($values['seo_keywords']);
  996. }
  997. if ($existingRecord && $existingRecord->seo_description) {
  998. unset($values['seo_description']);
  999. }
  1000. if ($existingRecord && $existingRecord->alias) {
  1001. unset($values['alias']);
  1002. }
  1003. // 执行 updateOrCreate 操作
  1004. WebsiteCategory::updateOrCreate($attributes, $values);
  1005. }
  1006. }
  1007. }
  1008. Db::commit();
  1009. return Result::success();
  1010. } catch (\Throwable $ex) {
  1011. Db::rollBack();
  1012. // var_dump($ex->getMessage());
  1013. return Result::error("修改失败" . $ex->getMessage(), 0);
  1014. }
  1015. }
  1016. /**
  1017. * 获取网站列表
  1018. * @param array $data
  1019. * @return array
  1020. */
  1021. public function getWebsiteCategoryList(array $data): array
  1022. {
  1023. $where = [];
  1024. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1025. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1026. }
  1027. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1028. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1029. }
  1030. $result = Website::where($where)
  1031. ->with(["websiteCategory" => function ($query) {
  1032. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1033. }])
  1034. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1035. ->get();
  1036. $count = Website::where($where)->count();
  1037. if (empty($result)) {
  1038. return Result::error("没有数据", 0);
  1039. }
  1040. $data = [
  1041. 'rows' => $result->toArray(),
  1042. 'count' => $count,
  1043. ];
  1044. if ($result) {
  1045. return Result::success($data);
  1046. } else {
  1047. return Result::error("查询失败", 0);
  1048. }
  1049. }
  1050. /**
  1051. * 删除网站下的所有导航
  1052. * @param array $data
  1053. * @return array
  1054. */
  1055. public function delWebsiteAllCategory(array $data): array
  1056. {
  1057. $website_id = $data['website_id'];
  1058. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1059. if ($result) {
  1060. return Result::success($result);
  1061. } else {
  1062. return Result::error("删除失败", 0);
  1063. }
  1064. }
  1065. /**
  1066. * 获取网站下的某一个导航
  1067. * @param array $data
  1068. * @return array
  1069. */
  1070. public function getWebsiteCategoryOnes(array $data): array
  1071. {
  1072. $website_id = $data['website_id'];
  1073. $category_id = $data['category_id'];
  1074. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1075. ->first();
  1076. if ($result) {
  1077. return Result::success($result);
  1078. } else {
  1079. return Result::error("查询失败", 0);
  1080. }
  1081. }
  1082. /**
  1083. * 更新网闸下的某一个导航
  1084. * @param array $data
  1085. * @return array
  1086. */
  1087. public function upWebsiteCategoryones(array $data): array
  1088. {
  1089. //处理所有数据
  1090. //获取所有数据
  1091. // $result = WebsiteCategory::get();
  1092. // $result = $result->toArray();
  1093. // var_dump($result, '------------');
  1094. // $pinyin = new Pinyin();
  1095. // for ($i = 0; $i < count($result); $i++) {
  1096. // $result[$i]['alias_pinyin'] = $pinyin->permalink($result[$i]['alias'], '') . $result[$i]['category_id'];
  1097. // var_dump($result[$i]['alias_pinyin'], $i . '----------');
  1098. // //更新数据
  1099. // WebsiteCategory::where(['id' => $result[$i]['id']])->update(['alias_pinyin' => $result[$i]['alias_pinyin']]);
  1100. // //判断是否更新成功
  1101. // }
  1102. // var_dump($result, '--------222----');
  1103. // //更新数据
  1104. // // $result = WebsiteCategory::update($result);
  1105. // //判断是否更新成功
  1106. // if ($result) {
  1107. // return Result::success($result);
  1108. // } else {
  1109. // return Result::error("更新失败", 0);
  1110. // }
  1111. //处理所有数据
  1112. $where = [
  1113. 'website_id' => $data['website_id'],
  1114. 'category_id' => $data['category_id'],
  1115. ];
  1116. $alias = $data['alias'];
  1117. $pinyin = new Pinyin();
  1118. $alias_pinyin = $pinyin->permalink($alias, '');
  1119. //从WebsiteCategory取出alis ,并转成拼音存放到alias_pinyin
  1120. $result = WebsiteCategory::where($where)->first();
  1121. $result = $result->toArray();
  1122. $data['alias_pinyin'] = $pinyin->permalink($result['alias'], '') . $result['category_id'];
  1123. $result = WebsiteCategory::where($where)->update($data);
  1124. if ($result) {
  1125. return Result::success($result);
  1126. } else {
  1127. return Result::error("更新失败", 0);
  1128. }
  1129. }
  1130. /**
  1131. * 获取网站下的所有导航(包含子导航)
  1132. * @param array $data
  1133. * @return array
  1134. */
  1135. public function getWebsiteAllCategory(array $data): array
  1136. {
  1137. $where = [];
  1138. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1139. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1140. }
  1141. // if (isset($data['name']) && !empty($data['name'])) {
  1142. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1143. // }
  1144. // if (isset($data['alias']) && !empty($data['alias'])) {
  1145. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1146. // }
  1147. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1148. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1149. // }
  1150. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1151. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1152. // }
  1153. $result = WebsiteCategory::where($where)
  1154. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1155. ->select("website_category.*", "category.name as name")
  1156. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1157. // ->leftJoin("department", 'category.department_id', 'department.id')
  1158. // ->leftJoin("district", 'category.city_id', 'district.id')
  1159. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1160. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1161. ->orderBy("sort", "asc")
  1162. ->get();
  1163. // $count = WebsiteCategory::where($where)
  1164. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1165. // ->leftJoin("department", 'category.department_id', 'department.id')
  1166. // ->leftJoin("district", 'category.city_id', 'district.id')
  1167. // ->count();
  1168. if (empty($result)) {
  1169. return Result::error("没有数据", 0);
  1170. }
  1171. // $data = [
  1172. // 'rows' => $result->toArray(),
  1173. // 'count' => $count,
  1174. // ];
  1175. if ($result) {
  1176. return Result::success($result->toArray());
  1177. } else {
  1178. return Result::error("查询失败", 0);
  1179. }
  1180. }
  1181. /**
  1182. * 递归查询数据
  1183. * @param $id
  1184. * @param $ids
  1185. * @return string
  1186. */
  1187. public function getUnderlingUIds($id, $ids = '')
  1188. {
  1189. $back = Category::where(['pid' => $id])->get();
  1190. $back = $back->toArray();
  1191. if (!empty($back) && is_array($back)) {
  1192. foreach ($back as $v) {
  1193. //防止当前人的ID重复去查询,形成恶性循环
  1194. if ($v['id'] == $id) {
  1195. continue;
  1196. }
  1197. $back2 = Category::where(['pid' => $id])->count('id');
  1198. if ($back2 > 0) {
  1199. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1200. } else {
  1201. $ids .= ',' . $v['id'];
  1202. }
  1203. }
  1204. }
  1205. $ids = $id . ',' . $ids . ',';
  1206. $ids = str_replace(',,', ",", $ids);
  1207. $ids = trim($ids, ',');
  1208. return $ids;
  1209. }
  1210. /**
  1211. * 检测网站名称是否重复
  1212. * @param array $data
  1213. * @return array
  1214. */
  1215. public function checkWebsiteName(array $data): array
  1216. {
  1217. if (isset($data['id'])) {
  1218. $data[] = ['id', "!=", $data['id']];
  1219. unset($data['id']);
  1220. }
  1221. $websiteInfo = Website::query()->where($data)->first();
  1222. if (empty($websiteInfo)) {
  1223. return Result::error("找不到网站", 0);
  1224. }
  1225. return Result::success($websiteInfo->toArray());
  1226. }
  1227. /**
  1228. * 检测网站url是否重复
  1229. * @param array $data
  1230. * @return array
  1231. */
  1232. public function checkWebsiteUrl(array $data): array
  1233. {
  1234. $whereData = [];
  1235. if (isset($data['id'])) {
  1236. $whereData = [['id', "!=", $data['id']]];
  1237. unset($data['id']);
  1238. }
  1239. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1240. if (empty($websiteInfo)) {
  1241. return Result::error("找不到URL", 0);
  1242. }
  1243. return Result::success($websiteInfo->toArray());
  1244. }
  1245. /**
  1246. * 获取网站底部基础信息
  1247. * @param array $data
  1248. * @return array
  1249. */
  1250. public function getWebsiteFootInfo(array $data): array
  1251. {
  1252. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1253. $website_head = Website::where('id', $data['website_id'])
  1254. ->where('status', 1)
  1255. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1256. ->first();
  1257. if (empty($website_head)) {
  1258. return Result::error("找不到网站", 0);
  1259. }
  1260. } else {
  1261. return Result::error("参数错误", 0);
  1262. }
  1263. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->where('status', 2)->first();
  1264. $website_head = Website::where('id', $data['website_id'])
  1265. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1266. if (empty($website_foot)) {
  1267. return Result::error("暂无底部基础信息", 0);
  1268. }
  1269. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1270. $result = [
  1271. 'website_foot' => $website_foot,
  1272. 'website_head' => $website_head,
  1273. ];
  1274. return Result::success($result);
  1275. }
  1276. /**
  1277. * 获取网站底部导航
  1278. * @param array $data
  1279. * @return array
  1280. */
  1281. public function getWebsiteFooterCategory(array $data): array
  1282. {
  1283. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1284. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1285. if (empty($website)) {
  1286. return Result::error("找不到网站", 0);
  1287. }
  1288. } else {
  1289. return Result::error("参数错误", 0);
  1290. }
  1291. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1292. if (empty($result)) {
  1293. return Result::error("暂无底部导航", 0);
  1294. }
  1295. return Result::success($result->toArray());
  1296. }
  1297. /**
  1298. * 获取网站底部导航列表
  1299. * @param array $data
  1300. * @return array
  1301. */
  1302. public function getWebsiteFooterCategoryList(array $data): array
  1303. {
  1304. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1305. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1306. if (empty($website)) {
  1307. return Result::error("找不到网站", 0);
  1308. }
  1309. } else {
  1310. return Result::error("参数错误", 0);
  1311. }
  1312. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1313. // '底部导航类型 0:内容型;1:列表型;',
  1314. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1315. return Result::error("底部导航id错误", 0);
  1316. } else {
  1317. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1318. if ($query->count() == 0) {
  1319. return Result::error("暂无底部导航列表", 0);
  1320. } elseif ($query->count() == 1) {
  1321. $result = $query->first();
  1322. } else {
  1323. $result = $query->get();
  1324. }
  1325. }
  1326. return Result::success($result);
  1327. }
  1328. /**
  1329. * 获取网站底部导航
  1330. * @param array $data
  1331. * @return array
  1332. */
  1333. public function getWebsiteFooterCategoryInfo(array $data): array
  1334. {
  1335. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1336. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1337. if (empty($website)) {
  1338. return Result::error("找不到网站", 0);
  1339. }
  1340. } else {
  1341. return Result::error("参数错误", 0);
  1342. }
  1343. if (isset($data['type']) && $data['type'] == 0) {
  1344. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1345. if (empty($fcatid)) {
  1346. return Result::error("底部导航id错误", 0);
  1347. }
  1348. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1349. } else {
  1350. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1351. }
  1352. if (empty($result)) {
  1353. return Result::error("暂无底部导航内容", 0);
  1354. }
  1355. return Result::success($result);
  1356. }
  1357. /*
  1358. * 搜索网站二级导航
  1359. * @param array $data
  1360. * @return array
  1361. * */
  1362. public function selectWebsiteCategory(array $data): array
  1363. {
  1364. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1365. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1366. if (empty($website)) {
  1367. return Result::error("暂无该网站", 0);
  1368. }
  1369. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1370. // return Result::success($category);
  1371. if (empty($category)) {
  1372. return Result::error("暂无此导航", 0);
  1373. }
  1374. $query = Category::whereIn('id', $category);
  1375. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1376. $cityid = District::where('id', $data['cityid'])->first();
  1377. if (empty($cityid)) {
  1378. return Result::error("暂无此城市", 0);
  1379. } else {
  1380. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1381. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1382. if (empty($city_category)) {
  1383. return Result::error("暂无此城市下的导航", 0);
  1384. }
  1385. $result['catid'] = $city_category;
  1386. $city = 1;
  1387. // var_dump("城市====================",$result);
  1388. }
  1389. }
  1390. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1391. $departmentid = Department::where('id', $data['department_id'])->first();
  1392. if (empty($departmentid)) {
  1393. return Result::error("暂无此部门", 0);
  1394. } else {
  1395. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1396. if (empty($depart_category)) {
  1397. return Result::error("暂无此部门下的导航", 0);
  1398. }
  1399. $result['catid'] = $depart_category;
  1400. // var_dump("职能部门*******************",$result);
  1401. $department = 1;
  1402. }
  1403. }
  1404. if (!empty($city) && !empty($department)) {
  1405. // var_dump("城市和职能部门----------------------",$result);
  1406. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1407. if (empty($sel_category)) {
  1408. return Result::error("暂无此城市和职能部门下的导航", 0);
  1409. }
  1410. $result['catid'] = $sel_category;
  1411. }
  1412. if (empty($result)) {
  1413. return Result::error("暂无导航", 0);
  1414. }
  1415. // department_id
  1416. } else {
  1417. return Result::error("参数错误", 0);
  1418. }
  1419. return Result::success($result);
  1420. }
  1421. /**
  1422. * 获取网站栏目seo
  1423. * @param array $data
  1424. * @return array
  1425. */
  1426. public function getWebsiteCategoryHead(array $data): array
  1427. {
  1428. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1429. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1430. if (empty($website)) {
  1431. return Result::error("找不到网站", 0);
  1432. }
  1433. } else {
  1434. return Result::error("参数错误", 0);
  1435. }
  1436. if (isset($data['catid']) && !empty($data['catid'])) {
  1437. $fcatid = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1438. if (empty($fcatid)) {
  1439. return Result::error("导航id错误", 0);
  1440. }
  1441. $result = Category::where('id', $data['catid'])->first();
  1442. $result['website_name'] = $website['website_name'] ?? '';
  1443. $result['suffix'] = $website['suffix'] ?? '';
  1444. }
  1445. if (empty($result)) {
  1446. return Result::error("暂无导航", 0);
  1447. }
  1448. return Result::success($result);
  1449. }
  1450. /*
  1451. * 获取某个栏目
  1452. * @param array $data
  1453. * @return array
  1454. * */
  1455. public function getOneWebsiteCategory(array $data): array
  1456. {
  1457. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1458. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1459. if (empty($website)) {
  1460. return Result::error("暂无该网站", 0);
  1461. }
  1462. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1463. if (empty($category)) {
  1464. return Result::error("暂无此导航", 0);
  1465. }
  1466. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1467. $parent = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $category['pid'])->select('category_id as parent_id', 'alias as parent_name')->first();
  1468. $category['parent_id'] = $parent['parent_id'] ?? '';
  1469. $category['parent_name'] = $parent['parent_name'] ?? '';
  1470. return Result::success($category);
  1471. } else {
  1472. return Result::error("参数错误", 0);
  1473. }
  1474. }
  1475. // 自助建站--fr-------------------
  1476. /**
  1477. * 获取并搜索 网站模板信息
  1478. * @param array $data
  1479. * @return array
  1480. */
  1481. public function getWebsiteintel(array $data): array
  1482. {
  1483. //查询所有网站模板信息
  1484. $query = Website::where('website.status', 1)
  1485. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1486. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1487. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1488. ->select(
  1489. "website_template_info.id",
  1490. "template_class.name",
  1491. "website.id as website_id",
  1492. "website.website_name",
  1493. "website.website_url",
  1494. "website_template_info.action_id",
  1495. "website_template_info.created_at",
  1496. "website_template_info.updated_at",
  1497. "website_template_info.page_type",
  1498. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1499. "template.template_name"
  1500. );
  1501. //若存在条件;则在$query的基础上进行筛选
  1502. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1503. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1504. }
  1505. if (isset($data['status']) && !empty($data['status'])) {
  1506. $query->where('website_template_info.status', $data['status']);
  1507. }
  1508. $count = $query->count();
  1509. $query->limit($data['pageSize'])
  1510. ->offset(($data['page'] - 1) * $data['pageSize'])
  1511. ->orderBy("website_template_info.updated_at", "desc");
  1512. $rep = $query->get();
  1513. if ($rep->count() == 0) {
  1514. return Result::error("没有查找到相关数据", 0);
  1515. } else {
  1516. $rep->each(function ($item) {
  1517. if (!empty($item->page_type)) {
  1518. $pageTypeArray = json_decode($item->page_type, true);
  1519. if (is_array($pageTypeArray)) {
  1520. $item->page_type = $pageTypeArray;
  1521. }
  1522. }
  1523. });
  1524. $result = [
  1525. "rows" => $rep->toArray(),
  1526. "count" => $count,
  1527. ];
  1528. return Result::success($result);
  1529. }
  1530. // return Result::success($result);
  1531. }
  1532. /**
  1533. * 添加网站基础信息
  1534. * @param array $data
  1535. * @return array
  1536. */
  1537. public function addWebsiteTemplateintel(array $data): array
  1538. {
  1539. $website = Website::where('website.id', $data['website_id'])->first();
  1540. if (empty($website)) {
  1541. return Result::error("请输入正确的网站id!", 0);
  1542. }
  1543. if (empty($website['website_column_id'])) {
  1544. return Result::error("请先关联导航池!", 0);
  1545. }
  1546. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1547. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1548. if (empty($foot_type)) {
  1549. return Result::error("请先关联底部导航池!", 0);
  1550. } elseif (in_array(1, $foot_type)) {
  1551. $foot_type = 1;
  1552. } else {
  1553. $foot_type = 0;
  1554. }
  1555. $result['foot_type'] = $foot_type;
  1556. // 友情链接类型'1:图片 2:文字 3:底部';
  1557. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1558. $missingTypes = [];
  1559. if (!in_array(1, $types)) {
  1560. $missingTypes[] = "文字链接";
  1561. }
  1562. if (!in_array(2, $types)) {
  1563. $missingTypes[] = "图片链接";
  1564. }
  1565. if (!in_array(3, $types)) {
  1566. $missingTypes[] = "底部链接";
  1567. }
  1568. if (!empty($missingTypes)) {
  1569. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1570. return Result::error($errorMessage, 0);
  1571. }
  1572. if (isset($data['page_type'])) {
  1573. // 删除重复元素并重新索引数组
  1574. $data['page_type'] = array_unique($data['page_type']);
  1575. $data['page_type'] = array_values($data['page_type']);
  1576. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1577. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1578. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1579. if (empty($info)) {
  1580. // 将数组转换为 JSON 字符串
  1581. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1582. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1583. $data['action_id'] = 1;
  1584. try {
  1585. $result = WebsiteTemplateInfo::insertGetId($data);
  1586. } catch (\Exception $e) {
  1587. if ($e->getCode() == 22001) {
  1588. $errorMessage = $e->getMessage();
  1589. return Result::error("请检查表单,某个字段超出了长度限制!");
  1590. } else {
  1591. // 处理其他类型的数据库错误
  1592. return Result::error("插入操作失败: " . $e->getMessage());
  1593. }
  1594. return Result::error("添加失败!", 0);
  1595. }
  1596. } else {
  1597. return Result::error("该网站已经构建过了!", 0);
  1598. }
  1599. } else {
  1600. return Result::error("请先选择首页和底部导航详情页!", 0);
  1601. }
  1602. }
  1603. if (empty($result)) {
  1604. return Result::error("添加失败!", 0);
  1605. } else {
  1606. return Result::success($result);
  1607. }
  1608. }
  1609. /**
  1610. * 获取网站基础信息
  1611. *
  1612. * @return array
  1613. */
  1614. public function getWebsiteTemplateintel(array $data): array
  1615. {
  1616. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])
  1617. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1618. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1619. ->select(
  1620. "website_template_info.*",
  1621. "footer_category.type",
  1622. "website.website_name",
  1623. "website.id as website_id"
  1624. )
  1625. ->first();
  1626. if (empty($result['website_name'])) {
  1627. return Result::error("请输入正确的网站id!", 0);
  1628. }
  1629. if ($result) {
  1630. $result['page_type'] = json_decode($result['page_type'], true);
  1631. return Result::success($result);
  1632. } else {
  1633. return Result::error("请先添加网站基础信息!", 0);
  1634. }
  1635. }
  1636. /**
  1637. * 修改网站基础信息
  1638. *
  1639. * @return array
  1640. */
  1641. public function upWebsiteTemplateintel(array $data): array
  1642. {
  1643. $web = Website::where('website.id', $data['website_id'])->first();
  1644. if (empty($web)) {
  1645. return Result::error("请输入正确的网站id!", 0);
  1646. }
  1647. $where = ['website_id' => $data['website_id']];
  1648. $result = WebsiteTemplateInfo::where($where)->first();
  1649. if (empty($result)) {
  1650. return Result::error("请先添加网站基础信息!", 0);
  1651. } else {
  1652. unset($data['website_id']);
  1653. // 删除重复元素
  1654. $data['page_type'] = array_unique($data['page_type']);
  1655. // 重新索引数组
  1656. $data['page_type'] = array_values($data['page_type']);
  1657. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1658. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1659. // 将数组转换为 JSON 字符串
  1660. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1661. try {
  1662. $result = WebsiteTemplateInfo::where($where)->update($data);
  1663. } catch (\Exception $e) {
  1664. if ($e->getCode() == 22001) {
  1665. $errorMessage = $e->getMessage();
  1666. return Result::error("请检查表单,某个字段超出了长度限制!");
  1667. } else {
  1668. // 处理其他类型的数据库错误
  1669. return Result::error("修改操作失败: " . $e->getMessage());
  1670. }
  1671. return Result::error("修改失败!", 0);
  1672. }
  1673. } else {
  1674. return Result::error("请先选择首页和底部导航详情页!", 0);
  1675. }
  1676. }
  1677. if (empty($result)) {
  1678. return Result::error("修改失败!", 0);
  1679. } else {
  1680. return Result::success($result);
  1681. }
  1682. }
  1683. /**
  1684. * 获取所有网站风格信息
  1685. *
  1686. * @return array
  1687. */
  1688. public function getAllTemplateClass(array $data): array
  1689. {
  1690. $result = TemplateClass::all();
  1691. if (empty($result)) {
  1692. return Result::error("没有查找到相关数据!", 0);
  1693. } else {
  1694. return Result::success($result);
  1695. }
  1696. }
  1697. /**
  1698. * 搜索并获取网站模板信息
  1699. * @param array $data
  1700. * @return array
  1701. */
  1702. public function getWebsiteTemplateList(array $data): array
  1703. {
  1704. $website = Website::where('website.id', $data['website_id'])->first();
  1705. if (empty($website)) {
  1706. return Result::error("请输入正确的网站id!", 0);
  1707. } else {
  1708. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1709. if (empty($page_type)) {
  1710. return Result::error("此网站还未添加基础信息!", 0);
  1711. } else {
  1712. $where = json_decode($page_type['page_type'], true);
  1713. // $where = $data["page_type"];
  1714. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1715. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1716. if (empty($template_class)) {
  1717. return Result::error("请输入正确的模板风格id!", 0);
  1718. } else {
  1719. // 获取指定风格下的模板
  1720. $rep = Template::where('template_class_id', $data['template_class_id'])
  1721. ->where(function ($query) use ($where) {
  1722. foreach ($where as $value) {
  1723. $query->whereJsonContains('template_img', ['value' => $value]);
  1724. }
  1725. })
  1726. ->limit($data['pageSize'])
  1727. ->offset(($data['page'] - 1) * $data['pageSize'])
  1728. ->orderBy("template.updated_at", "desc")
  1729. ->get();
  1730. $count = Template::where('template_class_id', $data['template_class_id'])
  1731. ->where(function ($query) use ($where) {
  1732. foreach ($where as $value) {
  1733. $query->whereJsonContains('template_img', ['value' => $value]);
  1734. }
  1735. })->count();
  1736. }
  1737. } else {
  1738. //获取所有模板
  1739. $rep = Template::where(function ($query) use ($where) {
  1740. foreach ($where as $value) {
  1741. $query->whereJsonContains('template_img', ['value' => $value]);
  1742. }
  1743. })
  1744. ->limit($data['pageSize'])
  1745. ->offset(($data['page'] - 1) * $data['pageSize'])
  1746. ->orderBy("template.updated_at", "desc")
  1747. ->get();
  1748. $count = Template::where(function ($query) use ($where) {
  1749. foreach ($where as $value) {
  1750. $query->whereJsonContains('template_img', ['value' => $value]);
  1751. }
  1752. })->count();
  1753. }
  1754. if (empty($rep) || $rep->count() == 0) {
  1755. return Result::error("没有查找到相关数据!", 0);
  1756. } else {
  1757. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1758. // $rep->each(function ($item) use ($where) {
  1759. // $template_img = json_decode($item->template_img, true);
  1760. // if(is_array($template_img)){
  1761. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1762. // return isset($img['value']) && in_array($img['value'], $where);
  1763. // });
  1764. // }
  1765. // $item->template_img = $filtered_img;
  1766. // });
  1767. // $result = [
  1768. // "rows" => $rep->toArray(),
  1769. // "count" => $count
  1770. // ];
  1771. $rep->each(function ($item) use ($where) {
  1772. $template_img = json_decode($item->template_img, true);
  1773. if (is_array($template_img)) {
  1774. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1775. return isset($img['value']) && in_array($img['value'], $where);
  1776. });
  1777. // 对过滤后的数组进行重新排序
  1778. $sorted_img = array_values($filtered_img);
  1779. $item->template_img = $sorted_img;
  1780. } else {
  1781. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1782. }
  1783. });
  1784. $result = [
  1785. "rows" => $rep->toArray(),
  1786. "count" => $count,
  1787. ];
  1788. }
  1789. }
  1790. }
  1791. return Result::success($result);
  1792. }
  1793. /**
  1794. * 添加网站模板
  1795. * @param array $data
  1796. */
  1797. public function addWebsiteTemplateclassintel(array $data): array
  1798. {
  1799. $template = Template::where('id', $data['template_id'])->first();
  1800. $web = Website::where('id', $data['website_id'])->first();
  1801. if (empty($template)) {
  1802. return Result::error("请输入正确的模板id", 0);
  1803. }
  1804. if (empty($web)) {
  1805. return Result::error("请输入正确的网站id", 0);
  1806. }
  1807. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1808. if (empty($result)) {
  1809. return Result::error("添加失败", 0);
  1810. } else {
  1811. return Result::success($result);
  1812. }
  1813. }
  1814. /**
  1815. * 获取网站模板信息
  1816. * @param array $data
  1817. */
  1818. public function getWebsiteTemplateclassintel(array $data): array
  1819. {
  1820. // return Result::success($data);
  1821. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1822. if (empty($template)) {
  1823. return Result::error("请输入正确的搭建网站id", 0);
  1824. }
  1825. $page_type = json_decode($template['page_type'], true);
  1826. // 获取指定网站下的基础信息和模板及风格信息
  1827. $result = WebsiteTemplateInfo::where([
  1828. ['website_template_info.website_id', $data['website_id']],
  1829. ['template_id', '!=', null],
  1830. ])
  1831. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1832. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1833. ->where(function ($query) use ($page_type) {
  1834. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1835. foreach ($page_type as $pageType) {
  1836. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1837. }
  1838. })
  1839. ->select(
  1840. 'template.template_name',
  1841. 'template.template_img',
  1842. 'template.id as tid',
  1843. 'template_class.name',
  1844. 'template_class.id as class_id',
  1845. 'website_template_info.page_type',
  1846. 'website_template_info.website_id')
  1847. ->first();
  1848. if (empty($result)) {
  1849. return Result::error("没有查找到相关数据", 0);
  1850. }
  1851. // return Result::success($result);
  1852. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1853. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1854. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1855. foreach ($template_img as $key => $value) {
  1856. if (!in_array($value['value'], $page_type)) {
  1857. unset($template_img[$key]);
  1858. }
  1859. }
  1860. // return Result::success($result);
  1861. $result['template_img'] = array_values($template_img);
  1862. $result['page_type'] = $page_type;
  1863. // $templateList = $this->getWebsiteTemplateList($data);
  1864. // $count = $templateList['data']['count']??'';
  1865. // $pages = $count / $data['pageSize'];
  1866. // $found = false;
  1867. // while ($pages && !$found) {
  1868. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1869. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1870. // if ($index !== false) {
  1871. // $found = true;
  1872. // $result['page'] = $pages;
  1873. // $pages = 0;
  1874. // // echo "查询到的 template 在第 $page 页";
  1875. // }
  1876. // $pages--;
  1877. // }
  1878. return Result::success($result);
  1879. }
  1880. /**
  1881. * 获取网站模板下的板块信息
  1882. * @param array $data
  1883. */
  1884. public function getWebsiteSectorList(array $data): array
  1885. {
  1886. $website = Website::where('id', $data['website_id'])->first();
  1887. if (empty($website)) {
  1888. return Result::error("请输入正确的网站id", 0);
  1889. }
  1890. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1891. if (empty($template['template_id'])) {
  1892. return Result::error("请先添加网站模板", 0);
  1893. }
  1894. $query = Sector::where('template_id', $template['template_id']);
  1895. if ($query->count() == 0) {
  1896. return Result::error("没有查找到相关板块数据", 0);
  1897. } elseif ($query->count() == 1) {
  1898. $sector = $query->first();
  1899. } else {
  1900. $sector = $query->get();
  1901. }
  1902. $result['page_type'] = $template['page_type'];
  1903. $result['tid'] = $template['template_id'];
  1904. $result = [
  1905. "tid" => $template['template_id'],
  1906. "page_type" => $template['page_type'],
  1907. "sector" => $sector,
  1908. ];
  1909. return Result::success($result);
  1910. }
  1911. // 自助建站---------------fr---------------
  1912. // 网站底基础信息--fr-------------------
  1913. /**
  1914. * 添加网站底基础信息
  1915. * @param array $data
  1916. */
  1917. public function addWebFootInfo(array $data): array
  1918. {
  1919. $website = Website::where('id', $data['website_id'])->first();
  1920. if (empty($website)) {
  1921. return Result::error("请输入正确的网站id!", 0);
  1922. }
  1923. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1924. if (!empty($info)) {
  1925. return Result::error("该网站已经添加过了!", 0);
  1926. }
  1927. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  1928. if (empty($result)) {
  1929. return Result::error("请先添加网站基础信息!", 0);
  1930. } else {
  1931. return Result::success($result);
  1932. }
  1933. }
  1934. /**
  1935. * 获取网站底基础信息
  1936. * @param array $data
  1937. */
  1938. public function getWebFootInfo(array $data): array
  1939. {
  1940. $website = Website::where('id', $data['website_id'])->first();
  1941. if (empty($website)) {
  1942. return Result::error("请输入正确的网站id!", 0);
  1943. }
  1944. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1945. if (empty($result)) {
  1946. return Result::error("请先添加网站基础信息!", 0);
  1947. }
  1948. return Result::success($result);
  1949. }
  1950. /**
  1951. * 修改网站底基础信息
  1952. * @param array $data
  1953. */
  1954. public function upWebFootInfo(array $data): array
  1955. {
  1956. // return Result::success($data);
  1957. $website = Website::where('id',$data['website_id'])->first();
  1958. if(empty($website)){
  1959. return Result::error("请输入正确的网站id!", 0);
  1960. }
  1961. $web = $data['website_id'];
  1962. unset($data['website_id']);
  1963. $where = ['website_id'=>$web];
  1964. // return Result::success($data);
  1965. $result = WebsiteTemplateInfo::where($where)->first();
  1966. if(empty($result)){
  1967. $data['website_id'] = $web;
  1968. $result = WebsiteTemplateInfo::insertGetId($data);
  1969. // return Result::error("请先添加网站基础信息!",0);
  1970. } else{
  1971. $result = WebsiteTemplateInfo::where($where)->update($data);
  1972. }
  1973. // $where = ['website_id'=>$data['website_id']];
  1974. // unset($data['website_id']);
  1975. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  1976. if(empty($result)){
  1977. return Result::error("修改失败!",0);
  1978. }
  1979. return Result::success($result);
  1980. }
  1981. /*
  1982. * 获取父级/子级栏目
  1983. * @param array $data
  1984. * @return array
  1985. * */
  1986. public function getWebsiteParentCategory(array $data): array
  1987. {
  1988. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1989. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1990. }
  1991. if (empty($website)) {
  1992. return Result::error("暂无该网站", 0);
  1993. }
  1994. if ($data['id'] == 0) {
  1995. $category = WebsiteCategory::where('website_id', $data['website_id'])
  1996. ->where('pid', 0)
  1997. ->pluck('aLIas_pinyin')
  1998. ->map(function ($alias) {
  1999. return "/{$alias}/:id";
  2000. })
  2001. ->values()
  2002. ->all();
  2003. } else {
  2004. $category = WebsiteCategory::where('website_id', $data['website_id'])
  2005. ->where('pid', '!=', 0)
  2006. ->pluck('aLIas_pinyin')
  2007. ->map(function ($alias) {
  2008. return "/{$alias}/:id";
  2009. })
  2010. ->values()
  2011. ->all();
  2012. }
  2013. if (empty($category)) {
  2014. return Result::error("暂无此导航", 0);
  2015. }
  2016. // $result = [];
  2017. return Result::success($category);
  2018. }
  2019. /**
  2020. * 获取网站栏目信息
  2021. * @param array $data
  2022. */
  2023. public function getWebsiteFootAll(array $data): array
  2024. {
  2025. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2026. if (empty($website)) {
  2027. return Result::error("暂无该网站", 0);
  2028. }
  2029. // 1:图片 2:文字 3:底部
  2030. $result['foot_cate'] = FooterCategory::where('website_id', $data['website_id'])->get()->all();
  2031. $result['link_img'] = Link::where('website_id', $data['website_id'])->where('type', 1)->where('status', 1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2032. $result['link_text'] = Link::where('website_id', $data['website_id'])->where('type', 2)->where('status', 1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2033. $result['link_foot'] = Link::where('website_id', $data['website_id'])->where('type', 3)->where('status', 1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2034. $result['foot_info'] = WebsiteTemplateInfo::where('website_id', $data['website_id'])->where('status', 2)->first();
  2035. if (empty($result)) {
  2036. return Result::error("暂无此网站信息", 0);
  2037. }
  2038. return Result::success($result);
  2039. }
  2040. /**
  2041. * 获取网站顶部信息
  2042. * @param array $data
  2043. */
  2044. public function getWebsiteHead(array $data): array
  2045. {
  2046. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2047. if (empty($result)) {
  2048. return Result::error("暂无该网站", 0);
  2049. }
  2050. return Result::success($result);
  2051. }
  2052. //20250212 网站标识
  2053. public function addWebsiteGroup(array $data): array
  2054. {
  2055. //判斷name是不是重复
  2056. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2057. if (!empty($websiteGroupInfo)) {
  2058. return Result::error("网站标识重复", 0);
  2059. }
  2060. //添加信息
  2061. $result = WebsiteGroup::insertGetId($data);
  2062. var_dump($result);
  2063. if (empty($result)) {
  2064. return Result::error("创建失败", 0);
  2065. } else {
  2066. return Result::success($result);
  2067. }
  2068. }
  2069. public function getWebsiteGroupList(array $data): array
  2070. {
  2071. $where = [];
  2072. if (isset($data['name']) && !empty($data['name'])) {
  2073. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2074. }
  2075. $result = WebsiteGroup::where($where)
  2076. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2077. ->get();
  2078. foreach ($result as $websiteGroup) {
  2079. $webIds = json_decode($websiteGroup->web_ids, true);
  2080. $websites = Website::whereIn('id', $webIds)->get();
  2081. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2082. $websiteGroup->website_names = $websiteNames;
  2083. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2084. }
  2085. $count = WebsiteGroup::where($where)->count();
  2086. if (empty($result)) {
  2087. return Result::error("没有数据", 0);
  2088. }
  2089. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2090. }
  2091. public function getWebsiteGroupInfo(array $data): array
  2092. {
  2093. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2094. $webIds = json_decode($websiteInfo->web_ids, true);
  2095. $websites = Website::whereIn('id', $webIds)->get();
  2096. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2097. $websiteInfo->website_names = $websiteNames;
  2098. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2099. if (empty($websiteInfo)) {
  2100. return Result::error("找不到URL", 0);
  2101. }
  2102. return Result::success($websiteInfo->toArray());
  2103. }
  2104. public function updateWebsiteGroup(array $data): array
  2105. {
  2106. $where = [
  2107. 'id' => $data['id'],
  2108. ];
  2109. $insertData = [
  2110. 'name' => $data['name'],
  2111. 'web_ids' => $data['web_ids'],
  2112. ];
  2113. //判斷name是不是重复
  2114. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2115. if (!empty($websiteGroupInfo)) {
  2116. return Result::error("网站标识重复", 0);
  2117. }
  2118. $result = WebsiteGroup::where($where)->update($insertData);
  2119. var_dump($result, '------更新');
  2120. if (empty($result)) {
  2121. return Result::error("更新失败", 0);
  2122. } else {
  2123. return Result::success();
  2124. }
  2125. }
  2126. public function deleteWebsiteGroup(array $data): array
  2127. {
  2128. $where = [
  2129. 'id' => $data['id'],
  2130. ];
  2131. //看看user表是不是有这个id
  2132. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2133. if (!empty($userGroup)) {
  2134. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2135. }
  2136. $result = WebsiteGroup::where($where)->delete();
  2137. if (empty($result)) {
  2138. return Result::error("删除失败", 0);
  2139. } else {
  2140. return Result::success();
  2141. }
  2142. }
  2143. public function getWebsiteNavList(array $data): array
  2144. {
  2145. $where = [];
  2146. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2147. array_push($where, ['website_id', '=', $data['website_id']]);
  2148. }
  2149. if (isset($data['pid'])) {
  2150. array_push($where, ['pid', '=', $data['pid']]);
  2151. }
  2152. $list = WebsiteCategory::query()->where($where)->get();
  2153. if (empty($list)) {
  2154. return Result::error("获取失败", 0);
  2155. } else {
  2156. return Result::success($list);
  2157. }
  2158. }
  2159. // 20250307 根据网站标识获和导航获取站点
  2160. public function getWebsiteNavPoolSite(array $data): array
  2161. {
  2162. $where = [];
  2163. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2164. array_push($where, ['category_id', '=', $data['category_id']]);
  2165. }
  2166. $list = WebsiteCategory::query()->where($where)
  2167. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2168. ->select('website_category.*', 'website.website_name')
  2169. ->distinct()
  2170. ->get();
  2171. if (empty($list)) {
  2172. return Result::error("获取失败", 0);
  2173. } else {
  2174. return Result::success($list);
  2175. }
  2176. }
  2177. public function getWebsiteNavPool(array $data): array
  2178. {
  2179. // 初始化查询条件数组
  2180. $query = WebsiteCategory::query();
  2181. // 如果传入了 websiteids,则添加到查询条件中
  2182. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2183. $query->whereIn('website_id', $data['websiteids']);
  2184. }
  2185. // 如果传入了 pid,则添加到查询条件中
  2186. if (isset($data['pid'])) {
  2187. $query->where('pid', '=', $data['pid']);
  2188. }
  2189. // 执行查询 唯一性 id
  2190. $list = $query->distinct()->get();
  2191. // 根据查询结果返回相应的结果
  2192. if ($list->isEmpty()) {
  2193. return Result::error("获取失败", 0);
  2194. } else {
  2195. return Result::success($list->toArray());
  2196. }
  2197. }
  2198. /**
  2199. *
  2200. * @param array $data
  2201. * @return array
  2202. */
  2203. public function getAllCategory(array $data): array
  2204. {
  2205. $result = Category::when($data, function ($query) use ($data) {
  2206. if (isset($data['name']) && !empty($data['name'])) {
  2207. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2208. }
  2209. })->get();
  2210. if (empty($result)) {
  2211. return Result::error("获取失败", 0);
  2212. } else {
  2213. return Result::success($result);
  2214. }
  2215. }
  2216. /**
  2217. * 修改网站栏目排序
  2218. * @param array $data
  2219. * @return array
  2220. */
  2221. public function upWebsiteCategorySort(array $data): array
  2222. {
  2223. $where = [
  2224. 'website_id' => $data['website_id'],
  2225. 'id' => $data['id'],
  2226. ];
  2227. $result = WebsiteCategory::where($where)->update($data);
  2228. if ($result) {
  2229. return Result::success($result);
  2230. } else {
  2231. return Result::error("更新失败", 0);
  2232. }
  2233. }
  2234. }