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