WebsiteService.php 54 KB

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