WebsiteService.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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 function PHPUnit\Framework\isNull;
  29. use Overtrue\Pinyin\Pinyin;
  30. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  31. class WebsiteService implements WebsiteServiceInterface
  32. {
  33. /**
  34. * 获取网站列表
  35. * @param string $keyword
  36. * @param int $page
  37. * @param int $pageSize
  38. * @return array
  39. */
  40. public function getWebsitetList(array $data): array
  41. {
  42. // 构建查询
  43. $query = Website::when($data, function ($query) use ($data) {
  44. if (isset($data['keyword']) && !empty($data['keyword'])) {
  45. $query->where('website.website_name', 'like', '%' . trim($data['keyword']) . '%');
  46. }
  47. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  48. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  49. }
  50. if (isset($data['city_id']) && !empty($data['city_id'])) {
  51. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  52. }
  53. });
  54. // 执行查询并获取总数和结果
  55. $queryWithJoins = $query->leftJoin("website_column", "website.website_column_id", "website_column.id")
  56. ->leftJoin("district", "district.id", "website.city_id");
  57. $count = $queryWithJoins->count();
  58. $result = $queryWithJoins->select("website.*", "website_column.column_name", "district.name as city_name")
  59. ->limit($data['pageSize'])
  60. ->offset(($data['page'] - 1) * $data['pageSize'])
  61. ->orderBy("website.updated_at", "desc")
  62. ->get();
  63. if (empty($result)) {
  64. return Result::error("没有数据", 0);
  65. }
  66. $data = [
  67. 'rows' => $result->toArray(),
  68. 'count' => $count,
  69. ];
  70. return Result::success($data);
  71. }
  72. /**
  73. * @param array $data
  74. * @return array
  75. */
  76. public function createWebsite(array $data): array
  77. {
  78. var_dump("网站数据:", $data);
  79. $insertData = [
  80. 'website_name' => $data['website_name'],
  81. 'logo' => $data['logo'] ?? '',
  82. 'website_url' => $data['website_url'] ?? '',
  83. // 'city_id'=>$data['city_id']??0,
  84. 'website_column_id' => $data['website_column_id'],
  85. 'title' => $data['title'] ?? '',
  86. 'keywords' => $data['keywords'] ?? '',
  87. 'description' => $data['description'] ?? '',
  88. 'status' => $data['status'] ?? 1,
  89. 'website_column_arr_id' => $data['website_column_arr_id'],
  90. // 'city_arr_id'=>$data['city_arr_id']??[0],
  91. 'template_id' => $data['template_id'] ?? 0,
  92. 'suffix' => $data['suffix'] ?? "",
  93. ];
  94. $result = Website::insertGetId($insertData);
  95. if (empty($result)) {
  96. return Result::error("创建失败", 0);
  97. } else {
  98. return Result::success(["id" => $result]);
  99. }
  100. }
  101. /**
  102. * @param int $id
  103. * @param array $data
  104. * @return array
  105. */
  106. public function updateWebsite(int $id, array $data): array
  107. {
  108. $insertData = [
  109. 'website_name' => $data['website_name'],
  110. 'logo' => $data['logo'] ?? '',
  111. 'website_url' => $data['website_url'] ?? '',
  112. // 'city_id'=>$data['city_id']??0,
  113. 'website_column_id' => $data['website_column_id'],
  114. 'title' => $data['title'] ?? '',
  115. 'keywords' => $data['keywords'] ?? '',
  116. 'description' => $data['description'] ?? '',
  117. // 'status' => $data['status'] ?? 0,
  118. 'website_column_arr_id' => $data['website_column_arr_id'],
  119. // 'city_arr_id'=>$data['city_arr_id']??[0],
  120. 'template_id' => $data['template_id'] ?? 0,
  121. 'suffix' => $data['suffix'] ?? "",
  122. ];
  123. var_dump("=========:", $insertData);
  124. $result = Website::where('id', $id)->update($insertData);
  125. var_dump("更新站点", $result);
  126. if (empty($result)) {
  127. return Result::error("更新失败", 0);
  128. } else {
  129. return Result::success();
  130. }
  131. }
  132. /**
  133. * @param int $id
  134. * @return array
  135. */
  136. public function delWebsite(int $id): array
  137. {
  138. $result = Website::where('id', $id)->delete();
  139. if (empty($result)) {
  140. return Result::error("删除失败", 0);
  141. } else {
  142. return Result::success();
  143. }
  144. }
  145. /**
  146. * @param int $id
  147. * @return array
  148. */
  149. public function getWebsiteInfo(int $id): array
  150. {
  151. $where = [
  152. ['website.id', '=', $id],
  153. ];
  154. $result = Website::where($where)
  155. ->leftJoin("template", "template.id", "website.template_id")
  156. ->select("website.*", "template.template_name", "template.template_img")
  157. ->first();
  158. if (empty($result)) {
  159. return Result::error("数据不存在", 0);
  160. } else {
  161. return Result::success($result->toArray());
  162. }
  163. }
  164. /**
  165. * 查询所有的站点栏目
  166. * @return array
  167. */
  168. public function getWebsiteColumn(array $data): array
  169. {
  170. $result = WebsiteColumn::where($data)->get();
  171. if (empty($result)) {
  172. return Result::error("数据不存在", 0);
  173. } else {
  174. return Result::success($result->toArray());
  175. }
  176. }
  177. /**
  178. * @param string $keyword
  179. * @param int $page
  180. * @param int $pageSize
  181. * @return array
  182. */
  183. public function getWebsiteColumnList(array $data): array
  184. {
  185. $where = [];
  186. if (isset($data['keyword']) && $data['keyword']) {
  187. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  188. }
  189. $result = WebsiteColumn::where($where)
  190. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  191. ->select("website_column.*", "wc.column_name as parent_column_name")
  192. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  193. $count = WebsiteColumn::where($where)->count();
  194. if (empty($result)) {
  195. return Result::error("没有数据", 0);
  196. }
  197. $data = [
  198. 'rows' => $result->toArray(),
  199. 'count' => $count,
  200. ];
  201. return Result::success($data);
  202. }
  203. /**
  204. * @param array $data
  205. * @return array
  206. */
  207. public function createWebsiteColumn(array $data): array
  208. {
  209. $insertData = [
  210. 'column_name' => $data['column_name'],
  211. 'pid' => $data['pid'] ?? '',
  212. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  213. 'sort' => $data['sort'] ?? 0,
  214. 'remark' => $data['remark'] ?? '',
  215. ];
  216. $result = WebsiteColumn::insertGetId($insertData);
  217. if (empty($result)) {
  218. return Result::error("创建失败", 0);
  219. } else {
  220. return Result::success(["id" => $result]);
  221. }
  222. }
  223. /**
  224. * @param int $id
  225. * @param array $data
  226. * @return array
  227. */
  228. public function updateWebsiteColumn(int $id, array $data): array
  229. {
  230. $insertData = [
  231. 'column_name' => $data['column_name'],
  232. 'pid' => $data['pid'] ?? '',
  233. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  234. 'sort' => $data['sort'] ?? 0,
  235. 'remark' => $data['remark'] ?? '',
  236. ];
  237. $result = WebsiteColumn::where('id', $id)->update($insertData);
  238. if (empty($result)) {
  239. return Result::error("更新失败", 0);
  240. } else {
  241. return Result::success();
  242. }
  243. }
  244. /**
  245. * @param int $id
  246. * @return array
  247. */
  248. public function delWebsiteColumn(int $id): array
  249. {
  250. $list = WebsiteColumn::where(['pid' => $id])->get();
  251. if ($list) {
  252. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  253. }
  254. $result = WebsiteColumn::where('id', $id)->delete();
  255. if (empty($result)) {
  256. return Result::error("删除失败", 0);
  257. } else {
  258. return Result::success();
  259. }
  260. }
  261. /**
  262. * @param string $keyword
  263. * @param int $page
  264. * @param int $pageSize
  265. * @return array
  266. */
  267. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  268. {
  269. $where = [
  270. ['role.role_name', 'like', '%' . $keyword . '%'],
  271. ['website_role.website_id', '=', $websiteId],
  272. ];
  273. $result = WebsiteRole::where($where)
  274. ->leftJoin("role", "role.id", "website_role.role_id")
  275. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  276. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  277. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  278. if (empty($result)) {
  279. return Result::error("没有数据", 0);
  280. }
  281. $data = [
  282. 'rows' => $result->toArray(),
  283. 'count' => $count,
  284. ];
  285. return Result::success($data);
  286. }
  287. /**
  288. * @param array $data
  289. * @return array
  290. */
  291. public function createWebsiteRole(array $data): array
  292. {
  293. $insertData = [
  294. 'website_id' => $data['website_id'],
  295. 'role_id' => $data['role_id'] ?? '',
  296. ];
  297. $info = WebsiteRole::where($insertData)->first();
  298. if ($info) {
  299. return Result::error("不能重复添加角色", 0);
  300. }
  301. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  302. $insertData['type'] = $data['type'] ?? '';
  303. $result = WebsiteRole::insertGetId($insertData);
  304. if (empty($result)) {
  305. return Result::error("创建失败", 0);
  306. } else {
  307. return Result::success(["id" => $result]);
  308. }
  309. }
  310. /**
  311. * 暂时用不上
  312. * @param int $id
  313. * @param array $data
  314. * @return array
  315. */
  316. public function updateWebsiteRole(int $id, array $data): array
  317. {
  318. $insertData = [
  319. 'website_id' => $data['website_id'],
  320. 'type' => $data['type'] ?? '',
  321. ];
  322. $result = WebsiteRole::where('id', $id)->update($insertData);
  323. if (empty($result)) {
  324. return Result::error("更新失败", 0);
  325. } else {
  326. return Result::success();
  327. }
  328. }
  329. /**
  330. * @param int $id
  331. * @return array
  332. */
  333. public function delWebsiteRole(int $id): array
  334. {
  335. $result = WebsiteRole::where('id', $id)->delete();
  336. if (empty($result)) {
  337. return Result::error("删除失败", 0);
  338. } else {
  339. return Result::success();
  340. }
  341. }
  342. /**
  343. * @param string $keyword
  344. * @param int $page
  345. * @param int $pageSize
  346. * @return array
  347. */
  348. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  349. {
  350. $where = [
  351. ['website_role_user.website_id', '=', $websiteId],
  352. ['website_role_user.role_id', '=', $roleId],
  353. ];
  354. $count = WebsiteRoleUser::where($where)->count();
  355. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  356. $result = WebsiteRoleUser::where($where)
  357. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  358. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  359. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  360. ->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')
  361. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  362. if (empty($result)) {
  363. return Result::error("没有数据", 0);
  364. }
  365. $data = [
  366. 'rows' => $result->toArray(),
  367. 'count' => $count,
  368. ];
  369. return Result::success($data);
  370. }
  371. /**
  372. * @param array $data
  373. * @return array
  374. */
  375. public function createWebsiteRoleUser(array $data): array
  376. {
  377. $insertData = [
  378. 'website_id' => $data['website_id'],
  379. 'user_id' => $data['user_id'] ?? '',
  380. ];
  381. $info = WebsiteRoleUser::where($insertData)->first();
  382. if ($info) {
  383. return Result::error("不能重复添加角色用户", 0);
  384. }
  385. $insertData['role_id'] = $data['role_id'] ?? '';
  386. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  387. $insertData['type'] = $data['type'] ?? '';
  388. $result = WebsiteRoleUser::insertGetId($insertData);
  389. if (empty($result)) {
  390. return Result::error("创建失败", 0);
  391. } else {
  392. return Result::success(["id" => $result]);
  393. }
  394. }
  395. /**
  396. * @param int $id
  397. * @param array $data
  398. * @return array
  399. */
  400. public function updateWebsiteRoleUser(int $id, array $data): array
  401. {
  402. $insertData = [
  403. 'website_id' => $data['website_id'],
  404. 'type' => $data['type'] ?? '',
  405. 'type_id' => $data['type_id'] ?? '',
  406. 'role_id' => $data['role_id'] ?? '',
  407. ];
  408. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  409. if (empty($result)) {
  410. return Result::error("更新失败", 0);
  411. } else {
  412. return Result::success();
  413. }
  414. }
  415. /**
  416. * @param int $id
  417. * @return array
  418. */
  419. public function delWebsiteRoleUser(int $id): array
  420. {
  421. $result = WebsiteRoleUser::where('id', $id)->delete();
  422. if (empty($result)) {
  423. return Result::error("删除失败", 0);
  424. } else {
  425. return Result::success();
  426. }
  427. }
  428. /**
  429. * 根据域名获取网站 站点id
  430. * @param array $data
  431. * @return array
  432. */
  433. public function getWebsiteId(array $data): array
  434. {
  435. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  436. if (empty($result)) {
  437. return Result::error("查询站点失败", 0);
  438. } else {
  439. return Result::success($result);
  440. }
  441. }
  442. /**
  443. * 查询网站下面的导航
  444. * @param array $data
  445. * @return array
  446. */
  447. public function getWebsiteCategory(array $data): array
  448. {
  449. $where = [
  450. 'website_id' => $data['website_id'],
  451. ];
  452. // 使用递归查询构建树状结构
  453. $category = WebsiteCategory::where($where)
  454. ->with([
  455. 'children' => function ($query) {
  456. $query->orderBy('sort', 'asc');
  457. },
  458. ])
  459. ->orderBy('sort', 'asc')
  460. ->where('pid', 0) // 仅查询顶级节点
  461. ->get()->all();
  462. if (empty($category)) {
  463. return Result::error("查询站点栏目失败", 0);
  464. } else {
  465. $count = WebsiteCategory::where($where)->count();
  466. $result = [
  467. 'rep' => $category,
  468. 'count' => $count,
  469. ];
  470. return Result::success($result);
  471. }
  472. }
  473. /**
  474. * 查询网站的广告
  475. * @param array $data
  476. * @return array
  477. */
  478. public function getWebsiteAdvertisement(array $data): array
  479. {
  480. $where = [
  481. 'website_id' => $data['website_id'],
  482. 'ad_place.status' => 1,
  483. // 'ad.status' => 1,
  484. ];
  485. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  486. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  487. $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
  488. $result = AdPlace::where($where)
  489. ->leftJoin("ad", function ($join) use ($now) {
  490. $join->on("ad.pid", "=", "ad_place.id")
  491. ->where('ad.status', 1)
  492. ->where('ad.fromtime', '<=', $now)
  493. ->where('ad.totime', '>=', $now);
  494. })
  495. ->select("ad_place.*", 'ad.*', "ad_place.id as ad_place_id", "ad.name as ad_name")
  496. ->get()->all();
  497. if (empty($result)) {
  498. return Result::error("此广告位不存在!", 0);
  499. }
  500. } else {
  501. return Result::error("请选择广告位!", 0);
  502. }
  503. return Result::success($result);
  504. }
  505. /**
  506. *
  507. * @param array $data
  508. * @return array
  509. */
  510. /**网站行政职能搜索 */
  511. public function selectWebsiteDepartment(array $data): array
  512. {
  513. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  514. if (isset($data['keyword']) && !empty($data['keyword'])) {
  515. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  516. if (empty($departments)) {
  517. $result['message'] = "未查询到与此相关职能部门";
  518. } else {
  519. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  520. $m = [
  521. 'department' => $depart,
  522. 'type' => $departments,
  523. 'count' => $count,
  524. ];
  525. }
  526. $result['sele'] = $m;
  527. return Result::success($result['sele']);
  528. }
  529. $result = $depart;
  530. return Result::success($result);
  531. }
  532. /**
  533. * 搜索地区
  534. * @param array $data
  535. * @return array
  536. */
  537. public function selectWebsiteArea(array $data): array
  538. {
  539. $provinces = District::where('pid', 0)->where('status', 1)->orderBy('code')->get();
  540. if (isset($data['province'])) {
  541. $province = District::where('pid', 0)->where('status', 1)->where('id', $data['province'])->orderBy('code')->get();
  542. $province = $province->toArray();
  543. if (!empty($province)) {
  544. $citys = District::where('pid', $data['province'])->where('status', 1)->orderBy('code')->get();
  545. if (!empty($citys) && isset($data['city']) && !empty($data['city'])) {
  546. // $province = $province->toArray();
  547. $province_id = [];
  548. foreach ($province as $val) {
  549. array_push($province_id, $val['id']);
  550. }
  551. // var_dump($province_id);
  552. $city = District::whereIn('pid', $province_id)->where('status', 1)->where('id', $data['city'])->orderBy('code')->get();
  553. if (!empty($city)) {
  554. $city_id = [];
  555. foreach ($city as $val) {
  556. array_push($city_id, $val['id']);
  557. }
  558. $regions = District::whereIn('pid', $city_id)->where('status', 1)->orderBy('code')->get();
  559. $result = [
  560. 'province' => $province,
  561. 'city' => $city,
  562. 'region' => $regions,
  563. ];
  564. } else {
  565. return Result::error("未查询到此城市", 0);
  566. }
  567. } else {
  568. $result = [
  569. 'province' => $province,
  570. 'city' => $citys,
  571. 'region' => null,
  572. ];
  573. }
  574. } else {
  575. return Result::error("未查询到此省份", 0);
  576. }
  577. } else {
  578. // $keys = array('data');
  579. $result = $provinces;
  580. }
  581. return Result::success($result);
  582. }
  583. /**
  584. * 获取栏目
  585. * @param array $data
  586. * @return array
  587. */
  588. public function getWebsiteModelCategory(array $data): array
  589. {
  590. // return Result::success($data);
  591. $website_id = [
  592. 'website_id' => $data['website_id'],
  593. ];
  594. // 初始化 $pid 数组
  595. // $pid = [];
  596. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  597. $pid = WebsiteCategory::where($website_id)
  598. ->where('pid', '!=', 0)
  599. ->pluck('pid');
  600. $pid = array_values(array_unique($pid->toArray()));
  601. // 构建查询语句
  602. $query = WebsiteCategory::where($website_id)
  603. ->where('pid', $data['pid'])
  604. ->offset($data['placeid'])
  605. ->limit($data['num'])
  606. ->orderBy('sort');
  607. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  608. if (!empty($pid)) {
  609. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  610. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  611. } else {
  612. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  613. $query->select('website_category.*', DB::raw('0 as children_count'));
  614. }
  615. // 执行查询
  616. $result = $query->get();
  617. if (!empty($result)) {
  618. return Result::success($result);
  619. } else {
  620. return Result::error("本网站暂无栏目", 0);
  621. }
  622. }
  623. /**
  624. * 获取友情链接
  625. * @param array $data
  626. * @return array
  627. */
  628. public function selectWebsiteLinks(array $data): array
  629. {
  630. $where = [
  631. 'website_id' => $data['website_id'],
  632. 'status' => 1,
  633. 'type' => $data['type'],
  634. ];
  635. $num = $data['num'];
  636. $result = Link::where($where)->orderBy('sort')->limit($num)->get();
  637. if (!empty($result)) {
  638. return Result::success($result);
  639. } else {
  640. return Result::error("本网站暂无此类型友情链接", 0);
  641. }
  642. }
  643. /**
  644. * 网站首页数据统计, 管理员
  645. * @return void
  646. */
  647. public function getAdminIndex(array $data): array
  648. {
  649. var_dump("用户类型:", $data['type_id']);
  650. switch ($data['type_id']) {
  651. case 4:
  652. $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;');
  653. return Result::success($result);
  654. break;
  655. case 10000:
  656. $res = [];
  657. //网站
  658. $res['website']['count'] = 0;
  659. $res['website']['growth_rate'] = 0;
  660. //资讯
  661. $res['article']['count'] = 0;
  662. $res['article']['growth_rate'] = 0;
  663. //导航池
  664. $res['category']['count'] = 0;
  665. $res['category']['growth_rate'] = 0;
  666. //近一月数据
  667. $res['monthArticle'] = [];
  668. //用户类型
  669. $res['userType'] = [];
  670. $res['website']['count'] = Website::where([])->count();
  671. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  672. $res['category']['count'] = Category::where([])->count();
  673. $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;');
  674. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  675. return Result::success($res);
  676. }
  677. return [];
  678. }
  679. /**
  680. * 获取模板类型
  681. * @return void
  682. */
  683. public function getTemplateClass(array $data): array
  684. {
  685. $where = [];
  686. if (isset($data['name']) && $data['name']) {
  687. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  688. }
  689. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  690. if (empty($result)) {
  691. return Result::error("没有模板类型", 0);
  692. } else {
  693. return Result::success($result);
  694. }
  695. }
  696. /**
  697. * 添加模板类型
  698. * @param
  699. * @return void
  700. */
  701. public function addTemplateClass(array $data): array
  702. {
  703. $insertData = [
  704. 'name' => $data['name'],
  705. ];
  706. $result = TemplateClass::insertGetId($insertData);
  707. if (empty($result)) {
  708. return Result::error("创建失败", 0);
  709. } else {
  710. return Result::success(["id" => $result]);
  711. }
  712. }
  713. /**
  714. * 更新模板
  715. * @param array $data
  716. * @return array
  717. */
  718. public function upTemplateClass(array $data): array
  719. {
  720. $where = [
  721. 'id' => $data['id'],
  722. ];
  723. $insertData = [
  724. 'name' => $data['name'],
  725. ];
  726. $result = TemplateClass::where($where)->update($insertData);
  727. if (empty($result)) {
  728. return Result::error("更新失败", 0);
  729. } else {
  730. return Result::success();
  731. }
  732. }
  733. /**
  734. * 删除模板
  735. * @param array $data
  736. * @return array
  737. */
  738. public function delTemplateClass(array $data): array
  739. {
  740. $where = [
  741. 'id' => $data['id'],
  742. ];
  743. $result = TemplateClass::where($where)->delete();
  744. if (empty($result)) {
  745. return Result::error("删除失败", 0);
  746. } else {
  747. return Result::success();
  748. }
  749. }
  750. /**
  751. * 获取分类下的模板
  752. * @param array $data
  753. * @return array
  754. */
  755. public function getTemplate(array $data): array
  756. {
  757. $page = $data['page'];
  758. $pageSize = $data['pageSize'];
  759. $where = [];
  760. if (isset($data['template_class_id']) && $data['template_class_id']) {
  761. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  762. }
  763. $result = Template::where($where)
  764. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  765. $count = Template::where($where)->count();
  766. if (empty($result)) {
  767. return Result::error("没有数据", 0);
  768. }
  769. $data = [
  770. 'rows' => $result->toArray(),
  771. 'count' => $count,
  772. ];
  773. return Result::success($data);
  774. }
  775. /**
  776. * 创建模板
  777. * @param
  778. * @return void
  779. */
  780. public function addTemplate(array $data): array
  781. {
  782. $insertData = [
  783. 'template_name' => $data['template_name'],
  784. 'template_img' => json_encode($data['template_img']),
  785. 'template_class_id' => $data['template_class_id'],
  786. ];
  787. $result = Template::insertGetId($insertData);
  788. if (empty($result)) {
  789. return Result::error("创建模板失败", 0);
  790. } else {
  791. return Result::success(["id" => $result]);
  792. }
  793. }
  794. /**
  795. * 更新模板
  796. * @param array $data
  797. * @return array
  798. */
  799. public function upTemplate(array $data): array
  800. {
  801. $where = [
  802. 'id' => $data['id'],
  803. ];
  804. $insertData = [
  805. 'template_name' => $data['template_name'],
  806. 'template_img' => json_encode($data['template_img']),
  807. 'template_class_id' => $data['template_class_id'],
  808. ];
  809. $result = Template::where($where)->update($insertData);
  810. if (empty($result)) {
  811. return Result::error("更新模板失败", 0);
  812. } else {
  813. return Result::success();
  814. }
  815. }
  816. /**
  817. * 删除模板
  818. * @param array $data
  819. * @return array
  820. */
  821. public function delTemplate(array $data): array
  822. {
  823. $where = [
  824. 'id' => $data['id'],
  825. ];
  826. $result = Template::where($where)->delete();
  827. if (empty($result)) {
  828. return Result::error("删除模板失败", 0);
  829. } else {
  830. return Result::success();
  831. }
  832. }
  833. /**
  834. * 搜索网站
  835. * @param array $data
  836. * @return array
  837. */
  838. public function websiteList(array $data): array
  839. {
  840. $where = [];
  841. if (isset($data['keyword']) && !empty($data['keyword'])) {
  842. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  843. }
  844. $result = Website::where($where)->get();
  845. if ($result) {
  846. return Result::success($result);
  847. } else {
  848. return Result::error("没有网站", 0);
  849. }
  850. }
  851. public function addWebsiteCategory(array $data): array
  852. {
  853. $website_id = $data['website_id'];
  854. $category_arr_id = $data['category_arr_id'];
  855. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  856. $categoryListIds = [];
  857. if ($categoryList) {
  858. foreach ($categoryList->toArray() as $val) {
  859. array_push($categoryListIds, $val['id']);
  860. }
  861. }
  862. $arr = [];
  863. if ($categoryListIds) {
  864. foreach ($categoryListIds as $v) {
  865. $ids = $this->getUnderlingUIds(intval($v));
  866. $ids_arr = explode(",", $ids);
  867. array_push($arr, $ids_arr);
  868. }
  869. }
  870. $mergedArray = [];
  871. foreach ($arr as $subarray) {
  872. $mergedArray = array_merge($mergedArray, $subarray);
  873. }
  874. var_dump("所有:", $arr, $mergedArray);
  875. //查询出所有的分类进行分割插入 组装数据
  876. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  877. $categoryListData = $categoryListData->toArray();
  878. $insertData = [];
  879. if ($categoryListData) {
  880. foreach ($categoryListData as $key => $value) {
  881. $insertData[$key]['website_id'] = $website_id;
  882. $insertData[$key]['name'] = $value['name'];
  883. $insertData[$key]['sort'] = $value['sort'];
  884. $insertData[$key]['pid'] = $value['pid'];
  885. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  886. $insertData[$key]['seo_title'] = $value['seo_title'];
  887. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  888. $insertData[$key]['seo_description'] = $value['seo_description'];
  889. $insertData[$key]['alias'] = $value['name'];
  890. $insertData[$key]['category_id'] = $value['id'];
  891. }
  892. }
  893. $result = WebsiteCategory::insert($insertData);
  894. var_dump("插入数据状态:", $result);
  895. if ($result) {
  896. return Result::success($result);
  897. } else {
  898. return Result::error("创建失败", 0);
  899. }
  900. }
  901. /**
  902. * 删除网站导航
  903. * @param array $data
  904. * @return array
  905. */
  906. public function delWebsiteCategory(array $data): array
  907. {
  908. $website_id = $data['website_id'] ?? 0;
  909. $category_id = $data['category_id'] ?? 0;
  910. $ids = $this->getUnderlingUIds(intval($category_id));
  911. $ids_arr = explode(",", $ids);
  912. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  913. if ($result) {
  914. return Result::success($result);
  915. } else {
  916. return Result::error("删除失败", 0);
  917. }
  918. }
  919. /**
  920. * 获取网站导航
  921. * @param array $data
  922. * @return array
  923. */
  924. public function getAdminWebsiteCategory(array $data): array
  925. {
  926. $where = [
  927. 'website_id' => $data['website_id'],
  928. ];
  929. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  930. $list = [];
  931. if ($result) {
  932. foreach ($result->toArray() as $val) {
  933. array_push($list, json_decode($val['category_arr_id']));
  934. }
  935. }
  936. if ($result) {
  937. return Result::success($list);
  938. } else {
  939. return Result::error("查询失败", 0);
  940. }
  941. }
  942. /**
  943. * 更新网站导航
  944. * @param array $data
  945. * @return array
  946. */
  947. public function upWebsiteCategory(array $data): array
  948. {
  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. $values = [
  979. 'website_id' => $data['website_id'],
  980. 'name' => $info['name'] ?? '',
  981. 'alias' => $info['name'] ?? '',
  982. 'sort' => $info['sort'] ?? 0,
  983. 'pid' => $info['pid'] ?? 0,
  984. 'pid_arr' => $info['pid_arr'] ?? json_encode([]),
  985. 'seo_title' => $info['seo_title'] ?? "",
  986. 'seo_keywords' => $info['seo_keywords'] ?? "",
  987. 'seo_description' => $info['seo_description'] ?? "",
  988. 'is_url' => $info['is_url'] ?? 0,
  989. 'web_url' => $info['web_url'] ?? '',
  990. 'category_id' => end($value),
  991. 'category_arr_id' => $value ? json_encode($value) : json_encode([])
  992. ];
  993. // 检查数据库中是否已有该记录
  994. $existingRecord = WebsiteCategory::where($attributes)->first();
  995. if ($existingRecord && $existingRecord->name) {
  996. unset($values['name']);
  997. }
  998. if ($existingRecord && $existingRecord->sort >= 0) {
  999. unset($values['sort']);
  1000. }
  1001. if ($existingRecord && $existingRecord->seo_title) {
  1002. unset($values['seo_title']);
  1003. }
  1004. if ($existingRecord && $existingRecord->seo_keywords) {
  1005. unset($values['seo_keywords']);
  1006. }
  1007. if ($existingRecord && $existingRecord->seo_description) {
  1008. unset($values['seo_description']);
  1009. }
  1010. if ($existingRecord && $existingRecord->alias) {
  1011. unset($values['alias']);
  1012. }
  1013. // 执行 updateOrCreate 操作
  1014. WebsiteCategory::updateOrCreate($attributes, $values);
  1015. }
  1016. }
  1017. }
  1018. Db::commit();
  1019. return Result::success();
  1020. } catch (\Throwable $ex) {
  1021. Db::rollBack();
  1022. // var_dump($ex->getMessage());
  1023. return Result::error("修改失败" . $ex->getMessage(), 0);
  1024. }
  1025. }
  1026. /**
  1027. * 获取网站列表
  1028. * @param array $data
  1029. * @return array
  1030. */
  1031. public function getWebsiteCategoryList(array $data): array
  1032. {
  1033. $where = [];
  1034. if (isset($data['keyword']) && !empty($data['keyword'])) {
  1035. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  1036. }
  1037. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  1038. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  1039. }
  1040. $result = Website::where($where)
  1041. ->with(["websiteCategory" => function ($query) {
  1042. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  1043. }])
  1044. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1045. ->get();
  1046. $count = Website::where($where)->count();
  1047. if (empty($result)) {
  1048. return Result::error("没有数据", 0);
  1049. }
  1050. $data = [
  1051. 'rows' => $result->toArray(),
  1052. 'count' => $count,
  1053. ];
  1054. if ($result) {
  1055. return Result::success($data);
  1056. } else {
  1057. return Result::error("查询失败", 0);
  1058. }
  1059. }
  1060. /**
  1061. * 删除网站下的所有导航
  1062. * @param array $data
  1063. * @return array
  1064. */
  1065. public function delWebsiteAllCategory(array $data): array
  1066. {
  1067. $website_id = $data['website_id'];
  1068. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  1069. if ($result) {
  1070. return Result::success($result);
  1071. } else {
  1072. return Result::error("删除失败", 0);
  1073. }
  1074. }
  1075. /**
  1076. * 获取网站下的某一个导航
  1077. * @param array $data
  1078. * @return array
  1079. */
  1080. public function getWebsiteCategoryOnes(array $data): array
  1081. {
  1082. $website_id = $data['website_id'];
  1083. $category_id = $data['category_id'];
  1084. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  1085. ->first();
  1086. if ($result) {
  1087. return Result::success($result);
  1088. } else {
  1089. return Result::error("查询失败", 0);
  1090. }
  1091. }
  1092. /**
  1093. * 更新网闸下的某一个导航
  1094. * @param array $data
  1095. * @return array
  1096. */
  1097. public function upWebsiteCategoryones(array $data): array
  1098. {
  1099. //处理所有数据
  1100. //获取所有数据
  1101. // $result = WebsiteCategory::get();
  1102. // $result = $result->toArray();
  1103. // var_dump($result, '------------');
  1104. // $pinyin = new Pinyin();
  1105. // for ($i = 0; $i < count($result); $i++) {
  1106. // $result[$i]['alias_pinyin'] = $pinyin->permalink($result[$i]['alias'], '') . $result[$i]['category_id'];
  1107. // var_dump($result[$i]['alias_pinyin'], $i . '----------');
  1108. // //更新数据
  1109. // WebsiteCategory::where(['id' => $result[$i]['id']])->update(['alias_pinyin' => $result[$i]['alias_pinyin']]);
  1110. // //判断是否更新成功
  1111. // }
  1112. // var_dump($result, '--------222----');
  1113. // //更新数据
  1114. // // $result = WebsiteCategory::update($result);
  1115. // //判断是否更新成功
  1116. // if ($result) {
  1117. // return Result::success($result);
  1118. // } else {
  1119. // return Result::error("更新失败", 0);
  1120. // }
  1121. //处理所有数据
  1122. $where = [
  1123. 'website_id' => $data['website_id'],
  1124. 'category_id' => $data['category_id'],
  1125. ];
  1126. $alias = $data['alias'];
  1127. $pinyin = new Pinyin();
  1128. $alias_pinyin = $pinyin->permalink($alias, '');
  1129. //从WebsiteCategory取出alis ,并转成拼音存放到alias_pinyin
  1130. $result = WebsiteCategory::where($where)->first();
  1131. $result = $result->toArray();
  1132. $data['alias_pinyin'] = $pinyin->permalink($result['alias'], '') . $result['category_id'];
  1133. $result = WebsiteCategory::where($where)->update($data);
  1134. if ($result) {
  1135. return Result::success($result);
  1136. } else {
  1137. return Result::error("更新失败", 0);
  1138. }
  1139. }
  1140. /**
  1141. * 获取网站下的所有导航(包含子导航)
  1142. * @param array $data
  1143. * @return array
  1144. */
  1145. public function getWebsiteAllCategory(array $data): array
  1146. {
  1147. $where = [];
  1148. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1149. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  1150. }
  1151. // if (isset($data['name']) && !empty($data['name'])) {
  1152. // array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  1153. // }
  1154. // if (isset($data['alias']) && !empty($data['alias'])) {
  1155. // array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  1156. // }
  1157. // if (isset($data['department_id']) && !empty($data['department_id'])) {
  1158. // array_push($where, ['category.department_id', '=', $data['department_id']]);
  1159. // }
  1160. // if (isset($data['city_id']) && !empty($data['city_id'])) {
  1161. // array_push($where, ['category.city_id', '=', $data['city_id']]);
  1162. // }
  1163. $result = WebsiteCategory::where($where)
  1164. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1165. // ->leftJoin("department", 'category.department_id', 'department.id')
  1166. // ->leftJoin("district", 'category.city_id', 'district.id')
  1167. // ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  1168. // ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  1169. ->orderBy("sort", "asc")
  1170. ->get();
  1171. // $count = WebsiteCategory::where($where)
  1172. // ->leftJoin("category", 'website_category.category_id', 'category.id')
  1173. // ->leftJoin("department", 'category.department_id', 'department.id')
  1174. // ->leftJoin("district", 'category.city_id', 'district.id')
  1175. // ->count();
  1176. if (empty($result)) {
  1177. return Result::error("没有数据", 0);
  1178. }
  1179. // $data = [
  1180. // 'rows' => $result->toArray(),
  1181. // 'count' => $count,
  1182. // ];
  1183. if ($result) {
  1184. return Result::success($result->toArray());
  1185. } else {
  1186. return Result::error("查询失败", 0);
  1187. }
  1188. }
  1189. /**
  1190. * 递归查询数据
  1191. * @param $id
  1192. * @param $ids
  1193. * @return string
  1194. */
  1195. public function getUnderlingUIds($id, $ids = '')
  1196. {
  1197. $back = Category::where(['pid' => $id])->get();
  1198. $back = $back->toArray();
  1199. if (!empty($back) && is_array($back)) {
  1200. foreach ($back as $v) {
  1201. //防止当前人的ID重复去查询,形成恶性循环
  1202. if ($v['id'] == $id) {
  1203. continue;
  1204. }
  1205. $back2 = Category::where(['pid' => $id])->count('id');
  1206. if ($back2 > 0) {
  1207. $ids = $this->getUnderlingUIds($v['id'], $ids);
  1208. } else {
  1209. $ids .= ',' . $v['id'];
  1210. }
  1211. }
  1212. }
  1213. $ids = $id . ',' . $ids . ',';
  1214. $ids = str_replace(',,', ",", $ids);
  1215. $ids = trim($ids, ',');
  1216. return $ids;
  1217. }
  1218. /**
  1219. * 检测网站名称是否重复
  1220. * @param array $data
  1221. * @return array
  1222. */
  1223. public function checkWebsiteName(array $data): array
  1224. {
  1225. if (isset($data['id'])) {
  1226. $data[] = ['id', "!=", $data['id']];
  1227. unset($data['id']);
  1228. }
  1229. $websiteInfo = Website::query()->where($data)->first();
  1230. if (empty($websiteInfo)) {
  1231. return Result::error("找不到网站", 0);
  1232. }
  1233. return Result::success($websiteInfo->toArray());
  1234. }
  1235. /**
  1236. * 检测网站url是否重复
  1237. * @param array $data
  1238. * @return array
  1239. */
  1240. public function checkWebsiteUrl(array $data): array
  1241. {
  1242. $whereData = [];
  1243. if (isset($data['id'])) {
  1244. $whereData = [['id', "!=", $data['id']]];
  1245. unset($data['id']);
  1246. }
  1247. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1248. if (empty($websiteInfo)) {
  1249. return Result::error("找不到URL", 0);
  1250. }
  1251. return Result::success($websiteInfo->toArray());
  1252. }
  1253. /**
  1254. * 获取网站底部基础信息
  1255. * @param array $data
  1256. * @return array
  1257. */
  1258. public function getWebsiteFootInfo(array $data): array
  1259. {
  1260. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1261. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1262. if (empty($website)) {
  1263. return Result::error("找不到网站", 0);
  1264. }
  1265. } else {
  1266. return Result::error("参数错误", 0);
  1267. }
  1268. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->where('status', 2)->first();
  1269. $website_head = Website::where('id', $data['website_id'])
  1270. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1271. if (empty($website_foot)) {
  1272. return Result::error("暂无底部基础信息", 0);
  1273. }
  1274. if (empty($website_head)) {
  1275. return Result::error("暂无头部基础信息", 0);
  1276. }
  1277. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1278. $result = [
  1279. 'website_foot' => $website_foot,
  1280. 'website_head' => $website_head,
  1281. ];
  1282. return Result::success($result);
  1283. }
  1284. /**
  1285. * 获取网站底部导航
  1286. * @param array $data
  1287. * @return array
  1288. */
  1289. public function getWebsiteFooterCategory(array $data): array
  1290. {
  1291. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1292. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1293. if (empty($website)) {
  1294. return Result::error("找不到网站", 0);
  1295. }
  1296. } else {
  1297. return Result::error("参数错误", 0);
  1298. }
  1299. $result = FooterCategory::where('website_id', $data['website_id'])->get();
  1300. if (empty($result)) {
  1301. return Result::error("暂无底部导航", 0);
  1302. }
  1303. return Result::success($result->toArray());
  1304. }
  1305. /**
  1306. * 获取网站底部导航列表
  1307. * @param array $data
  1308. * @return array
  1309. */
  1310. public function getWebsiteFooterCategoryList(array $data): array
  1311. {
  1312. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1313. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1314. if (empty($website)) {
  1315. return Result::error("找不到网站", 0);
  1316. }
  1317. } else {
  1318. return Result::error("参数错误", 0);
  1319. }
  1320. $footercategory = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1321. // '底部导航类型 0:内容型;1:列表型;',
  1322. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1323. return Result::error("底部导航id错误", 0);
  1324. } else {
  1325. $query = FooterContent::where('fcat_id', $data['fcat_id']);
  1326. if ($query->count() == 0) {
  1327. return Result::error("暂无底部导航列表", 0);
  1328. } elseif ($query->count() == 1) {
  1329. $result = $query->first();
  1330. } else {
  1331. $result = $query->get();
  1332. }
  1333. }
  1334. return Result::success($result);
  1335. }
  1336. /**
  1337. * 获取网站底部导航
  1338. * @param array $data
  1339. * @return array
  1340. */
  1341. public function getWebsiteFooterCategoryInfo(array $data): array
  1342. {
  1343. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1344. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1345. if (empty($website)) {
  1346. return Result::error("找不到网站", 0);
  1347. }
  1348. } else {
  1349. return Result::error("参数错误", 0);
  1350. }
  1351. if (isset($data['type']) && $data['type'] == 0) {
  1352. $fcatid = FooterCategory::where('website_id', $data['website_id'])->where('id', $data['fcat_id'])->first();
  1353. if (empty($fcatid)) {
  1354. return Result::error("底部导航id错误", 0);
  1355. }
  1356. $result = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  1357. } else {
  1358. $result = FooterContent::where('id', $data['fcat_id'])->first();
  1359. }
  1360. if (empty($result)) {
  1361. return Result::error("暂无底部导航内容", 0);
  1362. }
  1363. return Result::success($result);
  1364. }
  1365. /*
  1366. * 搜索网站二级导航
  1367. * @param array $data
  1368. * @return array
  1369. * */
  1370. public function selectWebsiteCategory(array $data): array
  1371. {
  1372. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1373. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1374. if (empty($website)) {
  1375. return Result::error("暂无该网站", 0);
  1376. }
  1377. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id')->all();
  1378. // return Result::success($category);
  1379. if (empty($category)) {
  1380. return Result::error("暂无此导航", 0);
  1381. }
  1382. $query = Category::whereIn('id', $category);
  1383. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1384. $cityid = District::where('id', $data['cityid'])->first();
  1385. if (empty($cityid)) {
  1386. return Result::error("暂无此城市", 0);
  1387. } else {
  1388. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1389. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1390. if (empty($city_category)) {
  1391. return Result::error("暂无此城市下的导航", 0);
  1392. }
  1393. $result['catid'] = $city_category;
  1394. $city = 1;
  1395. // var_dump("城市====================",$result);
  1396. }
  1397. }
  1398. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1399. $departmentid = Department::where('id', $data['department_id'])->first();
  1400. if (empty($departmentid)) {
  1401. return Result::error("暂无此部门", 0);
  1402. } else {
  1403. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1404. if (empty($depart_category)) {
  1405. return Result::error("暂无此部门下的导航", 0);
  1406. }
  1407. $result['catid'] = $depart_category;
  1408. // var_dump("职能部门*******************",$result);
  1409. $department = 1;
  1410. }
  1411. }
  1412. if (!empty($city) && !empty($department)) {
  1413. // var_dump("城市和职能部门----------------------",$result);
  1414. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1415. if (empty($sel_category)) {
  1416. return Result::error("暂无此城市和职能部门下的导航", 0);
  1417. }
  1418. $result['catid'] = $sel_category;
  1419. }
  1420. if (empty($result)) {
  1421. return Result::error("暂无导航", 0);
  1422. }
  1423. // department_id
  1424. } else {
  1425. return Result::error("参数错误", 0);
  1426. }
  1427. return Result::success($result);
  1428. }
  1429. /**
  1430. * 获取网站栏目seo
  1431. * @param array $data
  1432. * @return array
  1433. */
  1434. public function getWebsiteCategoryHead(array $data): array
  1435. {
  1436. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1437. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1438. if (empty($website)) {
  1439. return Result::error("找不到网站", 0);
  1440. }
  1441. } else {
  1442. return Result::error("参数错误", 0);
  1443. }
  1444. if (isset($data['catid']) && !empty($data['catid'])) {
  1445. $fcatid = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->first();
  1446. if (empty($fcatid)) {
  1447. return Result::error("导航id错误", 0);
  1448. }
  1449. $result = Category::where('id', $data['catid'])->first();
  1450. $result['website_name'] = $website['website_name'] ?? '';
  1451. $result['suffix'] = $website['suffix'] ?? '';
  1452. }
  1453. if (empty($result)) {
  1454. return Result::error("暂无导航", 0);
  1455. }
  1456. return Result::success($result);
  1457. }
  1458. /*
  1459. * 获取某个栏目
  1460. * @param array $data
  1461. * @return array
  1462. * */
  1463. public function getOneWebsiteCategory(array $data): array
  1464. {
  1465. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1466. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1467. if (empty($website)) {
  1468. return Result::error("暂无该网站", 0);
  1469. }
  1470. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['category_id'])->first();
  1471. if (empty($category)) {
  1472. return Result::error("暂无此导航", 0);
  1473. }
  1474. $category['children_count'] = WebsiteCategory::where('website_id', $data['website_id'])->where('pid', $data['category_id'])->count();
  1475. return Result::success($category);
  1476. } else {
  1477. return Result::error("参数错误", 0);
  1478. }
  1479. }
  1480. //20250212 网站标识
  1481. public function addWebsiteGroup(array $data): array
  1482. {
  1483. //判斷name是不是重复
  1484. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1485. if (!empty($websiteGroupInfo)) {
  1486. return Result::error("网站标识重复", 0);
  1487. }
  1488. //添加信息
  1489. $result = WebsiteGroup::insertGetId($data);
  1490. var_dump($result);
  1491. if (empty($result)) {
  1492. return Result::error("创建失败", 0);
  1493. } else {
  1494. return Result::success($result);
  1495. }
  1496. }
  1497. public function getWebsiteGroupList(array $data): array
  1498. {
  1499. $where = [];
  1500. if (isset($data['name']) && !empty($data['name'])) {
  1501. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  1502. }
  1503. $result = WebsiteGroup::where($where)
  1504. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1505. ->get();
  1506. foreach ($result as $websiteGroup) {
  1507. $webIds = json_decode($websiteGroup->web_ids, true);
  1508. $websites = Website::whereIn('id', $webIds)->get();
  1509. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1510. $websiteGroup->website_names = $websiteNames;
  1511. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  1512. }
  1513. $count = WebsiteGroup::where($where)->count();
  1514. if (empty($result)) {
  1515. return Result::error("没有数据", 0);
  1516. }
  1517. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  1518. }
  1519. public function getWebsiteGroupInfo(array $data): array
  1520. {
  1521. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  1522. $webIds = json_decode($websiteInfo->web_ids, true);
  1523. $websites = Website::whereIn('id', $webIds)->get();
  1524. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1525. $websiteInfo->website_names = $websiteNames;
  1526. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  1527. if (empty($websiteInfo)) {
  1528. return Result::error("找不到URL", 0);
  1529. }
  1530. return Result::success($websiteInfo->toArray());
  1531. }
  1532. public function updateWebsiteGroup(array $data): array
  1533. {
  1534. $where = [
  1535. 'id' => $data['id'],
  1536. ];
  1537. $insertData = [
  1538. 'name' => $data['name'],
  1539. 'web_ids' => $data['web_ids'],
  1540. ];
  1541. //判斷name是不是重复
  1542. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1543. if (!empty($websiteGroupInfo)) {
  1544. return Result::error("网站标识重复", 0);
  1545. }
  1546. $result = WebsiteGroup::where($where)->update($insertData);
  1547. var_dump($result, '------更新');
  1548. if (empty($result)) {
  1549. return Result::error("更新失败", 0);
  1550. } else {
  1551. return Result::success();
  1552. }
  1553. }
  1554. public function deleteWebsiteGroup(array $data): array
  1555. {
  1556. $where = [
  1557. 'id' => $data['id'],
  1558. ];
  1559. //看看user表是不是有这个id
  1560. $userGroup = User::query()->where('sszq', $data['id'])->first();
  1561. if (!empty($userGroup)) {
  1562. return Result::error("有用户在使用该网站标识,不能删除", 0);
  1563. }
  1564. $result = WebsiteGroup::where($where)->delete();
  1565. if (empty($result)) {
  1566. return Result::error("删除失败", 0);
  1567. } else {
  1568. return Result::success();
  1569. }
  1570. }
  1571. public function getWebsiteNavList(array $data): array
  1572. {
  1573. $where = [];
  1574. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1575. array_push($where, ['website_id', '=', $data['website_id']]);
  1576. }
  1577. if (isset($data['pid'])) {
  1578. array_push($where, ['pid', '=', $data['pid']]);
  1579. }
  1580. $list = WebsiteCategory::query()->where($where)->get();
  1581. if (empty($list)) {
  1582. return Result::error("获取失败", 0);
  1583. } else {
  1584. return Result::success($list);
  1585. }
  1586. }
  1587. // 20250307 根据网站标识获和导航获取站点
  1588. public function getWebsiteNavPoolSite(array $data): array
  1589. {
  1590. $where = [];
  1591. if (isset($data['category_id']) && !empty($data['category_id'])) {
  1592. array_push($where, ['category_id', '=', $data['category_id']]);
  1593. }
  1594. $list = WebsiteCategory::query()->where($where)
  1595. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  1596. ->select('website_category.*', 'website.website_name')
  1597. ->distinct()
  1598. ->get();
  1599. if (empty($list)) {
  1600. return Result::error("获取失败", 0);
  1601. } else {
  1602. return Result::success($list);
  1603. }
  1604. }
  1605. public function getWebsiteNavPool(array $data): array
  1606. {
  1607. // 初始化查询条件数组
  1608. $query = WebsiteCategory::query();
  1609. // 如果传入了 websiteids,则添加到查询条件中
  1610. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  1611. $query->whereIn('website_id', $data['websiteids']);
  1612. }
  1613. // 如果传入了 pid,则添加到查询条件中
  1614. if (isset($data['pid'])) {
  1615. $query->where('pid', '=', $data['pid']);
  1616. }
  1617. // 执行查询 唯一性 id
  1618. $list = $query->distinct()->get();
  1619. // 根据查询结果返回相应的结果
  1620. if ($list->isEmpty()) {
  1621. return Result::error("获取失败", 0);
  1622. } else {
  1623. return Result::success($list->toArray());
  1624. }
  1625. }
  1626. /**
  1627. *
  1628. * @param array $data
  1629. * @return array
  1630. */
  1631. public function getAllCategory(array $data): array
  1632. {
  1633. $result = Category::when($data, function ($query) use ($data) {
  1634. if (isset($data['name']) && !empty($data['name'])) {
  1635. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  1636. }
  1637. })->get();
  1638. if (empty($result)) {
  1639. return Result::error("获取失败", 0);
  1640. } else {
  1641. return Result::success($result);
  1642. }
  1643. }
  1644. /**
  1645. * 修改网站栏目排序
  1646. * @param array $data
  1647. * @return array
  1648. */
  1649. public function upWebsiteCategorySort(array $data): array
  1650. {
  1651. $where = [
  1652. 'website_id' => $data['website_id'],
  1653. 'id' => $data['id'],
  1654. ];
  1655. $result = WebsiteCategory::where($where)->update($data);
  1656. if ($result) {
  1657. return Result::success($result);
  1658. } else {
  1659. return Result::error("更新失败", 0);
  1660. }
  1661. }
  1662. }