WebsiteService.php 52 KB

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