WebsiteService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Category;
  5. use App\Model\LetterOfComplaint;
  6. use App\Model\TemplateClass;
  7. use App\Model\Template;
  8. use App\Model\User;
  9. use App\Model\UserInfo;
  10. use App\Model\WebsiteRole;
  11. use App\Model\WebsiteRoleUser;
  12. use App\Model\Website;
  13. use App\Model\WebsiteColumn;
  14. use Hyperf\DbConnection\Db;
  15. use Hyperf\RpcServer\Annotation\RpcService;
  16. use App\Tools\Result;
  17. use App\Model\WebsiteCategory;
  18. use App\Model\WebsiteGroup;
  19. use function PHPUnit\Framework\isNull;
  20. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  21. class WebsiteService implements WebsiteServiceInterface
  22. {
  23. /**
  24. * 获取网站列表
  25. * @param string $keyword
  26. * @param int $page
  27. * @param int $pageSize
  28. * @return array
  29. */
  30. public function getWebsitetList(array $data): array
  31. {
  32. $resultWwhere = Website::when($data, function ($query) use ($data) {
  33. if (isset($data['keyword']) && !empty($data['keyword'])) {
  34. $query->where('website.website_name', 'like', '%' . $data['keyword'] . '%');
  35. }
  36. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  37. $query->whereJsonContains("website.website_column_arr_id", intval($data['website_column_id']));
  38. }
  39. if (isset($data['city_id']) && !empty($data['city_id'])) {
  40. $query->whereJsonContains("website.city_arr_id", intval($data['city_id']));
  41. }
  42. });
  43. $result = $resultWwhere->leftJoin("website_column", "website.website_column_id", "website_column.id")
  44. ->leftJoin("district", "district.id", "website.city_id")
  45. ->select("website.*", "website_column.column_name", "district.name as city_name")
  46. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->orderBy("website.id", "desc")->get();
  47. $count = $resultWwhere->count();
  48. if (empty($result)) {
  49. return Result::error("没有数据", 0);
  50. }
  51. $data = [
  52. 'rows' => $result->toArray(),
  53. 'count' => $count,
  54. ];
  55. return Result::success($data);
  56. }
  57. /**
  58. * @param array $data
  59. * @return array
  60. */
  61. public function createWebsite(array $data): array
  62. {
  63. var_dump("网站数据:", $data);
  64. $insertData = [
  65. 'website_name' => $data['website_name'],
  66. 'logo' => $data['logo'] ?? '',
  67. 'website_url' => $data['website_url'] ?? '',
  68. // 'city_id'=>$data['city_id']??0,
  69. 'website_column_id' => $data['website_column_id'],
  70. 'title' => $data['title'] ?? '',
  71. 'keywords' => $data['keywords'] ?? '',
  72. 'description' => $data['description'] ?? '',
  73. 'status' => $data['status'] ?? 0,
  74. 'website_column_arr_id' => $data['website_column_arr_id'],
  75. // 'city_arr_id'=>$data['city_arr_id']??[0],
  76. 'template_id' => $data['template_id'] ?? 0,
  77. ];
  78. $result = Website::insertGetId($insertData);
  79. if (empty($result)) {
  80. return Result::error("创建失败", 0);
  81. } else {
  82. return Result::success(["id" => $result]);
  83. }
  84. }
  85. /**
  86. * @param int $id
  87. * @param array $data
  88. * @return array
  89. */
  90. public function updateWebsite(int $id, array $data): array
  91. {
  92. $insertData = [
  93. 'website_name' => $data['website_name'],
  94. 'logo' => $data['logo'] ?? '',
  95. 'website_url' => $data['website_url'] ?? '',
  96. // 'city_id'=>$data['city_id']??0,
  97. 'website_column_id' => $data['website_column_id'],
  98. 'title' => $data['title'] ?? '',
  99. 'keywords' => $data['keywords'] ?? '',
  100. 'description' => $data['description'] ?? '',
  101. 'status' => $data['status'] ?? 0,
  102. 'website_column_arr_id' => $data['website_column_arr_id'],
  103. // 'city_arr_id'=>$data['city_arr_id']??[0],
  104. 'template_id' => $data['template_id'] ?? 0,
  105. ];
  106. $result = Website::where('id', $id)->update($insertData);
  107. var_dump("更新站点", $result);
  108. if (empty($result)) {
  109. return Result::error("更新失败", 0);
  110. } else {
  111. return Result::success();
  112. }
  113. }
  114. /**
  115. * @param int $id
  116. * @return array
  117. */
  118. public function delWebsite(int $id): array
  119. {
  120. $result = Website::where('id', $id)->delete();
  121. if (empty($result)) {
  122. return Result::error("删除失败", 0);
  123. } else {
  124. return Result::success();
  125. }
  126. }
  127. /**
  128. * @param int $id
  129. * @return array
  130. */
  131. public function getWebsiteInfo(int $id): array
  132. {
  133. $where = [
  134. ['website.id', '=', $id],
  135. ];
  136. $result = Website::where($where)
  137. ->leftJoin("template", "template.id", "website.template_id")
  138. ->select("website.*", "template.template_name", "template.template_img")
  139. ->first();
  140. if (empty($result)) {
  141. return Result::error("数据不存在", 0);
  142. } else {
  143. return Result::success($result->toArray());
  144. }
  145. }
  146. /**
  147. * 查询所有的站点栏目
  148. * @return array
  149. */
  150. public function getWebsiteColumn(array $data): array
  151. {
  152. $result = WebsiteColumn::where($data)->get();
  153. if (empty($result)) {
  154. return Result::error("数据不存在", 0);
  155. } else {
  156. return Result::success($result->toArray());
  157. }
  158. }
  159. /**
  160. * @param string $keyword
  161. * @param int $page
  162. * @param int $pageSize
  163. * @return array
  164. */
  165. public function getWebsiteColumnList(array $data): array
  166. {
  167. $where = [];
  168. if (isset($data['keyword']) && $data['keyword']) {
  169. array_push($where, ['website_column.column_name', 'like', '%' . $data['keyword'] . '%']);
  170. }
  171. $result = WebsiteColumn::where($where)
  172. ->leftJoin("website_column as wc", "website_column.pid", "wc.id")
  173. ->select("website_column.*", "wc.column_name as parent_column_name")
  174. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  175. $count = WebsiteColumn::where($where)->count();
  176. if (empty($result)) {
  177. return Result::error("没有数据", 0);
  178. }
  179. $data = [
  180. 'rows' => $result->toArray(),
  181. 'count' => $count,
  182. ];
  183. return Result::success($data);
  184. }
  185. /**
  186. * @param array $data
  187. * @return array
  188. */
  189. public function createWebsiteColumn(array $data): array
  190. {
  191. $insertData = [
  192. 'column_name' => $data['column_name'],
  193. 'pid' => $data['pid'] ?? '',
  194. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  195. 'sort' => $data['sort'] ?? 0,
  196. 'remark' => $data['remark'] ?? '',
  197. ];
  198. $result = WebsiteColumn::insertGetId($insertData);
  199. if (empty($result)) {
  200. return Result::error("创建失败", 0);
  201. } else {
  202. return Result::success(["id" => $result]);
  203. }
  204. }
  205. /**
  206. * @param int $id
  207. * @param array $data
  208. * @return array
  209. */
  210. public function updateWebsiteColumn(int $id, array $data): array
  211. {
  212. $insertData = [
  213. 'column_name' => $data['column_name'],
  214. 'pid' => $data['pid'] ?? '',
  215. 'column_arr_id' => $data['column_arr_id'] ?? [0],
  216. 'sort' => $data['sort'] ?? 0,
  217. 'remark' => $data['remark'] ?? '',
  218. ];
  219. $result = WebsiteColumn::where('id', $id)->update($insertData);
  220. if (empty($result)) {
  221. return Result::error("更新失败", 0);
  222. } else {
  223. return Result::success();
  224. }
  225. }
  226. /**
  227. * @param int $id
  228. * @return array
  229. */
  230. public function delWebsiteColumn(int $id): array
  231. {
  232. $list = WebsiteColumn::where(['pid' => $id])->get();
  233. if ($list) {
  234. return Result::error("存在子网系,不能删除,请先删除子网系", 0);
  235. }
  236. $result = WebsiteColumn::where('id', $id)->delete();
  237. if (empty($result)) {
  238. return Result::error("删除失败", 0);
  239. } else {
  240. return Result::success();
  241. }
  242. }
  243. /**
  244. * @param string $keyword
  245. * @param int $page
  246. * @param int $pageSize
  247. * @return array
  248. */
  249. public function getWebsiteRoleList(string $keyword, int $page, int $pageSize, int $websiteId): array
  250. {
  251. $where = [
  252. ['role.role_name', 'like', '%' . $keyword . '%'],
  253. ['website_role.website_id', '=', $websiteId],
  254. ];
  255. $result = WebsiteRole::where($where)
  256. ->leftJoin("role", "role.id", "website_role.role_id")
  257. ->select("role.*", "website_role.type", "website_role.role_id", "website_role.id as website_role_id", "website_role.website_id")
  258. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  259. $count = WebsiteRole::where($where)->leftJoin("role", "role.id", "website_role.role_id")->count();
  260. if (empty($result)) {
  261. return Result::error("没有数据", 0);
  262. }
  263. $data = [
  264. 'rows' => $result->toArray(),
  265. 'count' => $count,
  266. ];
  267. return Result::success($data);
  268. }
  269. /**
  270. * @param array $data
  271. * @return array
  272. */
  273. public function createWebsiteRole(array $data): array
  274. {
  275. $insertData = [
  276. 'website_id' => $data['website_id'],
  277. 'role_id' => $data['role_id'] ?? '',
  278. ];
  279. $info = WebsiteRole::where($insertData)->first();
  280. if ($info) {
  281. return Result::error("不能重复添加角色", 0);
  282. }
  283. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  284. $insertData['type'] = $data['type'] ?? '';
  285. $result = WebsiteRole::insertGetId($insertData);
  286. if (empty($result)) {
  287. return Result::error("创建失败", 0);
  288. } else {
  289. return Result::success(["id" => $result]);
  290. }
  291. }
  292. /**
  293. * 暂时用不上
  294. * @param int $id
  295. * @param array $data
  296. * @return array
  297. */
  298. public function updateWebsiteRole(int $id, array $data): array
  299. {
  300. $insertData = [
  301. 'website_id' => $data['website_id'],
  302. 'type' => $data['type'] ?? '',
  303. ];
  304. $result = WebsiteRole::where('id', $id)->update($insertData);
  305. if (empty($result)) {
  306. return Result::error("更新失败", 0);
  307. } else {
  308. return Result::success();
  309. }
  310. }
  311. /**
  312. * @param int $id
  313. * @return array
  314. */
  315. public function delWebsiteRole(int $id): array
  316. {
  317. $result = WebsiteRole::where('id', $id)->delete();
  318. if (empty($result)) {
  319. return Result::error("删除失败", 0);
  320. } else {
  321. return Result::success();
  322. }
  323. }
  324. /**
  325. * @param string $keyword
  326. * @param int $page
  327. * @param int $pageSize
  328. * @return array
  329. */
  330. public function getWebsiteRoleUserList(string $keyword, int $page, int $pageSize, int $websiteId, int $roleId): array
  331. {
  332. $where = [
  333. ['website_role_user.website_id', '=', $websiteId],
  334. ['website_role_user.role_id', '=', $roleId],
  335. ];
  336. $count = WebsiteRoleUser::where($where)->count();
  337. $where[] = ['u.user_name', 'like', '%' . $keyword . '%'];
  338. $result = WebsiteRoleUser::where($where)
  339. ->leftJoin("user as u", "website_role_user.user_id", "u.id")
  340. ->leftJoin("website as w", "website_role_user.website_id", "u.id")
  341. ->leftJoin("role as r", "website_role_user.role_id", "r.id")
  342. ->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')
  343. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  344. if (empty($result)) {
  345. return Result::error("没有数据", 0);
  346. }
  347. $data = [
  348. 'rows' => $result->toArray(),
  349. 'count' => $count,
  350. ];
  351. return Result::success($data);
  352. }
  353. /**
  354. * @param array $data
  355. * @return array
  356. */
  357. public function createWebsiteRoleUser(array $data): array
  358. {
  359. $insertData = [
  360. 'website_id' => $data['website_id'],
  361. 'user_id' => $data['user_id'] ?? '',
  362. ];
  363. $info = WebsiteRoleUser::where($insertData)->first();
  364. if ($info) {
  365. return Result::error("不能重复添加角色用户", 0);
  366. }
  367. $insertData['role_id'] = $data['role_id'] ?? '';
  368. $insertData['admin_user_id'] = $data['admin_user_id'] ?? '';
  369. $insertData['type'] = $data['type'] ?? '';
  370. $result = WebsiteRoleUser::insertGetId($insertData);
  371. if (empty($result)) {
  372. return Result::error("创建失败", 0);
  373. } else {
  374. return Result::success(["id" => $result]);
  375. }
  376. }
  377. /**
  378. * @param int $id
  379. * @param array $data
  380. * @return array
  381. */
  382. public function updateWebsiteRoleUser(int $id, array $data): array
  383. {
  384. $insertData = [
  385. 'website_id' => $data['website_id'],
  386. 'type' => $data['type'] ?? '',
  387. 'type_id' => $data['type_id'] ?? '',
  388. 'role_id' => $data['role_id'] ?? '',
  389. ];
  390. $result = WebsiteRoleUser::where('id', $id)->update($insertData);
  391. if (empty($result)) {
  392. return Result::error("更新失败", 0);
  393. } else {
  394. return Result::success();
  395. }
  396. }
  397. /**
  398. * @param int $id
  399. * @return array
  400. */
  401. public function delWebsiteRoleUser(int $id): array
  402. {
  403. $result = WebsiteRoleUser::where('id', $id)->delete();
  404. if (empty($result)) {
  405. return Result::error("删除失败", 0);
  406. } else {
  407. return Result::success();
  408. }
  409. }
  410. /**
  411. * 根据域名获取网站 站点id
  412. * @param array $data
  413. * @return array
  414. */
  415. public function getWebsiteId(array $data): array
  416. {
  417. $result = Website::whereJsonContains('website_url', $data['website_url'])->first();
  418. if (empty($result)) {
  419. return Result::error("查询站点失败", 0);
  420. } else {
  421. return Result::success($result);
  422. }
  423. }
  424. /**
  425. * 查询网站下面的导航
  426. * @param array $data
  427. * @return array
  428. */
  429. public function getWebsiteCategory(array $data): array
  430. {
  431. $where = [
  432. 'website_id' => $data['website_id'],
  433. 'pid' => 0,
  434. ];
  435. $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
  436. if (empty($result)) {
  437. return Result::error("查询站点栏目失败", 0);
  438. } else {
  439. return Result::success($result);
  440. }
  441. }
  442. /**
  443. * 网站首页数据统计, 管理员
  444. * @return void
  445. */
  446. public function getAdminIndex(array $data): array
  447. {
  448. var_dump("用户类型:", $data['type_id']);
  449. switch ($data['type_id']) {
  450. case 4:
  451. $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;');
  452. return Result::success($result);
  453. break;
  454. case 10000:
  455. $res = [];
  456. //网站
  457. $res['website']['count'] = 0;
  458. $res['website']['growth_rate'] = 0;
  459. //资讯
  460. $res['article']['count'] = 0;
  461. $res['article']['growth_rate'] = 0;
  462. //导航池
  463. $res['category']['count'] = 0;
  464. $res['category']['growth_rate'] = 0;
  465. //近一月数据
  466. $res['monthArticle'] = [];
  467. //用户类型
  468. $res['userType'] = [];
  469. $res['website']['count'] = Website::where([])->count();
  470. $res['article']['count'] = Article::whereNotIn('status', ['404'])->count();
  471. $res['category']['count'] = Category::where([])->count();
  472. $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;');
  473. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  474. return Result::success($res);
  475. }
  476. return [];
  477. }
  478. /**
  479. * 获取模板类型
  480. * @return void
  481. */
  482. public function getTemplateClass(array $data): array
  483. {
  484. $where = [];
  485. if (isset($data['name']) && $data['name']) {
  486. array_push($where, ['name', 'like', '%' . $data['name'] . '%']);
  487. }
  488. $result = TemplateClass::where($where)->orderBy('sort', 'asc')->get();
  489. if (empty($result)) {
  490. return Result::error("没有模板类型", 0);
  491. } else {
  492. return Result::success($result);
  493. }
  494. }
  495. /**
  496. * 添加模板类型
  497. * @param
  498. * @return void
  499. */
  500. public function addTemplateClass(array $data): array
  501. {
  502. $insertData = [
  503. 'name' => $data['name'],
  504. ];
  505. $result = TemplateClass::insertGetId($insertData);
  506. if (empty($result)) {
  507. return Result::error("创建失败", 0);
  508. } else {
  509. return Result::success(["id" => $result]);
  510. }
  511. }
  512. /**
  513. * 更新模板
  514. * @param array $data
  515. * @return array
  516. */
  517. public function upTemplateClass(array $data): array
  518. {
  519. $where = [
  520. 'id' => $data['id'],
  521. ];
  522. $insertData = [
  523. 'name' => $data['name'],
  524. ];
  525. $result = TemplateClass::where($where)->update($insertData);
  526. if (empty($result)) {
  527. return Result::error("更新失败", 0);
  528. } else {
  529. return Result::success();
  530. }
  531. }
  532. /**
  533. * 删除模板
  534. * @param array $data
  535. * @return array
  536. */
  537. public function delTemplateClass(array $data): array
  538. {
  539. $where = [
  540. 'id' => $data['id'],
  541. ];
  542. $result = TemplateClass::where($where)->delete();
  543. if (empty($result)) {
  544. return Result::error("删除失败", 0);
  545. } else {
  546. return Result::success();
  547. }
  548. }
  549. /**
  550. * 获取分类下的模板
  551. * @param array $data
  552. * @return array
  553. */
  554. public function getTemplate(array $data): array
  555. {
  556. $page = $data['page'];
  557. $pageSize = $data['pageSize'];
  558. $where = [];
  559. if (isset($data['template_class_id']) && $data['template_class_id']) {
  560. array_push($where, ['template_class_id', '=', $data['template_class_id']]);
  561. }
  562. $result = Template::where($where)
  563. ->limit($pageSize)->offset(($page - 1) * $pageSize)->get();
  564. $count = Template::where($where)->count();
  565. if (empty($result)) {
  566. return Result::error("没有数据", 0);
  567. }
  568. $data = [
  569. 'rows' => $result->toArray(),
  570. 'count' => $count,
  571. ];
  572. return Result::success($data);
  573. }
  574. /**
  575. * 创建模板
  576. * @param
  577. * @return void
  578. */
  579. public function addTemplate(array $data): array
  580. {
  581. $insertData = [
  582. 'template_name' => $data['template_name'],
  583. 'template_img' => json_encode($data['template_img']),
  584. 'template_class_id' => $data['template_class_id'],
  585. ];
  586. $result = Template::insertGetId($insertData);
  587. if (empty($result)) {
  588. return Result::error("创建模板失败", 0);
  589. } else {
  590. return Result::success(["id" => $result]);
  591. }
  592. }
  593. /**
  594. * 更新模板
  595. * @param array $data
  596. * @return array
  597. */
  598. public function upTemplate(array $data): array
  599. {
  600. $where = [
  601. 'id' => $data['id'],
  602. ];
  603. $insertData = [
  604. 'template_name' => $data['template_name'],
  605. 'template_img' => json_encode($data['template_img']),
  606. 'template_class_id' => $data['template_class_id'],
  607. ];
  608. $result = Template::where($where)->update($insertData);
  609. if (empty($result)) {
  610. return Result::error("更新模板失败", 0);
  611. } else {
  612. return Result::success();
  613. }
  614. }
  615. /**
  616. * 删除模板
  617. * @param array $data
  618. * @return array
  619. */
  620. public function delTemplate(array $data): array
  621. {
  622. $where = [
  623. 'id' => $data['id'],
  624. ];
  625. $result = Template::where($where)->delete();
  626. if (empty($result)) {
  627. return Result::error("删除模板失败", 0);
  628. } else {
  629. return Result::success();
  630. }
  631. }
  632. /**
  633. * 搜索网站
  634. * @param array $data
  635. * @return array
  636. */
  637. public function websiteList(array $data): array
  638. {
  639. $where = [];
  640. if (isset($data['keyword']) && !empty($data['keyword'])) {
  641. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  642. }
  643. $result = Website::where($where)->get();
  644. if ($result) {
  645. return Result::success($result);
  646. } else {
  647. return Result::error("没有网站", 0);
  648. }
  649. }
  650. public function addWebsiteCategory(array $data): array
  651. {
  652. $website_id = $data['website_id'];
  653. $category_arr_id = $data['category_arr_id'];
  654. $categoryList = Category::whereIn('id', $category_arr_id)->get();
  655. $categoryListIds = [];
  656. if ($categoryList) {
  657. foreach ($categoryList->toArray() as $val) {
  658. array_push($categoryListIds, $val['id']);
  659. }
  660. }
  661. $arr = [];
  662. if ($categoryListIds) {
  663. foreach ($categoryListIds as $v) {
  664. $ids = $this->getUnderlingUIds(intval($v));
  665. $ids_arr = explode(",", $ids);
  666. array_push($arr, $ids_arr);
  667. }
  668. }
  669. $mergedArray = [];
  670. foreach ($arr as $subarray) {
  671. $mergedArray = array_merge($mergedArray, $subarray);
  672. }
  673. var_dump("所有:", $arr, $mergedArray);
  674. //查询出所有的分类进行分割插入 组装数据
  675. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  676. $categoryListData = $categoryListData->toArray();
  677. $insertData = [];
  678. if ($categoryListData) {
  679. foreach ($categoryListData as $key => $value) {
  680. $insertData[$key]['website_id'] = $website_id;
  681. $insertData[$key]['name'] = $value['name'];
  682. $insertData[$key]['sort'] = $value['sort'];
  683. $insertData[$key]['pid'] = $value['pid'];
  684. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  685. $insertData[$key]['seo_title'] = $value['seo_title'];
  686. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  687. $insertData[$key]['seo_description'] = $value['seo_description'];
  688. $insertData[$key]['alias'] = $value['name'];
  689. $insertData[$key]['category_id'] = $value['id'];
  690. }
  691. }
  692. $result = WebsiteCategory::insert($insertData);
  693. var_dump("插入数据状态:", $result);
  694. if ($result) {
  695. return Result::success($result);
  696. } else {
  697. return Result::error("创建失败", 0);
  698. }
  699. }
  700. /**
  701. * 删除网站导航
  702. * @param array $data
  703. * @return array
  704. */
  705. public function delWebsiteCategory(array $data): array
  706. {
  707. $website_id = $data['website_id'] ?? 0;
  708. $category_id = $data['category_id'] ?? 0;
  709. $ids = $this->getUnderlingUIds(intval($category_id));
  710. $ids_arr = explode(",", $ids);
  711. $result = WebsiteCategory::where(['website_id' => $website_id])->whereIn("category_id", $ids_arr)->delete();
  712. if ($result) {
  713. return Result::success($result);
  714. } else {
  715. return Result::error("删除失败", 0);
  716. }
  717. }
  718. /**
  719. * 获取网站导航
  720. * @param array $data
  721. * @return array
  722. */
  723. public function getAdminWebsiteCategory(array $data): array
  724. {
  725. $where = [
  726. 'website_id' => $data['website_id'],
  727. 'pid' => 0,
  728. ];
  729. $result = WebsiteCategory::where($where)->get();
  730. if ($result) {
  731. return Result::success($result);
  732. } else {
  733. return Result::error("查询失败", 0);
  734. }
  735. }
  736. /**
  737. * 更新网站导航
  738. * @param array $data
  739. * @return array
  740. */
  741. public function upWebsiteCategory(array $data): array
  742. {
  743. Db::beginTransaction();
  744. try {
  745. //合并栏目id
  746. $reqIds = array_merge($data['old_category_arr_id'], $data['new_category_arr_id']);
  747. //对比old 数组差异化,把差异化的删除
  748. $result = WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])->get();
  749. $result = $result->toArray();
  750. $categoryIds = [];
  751. if ($result) {
  752. foreach ($result as $val) {
  753. array_push($categoryIds, $val['category_id']);
  754. }
  755. }
  756. //和原始数据对比取交际
  757. $reqidsIntersect = array_intersect($reqIds, $categoryIds);
  758. //再取差集 进行对比
  759. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds), array_diff($categoryIds, $reqidsIntersect));
  760. var_dump("差集:", $differenceIDS);
  761. $arr_ids = [];
  762. if (count($differenceIDS) > 0) {
  763. foreach ($differenceIDS as $vv) {
  764. $idV = $this->getUnderlingUIds(intval($vv));
  765. $ids_arrV = explode(",", $idV);
  766. array_push($arr_ids, $ids_arrV);
  767. }
  768. }
  769. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  770. //有差异 删除
  771. if (count($del_ids) > 0) {
  772. WebsiteCategory::where(['website_id' => $data['website_id']])->whereIn("category_id", $del_ids)->delete();
  773. }
  774. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  775. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect), array_diff($reqidsIntersect, $reqIds));
  776. var_dump("要存储的:", $insertIDS);
  777. //新的数组重新创建
  778. if (count($insertIDS) > 0) {
  779. $arr = [];
  780. $categoryListIds = $insertIDS;
  781. if ($categoryListIds) {
  782. foreach ($categoryListIds as $v) {
  783. $ids = $this->getUnderlingUIds(intval($v));
  784. $ids_arr = explode(",", $ids);
  785. array_push($arr, $ids_arr);
  786. }
  787. }
  788. $mergedArray = [];
  789. foreach ($arr as $subarray) {
  790. $mergedArray = array_merge($mergedArray, $subarray);
  791. }
  792. var_dump("要插入的ID:", $mergedArray);
  793. //查询出所有的分类进行分割插入 组装数据
  794. $categoryListData = Category::whereIn('id', $mergedArray)->get();
  795. $categoryListData = $categoryListData->toArray();
  796. $insertData = [];
  797. if ($categoryListData) {
  798. foreach ($categoryListData as $key => $value) {
  799. $insertData[$key]['website_id'] = $data['website_id'];
  800. $insertData[$key]['name'] = $value['name'];
  801. $insertData[$key]['sort'] = $value['sort'];
  802. $insertData[$key]['pid'] = $value['pid'];
  803. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  804. $insertData[$key]['seo_title'] = $value['seo_title'];
  805. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  806. $insertData[$key]['seo_description'] = $value['seo_description'];
  807. $insertData[$key]['alias'] = $value['name'];
  808. $insertData[$key]['category_id'] = $value['id'];
  809. }
  810. }
  811. WebsiteCategory::insert($insertData);
  812. }
  813. Db::commit();
  814. } catch (\Throwable $ex) {
  815. Db::rollBack();
  816. var_dump($ex->getMessage());
  817. return Result::error("修改失败", 0);
  818. }
  819. return Result::success();
  820. }
  821. /**
  822. * 获取网站列表
  823. * @param array $data
  824. * @return array
  825. */
  826. public function getWebsiteCategoryList(array $data): array
  827. {
  828. $where = [];
  829. if (isset($data['keyword']) && !empty($data['keyword'])) {
  830. array_push($where, ['website.website_name', 'like', '%' . $data['keyword'] . '%']);
  831. }
  832. if (isset($data['website_column_id']) && !empty($data['website_column_id'])) {
  833. array_push($where, ['website.website_column_id', '=', $data['website_column_id']]);
  834. }
  835. $result = Website::where($where)
  836. ->with(["websiteCategory" => function ($query) {
  837. $query->where(['pid' => 0])->select('website_id', 'name', 'alias', 'category_id');
  838. }])
  839. ->limit($data['pageSize'])->orderBy("updated_at", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  840. ->get();
  841. $count = Website::where($where)->count();
  842. if (empty($result)) {
  843. return Result::error("没有数据", 0);
  844. }
  845. $data = [
  846. 'rows' => $result->toArray(),
  847. 'count' => $count,
  848. ];
  849. if ($result) {
  850. return Result::success($data);
  851. } else {
  852. return Result::error("查询失败", 0);
  853. }
  854. }
  855. /**
  856. * 删除网站下的所有导航
  857. * @param array $data
  858. * @return array
  859. */
  860. public function delWebsiteAllCategory(array $data): array
  861. {
  862. $website_id = $data['website_id'];
  863. $result = WebsiteCategory::where(['website_id' => $website_id])->delete();
  864. if ($result) {
  865. return Result::success($result);
  866. } else {
  867. return Result::error("删除失败", 0);
  868. }
  869. }
  870. /**
  871. * 获取网站下的某一个导航
  872. * @param array $data
  873. * @return array
  874. */
  875. public function getWebsiteCategoryOnes(array $data): array
  876. {
  877. $website_id = $data['website_id'];
  878. $category_id = $data['category_id'];
  879. $result = WebsiteCategory::where(['website_category.website_id' => $website_id, 'website_category.category_id' => $category_id])
  880. ->first();
  881. if ($result) {
  882. return Result::success($result);
  883. } else {
  884. return Result::error("查询失败", 0);
  885. }
  886. }
  887. /**
  888. * 更新网闸下的某一个导航
  889. * @param array $data
  890. * @return array
  891. */
  892. public function upWebsiteCategoryones(array $data): array
  893. {
  894. $where = [
  895. 'website_id' => $data['website_id'],
  896. 'category_id' => $data['category_id'],
  897. ];
  898. $result = WebsiteCategory::where($where)->update($data);
  899. if ($result) {
  900. return Result::success($result);
  901. } else {
  902. return Result::error("更新失败", 0);
  903. }
  904. }
  905. /**
  906. * 获取网站下的所有导航(包含子导航)
  907. * @param array $data
  908. * @return array
  909. */
  910. public function getWebsiteAllCategory(array $data): array
  911. {
  912. $where = [];
  913. if (isset($data['website_id']) && !empty($data['website_id'])) {
  914. array_push($where, ['website_category.website_id', '=', $data['website_id']]);
  915. }
  916. if (isset($data['name']) && !empty($data['name'])) {
  917. array_push($where, ['website_category.name', 'like', '%' . $data['name'] . '%']);
  918. }
  919. if (isset($data['alias']) && !empty($data['alias'])) {
  920. array_push($where, ['website_category.alias', 'like', '%' . $data['alias'] . '%']);
  921. }
  922. if (isset($data['department_id']) && !empty($data['department_id'])) {
  923. array_push($where, ['category.department_id', '=', $data['department_id']]);
  924. }
  925. if (isset($data['city_id']) && !empty($data['city_id'])) {
  926. array_push($where, ['category.city_id', '=', $data['city_id']]);
  927. }
  928. $result = WebsiteCategory::where($where)
  929. ->leftJoin("category", 'website_category.category_id', 'category.id')
  930. ->leftJoin("department", 'category.department_id', 'department.id')
  931. ->leftJoin("district", 'category.city_id', 'district.id')
  932. ->select("website_category.*", "department.name as department_name", "district.name as city_name")
  933. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  934. ->get();
  935. $count = WebsiteCategory::where($where)
  936. ->leftJoin("category", 'website_category.category_id', 'category.id')
  937. ->leftJoin("department", 'category.department_id', 'department.id')
  938. ->leftJoin("district", 'category.city_id', 'district.id')
  939. ->count();
  940. if (empty($result)) {
  941. return Result::error("没有数据", 0);
  942. }
  943. $data = [
  944. 'rows' => $result->toArray(),
  945. 'count' => $count,
  946. ];
  947. if ($result) {
  948. return Result::success($data);
  949. } else {
  950. return Result::error("查询失败", 0);
  951. }
  952. if ($result) {
  953. return Result::success($result);
  954. } else {
  955. return Result::error("查询失败", 0);
  956. }
  957. }
  958. /**
  959. * 递归查询数据
  960. * @param $id
  961. * @param $ids
  962. * @return string
  963. */
  964. public function getUnderlingUIds($id, $ids = '')
  965. {
  966. $back = Category::where(['pid' => $id])->get();
  967. $back = $back->toArray();
  968. if (!empty($back) && is_array($back)) {
  969. foreach ($back as $v) {
  970. //防止当前人的ID重复去查询,形成恶性循环
  971. if ($v['id'] == $id) {
  972. continue;
  973. }
  974. $back2 = Category::where(['pid' => $id])->count('id');
  975. if ($back2 > 0) {
  976. $ids = $this->getUnderlingUIds($v['id'], $ids);
  977. } else {
  978. $ids .= ',' . $v['id'];
  979. }
  980. }
  981. }
  982. $ids = $id . ',' . $ids . ',';
  983. $ids = str_replace(',,', ",", $ids);
  984. $ids = trim($ids, ',');
  985. return $ids;
  986. }
  987. /**
  988. * 检测网站名称是否重复
  989. * @param array $data
  990. * @return array
  991. */
  992. public function checkWebsiteName(array $data): array
  993. {
  994. if (isset($data['id'])) {
  995. $data[] = ['id', "!=", $data['id']];
  996. unset($data['id']);
  997. }
  998. $websiteInfo = Website::query()->where($data)->first();
  999. if (empty($websiteInfo)) {
  1000. return Result::error("找不到网站", 0);
  1001. }
  1002. return Result::success($websiteInfo->toArray());
  1003. }
  1004. /**
  1005. * 检测网站url是否重复
  1006. * @param array $data
  1007. * @return array
  1008. */
  1009. public function checkWebsiteUrl(array $data): array
  1010. {
  1011. $whereData = [];
  1012. if (isset($data['id'])) {
  1013. $whereData = [['id', "!=", $data['id']]];
  1014. unset($data['id']);
  1015. }
  1016. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1017. if (empty($websiteInfo)) {
  1018. return Result::error("找不到URL", 0);
  1019. }
  1020. return Result::success($websiteInfo->toArray());
  1021. }
  1022. //20250212 网站标识
  1023. public function addWebsiteGroup(array $data): array
  1024. {
  1025. //添加信息
  1026. $result = WebsiteGroup::insertGetId($data);
  1027. var_dump($result);
  1028. //判斷name是不是重复
  1029. $websiteGroupInfo = WebsiteGroup::query()->where('name', $data['name'])->first();
  1030. if (!empty($websiteGroupInfo)) {
  1031. return Result::error("网站标识重复", 0);
  1032. }
  1033. if (empty($result)) {
  1034. return Result::error("创建失败", 0);
  1035. } else {
  1036. return Result::success($result);
  1037. }
  1038. }
  1039. public function getWebsiteGroupList(array $data): array
  1040. {
  1041. $where = [];
  1042. if (isset($data['name']) && !empty($data['name'])) {
  1043. array_push($where, ['website_group.name', 'like', '%' . $data['name'] . '%']);
  1044. }
  1045. $result = WebsiteGroup::where($where)
  1046. ->limit($data['pageSize'])->orderBy("id", "desc")->offset(($data['page'] - 1) * $data['pageSize'])
  1047. ->get();
  1048. foreach ($result as $websiteGroup) {
  1049. $webIds = json_decode($websiteGroup->web_ids, true);
  1050. $websites = Website::whereIn('id', $webIds)->get();
  1051. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1052. $websiteGroup->website_names = $websiteNames;
  1053. $websiteGroup->website_names1 = implode(',', array_values($websiteNames));
  1054. }
  1055. $count = WebsiteGroup::where($where)->count();
  1056. if (empty($result)) {
  1057. return Result::error("没有数据", 0);
  1058. }
  1059. return Result::success(['list' => $result->toArray(), 'count' => $count]);
  1060. }
  1061. public function getWebsiteGroupInfo(array $data): array
  1062. {
  1063. $websiteInfo = WebsiteGroup::query()->where('id', $data['id'])->first();
  1064. $webIds = json_decode($websiteInfo->web_ids, true);
  1065. $websites = Website::whereIn('id', $webIds)->get();
  1066. $websiteNames = $websites->pluck('website_name', 'id')->toArray();
  1067. $websiteInfo->website_names = $websiteNames;
  1068. $websiteInfo->website_names1 = implode(',', array_values($websiteNames));
  1069. if (empty($websiteInfo)) {
  1070. return Result::error("找不到URL", 0);
  1071. }
  1072. return Result::success($websiteInfo->toArray());
  1073. }
  1074. public function updateWebsiteGroup(array $data): array
  1075. {
  1076. $where = [
  1077. 'id' => $data['id'],
  1078. ];
  1079. $insertData = [
  1080. 'name' => $data['name'],
  1081. 'web_ids' => $data['web_ids'],
  1082. ];
  1083. $result = WebsiteGroup::where($where)->update($insertData);
  1084. var_dump($result, '------更新');
  1085. if (empty($result)) {
  1086. return Result::error("更新失败", 0);
  1087. } else {
  1088. return Result::success();
  1089. }
  1090. }
  1091. public function deleteWebsiteGroup(array $data): array
  1092. {
  1093. $where = [
  1094. 'id' => $data['id'],
  1095. ];
  1096. //看看user表是不是有这个id
  1097. $userGroup = User::query()->where('sszq', $data['id'])->first();
  1098. if (!empty($userGroup)) {
  1099. return Result::error("有用户在使用该网站标识,不能删除", 0);
  1100. }
  1101. $result = WebsiteGroup::where($where)->delete();
  1102. if (empty($result)) {
  1103. return Result::error("删除失败", 0);
  1104. } else {
  1105. return Result::success();
  1106. }
  1107. }
  1108. public function getWebsiteNavList(array $data): array
  1109. {
  1110. $where = [];
  1111. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1112. array_push($where, ['website_id', '=', $data['website_id']]);
  1113. }
  1114. if (isset($data['pid']) && !empty($data['pid'])) {
  1115. array_push($where, ['pid', '=', $data['pid']]);
  1116. }
  1117. $list = WebsiteCategory::query()->where($where)->get();
  1118. if (empty($list)) {
  1119. return Result::error("获取失败", 0);
  1120. } else {
  1121. return Result::success($list);
  1122. }
  1123. }
  1124. }