WebsiteService.php 58 KB

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