WebsiteService.php 50 KB

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