WebsiteService.php 54 KB

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