WebsiteService.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. ->select(
  1038. "website.website_name",
  1039. "website.website_url",
  1040. "website_template_info.id as tid",
  1041. "website_template_info.created_at",
  1042. "website_template_info.updated_at",
  1043. "website_template_info.page_type",
  1044. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1045. "template.template_name"
  1046. );
  1047. //若存在条件;则在$query的基础上进行筛选
  1048. if (isset($data['website_name'])) {
  1049. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1050. }
  1051. if (isset($data['status'])) {
  1052. $query->where('website_template_info.status', $data['status']);
  1053. }
  1054. $count = $query->count();
  1055. $query->limit($data['pageSize'])
  1056. ->offset(($data['page'] - 1) * $data['pageSize'])
  1057. ->orderBy("website_template_info.updated_at", "desc");
  1058. $rep = $query->get();
  1059. if ($rep->count() == 0) {
  1060. return Result::error("没有查找到相关数据", 0);
  1061. } else {
  1062. $rep->each(function ($item) {
  1063. if (!empty($item->page_type)) {
  1064. $pageTypeArray = json_decode($item->page_type, true);
  1065. if (is_array($pageTypeArray)) {
  1066. $item->page_type = $pageTypeArray;
  1067. }
  1068. }
  1069. });
  1070. $result = [
  1071. "rows" => $rep->toArray(),
  1072. "count" => $count,
  1073. ];
  1074. return Result::success($result);
  1075. }
  1076. // return Result::success($result);
  1077. }
  1078. /**
  1079. * 添加网站基础信息
  1080. * @param array $data
  1081. * @return array
  1082. */
  1083. public function addWebsiteTemplateintel(array $data): array
  1084. {
  1085. $website = Website::where('website.id', $data['website_id'])->first();
  1086. if (empty($website)) {
  1087. return Result::error("请输入正确的网站id!", 0);
  1088. }
  1089. if (empty($website['website_column_id'])) {
  1090. return Result::error("请先关联导航池!", 0);
  1091. }
  1092. if (isset($data['page_type'])) {
  1093. // 删除重复元素并重新索引数组
  1094. $data['page_type'] = array_unique($data['page_type']);
  1095. $data['page_type'] = array_values($data['page_type']);
  1096. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1097. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1098. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1099. if (empty($info)) {
  1100. // 将数组转换为 JSON 字符串
  1101. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1102. $result = WebsiteTemplateInfo::insertGetId($data);
  1103. } else {
  1104. return Result::error("该网站已经存在首页和底部导航详情页!", 0);
  1105. }
  1106. } else {
  1107. return Result::error("请先选择首页和底部导航详情页!", 0);
  1108. }
  1109. } else {
  1110. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1111. $foot_type = FooterCategory::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1112. if (empty($foot_type)) {
  1113. return Result::error("请先关联导航池!", 0);
  1114. } elseif (in_array(1, $foot_type)) {
  1115. $foot_type = 1;
  1116. } else {
  1117. $foot_type = 0;
  1118. }
  1119. $result['foot_type'] = $foot_type;
  1120. // 友情链接类型'1:图片 2:文字 3:底部';
  1121. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1122. $missingTypes = [];
  1123. if (!in_array(1, $types)) {
  1124. $missingTypes[] = "文字链接";
  1125. }
  1126. if (!in_array(2, $types)) {
  1127. $missingTypes[] = "图片链接";
  1128. }
  1129. if (!in_array(3, $types)) {
  1130. $missingTypes[] = "底部链接";
  1131. }
  1132. if (!empty($missingTypes)) {
  1133. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1134. return Result::error($errorMessage, 0);
  1135. }
  1136. }
  1137. if (empty($result)) {
  1138. return Result::error("添加失败!", 0);
  1139. } else {
  1140. return Result::success($result);
  1141. }
  1142. }
  1143. /**
  1144. * 获取网站基础信息
  1145. *
  1146. * @return array
  1147. */
  1148. public function getWebsiteTemplateintel(array $data): array
  1149. {
  1150. $result = WebsiteTemplateInfo::where('website_template_info.id', $data['id'])
  1151. ->leftJoin('footer_category', 'footer_category.website_id', 'website_template_info.website_id')
  1152. ->leftJoin('website', 'website.id', 'website_template_info.website_id')
  1153. ->select(
  1154. "website_template_info.*",
  1155. "footer_category.type",
  1156. "website.website_name"
  1157. )
  1158. ->first();
  1159. if (empty($result['website_name'])) {
  1160. return Result::error("请输入正确的网站id!", 0);
  1161. }
  1162. if ($result) {
  1163. $result['page_type'] = json_decode($result['page_type'], true);
  1164. return Result::success($result);
  1165. } else {
  1166. return Result::error("请先添加网站基础信息!", 0);
  1167. }
  1168. }
  1169. /**
  1170. * 修改网站基础信息
  1171. *
  1172. * @return array
  1173. */
  1174. public function upWebsiteTemplateintel(array $data): array
  1175. {
  1176. $web = Website::where('website.id', $data['website_id'])->first();
  1177. if (empty($web)) {
  1178. return Result::error("请输入正确的网站id!", 0);
  1179. }
  1180. $where = ['website_id' => $data['website_id']];
  1181. $result = WebsiteTemplateInfo::where($where)->first();
  1182. if (empty($result)) {
  1183. return Result::error("请先添加网站基础信息!", 0);
  1184. } else {
  1185. unset($data['website_id']);
  1186. // 删除重复元素
  1187. $data['page_type'] = array_unique($data['page_type']);
  1188. // 重新索引数组
  1189. $data['page_type'] = array_values($data['page_type']);
  1190. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1191. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1192. // 将数组转换为 JSON 字符串
  1193. $data['page_type'] = json_encode($data['page_type']) ?? '';
  1194. $result = WebsiteTemplateInfo::where($where)->update($data);
  1195. return Result::success($result);
  1196. } else {
  1197. return Result::error("请先选择首页和底部导航详情页!", 0);
  1198. }
  1199. }
  1200. if (empty($result)) {
  1201. return Result::error("修改失败!", 0);
  1202. } else {
  1203. return Result::success($result);
  1204. }
  1205. }
  1206. /**
  1207. * 获取所有网站风格信息
  1208. *
  1209. * @return array
  1210. */
  1211. public function getAllTemplateClass(array $data): array
  1212. {
  1213. $result = TemplateClass::all();
  1214. if (empty($result)) {
  1215. return Result::error("没有查找到相关数据!", 0);
  1216. } else {
  1217. return Result::success($result);
  1218. }
  1219. }
  1220. /**
  1221. * 搜索并获取网站模板信息
  1222. * @param array $data
  1223. * @return array
  1224. */
  1225. public function getWebsiteTemplateList(array $data): array
  1226. {
  1227. $website = Website::where('website.id', $data['website_id'])->first();
  1228. if (empty($website)) {
  1229. $message = "请输入正确的网站id!";
  1230. } else {
  1231. $page_type = WebsiteTemplateInfo::where('website_id', $data['website_id'])->select('page_type')->first();
  1232. if (empty($page_type)) {
  1233. $message = "此网站还未添加基础信息!";
  1234. } else {
  1235. $where = json_decode($page_type['page_type'], true);
  1236. // $where = $data["page_type"];
  1237. if (isset($data['template_class_id'])) {
  1238. $template_class = TemplateClass::where('id', $data['template_class_id'])->first();
  1239. if (empty($template_class)) {
  1240. $message = "请输入正确的模板风格id!";
  1241. } else {
  1242. // 获取指定风格下的模板
  1243. $rep = Template::where('template_class_id', $data['template_class_id'])
  1244. ->where(function ($query) use ($where) {
  1245. foreach ($where as $value) {
  1246. $query->whereJsonContains('template_img', ['value' => $value]);
  1247. }
  1248. })
  1249. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1250. ->select('template.*', 'template_class.name', 'template_class.id as class_id')
  1251. ->limit($data['pageSize'])
  1252. ->offset(($data['page'] - 1) * $data['pageSize'])
  1253. ->orderBy("template.updated_at", "desc")
  1254. ->get();
  1255. $count = Template::where('template_class_id', $data['template_class_id'])
  1256. ->where(function ($query) use ($where) {
  1257. foreach ($where as $value) {
  1258. $query->whereJsonContains('template_img', ['value' => $value]);
  1259. }
  1260. })->count();
  1261. }
  1262. } else {
  1263. //获取所有模板
  1264. $rep = Template::where(function ($query) use ($where) {
  1265. foreach ($where as $value) {
  1266. $query->whereJsonContains('template_img', ['value' => $value]);
  1267. }
  1268. })
  1269. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1270. ->select('template.*', 'template_class.name', 'template_class.id as class_id')
  1271. ->limit($data['pageSize'])
  1272. ->offset(($data['page'] - 1) * $data['pageSize'])
  1273. ->orderBy("template.updated_at", "desc")
  1274. ->get();
  1275. $count = Template::where('template_class_id', $data['template_class_id'])
  1276. ->where(function ($query) use ($where) {
  1277. foreach ($where as $value) {
  1278. $query->whereJsonContains('template_img', ['value' => $value]);
  1279. }
  1280. })->count();
  1281. }
  1282. if (empty($rep) || $rep->count() == 0) {
  1283. $message = "没有查找到相关数据!";
  1284. } else {
  1285. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1286. $rep->each(function ($item) use ($where) {
  1287. $template_img = json_decode($item->template_img, true);
  1288. if (is_array($template_img)) {
  1289. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1290. return isset($img['value']) && in_array($img['value'], $where);
  1291. });
  1292. }
  1293. $item->template_img = $filtered_img;
  1294. });
  1295. $result = [
  1296. "rows" => $rep->toArray(),
  1297. "count" => $count,
  1298. ];
  1299. }
  1300. }
  1301. }
  1302. if (!empty($message)) {
  1303. return Result::error($message, 0);
  1304. } else {
  1305. return Result::success($result);
  1306. }
  1307. }
  1308. /**
  1309. * 添加网站模板
  1310. * @param array $data
  1311. */
  1312. public function addWebsiteTemplateclassintel(array $data): array
  1313. {
  1314. $template = Template::where('id', $data['template_id'])->first();
  1315. $web = Website::where('id', $data['website_id'])->first();
  1316. if (empty($template)) {
  1317. return Result::error("请输入正确的模板id", 0);
  1318. }
  1319. if (empty($web)) {
  1320. return Result::error("请输入正确的网站id", 0);
  1321. }
  1322. $result = WebsiteTemplateInfo::where('website_id', $data['website_id'])->update(['template_id' => $data['template_id']]);
  1323. if (empty($result)) {
  1324. return Result::error("添加失败", 0);
  1325. } else {
  1326. return Result::success($result);
  1327. }
  1328. }
  1329. /**
  1330. * 获取网站模板信息
  1331. * @param array $data
  1332. */
  1333. public function getWebsiteTemplateclassintel(array $data): array
  1334. {
  1335. $template = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1336. if (empty($template)) {
  1337. return Result::error("请输入正确的搭建网站id", 0);
  1338. }
  1339. $page_type = json_decode($template['page_type'], true);
  1340. // 获取指定网站下的基础信息和模板及风格信息
  1341. $result = WebsiteTemplateInfo::where([
  1342. ['website_template_info.website_id', $data['website_id']],
  1343. ['template_id', '!=', null],
  1344. ])
  1345. ->leftJoin('template', 'template.id', 'website_template_info.template_id')
  1346. ->leftJoin('template_class', 'template_class.id', 'template.template_class_id')
  1347. ->where(function ($query) use ($page_type) {
  1348. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1349. foreach ($page_type as $pageType) {
  1350. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1351. }
  1352. })
  1353. ->select(
  1354. 'template.template_name',
  1355. 'template.template_img',
  1356. 'template.id as tid', 'template_class.name',
  1357. 'template_class.id as class_id',
  1358. 'website_template_info.*')
  1359. ->first();
  1360. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1361. $template_img = json_decode($result['template_img'], true);
  1362. $page_type = json_decode($result['page_type'], true);
  1363. foreach ($template_img as $key => $value) {
  1364. if (!in_array($value['value'], $page_type)) {
  1365. unset($template_img[$key]);
  1366. }
  1367. }
  1368. $result['template_img'] = $template_img;
  1369. if (empty($result)) {
  1370. return Result::error("没有查找到相关数据", 0);
  1371. } else {
  1372. return Result::success($result);
  1373. }
  1374. }
  1375. }