WebsiteService.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. $result = WebsiteTemplateInfo::insertGetId($data);
  1136. } else {
  1137. return Result::error("该网站已经构建过了!", 0);
  1138. }
  1139. } else {
  1140. return Result::error("请先选择首页和底部导航详情页!", 0);
  1141. }
  1142. }
  1143. if(empty($result)){
  1144. return Result::error("添加失败!",0);
  1145. }else{
  1146. return Result::success($result);
  1147. }
  1148. }
  1149. /**
  1150. * 获取网站基础信息
  1151. *
  1152. * @return array
  1153. */
  1154. public function getWebsiteTemplateintel(array $data): array
  1155. {
  1156. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1157. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1158. ->leftJoin('website','website.id','website_template_info.website_id')
  1159. ->select(
  1160. "website_template_info.*",
  1161. "footer_category.type",
  1162. "website.website_name",
  1163. "website.id as website_id"
  1164. )
  1165. ->first();
  1166. if(empty($result['website_name'])){
  1167. return Result::error("请输入正确的网站id!", 0);
  1168. }
  1169. if($result){
  1170. $result['page_type'] = json_decode($result['page_type'],true);
  1171. return Result::success($result);
  1172. }else{
  1173. return Result::error("请先添加网站基础信息!",0);
  1174. }
  1175. }
  1176. /**
  1177. * 修改网站基础信息
  1178. *
  1179. * @return array
  1180. */
  1181. public function upWebsiteTemplateintel(array $data): array
  1182. {
  1183. $web = Website::where('website.id',$data['website_id'])->first();
  1184. if(empty($web)){
  1185. return Result::error("请输入正确的网站id!", 0);
  1186. }
  1187. $where = ['website_id'=>$data['website_id']];
  1188. $result = WebsiteTemplateInfo::where($where)->first();
  1189. if(empty($result)){
  1190. return Result::error("请先添加网站基础信息!",0);
  1191. }else{
  1192. unset($data['website_id']);
  1193. // 删除重复元素
  1194. $data['page_type'] = array_unique($data['page_type']);
  1195. // 重新索引数组
  1196. $data['page_type'] = array_values($data['page_type']);
  1197. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1198. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1199. // 将数组转换为 JSON 字符串
  1200. $data['page_type'] = json_encode($data['page_type'])??'';
  1201. $result = WebsiteTemplateInfo::where($where)->update($data);
  1202. return Result::success($result);
  1203. }else{
  1204. return Result::error("请先选择首页和底部导航详情页!",0);
  1205. }
  1206. }
  1207. if(empty($result)){
  1208. return Result::error("修改失败!",0);
  1209. }else{
  1210. return Result::success($result);
  1211. }
  1212. }
  1213. /**
  1214. * 获取所有网站风格信息
  1215. *
  1216. * @return array
  1217. */
  1218. public function getAllTemplateClass(array $data): array
  1219. {
  1220. $result = TemplateClass::all();
  1221. if(empty($result)){
  1222. return Result::error("没有查找到相关数据!",0);
  1223. }else{
  1224. return Result::success($result);
  1225. }
  1226. }
  1227. /**
  1228. * 搜索并获取网站模板信息
  1229. * @param array $data
  1230. * @return array
  1231. */
  1232. public function getWebsiteTemplateList(array $data): array
  1233. {
  1234. $website = Website::where('website.id',$data['website_id'])->first();
  1235. if(empty($website)){
  1236. return Result::error("请输入正确的网站id!",0);
  1237. }else{
  1238. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1239. if(empty($page_type)){
  1240. return Result::error("此网站还未添加基础信息!",0);
  1241. } else{
  1242. $where = json_decode($page_type['page_type'], true);
  1243. // $where = $data["page_type"];
  1244. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1245. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1246. if(empty($template_class)){
  1247. return Result::error("请输入正确的模板风格id!",0);
  1248. }else{
  1249. // 获取指定风格下的模板
  1250. $rep = Template::where('template_class_id', $data['template_class_id'])
  1251. ->where(function ($query) use ($where) {
  1252. foreach ($where as $value) {
  1253. $query->whereJsonContains('template_img', ['value' => $value]);
  1254. }
  1255. })
  1256. ->limit($data['pageSize'])
  1257. ->offset(($data['page'] - 1) * $data['pageSize'])
  1258. ->orderBy("template.updated_at", "desc")
  1259. ->get();
  1260. $count = Template::where('template_class_id', $data['template_class_id'])
  1261. ->where(function ($query) use ($where) {
  1262. foreach ($where as $value) {
  1263. $query->whereJsonContains('template_img', ['value' => $value]);
  1264. }
  1265. })->count();
  1266. }
  1267. }else{
  1268. //获取所有模板
  1269. $rep = Template::where(function ($query) use ($where) {
  1270. foreach ($where as $value) {
  1271. $query->whereJsonContains('template_img', ['value' => $value]);
  1272. }
  1273. })
  1274. ->limit($data['pageSize'])
  1275. ->offset(($data['page'] - 1) * $data['pageSize'])
  1276. ->orderBy("template.updated_at", "desc")
  1277. ->get();
  1278. $count = Template::where(function ($query) use ($where) {
  1279. foreach ($where as $value) {
  1280. $query->whereJsonContains('template_img', ['value' => $value]);
  1281. }
  1282. })->count();
  1283. }
  1284. if(empty($rep) || $rep->count()==0){
  1285. return Result::error("没有查找到相关数据!",0);
  1286. }else{
  1287. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1288. // $rep->each(function ($item) use ($where) {
  1289. // $template_img = json_decode($item->template_img, true);
  1290. // if(is_array($template_img)){
  1291. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1292. // return isset($img['value']) && in_array($img['value'], $where);
  1293. // });
  1294. // }
  1295. // $item->template_img = $filtered_img;
  1296. // });
  1297. // $result = [
  1298. // "rows" => $rep->toArray(),
  1299. // "count" => $count
  1300. // ];
  1301. $rep->each(function ($item) use ($where) {
  1302. $template_img = json_decode($item->template_img, true);
  1303. if (is_array($template_img)) {
  1304. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1305. return isset($img['value']) && in_array($img['value'], $where);
  1306. });
  1307. // 对过滤后的数组进行重新排序
  1308. $sorted_img = array_values($filtered_img);
  1309. $item->template_img = $sorted_img;
  1310. } else {
  1311. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1312. }
  1313. });
  1314. $result = [
  1315. "rows" => $rep->toArray(),
  1316. "count" => $count
  1317. ];
  1318. }
  1319. }
  1320. }
  1321. return Result::success($result);
  1322. }
  1323. /**
  1324. * 添加网站模板
  1325. * @param array $data
  1326. */
  1327. public function addWebsiteTemplateclassintel(array $data): array
  1328. {
  1329. $template = Template::where('id',$data['template_id'])->first();
  1330. $web = Website::where('id',$data['website_id'])->first();
  1331. if(empty($template)){
  1332. return Result::error("请输入正确的模板id",0);
  1333. }
  1334. if(empty($web)){
  1335. return Result::error("请输入正确的网站id",0);
  1336. }
  1337. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1338. if(empty($result)){
  1339. return Result::error("添加失败",0);
  1340. }else{
  1341. return Result::success($result);
  1342. }
  1343. }
  1344. /**
  1345. * 获取网站模板信息
  1346. * @param array $data
  1347. */
  1348. public function getWebsiteTemplateclassintel(array $data): array
  1349. {
  1350. // return Result::success($data);
  1351. $template = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1352. if(empty($template)){
  1353. return Result::error("请输入正确的搭建网站id",0);
  1354. }
  1355. $page_type = json_decode($template['page_type'], true);
  1356. // 获取指定网站下的基础信息和模板及风格信息
  1357. $result = WebsiteTemplateInfo::where([
  1358. ['website_template_info.website_id',$data['website_id']],
  1359. ['template_id','!=',null]
  1360. ])
  1361. ->leftJoin('template','template.id','website_template_info.template_id')
  1362. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1363. ->where(function ($query) use ($page_type) {
  1364. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1365. foreach ($page_type as $pageType) {
  1366. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1367. }
  1368. })
  1369. ->select(
  1370. 'template.template_name',
  1371. 'template.template_img',
  1372. 'template.id as tid',
  1373. 'template_class.name',
  1374. 'template_class.id as class_id',
  1375. 'website_template_info.page_type',
  1376. 'website_template_info.website_id')
  1377. ->first();
  1378. // return Result::success($result);
  1379. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1380. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1381. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1382. foreach ($template_img as $key => $value) {
  1383. if (!in_array($value['value'], $page_type)) {
  1384. unset($template_img[$key]);
  1385. }
  1386. }
  1387. // return Result::success($result);
  1388. $result['template_img'] = array_values($template_img);
  1389. $result['page_type'] = $page_type;
  1390. // $templateList = $this->getWebsiteTemplateList($data);
  1391. // $count = $templateList['data']['count']??'';
  1392. // $pages = $count / $data['pageSize'];
  1393. // $found = false;
  1394. // while ($pages && !$found) {
  1395. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1396. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1397. // if ($index !== false) {
  1398. // $found = true;
  1399. // $result['page'] = $pages;
  1400. // $pages = 0;
  1401. // // echo "查询到的 template 在第 $page 页";
  1402. // }
  1403. // $pages--;
  1404. // }
  1405. if(empty($result)){
  1406. return Result::error("没有查找到相关数据",0);
  1407. }else{
  1408. return Result::success($result);
  1409. }
  1410. }
  1411. }