WebsiteService.php 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. 'ad_place.status' => 1,
  482. ];
  483. if (isset($data['ad_tag']) && !empty($data['ad_tag'])) {
  484. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  485. $where[] = ['ad_place.ad_tag','like','%'.$data['ad_tag'].'%'];
  486. // return Result::success($where);
  487. $result = AdPlace::where($where)
  488. ->leftJoin("ad", function ($join) use ($now) {
  489. $join->on("ad.pid", "=", "ad_place.id")
  490. ->where('ad.status', 1)
  491. ->where('ad.fromtime', '<=', $now)
  492. ->where('ad.totime', '>=', $now);
  493. })
  494. ->select("ad_place.*",'ad.*',"ad_place.id as ad_place_id","ad.name as ad_name")
  495. ->get()->all();
  496. if(empty($result)){
  497. return Result::error("此广告位不存在!",0);
  498. }
  499. }else{
  500. return Result::error("请选择广告位!",0);
  501. }
  502. return Result::success($result);
  503. }
  504. /**
  505. *
  506. * @param array $data
  507. * @return array
  508. */
  509. /**网站行政职能搜索 */
  510. public function selectWebsiteDepartment(array $data): array
  511. {
  512. $depart = Department::where('pid', 0)->orderBy('id', 'asc')->get();
  513. if (isset($data['keyword']) && !empty($data['keyword'])) {
  514. $departments = Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  515. if (empty($departments)) {
  516. $result['message'] = "未查询到与此相关职能部门";
  517. } else {
  518. $count = Department::where('name', 'like', "%{$data['keyword']}%")->count();
  519. $m = [
  520. 'department' => $depart,
  521. 'type' => $departments,
  522. 'count' => $count,
  523. ];
  524. }
  525. $result['sele'] = $m;
  526. return Result::success($result['sele']);
  527. }
  528. $result = $depart;
  529. return Result::success($result);
  530. }
  531. /**
  532. * 搜索地区
  533. * @param array $data
  534. * @return array
  535. */
  536. public function selectWebsiteArea(array $data): array
  537. {
  538. $provinces = District::where('pid', 0)->where('status', 1)->orderBy('code')->get();
  539. if (isset($data['province'])) {
  540. $province = District::where('pid', 0)->where('status', 1)->where('id', $data['province'])->orderBy('code')->get();
  541. $province = $province->toArray();
  542. if (!empty($province)) {
  543. $citys = District::where('pid', $data['province'])->where('status', 1)->orderBy('code')->get();
  544. if (!empty($citys) && isset($data['city']) && !empty($data['city'])) {
  545. // $province = $province->toArray();
  546. $province_id = [];
  547. foreach ($province as $val) {
  548. array_push($province_id, $val['id']);
  549. }
  550. // var_dump($province_id);
  551. $city = District::whereIn('pid', $province_id)->where('status', 1)->where('id', $data['city'])->orderBy('code')->get();
  552. if (!empty($city)) {
  553. $city_id = [];
  554. foreach ($city as $val) {
  555. array_push($city_id, $val['id']);
  556. }
  557. $regions = District::whereIn('pid', $city_id)->where('status', 1)->orderBy('code')->get();
  558. $result = [
  559. 'province' => $province,
  560. 'city' => $city,
  561. 'region' => $regions,
  562. ];
  563. } else {
  564. return Result::error("未查询到此城市", 0);
  565. }
  566. } else {
  567. $result = [
  568. 'province' => $province,
  569. 'city' => $citys,
  570. 'region' => null,
  571. ];
  572. }
  573. } else {
  574. return Result::error("未查询到此省份", 0);
  575. }
  576. } else {
  577. // $keys = array('data');
  578. $result = $provinces;
  579. }
  580. return Result::success($result);
  581. }
  582. /**
  583. * 获取栏目
  584. * @param array $data
  585. * @return array
  586. */
  587. public function getWebsiteModelCategory(array $data): array
  588. {
  589. // return Result::success($data);
  590. $website_id = [
  591. 'website_id' => $data['website_id'],
  592. ];
  593. // 初始化 $pid 数组
  594. // $pid = [];
  595. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  596. $pid = WebsiteCategory::where($website_id)
  597. ->where('pid', '!=', 0)
  598. ->pluck('pid');
  599. $pid = array_values(array_unique($pid->toArray()));
  600. // 构建查询语句
  601. $query = WebsiteCategory::where($website_id)
  602. ->where('pid', $data['pid'])
  603. ->offset($data['placeid'])
  604. ->limit($data['num'])
  605. ->orderBy('sort');
  606. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  607. if (!empty($pid)) {
  608. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  609. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  610. } else {
  611. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  612. $query->select('website_category.*', DB::raw('0 as children_count'));
  613. }
  614. // 执行查询
  615. $placeid = $data['placeid']-1;
  616. $result = $query->offset($placeid)->limit($data['num'])->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_head = Website::where('id',$data['website_id'])
  1262. ->where('status',1)
  1263. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1264. ->first();
  1265. if (empty($website_head)) {
  1266. return Result::error("找不到网站",0);
  1267. }
  1268. } else {
  1269. return Result::error("参数错误", 0);
  1270. }
  1271. $website_foot = WebsiteTemplateInfo::where('website_id', $data['website_id'])->where('status', 2)->first();
  1272. $website_head = Website::where('id', $data['website_id'])
  1273. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')->first();
  1274. if (empty($website_foot)) {
  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. $parent = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$category['pid'])->select('category_id as parent_id','alias as parent_name')->first();
  1476. $category['parent_id'] = $parent['parent_id']?? '';
  1477. $category['parent_name'] = $parent['parent_name']?? '';
  1478. return Result::success($category);
  1479. } else {
  1480. return Result::error("参数错误", 0);
  1481. }
  1482. }
  1483. //20250212 网站标识
  1484. public function addWebsiteGroup(array $data): array
  1485. {
  1486. //判斷name是不是重复
  1487. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1488. if (!empty($websiteGroupInfo)) {
  1489. return Result::error("网站标识重复", 0);
  1490. }
  1491. //添加信息
  1492. $result = WebsiteGroup::insertGetId($data);
  1493. var_dump($result);
  1494. if (empty($result)) {
  1495. return Result::error("创建失败", 0);
  1496. } else {
  1497. return Result::success($result);
  1498. }
  1499. }
  1500. public function getWebsiteGroupList(array $data): array
  1501. {
  1502. $where = [];
  1503. if (isset($data['name']) && !empty($data['name'])) {
  1504. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  1505. }
  1506. $result = WebsiteGroup::where($where)
  1507. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1508. ->get();
  1509. foreach ($result as $websiteGroup) {
  1510. $webIds = json_decode($websiteGroup->web_ids, true);
  1511. $websites = Website::whereIn('id', $webIds)->get();
  1512. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1513. $websiteGroup->website_names = $websiteNames;
  1514. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  1515. }
  1516. $count = WebsiteGroup::where($where)->count();
  1517. if (empty($result)) {
  1518. return Result::error("没有数据", 0);
  1519. }
  1520. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  1521. }
  1522. public function getWebsiteGroupInfo(array $data): array
  1523. {
  1524. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  1525. $webIds = json_decode($websiteInfo->web_ids, true);
  1526. $websites = Website::whereIn('id', $webIds)->get();
  1527. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1528. $websiteInfo->website_names = $websiteNames;
  1529. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  1530. if (empty($websiteInfo)) {
  1531. return Result::error("找不到URL", 0);
  1532. }
  1533. return Result::success($websiteInfo->toArray());
  1534. }
  1535. public function updateWebsiteGroup(array $data): array
  1536. {
  1537. $where = [
  1538. 'id' => $data['id'],
  1539. ];
  1540. $insertData = [
  1541. 'name' => $data['name'],
  1542. 'web_ids' => $data['web_ids'],
  1543. ];
  1544. //判斷name是不是重复
  1545. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1546. if (!empty($websiteGroupInfo)) {
  1547. return Result::error("网站标识重复", 0);
  1548. }
  1549. $result = WebsiteGroup::where($where)->update($insertData);
  1550. var_dump($result, '------更新');
  1551. if (empty($result)) {
  1552. return Result::error("更新失败", 0);
  1553. } else {
  1554. return Result::success();
  1555. }
  1556. }
  1557. public function deleteWebsiteGroup(array $data): array
  1558. {
  1559. $where = [
  1560. 'id' => $data['id'],
  1561. ];
  1562. //看看user表是不是有这个id
  1563. $userGroup = User::query()->where('sszq', $data['id'])->first();
  1564. if (!empty($userGroup)) {
  1565. return Result::error("有用户在使用该网站标识,不能删除", 0);
  1566. }
  1567. $result = WebsiteGroup::where($where)->delete();
  1568. if (empty($result)) {
  1569. return Result::error("删除失败", 0);
  1570. } else {
  1571. return Result::success();
  1572. }
  1573. }
  1574. public function getWebsiteNavList(array $data): array
  1575. {
  1576. $where = [];
  1577. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1578. array_push($where, ['website_id', '=', $data['website_id']]);
  1579. }
  1580. if (isset($data['pid'])) {
  1581. array_push($where, ['pid', '=', $data['pid']]);
  1582. }
  1583. $list = WebsiteCategory::query()->where($where)->get();
  1584. if (empty($list)) {
  1585. return Result::error("获取失败", 0);
  1586. } else {
  1587. return Result::success($list);
  1588. }
  1589. }
  1590. // 20250307 根据网站标识获和导航获取站点
  1591. public function getWebsiteNavPoolSite(array $data): array
  1592. {
  1593. $where = [];
  1594. if (isset($data['category_id']) && !empty($data['category_id'])) {
  1595. array_push($where, ['category_id', '=', $data['category_id']]);
  1596. }
  1597. $list = WebsiteCategory::query()->where($where)
  1598. ->leftJoin('website', 'website.id', '=', 'website_category.website_id')
  1599. ->select('website_category.*', 'website.website_name')
  1600. ->distinct()
  1601. ->get();
  1602. if (empty($list)) {
  1603. return Result::error("获取失败", 0);
  1604. } else {
  1605. return Result::success($list);
  1606. }
  1607. }
  1608. public function getWebsiteNavPool(array $data): array
  1609. {
  1610. // 初始化查询条件数组
  1611. $query = WebsiteCategory::query();
  1612. // 如果传入了 websiteids,则添加到查询条件中
  1613. if (isset($data['websiteids']) && !empty($data['websiteids'])) {
  1614. $query->whereIn('website_id', $data['websiteids']);
  1615. }
  1616. // 如果传入了 pid,则添加到查询条件中
  1617. if (isset($data['pid'])) {
  1618. $query->where('pid', '=', $data['pid']);
  1619. }
  1620. // 执行查询 唯一性 id
  1621. $list = $query->distinct()->get();
  1622. // 根据查询结果返回相应的结果
  1623. if ($list->isEmpty()) {
  1624. return Result::error("获取失败", 0);
  1625. } else {
  1626. return Result::success($list->toArray());
  1627. }
  1628. }
  1629. /**
  1630. *
  1631. * @param array $data
  1632. * @return array
  1633. */
  1634. public function getAllCategory(array $data): array
  1635. {
  1636. $result = Category::when($data, function ($query) use ($data) {
  1637. if (isset($data['name']) && !empty($data['name'])) {
  1638. $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
  1639. }
  1640. })->get();
  1641. if (empty($result)) {
  1642. return Result::error("获取失败", 0);
  1643. } else {
  1644. return Result::success($result);
  1645. }
  1646. }
  1647. /**
  1648. * 修改网站栏目排序
  1649. * @param array $data
  1650. * @return array
  1651. */
  1652. public function upWebsiteCategorySort(array $data): array
  1653. {
  1654. $where = [
  1655. 'website_id' => $data['website_id'],
  1656. 'id' => $data['id'],
  1657. ];
  1658. $result = WebsiteCategory::where($where)->update($data);
  1659. if ($result) {
  1660. return Result::success($result);
  1661. } else {
  1662. return Result::error("更新失败", 0);
  1663. }
  1664. }
  1665. }