WebsiteService.php 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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(
  498. 'ad_place.name as place_name',
  499. 'ad_place.thumb',
  500. 'ad_place.ad_tag',
  501. 'ad_place.introduce',
  502. 'ad.name as ad_name',
  503. 'ad.image_src',
  504. 'ad.image_url',
  505. 'ad.image_alt')
  506. ->get()->all();
  507. if(empty($result)){
  508. return Result::error("此广告位不存在!",0);
  509. }
  510. }else{
  511. return Result::error("请选择广告位!",0);
  512. }
  513. return Result::success($result);
  514. }
  515. /**
  516. *
  517. * @param array $data
  518. * @return array
  519. */
  520. /**网站行政职能搜索 */
  521. public function selectWebsiteDepartment(array $data): array
  522. {
  523. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  524. if (isset($data['keyword']) && !empty($data['keyword'])) {
  525. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  526. if (empty($departments)) {
  527. $result['message'] = "未查询到与此相关职能部门";
  528. } else {
  529. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  530. $m = [
  531. 'department' => $depart,
  532. 'type' => $departments,
  533. 'count' => $count,
  534. ];
  535. }
  536. $result['sele'] = $m;
  537. return Result::success($result['sele']);
  538. }
  539. $result = $depart;
  540. return Result::success($result);
  541. }
  542. /**
  543. * 搜索地区
  544. * @param array $data
  545. * @return array
  546. */
  547. public function selectWebsiteArea(array $data): array
  548. {
  549. // return Result::success($data);
  550. $pid = $data['pid'] ?? 0;
  551. $result = District::where('pid', $pid)->where('status', 1)->orderBy('code')->get();
  552. if (empty($result)) {
  553. return Result::error("未查询到此地区", 0);
  554. } else {
  555. return Result::success($result);
  556. }
  557. }
  558. /**
  559. * 获取栏目
  560. * @param array $data
  561. * @return array
  562. */
  563. public function getWebsiteModelCategory(array $data): array
  564. {
  565. // return Result::success($data);
  566. $website_id = [
  567. 'website_id' => $data['website_id'],
  568. ];
  569. // 初始化 $pid 数组
  570. // $pid = [];
  571. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  572. $pidQuery = WebsiteCategory::where($website_id)
  573. ->where('pid', '!=', 0)
  574. ->orderBy('sort')
  575. ->select('pid', 'category_id', 'alias');
  576. $pid = $pidQuery->pluck('pid');
  577. $pid = array_values(array_unique($pid->toArray()));
  578. // 构建查询语句
  579. $query = WebsiteCategory::where($website_id)
  580. ->where('pid', $data['pid'])
  581. ->offset($data['placeid'])
  582. ->limit($data['num'])
  583. ->orderBy('sort');
  584. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  585. if (!empty($pid)) {
  586. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  587. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  588. } else {
  589. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  590. $query->select('website_category.*', DB::raw('0 as children_count'));
  591. }
  592. // 执行查询
  593. $placeid = $data['placeid'] - 1;
  594. $result = $query->offset($placeid)->limit($data['num'])->get();
  595. if (!empty($result)) {
  596. $pidResults = $pidQuery->get();
  597. $pidMap = $pidResults->keyBy('pid');
  598. $result->each(function ($record) use ($pidMap) {
  599. if ($pidMap->has($record->category_id)) {
  600. $pidResult = $pidMap->get($record->category_id);
  601. $record->chilid_id = $pidResult->category_id;
  602. $record->chilid_alias = $pidResult->alias;
  603. }
  604. });
  605. return Result::success($result);
  606. } else {
  607. return Result::error("本网站暂无栏目", 0);
  608. }
  609. }
  610. /**
  611. * 获取友情链接
  612. * @param array $data
  613. * @return array
  614. */
  615. public function selectWebsiteLinks(array $data): array
  616. {
  617. $where = [
  618. 'website_id' => $data['website_id'],
  619. 'status' => 1,
  620. 'type' => $data['type'],
  621. ];
  622. $num = $data['num'];
  623. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  624. if (!empty($result)) {
  625. return Result::success($result);
  626. } else {
  627. return Result::error("本网站暂无此类型友情链接", 0);
  628. }
  629. }
  630. /**
  631. * 网站首页数据统计, 管理员
  632. * @return void
  633. */
  634. public function getAdminIndex(array $data): array
  635. {
  636. var_dump("用户类型:", $data['type_id']);
  637. switch ($data['type_id']) {
  638. case 4:
  639. $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;');
  640. return Result::success($result);
  641. break;
  642. case 10000:
  643. $res = [];
  644. //网站
  645. $res['website']['count'] = 0;
  646. $res['website']['growth_rate'] = 0;
  647. //资讯
  648. $res['article']['count'] = 0;
  649. $res['article']['growth_rate'] = 0;
  650. //导航池
  651. $res['category']['count'] = 0;
  652. $res['category']['growth_rate'] = 0;
  653. //近一月数据
  654. $res['monthArticle'] = [];
  655. //用户类型
  656. $res['userType'] = [];
  657. $res['website']['count'] = Website::where([])->count();
  658. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  659. $res['category']['count'] = Category::where([])->count();
  660. $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;');
  661. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  662. return Result::success($res);
  663. case 1:
  664. $res = [];
  665. //查询已审核文章
  666. $user_id = $data['user_id'];
  667. $article = Article::where('status', 1)
  668. ->where('admin_user_id', $user_id)
  669. ->orderBy('updated_at', 'desc')
  670. ->limit(10)
  671. ->get();
  672. $res['article'] = $article;
  673. return Result::success($res);
  674. }
  675. return [];
  676. }
  677. /**
  678. * 获取模板类型
  679. * @return void
  680. */
  681. public function getTemplateClass(array $data): array
  682. {
  683. $where = [];
  684. if (isset($data['name']) && $data['name']) {
  685. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  686. }
  687. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  688. if (empty($result)) {
  689. return Result::error("没有模板类型", 0);
  690. } else {
  691. return Result::success($result);
  692. }
  693. }
  694. /**
  695. * 添加模板类型
  696. * @param
  697. * @return void
  698. */
  699. public function addTemplateClass(array $data): array
  700. {
  701. $insertData = [
  702. 'name' => $data['name'],
  703. ];
  704. $result = TemplateClass::insertGetId($insertData);
  705. if (empty($result)) {
  706. return Result::error("创建失败", 0);
  707. } else {
  708. return Result::success(["id" => $result]);
  709. }
  710. }
  711. /**
  712. * 更新模板
  713. * @param array $data
  714. * @return array
  715. */
  716. public function upTemplateClass(array $data): array
  717. {
  718. $where = [
  719. 'id' => $data['id'],
  720. ];
  721. $insertData = [
  722. 'name' => $data['name'],
  723. ];
  724. $result = TemplateClass::where($where)->update($insertData);
  725. if (empty($result)) {
  726. return Result::error("更新失败", 0);
  727. } else {
  728. return Result::success();
  729. }
  730. }
  731. /**
  732. * 删除模板
  733. * @param array $data
  734. * @return array
  735. */
  736. public function delTemplateClass(array $data): array
  737. {
  738. $where = [
  739. 'id' => $data['id'],
  740. ];
  741. $result = TemplateClass::where($where)->delete();
  742. if (empty($result)) {
  743. return Result::error("删除失败", 0);
  744. } else {
  745. return Result::success();
  746. }
  747. }
  748. /**
  749. * 获取分类下的模板
  750. * @param array $data
  751. * @return array
  752. */
  753. public function getTemplate(array $data): array
  754. {
  755. $page = $data['page'];
  756. $pageSize = $data['pageSize'];
  757. $where = [];
  758. if (isset($data['template_class_id']) && $data['template_class_id']) {
  759. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  760. }
  761. $result = Template::where($where)
  762. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  763. $count = Template::where($where)->count();
  764. if (empty($result)) {
  765. return Result::error("没有数据", 0);
  766. }
  767. $data = [
  768. 'rows' => $result->toArray(),
  769. 'count' => $count,
  770. ];
  771. return Result::success($data);
  772. }
  773. /**
  774. * 创建模板
  775. * @param
  776. * @return void
  777. */
  778. public function addTemplate(array $data): array
  779. {
  780. $insertData = [
  781. 'template_name' => $data['template_name'],
  782. 'template_img' => json_encode($data['template_img']),
  783. 'template_class_id' => $data['template_class_id'],
  784. ];
  785. $result = Template::insertGetId($insertData);
  786. if (empty($result)) {
  787. return Result::error("创建模板失败", 0);
  788. } else {
  789. return Result::success(["id" => $result]);
  790. }
  791. }
  792. /**
  793. * 更新模板
  794. * @param array $data
  795. * @return array
  796. */
  797. public function upTemplate(array $data): array
  798. {
  799. $where = [
  800. 'id' => $data['id'],
  801. ];
  802. $insertData = [
  803. 'template_name' => $data['template_name'],
  804. 'template_img' => json_encode($data['template_img']),
  805. 'template_class_id' => $data['template_class_id'],
  806. ];
  807. $result = Template::where($where)->update($insertData);
  808. if (empty($result)) {
  809. return Result::error("更新模板失败", 0);
  810. } else {
  811. return Result::success();
  812. }
  813. }
  814. /**
  815. * 删除模板
  816. * @param array $data
  817. * @return array
  818. */
  819. public function delTemplate(array $data): array
  820. {
  821. $where = [
  822. 'id' => $data['id'],
  823. ];
  824. $result = Template::where($where)->delete();
  825. if (empty($result)) {
  826. return Result::error("删除模板失败", 0);
  827. } else {
  828. return Result::success();
  829. }
  830. }
  831. /**
  832. * 搜索网站
  833. * @param array $data
  834. * @return array
  835. */
  836. public function websiteList(array $data): array
  837. {
  838. $where = [];
  839. if (isset($data['keyword']) && !empty($data['keyword'])) {
  840. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  841. }
  842. $result = Website::where($where)->get();
  843. if ($result) {
  844. return Result::success($result);
  845. } else {
  846. return Result::error("没有网站", 0);
  847. }
  848. }
  849. public function addWebsiteCategory(array $data): array
  850. {
  851. $website_id = $data['website_id'];
  852. $category_arr_id = $data['category_arr_id'];
  853. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  854. $categoryListIds = [];
  855. if ($categoryList) {
  856. foreach ($categoryList->toArray() as $val) {
  857. array_push($categoryListIds, $val['id']);
  858. }
  859. }
  860. $arr = [];
  861. if ($categoryListIds) {
  862. foreach ($categoryListIds as $v) {
  863. $ids = $this->getUnderlingUIds(intval($v));
  864. $ids_arr = explode(",", $ids);
  865. array_push($arr, $ids_arr);
  866. }
  867. }
  868. $mergedArray = [];
  869. foreach ($arr as $subarray) {
  870. $mergedArray = array_merge($mergedArray, $subarray);
  871. }
  872. var_dump("所有:", $arr, $mergedArray);
  873. //查询出所有的分类进行分割插入 组装数据
  874. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  875. $categoryListData = $categoryListData->toArray();
  876. $insertData = [];
  877. if ($categoryListData) {
  878. foreach ($categoryListData as $key => $value) {
  879. $insertData[$key]['website_id'] = $website_id;
  880. $insertData[$key]['name'] = $value['name'];
  881. $insertData[$key]['sort'] = $value['sort'];
  882. $insertData[$key]['pid'] = $value['pid'];
  883. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  884. $insertData[$key]['seo_title'] = $value['seo_title'];
  885. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  886. $insertData[$key]['seo_description'] = $value['seo_description'];
  887. $insertData[$key]['alias'] = $value['name'];
  888. $insertData[$key]['category_id'] = $value['id'];
  889. }
  890. }
  891. $result = WebsiteCategory::insert($insertData);
  892. var_dump("插入数据状态:", $result);
  893. if ($result) {
  894. return Result::success($result);
  895. } else {
  896. return Result::error("创建失败", 0);
  897. }
  898. }
  899. /**
  900. * 删除网站导航
  901. * @param array $data
  902. * @return array
  903. */
  904. public function delWebsiteCategory(array $data): array
  905. {
  906. $website_id = $data['website_id'] ?? 0;
  907. $category_id = $data['category_id'] ?? 0;
  908. $ids = $this->getUnderlingUIds(intval($category_id));
  909. $ids_arr = explode(",", $ids);
  910. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  911. if ($result) {
  912. return Result::success($result);
  913. } else {
  914. return Result::error("删除失败", 0);
  915. }
  916. }
  917. /**
  918. * 获取网站导航
  919. * @param array $data
  920. * @return array
  921. */
  922. public function getAdminWebsiteCategory(array $data): array
  923. {
  924. $where = [
  925. 'website_id' => $data['website_id'],
  926. ];
  927. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  928. $list = [];
  929. if ($result) {
  930. foreach ($result->toArray() as $val) {
  931. array_push($list, json_decode($val['category_arr_id']));
  932. }
  933. }
  934. if ($result) {
  935. return Result::success($list);
  936. } else {
  937. return Result::error("查询失败", 0);
  938. }
  939. }
  940. /**
  941. * 更新网站导航
  942. * @param array $data
  943. * @return array
  944. */
  945. public function upWebsiteCategory(array $data): array
  946. {
  947. var_dump("更新数据:", $data);
  948. $pinyin = new Pinyin();
  949. Db::beginTransaction();
  950. try {
  951. if ($data['category_arr_id']) {
  952. $oldCategoryList = WebsiteCategory::where(['website_id' => $data['website_id']])->get();
  953. if ($oldCategoryList) {
  954. $oldCategoryList = $oldCategoryList->toArray();
  955. $oldCategoryListIds = [];
  956. foreach ($oldCategoryList as $val) {
  957. array_push($oldCategoryListIds, $val['category_id']);
  958. }
  959. $newCategoryListIds = [];
  960. foreach ($data['category_arr_id'] as $v) {
  961. array_push($newCategoryListIds, end($v));
  962. }
  963. $differenceIDS = array_merge(array_diff($oldCategoryListIds, $newCategoryListIds), array_diff($newCategoryListIds, $oldCategoryListIds));
  964. if ($differenceIDS) {
  965. foreach ($differenceIDS as $v) {
  966. WebsiteCategory::where(['website_id' => $data['website_id'], 'category_id' => $v])->delete();
  967. }
  968. }
  969. foreach ($data['category_arr_id'] as $key => $value) {
  970. $info = Category::where(['id' => end($value)])->first();
  971. $info = $info->toArray();
  972. // 假设 $data、$value 和 $info 是已经定义好的变量
  973. $attributes = [
  974. 'website_id' => $data['website_id'],
  975. 'category_id' => end($value),
  976. ];
  977. // 先复制一份要更新或创建的数据
  978. $aLIas_pinyin = $pinyin->permalink($info['name'], '');
  979. var_dump($aLIas_pinyin, '别名');
  980. $values = [
  981. 'website_id' => $data['website_id'],
  982. 'name' => $info['name'] ?? '',
  983. 'alias' => $info['name'] ?? '',
  984. 'aLIas_pinyin' => $aLIas_pinyin ?? '',
  985. 'sort' => $info['sort'] ?? 0,
  986. 'pid' => $info['pid'] ?? 0,
  987. 'pid_arr' => $info['pid_arr'] ?? json_encode([]),
  988. 'seo_title' => $info['seo_title'] ?? "",
  989. 'seo_keywords' => $info['seo_keywords'] ?? "",
  990. 'seo_description' => $info['seo_description'] ?? "",
  991. 'is_url' => $info['is_url'] ?? 0,
  992. 'web_url' => $info['web_url'] ?? '',
  993. 'category_id' => end($value),
  994. 'category_arr_id' => $value ? json_encode($value) : json_encode([])
  995. ];
  996. // 检查数据库中是否已有该记录
  997. $existingRecord = WebsiteCategory::where($attributes)->first();
  998. if ($existingRecord && $existingRecord->name) {
  999. unset($values['name']);
  1000. }
  1001. if ($existingRecord && $existingRecord->sort >= 0) {
  1002. unset($values['sort']);
  1003. }
  1004. if ($existingRecord && $existingRecord->seo_title) {
  1005. unset($values['seo_title']);
  1006. }
  1007. if ($existingRecord && $existingRecord->seo_keywords) {
  1008. unset($values['seo_keywords']);
  1009. }
  1010. if ($existingRecord && $existingRecord->seo_description) {
  1011. unset($values['seo_description']);
  1012. }
  1013. if ($existingRecord && $existingRecord->alias) {
  1014. unset($values['alias']);
  1015. }
  1016. // 执行 updateOrCreate 操作
  1017. var_dump("更新数据:", $values);
  1018. WebsiteCategory::updateOrCreate($attributes, $values);
  1019. }
  1020. }
  1021. }
  1022. Db::commit();
  1023. return Result::success();
  1024. } catch (\Throwable $ex) {
  1025. Db::rollBack();
  1026. // var_dump($ex->getMessage());
  1027. return Result::error("修改失败" . $ex->getMessage(), 0);
  1028. }
  1029. }
  1030. /**
  1031. * 获取网站列表
  1032. * @param array $data
  1033. * @return array
  1034. */
  1035. public function getWebsiteCategoryList(array $data): array
  1036. {
  1037. $where = [];
  1038. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1039. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1040. }
  1041. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1042. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1043. }
  1044. $result = Website::where($where)
  1045. ->with(["websiteCategory" => function ($query) {
  1046. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1047. }])
  1048. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1049. ->get();
  1050. $count = Website::where($where)->count();
  1051. if (empty($result)) {
  1052. return Result::error("没有数据", 0);
  1053. }
  1054. $data = [
  1055. 'rows' => $result->toArray(),
  1056. 'count' => $count,
  1057. ];
  1058. if ($result) {
  1059. return Result::success($data);
  1060. } else {
  1061. return Result::error("查询失败", 0);
  1062. }
  1063. }
  1064. /**
  1065. * 删除网站下的所有导航
  1066. * @param array $data
  1067. * @return array
  1068. */
  1069. public function delWebsiteAllCategory(array $data): array
  1070. {
  1071. $website_id = $data['website_id'];
  1072. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1073. if ($result) {
  1074. return Result::success($result);
  1075. } else {
  1076. return Result::error("删除失败", 0);
  1077. }
  1078. }
  1079. /**
  1080. * 获取网站下的某一个导航
  1081. * @param array $data
  1082. * @return array
  1083. */
  1084. public function getWebsiteCategoryOnes(array $data): array
  1085. {
  1086. $website_id = $data['website_id'];
  1087. $category_id = $data['category_id'];
  1088. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1089. ->first();
  1090. if ($result) {
  1091. return Result::success($result);
  1092. } else {
  1093. return Result::error("查询失败", 0);
  1094. }
  1095. }
  1096. /**
  1097. * 更新网闸下的某一个导航
  1098. * @param array $data
  1099. * @return array
  1100. */
  1101. public function upWebsiteCategoryones(array $data): array
  1102. {
  1103. //处理所有数据
  1104. //获取所有数据
  1105. if (isset($data['all']) && $data['all'] == 'all') {
  1106. $result = WebsiteCategory::get();
  1107. $result = $result->toArray();
  1108. var_dump($result, '------------');
  1109. $pinyin = new Pinyin();
  1110. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1111. for ($i = 0; $i < count($result); $i++) {
  1112. // $randss = substr(str_shuffle($letters), 0, 4);
  1113. $result[$i]['aLIas_pinyin'] = $pinyin->permalink($result[$i]['alias'], '');
  1114. WebsiteCategory::where(['id' => $result[$i]['id']])->update(['aLIas_pinyin' => $result[$i]['aLIas_pinyin']]);
  1115. }
  1116. var_dump($result, '--------222----');
  1117. if ($result) {
  1118. return Result::success($result);
  1119. } else {
  1120. return Result::error("更新失败", 0);
  1121. }
  1122. }
  1123. //处理所有数据
  1124. $where = [
  1125. 'website_id' => $data['website_id'],
  1126. 'category_id' => $data['category_id'],
  1127. ];
  1128. $alias = $data['alias'];
  1129. $pinyin = new Pinyin();
  1130. $aLIas_pinyin = $pinyin->permalink($alias, '');
  1131. //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
  1132. $result = WebsiteCategory::where($where)->first();
  1133. $result = $result->toArray();
  1134. //+四位随机字母
  1135. $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  1136. // 打乱字符串顺序并截取前四位
  1137. // $randss = substr(str_shuffle($letters), 0, 4);
  1138. if ($data['alias_pinyin'] == '') {
  1139. $data['aLIas_pinyin'] = $aLIas_pinyin;
  1140. }
  1141. $result = WebsiteCategory::where($where)->update($data);
  1142. if ($result) {
  1143. return Result::success($result);
  1144. } else {
  1145. return Result::error("更新失败", 0);
  1146. }
  1147. }
  1148. /**
  1149. * 获取网站下的所有导航(包含子导航)
  1150. * @param array $data
  1151. * @return array
  1152. */
  1153. public function getWebsiteAllCategory(array $data): array
  1154. {
  1155. $where = [];
  1156. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1157. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1158. }
  1159. // if (isset($data['name']) && !empty($data['name'])) {
  1160. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1161. // }
  1162. // if (isset($data['alias']) && !empty($data['alias'])) {
  1163. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1164. // }
  1165. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1166. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1167. // }
  1168. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1169. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1170. // }
  1171. $result = WebsiteCategory::where($where)
  1172. ->leftJoin("category", 'website_category.category_id', 'category.id')
  1173. ->select("website_category.*", "category.name as name")
  1174. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1175. // ->leftJoin("department", 'category.department_id', 'department.id')
  1176. // ->leftJoin("district", 'category.city_id', 'district.id')
  1177. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1178. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1179. ->orderBy("sort", "asc")
  1180. ->get();
  1181. // $count = WebsiteCategory::where($where)
  1182. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1183. // ->leftJoin("department", 'category.department_id', 'department.id')
  1184. // ->leftJoin("district", 'category.city_id', 'district.id')
  1185. // ->count();
  1186. if (empty($result)) {
  1187. return Result::error("没有数据", 0);
  1188. }
  1189. // $data = [
  1190. // 'rows' => $result->toArray(),
  1191. // 'count' => $count,
  1192. // ];
  1193. if ($result) {
  1194. return Result::success($result->toArray());
  1195. } else {
  1196. return Result::error("查询失败", 0);
  1197. }
  1198. }
  1199. /**
  1200. * 递归查询数据
  1201. * @param $id
  1202. * @param $ids
  1203. * @return string
  1204. */
  1205. public function getUnderlingUIds($id, $ids = '')
  1206. {
  1207. $back = Category::where(['pid' => $id])->get();
  1208. $back = $back->toArray();
  1209. if (!empty($back) && is_array($back)) {
  1210. foreach ($back as $v) {
  1211. //防止当前人的ID重复去查询,形成恶性循环
  1212. if ($v['id'] == $id) {
  1213. continue;
  1214. }
  1215. $back2 = Category::where(['pid' => $id])->count('id');
  1216. if ($back2 > 0) {
  1217. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1218. } else {
  1219. $ids .= ',' . $v['id'];
  1220. }
  1221. }
  1222. }
  1223. $ids = $id . ',' . $ids . ',';
  1224. $ids = str_replace(',,', ",", $ids);
  1225. $ids = trim($ids, ',');
  1226. return $ids;
  1227. }
  1228. /**
  1229. * 检测网站名称是否重复
  1230. * @param array $data
  1231. * @return array
  1232. */
  1233. public function checkWebsiteName(array $data): array
  1234. {
  1235. if (isset($data['id'])) {
  1236. $data[] = ['id', "!=", $data['id']];
  1237. unset($data['id']);
  1238. }
  1239. $websiteInfo = Website::query()->where($data)->first();
  1240. if (empty($websiteInfo)) {
  1241. return Result::error("找不到网站", 0);
  1242. }
  1243. return Result::success($websiteInfo->toArray());
  1244. }
  1245. /**
  1246. * 检测网站url是否重复
  1247. * @param array $data
  1248. * @return array
  1249. */
  1250. public function checkWebsiteUrl(array $data): array
  1251. {
  1252. $whereData = [];
  1253. if (isset($data['id'])) {
  1254. $whereData = [['id', "!=", $data['id']]];
  1255. unset($data['id']);
  1256. }
  1257. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1258. if (empty($websiteInfo)) {
  1259. return Result::error("找不到URL", 0);
  1260. }
  1261. return Result::success($websiteInfo->toArray());
  1262. }
  1263. /**
  1264. * 获取网站底部基础信息
  1265. * @param array $data
  1266. * @return array
  1267. */
  1268. public function getWebsiteFootInfo(array $data): array
  1269. {
  1270. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1271. $website_head = Website::where('id', $data['website_id'])
  1272. ->where('status', 1)
  1273. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1274. ->first();
  1275. if (empty($website_head)) {
  1276. return Result::error("找不到网站", 0);
  1277. }
  1278. } else {
  1279. return Result::error("参数错误", 0);
  1280. }
  1281. $website_head = Website::where('id', $data['website_id'])
  1282. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1283. $website_foot = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1284. if (empty($website_foot)) {
  1285. return Result::error("暂无底部基础信息", 0);
  1286. }
  1287. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1288. $result = [
  1289. 'website_foot' => $website_foot,
  1290. 'website_head' => $website_head,
  1291. ];
  1292. return Result::success($result);
  1293. }
  1294. /**
  1295. * 获取网站底部导航
  1296. * @param array $data
  1297. * @return array
  1298. */
  1299. public function getWebsiteFooterCategory(array $data): array
  1300. {
  1301. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1302. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1303. if (empty($website)) {
  1304. return Result::error("找不到网站", 0);
  1305. }
  1306. } else {
  1307. return Result::error("参数错误", 0);
  1308. }
  1309. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1310. if (empty($result)) {
  1311. return Result::error("暂无底部导航", 0);
  1312. }
  1313. return Result::success($result->toArray());
  1314. }
  1315. /**
  1316. * 获取网站底部导航列表
  1317. * @param array $data
  1318. * @return array
  1319. */
  1320. public function getWebsiteFooterCategoryList(array $data): array
  1321. {
  1322. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1323. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1324. if (empty($website)) {
  1325. return Result::error("找不到网站", 0);
  1326. }
  1327. } else {
  1328. return Result::error("参数错误", 0);
  1329. }
  1330. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1331. // '底部导航类型 0:内容型;1:列表型;',
  1332. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1333. return Result::error("底部导航id错误", 0);
  1334. } else {
  1335. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1336. if ($query->count() == 0) {
  1337. return Result::error("暂无底部导航列表", 0);
  1338. } elseif ($query->count() == 1) {
  1339. $result = $query->first();
  1340. } else {
  1341. $result = $query->get();
  1342. }
  1343. }
  1344. return Result::success($result);
  1345. }
  1346. /**
  1347. * 获取网站底部导航
  1348. * @param array $data
  1349. * @return array
  1350. */
  1351. public function getWebsiteFooterCategoryInfo(array $data): array
  1352. {
  1353. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1354. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1355. if (empty($website)) {
  1356. return Result::error("找不到网站", 0);
  1357. }
  1358. } else {
  1359. return Result::error("参数错误", 0);
  1360. }
  1361. if (isset($data['type']) && $data['type'] == 0) {
  1362. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1363. if (empty($fcatid)) {
  1364. return Result::error("底部导航id错误", 0);
  1365. }
  1366. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1367. } else {
  1368. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1369. }
  1370. if (empty($result)) {
  1371. return Result::error("暂无底部导航内容", 0);
  1372. }
  1373. return Result::success($result);
  1374. }
  1375. /*
  1376. * 搜索网站二级导航
  1377. * @param array $data
  1378. * @return array
  1379. * */
  1380. public function selectWebsiteCategory(array $data): array
  1381. {
  1382. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1383. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1384. if (empty($website)) {
  1385. return Result::error("暂无该网站", 0);
  1386. }
  1387. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1388. // return Result::success($category);
  1389. if (empty($category)) {
  1390. return Result::error("暂无此导航", 0);
  1391. }
  1392. $query = Category::whereIn('id', $category);
  1393. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1394. $cityid = District::where('id', $data['cityid'])->first();
  1395. if (empty($cityid)) {
  1396. return Result::error("暂无此城市", 0);
  1397. } else {
  1398. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1399. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1400. if (empty($city_category)) {
  1401. return Result::error("暂无此城市下的导航", 0);
  1402. }
  1403. $result['catid'] = $city_category;
  1404. $city = 1;
  1405. // var_dump("城市====================",$result);
  1406. }
  1407. }
  1408. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1409. $departmentid = Department::where('id', $data['department_id'])->first();
  1410. if (empty($departmentid)) {
  1411. return Result::error("暂无此部门", 0);
  1412. } else {
  1413. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1414. if (empty($depart_category)) {
  1415. return Result::error("暂无此部门下的导航", 0);
  1416. }
  1417. $result['catid'] = $depart_category;
  1418. // var_dump("职能部门*******************",$result);
  1419. $department = 1;
  1420. }
  1421. }
  1422. if (!empty($city) && !empty($department)) {
  1423. // var_dump("城市和职能部门----------------------",$result);
  1424. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1425. if (empty($sel_category)) {
  1426. return Result::error("暂无此城市和职能部门下的导航", 0);
  1427. }
  1428. $result['catid'] = $sel_category;
  1429. }
  1430. if (empty($result)) {
  1431. return Result::error("暂无导航", 0);
  1432. }
  1433. // department_id
  1434. } else {
  1435. return Result::error("参数错误", 0);
  1436. }
  1437. return Result::success($result);
  1438. }
  1439. /**
  1440. * 获取网站栏目seo
  1441. * @param array $data
  1442. * @return array
  1443. */
  1444. public function getWebsiteCategoryHead(array $data): array
  1445. {
  1446. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1447. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1448. if (empty($website)) {
  1449. return Result::error("找不到网站", 0);
  1450. }
  1451. } else {
  1452. return Result::error("参数错误", 0);
  1453. }
  1454. if (isset($data['catid']) && !empty($data['catid'])) {
  1455. $result = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->first();
  1456. }
  1457. if (empty($result)) {
  1458. return Result::error("暂无导航", 0);
  1459. }
  1460. $result['website_name'] = $website['website_name'] ?? '';
  1461. $result['suffix'] = $website['suffix'] ?? '';
  1462. return Result::success($result);
  1463. }
  1464. /*
  1465. * 获取某个栏目
  1466. * @param array $data
  1467. * @return array
  1468. * */
  1469. public function getOneWebsiteCategory(array $data): array
  1470. {
  1471. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1472. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1473. if (empty($website)) {
  1474. return Result::error("暂无该网站", 0);
  1475. }
  1476. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1477. if (empty($category)) {
  1478. return Result::error("暂无此导航", 0);
  1479. }
  1480. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1481. $parent = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $category['pid'])->select('category_id as parent_id', 'alias as parent_name', 'aLIas_pinyin')->first();
  1482. $category['parent_id'] = $parent['parent_id'] ?? '';
  1483. $category['parent_pinyin'] = $parent['aLIas_pinyin'] ?? '';
  1484. $category['parent_name'] = $parent['parent_name'] ?? '';
  1485. return Result::success($category);
  1486. } else {
  1487. return Result::error("参数错误", 0);
  1488. }
  1489. }
  1490. // 自助建站--fr-------------------
  1491. /**
  1492. * 获取并搜索 网站模板信息
  1493. * @param array $data
  1494. * @return array
  1495. */
  1496. public function getWebsiteintel(array $data): array
  1497. {
  1498. //查询所有网站模板信息
  1499. $query = Website::where('website.status', 1)
  1500. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1501. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1502. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1503. ->select(
  1504. "website_template_info.id",
  1505. "template_class.name",
  1506. "website.id as website_id",
  1507. "website.website_name",
  1508. "website.website_url",
  1509. "website_template_info.action_id",
  1510. "website_template_info.created_at",
  1511. "website_template_info.updated_at",
  1512. "website_template_info.page_type",
  1513. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1514. "template.template_name"
  1515. );
  1516. //若存在条件;则在$query的基础上进行筛选
  1517. if (isset($data['website_name']) && !empty($data['website_name'])) {
  1518. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1519. }
  1520. if (isset($data['status']) && !empty($data['status'])) {
  1521. $query->where('website_template_info.status', $data['status']);
  1522. }
  1523. $count = $query->count();
  1524. $query->limit($data['pageSize'])
  1525. ->offset(($data['page'] - 1) * $data['pageSize'])
  1526. ->orderBy("website_template_info.updated_at", "desc");
  1527. $rep = $query->get();
  1528. if ($rep->count() == 0) {
  1529. return Result::error("没有查找到相关数据", 0);
  1530. } else {
  1531. $rep->each(function ($item) {
  1532. if (!empty($item->page_type)) {
  1533. $pageTypeArray = json_decode($item->page_type, true);
  1534. if (is_array($pageTypeArray)) {
  1535. $item->page_type = $pageTypeArray;
  1536. }
  1537. }
  1538. });
  1539. $result = [
  1540. "rows" => $rep->toArray(),
  1541. "count" => $count,
  1542. ];
  1543. return Result::success($result);
  1544. }
  1545. // return Result::success($result);
  1546. }
  1547. /**
  1548. * 添加网站基础信息
  1549. * @param array $data
  1550. * @return array
  1551. */
  1552. public function addWebsiteTemplateintel(array $data): array
  1553. {
  1554. $website = Website::where('website.id', $data['website_id'])->first();
  1555. if (empty($website)) {
  1556. return Result::error("请输入正确的网站id!", 0);
  1557. }
  1558. if (empty($website['website_column_id'])) {
  1559. return Result::error("请先关联导航池!", 0);
  1560. }
  1561. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1562. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1563. if (empty($foot_type)) {
  1564. return Result::error("请先关联底部导航池!", 0);
  1565. } elseif (in_array(1, $foot_type)) {
  1566. $foot_type = 1;
  1567. } else {
  1568. $foot_type = 0;
  1569. }
  1570. $result['foot_type'] = $foot_type;
  1571. // 友情链接类型'1:图片 2:文字 3:底部';
  1572. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1573. $missingTypes = [];
  1574. if (!in_array(1, $types)) {
  1575. $missingTypes[] = "文字链接";
  1576. }
  1577. if (!in_array(2, $types)) {
  1578. $missingTypes[] = "图片链接";
  1579. }
  1580. if (!in_array(3, $types)) {
  1581. $missingTypes[] = "底部链接";
  1582. }
  1583. if (!empty($missingTypes)) {
  1584. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1585. return Result::error($errorMessage, 0);
  1586. }
  1587. if (isset($data['page_type'])) {
  1588. // 删除重复元素并重新索引数组
  1589. $data['page_type'] = array_unique($data['page_type']);
  1590. $data['page_type'] = array_values($data['page_type']);
  1591. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1592. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1593. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1594. if (empty($info)) {
  1595. // 将数组转换为 JSON 字符串
  1596. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1597. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1598. $data['action_id'] = 1;
  1599. try {
  1600. $result = WebsiteTemplateInfo::insertGetId($data);
  1601. } catch (\Exception $e) {
  1602. if ($e->getCode() == 22001) {
  1603. $errorMessage = $e->getMessage();
  1604. return Result::error("请检查表单,某个字段超出了长度限制!");
  1605. } else {
  1606. // 处理其他类型的数据库错误
  1607. return Result::error("插入操作失败: " . $e->getMessage());
  1608. }
  1609. return Result::error("添加失败!", 0);
  1610. }
  1611. } else {
  1612. return Result::error("该网站已经构建过了!", 0);
  1613. }
  1614. } else {
  1615. return Result::error("请先选择首页和底部导航详情页!", 0);
  1616. }
  1617. }
  1618. if (empty($result)) {
  1619. return Result::error("添加失败!", 0);
  1620. } else {
  1621. return Result::success($result);
  1622. }
  1623. }
  1624. /**
  1625. * 获取网站基础信息
  1626. *
  1627. * @return array
  1628. */
  1629. public function getWebsiteTemplateintel(array $data): array
  1630. {
  1631. $result = WebsiteTemplateInfo::where('website_template_info.website_id', $data['website_id'])
  1632. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1633. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1634. ->select(
  1635. "website_template_info.*",
  1636. "footer_category.type",
  1637. "website.website_name",
  1638. "website.id as website_id"
  1639. )
  1640. ->first();
  1641. if (empty($result['website_name'])) {
  1642. return Result::error("请输入正确的网站id!", 0);
  1643. }
  1644. if ($result) {
  1645. $result['page_type'] = json_decode($result['page_type'], true);
  1646. return Result::success($result);
  1647. } else {
  1648. return Result::error("请先添加网站基础信息!", 0);
  1649. }
  1650. }
  1651. /**
  1652. * 修改网站基础信息
  1653. *
  1654. * @return array
  1655. */
  1656. public function upWebsiteTemplateintel(array $data): array
  1657. {
  1658. $web = Website::where('website.id', $data['website_id'])->first();
  1659. if (empty($web)) {
  1660. return Result::error("请输入正确的网站id!", 0);
  1661. }
  1662. $where = ['website_id' => $data['website_id']];
  1663. $result = WebsiteTemplateInfo::where($where)->first();
  1664. if (empty($result)) {
  1665. return Result::error("请先添加网站基础信息!", 0);
  1666. } else {
  1667. unset($data['website_id']);
  1668. // 删除重复元素
  1669. $data['page_type'] = array_unique($data['page_type']);
  1670. // 重新索引数组
  1671. $data['page_type'] = array_values($data['page_type']);
  1672. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1673. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1674. // 将数组转换为 JSON 字符串
  1675. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1676. try {
  1677. $result = WebsiteTemplateInfo::where($where)->update($data);
  1678. } catch (\Exception $e) {
  1679. if ($e->getCode() == 22001) {
  1680. $errorMessage = $e->getMessage();
  1681. return Result::error("请检查表单,某个字段超出了长度限制!");
  1682. } else {
  1683. // 处理其他类型的数据库错误
  1684. return Result::error("修改操作失败: " . $e->getMessage());
  1685. }
  1686. return Result::error("修改失败!", 0);
  1687. }
  1688. } else {
  1689. return Result::error("请先选择首页和底部导航详情页!", 0);
  1690. }
  1691. }
  1692. if (empty($result)) {
  1693. return Result::error("修改失败!", 0);
  1694. } else {
  1695. return Result::success($result);
  1696. }
  1697. }
  1698. /**
  1699. * 获取所有网站风格信息
  1700. *
  1701. * @return array
  1702. */
  1703. public function getAllTemplateClass(array $data): array
  1704. {
  1705. $result = TemplateClass::all();
  1706. if (empty($result)) {
  1707. return Result::error("没有查找到相关数据!", 0);
  1708. } else {
  1709. return Result::success($result);
  1710. }
  1711. }
  1712. /**
  1713. * 搜索并获取网站模板信息
  1714. * @param array $data
  1715. * @return array
  1716. */
  1717. public function getWebsiteTemplateList(array $data): array
  1718. {
  1719. $website = Website::where('website.id', $data['website_id'])->first();
  1720. if (empty($website)) {
  1721. return Result::error("请输入正确的网站id!", 0);
  1722. } else {
  1723. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1724. if (empty($page_type)) {
  1725. return Result::error("此网站还未添加基础信息!", 0);
  1726. } else {
  1727. $where = json_decode($page_type['page_type'], true);
  1728. // $where = $data["page_type"];
  1729. if (isset($data['template_class_id']) && !empty($data['template_class_id'])) {
  1730. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1731. if (empty($template_class)) {
  1732. return Result::error("请输入正确的模板风格id!", 0);
  1733. } else {
  1734. // 获取指定风格下的模板
  1735. $rep = Template::where('template_class_id', $data['template_class_id'])
  1736. ->where(function ($query) use ($where) {
  1737. foreach ($where as $value) {
  1738. $query->whereJsonContains('template_img', ['value' => $value]);
  1739. }
  1740. })
  1741. ->limit($data['pageSize'])
  1742. ->offset(($data['page'] - 1) * $data['pageSize'])
  1743. ->orderBy("template.updated_at", "desc")
  1744. ->get();
  1745. $count = Template::where('template_class_id', $data['template_class_id'])
  1746. ->where(function ($query) use ($where) {
  1747. foreach ($where as $value) {
  1748. $query->whereJsonContains('template_img', ['value' => $value]);
  1749. }
  1750. })->count();
  1751. }
  1752. } else {
  1753. //获取所有模板
  1754. $rep = Template::where(function ($query) use ($where) {
  1755. foreach ($where as $value) {
  1756. $query->whereJsonContains('template_img', ['value' => $value]);
  1757. }
  1758. })
  1759. ->limit($data['pageSize'])
  1760. ->offset(($data['page'] - 1) * $data['pageSize'])
  1761. ->orderBy("template.updated_at", "desc")
  1762. ->get();
  1763. $count = Template::where(function ($query) use ($where) {
  1764. foreach ($where as $value) {
  1765. $query->whereJsonContains('template_img', ['value' => $value]);
  1766. }
  1767. })->count();
  1768. }
  1769. if (empty($rep) || $rep->count() == 0) {
  1770. return Result::error("没有查找到相关数据!", 0);
  1771. } else {
  1772. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1773. // $rep->each(function ($item) use ($where) {
  1774. // $template_img = json_decode($item->template_img, true);
  1775. // if(is_array($template_img)){
  1776. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1777. // return isset($img['value']) && in_array($img['value'], $where);
  1778. // });
  1779. // }
  1780. // $item->template_img = $filtered_img;
  1781. // });
  1782. // $result = [
  1783. // "rows" => $rep->toArray(),
  1784. // "count" => $count
  1785. // ];
  1786. $rep->each(function ($item) use ($where) {
  1787. $template_img = json_decode($item->template_img, true);
  1788. if (is_array($template_img)) {
  1789. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1790. return isset($img['value']) && in_array($img['value'], $where);
  1791. });
  1792. // 对过滤后的数组进行重新排序
  1793. $sorted_img = array_values($filtered_img);
  1794. $item->template_img = $sorted_img;
  1795. } else {
  1796. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1797. }
  1798. });
  1799. $result = [
  1800. "rows" => $rep->toArray(),
  1801. "count" => $count,
  1802. ];
  1803. }
  1804. }
  1805. }
  1806. return Result::success($result);
  1807. }
  1808. /**
  1809. * 添加网站模板
  1810. * @param array $data
  1811. */
  1812. public function addWebsiteTemplateclassintel(array $data): array
  1813. {
  1814. $template = Template::where('id', $data['template_id'])->first();
  1815. $web = Website::where('id', $data['website_id'])->first();
  1816. if (empty($template)) {
  1817. return Result::error("请输入正确的模板id", 0);
  1818. }
  1819. if (empty($web)) {
  1820. return Result::error("请输入正确的网站id", 0);
  1821. }
  1822. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id'], 'action_id' => 2]);
  1823. if (empty($result)) {
  1824. return Result::error("添加失败", 0);
  1825. } else {
  1826. return Result::success($result);
  1827. }
  1828. }
  1829. /**
  1830. * 获取网站模板信息
  1831. * @param array $data
  1832. */
  1833. public function getWebsiteTemplateclassintel(array $data): array
  1834. {
  1835. // return Result::success($data);
  1836. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1837. if (empty($template)) {
  1838. return Result::error("请输入正确的搭建网站id", 0);
  1839. }
  1840. $page_type = json_decode($template['page_type'], true);
  1841. // 获取指定网站下的基础信息和模板及风格信息
  1842. $result = WebsiteTemplateInfo::where([
  1843. ['website_template_info.website_id', $data['website_id']],
  1844. ['template_id', '!=', null],
  1845. ])
  1846. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1847. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1848. ->where(function ($query) use ($page_type) {
  1849. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1850. foreach ($page_type as $pageType) {
  1851. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1852. }
  1853. })
  1854. ->select(
  1855. 'template.template_name',
  1856. 'template.template_img',
  1857. 'template.id as tid',
  1858. 'template_class.name',
  1859. 'template_class.id as class_id',
  1860. 'website_template_info.page_type',
  1861. 'website_template_info.website_id'
  1862. )
  1863. ->first();
  1864. if (empty($result)) {
  1865. return Result::error("没有查找到相关数据", 0);
  1866. }
  1867. // return Result::success($result);
  1868. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1869. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1870. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1871. foreach ($template_img as $key => $value) {
  1872. if (!in_array($value['value'], $page_type)) {
  1873. unset($template_img[$key]);
  1874. }
  1875. }
  1876. // return Result::success($result);
  1877. $result['template_img'] = array_values($template_img);
  1878. $result['page_type'] = $page_type;
  1879. // $templateList = $this->getWebsiteTemplateList($data);
  1880. // $count = $templateList['data']['count']??'';
  1881. // $pages = $count / $data['pageSize'];
  1882. // $found = false;
  1883. // while ($pages && !$found) {
  1884. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1885. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1886. // if ($index !== false) {
  1887. // $found = true;
  1888. // $result['page'] = $pages;
  1889. // $pages = 0;
  1890. // // echo "查询到的 template 在第 $page 页";
  1891. // }
  1892. // $pages--;
  1893. // }
  1894. return Result::success($result);
  1895. }
  1896. /**
  1897. * 获取网站模板下的板块信息
  1898. * @param array $data
  1899. */
  1900. public function getWebsiteSectorList(array $data): array
  1901. {
  1902. $website = Website::where('id', $data['website_id'])->first();
  1903. if (empty($website)) {
  1904. return Result::error("请输入正确的网站id", 0);
  1905. }
  1906. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1907. if (empty($template['template_id'])) {
  1908. return Result::error("请先添加网站模板", 0);
  1909. }
  1910. $query = Sector::where('template_id', $template['template_id']);
  1911. if ($query->count() == 0) {
  1912. return Result::error("没有查找到相关板块数据", 0);
  1913. } elseif ($query->count() == 1) {
  1914. $sector = $query->first();
  1915. } else {
  1916. $sector = $query->get();
  1917. }
  1918. $result['page_type'] = $template['page_type'];
  1919. $result['tid'] = $template['template_id'];
  1920. $result = [
  1921. "tid" => $template['template_id'],
  1922. "page_type" => $template['page_type'],
  1923. "sector" => $sector,
  1924. ];
  1925. return Result::success($result);
  1926. }
  1927. // 自助建站---------------fr---------------
  1928. // 网站底基础信息--fr-------------------
  1929. /**
  1930. * 添加网站底基础信息
  1931. * @param array $data
  1932. */
  1933. public function addWebFootInfo(array $data): array
  1934. {
  1935. $website = Website::where('id', $data['website_id'])->first();
  1936. if (empty($website)) {
  1937. return Result::error("请输入正确的网站id!", 0);
  1938. }
  1939. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1940. if (!empty($info)) {
  1941. return Result::error("该网站已经添加过了!", 0);
  1942. }
  1943. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->insertGetId($data);
  1944. if (empty($result)) {
  1945. return Result::error("请先添加网站基础信息!", 0);
  1946. } else {
  1947. return Result::success($result);
  1948. }
  1949. }
  1950. /**
  1951. * 获取网站底基础信息
  1952. * @param array $data
  1953. */
  1954. public function getWebFootInfo(array $data): array
  1955. {
  1956. $website = Website::where('id', $data['website_id'])->first();
  1957. if (empty($website)) {
  1958. return Result::error("请输入正确的网站id!", 0);
  1959. }
  1960. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1961. if (empty($result)) {
  1962. return Result::error("请先添加网站基础信息!", 0);
  1963. }
  1964. return Result::success($result);
  1965. }
  1966. /**
  1967. * 修改网站底基础信息
  1968. * @param array $data
  1969. */
  1970. public function upWebFootInfo(array $data): array
  1971. {
  1972. // return Result::success($data);
  1973. $website = Website::where('id', $data['website_id'])->first();
  1974. if (empty($website)) {
  1975. return Result::error("请输入正确的网站id!", 0);
  1976. }
  1977. $web = $data['website_id'];
  1978. unset($data['website_id']);
  1979. $where = ['website_id' => $web];
  1980. // return Result::success($data);
  1981. $result = WebsiteTemplateInfo::where($where)->first();
  1982. if (empty($result)) {
  1983. $data['website_id'] = $web;
  1984. $result = WebsiteTemplateInfo::insertGetId($data);
  1985. // return Result::error("请先添加网站基础信息!",0);
  1986. } else {
  1987. $result = WebsiteTemplateInfo::where($where)->update($data);
  1988. }
  1989. // $where = ['website_id'=>$data['website_id']];
  1990. // unset($data['website_id']);
  1991. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  1992. if (empty($result)) {
  1993. return Result::error("修改失败!", 0);
  1994. }
  1995. return Result::success($result);
  1996. }
  1997. /*
  1998. * 获取父级/子级栏目------路由
  1999. * @param array $data
  2000. * @return array
  2001. * */
  2002. public function getWebsiteParentCategory(array $data): array
  2003. {
  2004. // return Result::success($data);
  2005. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2006. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2007. }
  2008. if (empty($website)) {
  2009. return Result::error("暂无该网站", 0);
  2010. }
  2011. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  2012. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  2013. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  2014. ->where('pid', 0)
  2015. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  2016. ->get() // 添加 get() 方法获取结果集合
  2017. ->all();
  2018. // 提前查询所有父级栏目,避免在 map 中多次查询
  2019. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  2020. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  2021. ->where('pid', 0)
  2022. ->select('category_id', 'aLIas_pinyin')
  2023. ->get()
  2024. ->keyBy('category_id');
  2025. // 直接查询子栏目信息
  2026. $child = WebsiteCategory::where('website_id', $data['website_id'])
  2027. ->where('pid', '!=', 0)
  2028. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2029. ->get();
  2030. // 使用集合的 map 方法处理子栏目信息
  2031. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2032. $alias = $item->alias;
  2033. $category_id = $item->category_id;
  2034. $aLIas_pinyin = $item->aLIas_pinyin;
  2035. $parent = $parentCategories->get($item->pid);
  2036. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2037. $result = [
  2038. 'name' => $alias,
  2039. 'cid' => $category_id
  2040. ];
  2041. if (!empty($aLIas_pinyin) && !empty($parentAlias)) {
  2042. $result['path'] = "/{$parentAlias}/{$aLIas_pinyin}/";
  2043. }
  2044. return $result;
  2045. })->values()->all();
  2046. if (empty($category)) {
  2047. return Result::error("暂无此导航", 0);
  2048. }
  2049. return Result::success($category);
  2050. }
  2051. /**
  2052. * 获取网站栏目信息
  2053. * @param array $data
  2054. */
  2055. public function getWebsiteFootAll(array $data): array
  2056. {
  2057. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2058. if (empty($website)) {
  2059. return Result::error("暂无该网站", 0);
  2060. }
  2061. // 1:图片 2:文字 3:底部
  2062. $result['foot_cate'] = FooterCategory::where('website_id',$data['website_id'])->get()->all();
  2063. $result['link_img'] = Link::where('website_id',$data['website_id'])->where('type',1)->where('status',1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2064. $result['link_text'] = Link::where('website_id',$data['website_id'])->where('type',2)->where('status',1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2065. $result['link_foot'] = Link::where('website_id',$data['website_id'])->where('type',3)->where('status',1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2066. $result['foot_info'] = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  2067. if(empty($result)){
  2068. return Result::error("暂无此网站信息",0);
  2069. }
  2070. return Result::success($result);
  2071. }
  2072. /**
  2073. * 获取网站顶部信息
  2074. * @param array $data
  2075. */
  2076. public function getWebsiteHead(array $data): array
  2077. {
  2078. $result = Website::where('id', $data['website_id'])->where('status', 1)->select('website_name', 'logo')->first();
  2079. if (empty($result)) {
  2080. return Result::error("暂无该网站", 0);
  2081. }
  2082. return Result::success($result);
  2083. }
  2084. /**
  2085. * 路由匹配
  2086. * @param array $data
  2087. */
  2088. public function getWebsiteRoute(array $data): array
  2089. {
  2090. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  2091. if (empty($website)) {
  2092. return Result::error("暂无该网站", 0);
  2093. }
  2094. // return Result::success($data);
  2095. if (isset($data['pinyin']) && !empty($data['pinyin'])) {
  2096. $result = WebsiteCategory::where('website_id', $data['website_id'])->where('aLIas_pinyin', $data['pinyin'])->first(['category_id']);
  2097. }
  2098. if (isset($data['foot_pinyin']) && !empty($data['foot_pinyin'])) {
  2099. $result = FooterCategory::where('website_id', $data['website_id'])->where('name_pinyin', $data['foot_pinyin'])->first(['id']);
  2100. }
  2101. if (!isset($result) || empty($result)) {
  2102. return Result::error("暂无该导航", 0);
  2103. }
  2104. return Result::success($result);
  2105. }
  2106. //20250212 网站标识
  2107. public function addWebsiteGroup(array $data): array
  2108. {
  2109. //判斷name是不是重复
  2110. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2111. if (!empty($websiteGroupInfo)) {
  2112. return Result::error("网站标识重复", 0);
  2113. }
  2114. //添加信息
  2115. $result = WebsiteGroup::insertGetId($data);
  2116. var_dump($result);
  2117. if (empty($result)) {
  2118. return Result::error("创建失败", 0);
  2119. } else {
  2120. return Result::success($result);
  2121. }
  2122. }
  2123. public function getWebsiteGroupList(array $data): array
  2124. {
  2125. $where = [];
  2126. if (isset($data['name']) && !empty($data['name'])) {
  2127. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  2128. }
  2129. $result = WebsiteGroup::where($where)
  2130. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  2131. ->get();
  2132. foreach ($result as $websiteGroup) {
  2133. $webIds = json_decode($websiteGroup->web_ids, true);
  2134. $websites = Website::whereIn('id', $webIds)->get();
  2135. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2136. $websiteGroup->website_names = $websiteNames;
  2137. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  2138. }
  2139. $count = WebsiteGroup::where($where)->count();
  2140. if (empty($result)) {
  2141. return Result::error("没有数据", 0);
  2142. }
  2143. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  2144. }
  2145. public function getWebsiteGroupInfo(array $data): array
  2146. {
  2147. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  2148. $webIds = json_decode($websiteInfo->web_ids, true);
  2149. $websites = Website::whereIn('id', $webIds)->get();
  2150. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  2151. $websiteInfo->website_names = $websiteNames;
  2152. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  2153. if (empty($websiteInfo)) {
  2154. return Result::error("找不到URL", 0);
  2155. }
  2156. return Result::success($websiteInfo->toArray());
  2157. }
  2158. public function updateWebsiteGroup(array $data): array
  2159. {
  2160. $where = [
  2161. 'id' => $data['id'],
  2162. ];
  2163. $insertData = [
  2164. 'name' => $data['name'],
  2165. 'web_ids' => $data['web_ids'],
  2166. ];
  2167. //判斷name是不是重复
  2168. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  2169. if (!empty($websiteGroupInfo) && $websiteGroupInfo->id != $data['id']) {
  2170. return Result::error("网站标识重复", 0);
  2171. }
  2172. $result = WebsiteGroup::where($where)->update($insertData);
  2173. var_dump($result, '------更新');
  2174. if (empty($result)) {
  2175. return Result::error("更新失败", 0);
  2176. } else {
  2177. return Result::success();
  2178. }
  2179. }
  2180. public function deleteWebsiteGroup(array $data): array
  2181. {
  2182. $where = [
  2183. 'id' => $data['id'],
  2184. ];
  2185. //看看user表是不是有这个id
  2186. $userGroup = User::query()->where('sszq', $data['id'])->first();
  2187. if (!empty($userGroup)) {
  2188. return Result::error("有用户在使用该网站标识,不能删除", 0);
  2189. }
  2190. $result = WebsiteGroup::where($where)->delete();
  2191. if (empty($result)) {
  2192. return Result::error("删除失败", 0);
  2193. } else {
  2194. return Result::success();
  2195. }
  2196. }
  2197. public function getWebsiteNavList(array $data): array
  2198. {
  2199. $where = [];
  2200. if (isset($data['website_id']) && !empty($data['website_id'])) {
  2201. array_push($where, ['website_id', '=', $data['website_id']]);
  2202. }
  2203. if (isset($data['pid'])) {
  2204. array_push($where, ['pid', '=', $data['pid']]);
  2205. }
  2206. $list = WebsiteCategory::query()->where($where)->get();
  2207. if (empty($list)) {
  2208. return Result::error("获取失败", 0);
  2209. } else {
  2210. return Result::success($list);
  2211. }
  2212. }
  2213. // 20250307 根据网站标识获和导航获取站点
  2214. public function getWebsiteNavPoolSite(array $data): array
  2215. {
  2216. $where = [];
  2217. if (isset($data['category_id']) && !empty($data['category_id'])) {
  2218. array_push($where, ['category_id', '=', $data['category_id']]);
  2219. }
  2220. $list = WebsiteCategory::query()->where($where)
  2221. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  2222. ->select('website_category.*', 'website.website_name')
  2223. ->distinct()
  2224. ->get();
  2225. if (empty($list)) {
  2226. return Result::error("获取失败", 0);
  2227. } else {
  2228. return Result::success($list);
  2229. }
  2230. }
  2231. public function getWebsiteNavPool(array $data): array
  2232. {
  2233. // 初始化查询条件数组
  2234. $query = WebsiteCategory::query();
  2235. // 如果传入了 websiteids,则添加到查询条件中
  2236. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  2237. $query->whereIn('website_id', $data['websiteids']);
  2238. }
  2239. // 如果传入了 pid,则添加到查询条件中
  2240. if (isset($data['pid'])) {
  2241. $query->where('pid', '=', $data['pid']);
  2242. }
  2243. // 执行查询 唯一性 id
  2244. $list = $query->distinct()->get();
  2245. // 根据查询结果返回相应的结果
  2246. if ($list->isEmpty()) {
  2247. return Result::error("获取失败", 0);
  2248. } else {
  2249. return Result::success($list->toArray());
  2250. }
  2251. }
  2252. /**
  2253. *
  2254. * @param array $data
  2255. * @return array
  2256. */
  2257. public function getAllCategory(array $data): array
  2258. {
  2259. $result = Category::when($data, function ($query) use ($data) {
  2260. if (isset($data['name']) && !empty($data['name'])) {
  2261. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  2262. }
  2263. })->get();
  2264. if (empty($result)) {
  2265. return Result::error("获取失败", 0);
  2266. } else {
  2267. return Result::success($result);
  2268. }
  2269. }
  2270. /**
  2271. * 修改网站栏目排序
  2272. * @param array $data
  2273. * @return array
  2274. */
  2275. public function upWebsiteCategorySort(array $data): array
  2276. {
  2277. $where = [
  2278. 'website_id' => $data['website_id'],
  2279. 'id' => $data['id'],
  2280. ];
  2281. $result = WebsiteCategory::where($where)->update($data);
  2282. if ($result) {
  2283. return Result::success($result);
  2284. } else {
  2285. return Result::error("更新失败", 0);
  2286. }
  2287. }
  2288. }