WebsiteService.php 53 KB

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