WebsiteService.php 50 KB

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