WebsiteService.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Ad;
  5. use App\Model\Category;
  6. use App\Model\AdPlace;
  7. use App\Model\Department;
  8. use App\Model\District;
  9. use App\Model\FooterCategory;
  10. use App\Model\FooterContent;
  11. use App\Model\WebsiteTemplateInfo;
  12. use App\Model\Link;
  13. use App\Model\LetterOfComplaint;
  14. use App\Model\TemplateClass;
  15. use App\Model\Template;
  16. use App\Model\User;
  17. use App\Model\WebsiteRole;
  18. use App\Model\WebsiteRoleUser;
  19. use App\Model\Website;
  20. use App\Model\WebsiteColumn;
  21. use Hyperf\DbConnection\Db;
  22. use Hyperf\RpcServer\Annotation\RpcService;
  23. use App\Tools\Result;
  24. use Carbon\Carbon;
  25. use App\Model\WebsiteCategory;
  26. use App\Model\WebsiteTemplate;
  27. use App\Model\Sector;
  28. use PhpParser\Node\Stmt\Return_;
  29. use function PHPUnit\Framework\isNull;
  30. use App\Tools\buildTree;
  31. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  32. class WebsiteService implements WebsiteServiceInterface
  33. {
  34. /**
  35. * @param string $keyword
  36. * @param int $page
  37. * @param int $pageSize
  38. * @return array
  39. */
  40. public function getWebsitetList(array $data): array
  41. {
  42. $where = [];
  43. if(isset($data['keyword']) && !empty($data['keyword'])){
  44. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  45. }
  46. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  47. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  48. }
  49. if(isset($data['city_id']) && !empty($data['city_id'])){
  50. array_push($where,['website.city_id','=',$data['city_id']]);
  51. }
  52. $result = Website::where($where)
  53. ->leftJoin("website_column","website.website_column_id","website_column.id")
  54. ->leftJoin("district","district.id","website.city_id")
  55. ->select("website.*","website_column.column_name","district.name as city_name")
  56. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("website.id","desc")->get();
  57. $count = Website::where($where)->count();
  58. if (empty($result)) {
  59. return Result::error("没有数据",0);
  60. }
  61. $data = [
  62. 'rows'=>$result->toArray(),
  63. 'count'=>$count
  64. ];
  65. return Result::success($data);
  66. }
  67. /**
  68. * @param array $data
  69. * @return array
  70. */
  71. public function createWebsite(array $data): array
  72. {
  73. var_dump("网站数据:",$data);
  74. $insertData = [
  75. 'website_name'=>$data['website_name'],
  76. 'logo'=>$data['logo']??'',
  77. 'website_url'=>$data['website_url']??'',
  78. 'city_id'=>$data['city_id']??0,
  79. 'website_column_id'=>$data['website_column_id'],
  80. 'title'=>$data['title']??'',
  81. 'keywords'=>$data['keywords']??'',
  82. 'description'=>$data['description']??'',
  83. 'status'=>$data['status']??0,
  84. 'website_column_arr_id'=>$data['website_column_arr_id'],
  85. 'city_arr_id'=>$data['city_arr_id']??[0],
  86. 'template_id' =>$data['template_id']??0,
  87. ];
  88. $result = Website::insertGetId($insertData);
  89. if(empty($result)){
  90. return Result::error("创建失败",0);
  91. }else{
  92. return Result::success(["id"=>$result]);
  93. }
  94. }
  95. /**
  96. * @param int $id
  97. * @param array $data
  98. * @return array
  99. */
  100. public function updateWebsite(int $id,array $data): array
  101. {
  102. $insertData = [
  103. 'website_name'=>$data['website_name'],
  104. 'logo'=>$data['logo']??'',
  105. 'website_url'=>$data['website_url']??'',
  106. 'city_id'=>$data['city_id']??0,
  107. 'website_column_id'=>$data['website_column_id'],
  108. 'title'=>$data['title']??'',
  109. 'keywords'=>$data['keywords']??'',
  110. 'description'=>$data['description']??'',
  111. 'status'=>$data['status']??0,
  112. 'website_column_arr_id'=>$data['website_column_arr_id'],
  113. 'city_arr_id'=>$data['city_arr_id']??[0],
  114. 'template_id' =>$data['template_id']??0,
  115. ];
  116. $result = Website::where('id',$id)->update($insertData);
  117. var_dump("更新站点",$result);
  118. if(empty($result)){
  119. return Result::error("更新失败",0);
  120. }else{
  121. return Result::success();
  122. }
  123. }
  124. /**
  125. * @param int $id
  126. * @return array
  127. */
  128. public function delWebsite(int $id): array
  129. {
  130. $result = Website::where('id',$id )->delete();
  131. if(empty($result)){
  132. return Result::error("删除失败",0);
  133. }else{
  134. return Result::success();
  135. }
  136. }
  137. /**
  138. * @param int $id
  139. * @return array
  140. */
  141. public function getWebsiteInfo(int $id): array
  142. {
  143. $where = [
  144. ['website.id','=',$id]
  145. ];
  146. $result = Website::where($where )
  147. ->leftJoin("template","template.id","website.template_id")
  148. ->select("website.*","template.template_name","template.template_img")
  149. ->first();
  150. if(empty($result)){
  151. return Result::error("数据不存在",0);
  152. }else{
  153. return Result::success($result->toArray());
  154. }
  155. }
  156. /**
  157. * 查询所有的站点栏目
  158. * @return array
  159. */
  160. public function getWebsiteColumn(array $data): array
  161. {
  162. $result = WebsiteColumn::where($data)->get();
  163. if(empty($result)){
  164. return Result::error("数据不存在",0);
  165. }else{
  166. return Result::success($result->toArray());
  167. }
  168. }
  169. /**
  170. * @param string $keyword
  171. * @param int $page
  172. * @param int $pageSize
  173. * @return array
  174. */
  175. public function getWebsiteColumnList(array $data):array
  176. {
  177. $where = [];
  178. if(isset($data['keyword']) && $data['keyword']){
  179. array_push($where,['website_column.column_name','like','%'.$data['keyword'].'%']);
  180. }
  181. $result = WebsiteColumn::where($where)
  182. ->leftJoin("website_column as wc","website_column.pid","wc.id")
  183. ->select("website_column.*","wc.column_name as parent_column_name")
  184. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->get();
  185. $count = WebsiteColumn::where($where)->count();
  186. if (empty($result)) {
  187. return Result::error("没有数据",0);
  188. }
  189. $data = [
  190. 'rows'=>$result->toArray(),
  191. 'count'=>$count
  192. ];
  193. return Result::success($data);
  194. }
  195. /**
  196. * @param array $data
  197. * @return array
  198. */
  199. public function createWebsiteColumn(array $data): array
  200. {
  201. $insertData = [
  202. 'column_name'=>$data['column_name'],
  203. 'pid'=>$data['pid']??'',
  204. 'column_arr_id'=>$data['column_arr_id']??[0],
  205. 'sort'=>$data['sort']??0,
  206. 'remark'=>$data['remark']??'',
  207. ];
  208. $result = WebsiteColumn::insertGetId($insertData);
  209. if(empty($result)){
  210. return Result::error("创建失败",0);
  211. }else{
  212. return Result::success(["id"=>$result]);
  213. }
  214. }
  215. /**
  216. * @param int $id
  217. * @param array $data
  218. * @return array
  219. */
  220. public function updateWebsiteColumn(int $id,array $data): array
  221. {
  222. $insertData = [
  223. 'column_name'=>$data['column_name'],
  224. 'pid'=>$data['pid']??'',
  225. 'column_arr_id'=>$data['column_arr_id']??[0],
  226. 'sort'=>$data['sort']??0,
  227. 'remark'=>$data['remark']??'',
  228. ];
  229. $result = WebsiteColumn::where('id',$id)->update($insertData);
  230. if(empty($result)){
  231. return Result::error("更新失败",0);
  232. }else{
  233. return Result::success();
  234. }
  235. }
  236. /**
  237. * @param int $id
  238. * @return array
  239. */
  240. public function delWebsiteColumn(int $id): array
  241. {
  242. $list = WebsiteColumn::where(['pid'=>$id])->get();
  243. if($list){
  244. return Result::error("存在子网系,不能删除,请先删除子网系",0);
  245. }
  246. $result = WebsiteColumn::where('id',$id )->delete();
  247. if(empty($result)){
  248. return Result::error("删除失败",0);
  249. }else{
  250. return Result::success();
  251. }
  252. }
  253. /**
  254. * @param string $keyword
  255. * @param int $page
  256. * @param int $pageSize
  257. * @return array
  258. */
  259. public function getWebsiteRoleList(string $keyword,int $page,int $pageSize,int $websiteId):array
  260. {
  261. $where = [
  262. ['role.role_name','like','%'.$keyword.'%'],
  263. ['website_role.website_id','=',$websiteId],
  264. ];
  265. $result = WebsiteRole::where($where)
  266. ->leftJoin("role","role.id","website_role.role_id")
  267. ->select("role.*","website_role.type","website_role.role_id","website_role.id as website_role_id","website_role.website_id")
  268. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  269. $count = WebsiteRole::where($where) ->leftJoin("role","role.id","website_role.role_id")->count();
  270. if (empty($result)) {
  271. return Result::error("没有数据",0);
  272. }
  273. $data = [
  274. 'rows'=>$result->toArray(),
  275. 'count'=>$count
  276. ];
  277. return Result::success($data);
  278. }
  279. /**
  280. * @param array $data
  281. * @return array
  282. */
  283. public function createWebsiteRole(array $data): array
  284. {
  285. $insertData = [
  286. 'website_id'=>$data['website_id'],
  287. 'role_id'=>$data['role_id']??''
  288. ];
  289. $info = WebsiteRole::where($insertData)->first();
  290. if($info){
  291. return Result::error("不能重复添加角色",0);
  292. }
  293. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  294. $insertData['type'] = $data['type']??'';
  295. $result = WebsiteRole::insertGetId($insertData);
  296. if(empty($result)){
  297. return Result::error("创建失败",0);
  298. }else{
  299. return Result::success(["id"=>$result]);
  300. }
  301. }
  302. /**
  303. * 暂时用不上
  304. * @param int $id
  305. * @param array $data
  306. * @return array
  307. */
  308. public function updateWebsiteRole(int $id,array $data): array
  309. {
  310. $insertData = [
  311. 'website_id'=>$data['website_id'],
  312. 'type'=>$data['type']??'',
  313. ];
  314. $result = WebsiteRole::where('id',$id)->update($insertData);
  315. if(empty($result)){
  316. return Result::error("更新失败",0);
  317. }else{
  318. return Result::success();
  319. }
  320. }
  321. /**
  322. * @param int $id
  323. * @return array
  324. */
  325. public function delWebsiteRole(int $id): array
  326. {
  327. $result = WebsiteRole::where('id',$id )->delete();
  328. if(empty($result)){
  329. return Result::error("删除失败",0);
  330. }else{
  331. return Result::success();
  332. }
  333. }
  334. /**
  335. * @param string $keyword
  336. * @param int $page
  337. * @param int $pageSize
  338. * @return array
  339. */
  340. public function getWebsiteRoleUserList(string $keyword,int $page,int $pageSize,int $websiteId,int $roleId):array
  341. {
  342. $where = [
  343. ['website_role_user.website_id','=',$websiteId],
  344. ['website_role_user.role_id','=',$roleId],
  345. ];
  346. $count = WebsiteRoleUser::where($where)->count();
  347. $where[] = ['u.user_name','like','%'.$keyword.'%'];
  348. $result = WebsiteRoleUser::where($where)
  349. ->leftJoin("user as u","website_role_user.user_id","u.id")
  350. ->leftJoin("website as w","website_role_user.website_id","u.id")
  351. ->leftJoin("role as r","website_role_user.role_id","r.id")
  352. ->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')
  353. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  354. if (empty($result)) {
  355. return Result::error("没有数据",0);
  356. }
  357. $data = [
  358. 'rows'=>$result->toArray(),
  359. 'count'=>$count
  360. ];
  361. return Result::success($data);
  362. }
  363. /**
  364. * @param array $data
  365. * @return array
  366. */
  367. public function createWebsiteRoleUser(array $data): array
  368. {
  369. $insertData = [
  370. 'website_id'=>$data['website_id'],
  371. 'user_id'=>$data['user_id']??'',
  372. ];
  373. $info = WebsiteRoleUser::where($insertData)->first();
  374. if($info){
  375. return Result::error("不能重复添加角色用户",0);
  376. }
  377. $insertData['role_id'] = $data['role_id']??'';
  378. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  379. $insertData['type'] = $data['type']??'';
  380. $result = WebsiteRoleUser::insertGetId($insertData);
  381. if(empty($result)){
  382. return Result::error("创建失败",0);
  383. }else{
  384. return Result::success(["id"=>$result]);
  385. }
  386. }
  387. /**
  388. * @param int $id
  389. * @param array $data
  390. * @return array
  391. */
  392. public function updateWebsiteRoleUser(int $id,array $data): array
  393. {
  394. $insertData = [
  395. 'website_id'=>$data['website_id'],
  396. 'type'=>$data['type']??'',
  397. 'type_id'=>$data['type_id']??'',
  398. 'role_id'=>$data['role_id']??'',
  399. ];
  400. $result = WebsiteRoleUser::where('id',$id)->update($insertData);
  401. if(empty($result)){
  402. return Result::error("更新失败",0);
  403. }else{
  404. return Result::success();
  405. }
  406. }
  407. /**
  408. * @param int $id
  409. * @return array
  410. */
  411. public function delWebsiteRoleUser(int $id): array
  412. {
  413. $result = WebsiteRoleUser::where('id',$id )->delete();
  414. if(empty($result)){
  415. return Result::error("删除失败",0);
  416. }else{
  417. return Result::success();
  418. }
  419. }
  420. /**
  421. * 根据域名获取网站 站点id
  422. * @param array $data
  423. * @return array
  424. */
  425. public function getWebsiteId(array $data): array
  426. {
  427. $result = Website::whereJsonContains('website_url',$data['website_url'])->first();
  428. if(empty($result)){
  429. return Result::error("查询站点失败",0);
  430. }else{
  431. return Result::success($result);
  432. }
  433. }
  434. /**
  435. * 查询网站下面的导航
  436. * @param array $data
  437. * @return array
  438. */
  439. public function getWebsiteCategory(array $data): array
  440. {
  441. $where = [
  442. 'website_id'=>$data['website_id']
  443. ];
  444. // 使用递归查询构建树状结构
  445. $category = WebsiteCategory::where($where)
  446. ->with([
  447. 'children' => function ($query) {
  448. $query->orderBy('sort', 'asc');
  449. }
  450. ])
  451. ->orderBy('sort', 'asc')
  452. ->where('pid', 0) // 仅查询顶级节点
  453. ->get()->all();
  454. if(empty($category)){
  455. return Result::error("查询站点栏目失败",0);
  456. }else{
  457. $count = WebsiteCategory::where($where)->count();
  458. $result = [
  459. 'rep' => $category,
  460. 'count' => $count
  461. ];
  462. return Result::success($result);
  463. }
  464. }
  465. /**
  466. * 查询网站的广告
  467. * @param array $data
  468. * @return array
  469. */
  470. public function getWebsiteAdvertisement(array $data): array
  471. {
  472. $where = [
  473. 'ad_place.status' => 1,
  474. ];
  475. if(isset($data['ad_tag']) && !empty($data['ad_tag'])){
  476. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  477. $where[] = ['ad_place.ad_tag','like','%'.$data['ad_tag'].'%'];
  478. // return Result::success($where);
  479. $result = AdPlace::where($where)
  480. ->leftJoin("ad", function ($join) use ($now) {
  481. $join->on("ad.pid", "=", "ad_place.id")
  482. ->where('ad.status', 1)
  483. ->where('ad.fromtime', '<=', $now)
  484. ->where('ad.totime', '>=', $now);
  485. })
  486. ->select(
  487. 'ad_place.name as place_name',
  488. 'ad_place.thumb',
  489. 'ad_place.ad_tag',
  490. 'ad_place.introduce',
  491. 'ad.name as ad_name',
  492. 'ad.image_src',
  493. 'ad.image_url',
  494. 'ad.image_alt',
  495. 'ad_place.ad_url')
  496. ->get()->all();
  497. }else{
  498. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  499. // return Result::success($where);
  500. $result = AdPlace::where($where)
  501. ->where('ad_place.website_id',$data['website_id'])
  502. ->leftJoin("ad", function ($join) use ($now) {
  503. $join->on("ad.pid", "=", "ad_place.id")
  504. ->where('ad.status', 1)
  505. ->where('ad.fromtime', '<=', $now)
  506. ->where('ad.totime', '>=', $now);
  507. })
  508. ->select(
  509. 'ad_place.name as place_name',
  510. 'ad_place.thumb',
  511. 'ad_place.ad_tag',
  512. 'ad_place.introduce',
  513. 'ad.name as ad_name',
  514. 'ad.image_src',
  515. 'ad.image_url',
  516. 'ad.image_alt',
  517. 'ad_place.ad_url',)
  518. ->get()->all();
  519. // return Result::error("请选择广告位!",0);
  520. }
  521. if(empty($result)){
  522. return Result::error("此广告位不存在!",0);
  523. }
  524. return Result::success($result);
  525. }
  526. /**
  527. *
  528. * @param array $data
  529. * @return array
  530. */
  531. /**网站行政职能搜索 */
  532. public function selectWebsiteDepartment(array $data): array
  533. {
  534. $depart = Department::where('pid',0)->orderBy('id','asc')->get();
  535. if(isset($data['keyword']) && !empty($data['keyword'])){
  536. $departments= Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  537. if(empty($departments)){
  538. $result['message']="未查询到与此相关职能部门";
  539. }else{
  540. $count = Department::where('name','like',"%{$data['keyword']}%")->count();
  541. $m = [
  542. 'department'=>$depart,
  543. 'type'=>$departments,
  544. 'count'=>$count
  545. ];
  546. }
  547. $result['sele'] = $m;
  548. return Result::success($result['sele']);
  549. }
  550. $result=$depart;
  551. return Result::success($result);
  552. }
  553. /**
  554. * 搜索地区
  555. * @param array $data
  556. * @return array
  557. */
  558. public function selectWebsiteArea(array $data): array
  559. {
  560. // return Result::success($data);
  561. $pid = $data['pid'] ?? 0;
  562. $result = District::leftJoin('district as district2','district.pid','=','district2.id')
  563. ->where('district.pid',$pid)
  564. ->where('district.status',1)
  565. ->select('district.*','district2.name as pid_name')
  566. ->orderBy('district.code')->get();
  567. if(empty($result)){
  568. return Result::error("未查询到此地区",0);
  569. }else{
  570. return Result::success($result);
  571. }
  572. }
  573. /**
  574. * 获取栏目
  575. * @param array $data
  576. * @return array
  577. */
  578. public function getWebsiteModelCategory(array $data): array
  579. {
  580. // return Result::success($data);
  581. $website_id = [
  582. 'website_id' => $data['website_id'],
  583. ];
  584. // 初始化 $pid 数组
  585. // $pid = [];
  586. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  587. $pidQuery = WebsiteCategory::where($website_id)
  588. ->where('pid', '!=', 0)
  589. ->orderBy('sort')
  590. ->select('pid', 'category_id', 'alias');
  591. $pid = $pidQuery->pluck('pid');
  592. $pid = array_values(array_unique($pid->toArray()));
  593. // 构建查询语句
  594. $query = WebsiteCategory::where($website_id)
  595. ->where('pid', $data['pid'])
  596. ->offset($data['placeid'])
  597. ->limit($data['num'])
  598. ->orderBy('sort');
  599. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  600. if (!empty($pid)) {
  601. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  602. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  603. } else {
  604. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  605. $query->select('website_category.*', DB::raw('0 as children_count'));
  606. }
  607. // 执行查询
  608. $placeid = $data['placeid'] - 1;
  609. $result = $query->offset($placeid)->limit($data['num'])->get();
  610. if (!empty($result)) {
  611. $pidResults = $pidQuery->get();
  612. if(isset($data['type']) && $data['type'] == 1){
  613. $result = $result->map(function ($item) use ($pidResults) {
  614. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  615. if(!empty($child)){
  616. return $child;
  617. }
  618. });
  619. // 重置索引,使 key 值从 0 开始
  620. $item->children = $children->values();
  621. return $item;
  622. });
  623. }else{
  624. $pidMap = $pidResults->keyBy('pid');
  625. $result->each(function ($record) use ($pidMap) {
  626. if ($pidMap->has($record->category_id)) {
  627. $pidResult = $pidMap->get($record->category_id);
  628. $record->chilid_id = $pidResult->category_id;
  629. $record->chilid_alias = $pidResult->alias;
  630. }
  631. });
  632. }
  633. return Result::success($result);
  634. } else {
  635. return Result::error("本网站暂无栏目", 0);
  636. }
  637. }
  638. /**
  639. * 获取友情链接
  640. * @param array $data
  641. * @return array
  642. */
  643. public function selectWebsiteLinks(array $data): array
  644. {
  645. $where = [
  646. 'website_id' => $data['website_id'],
  647. 'status' => 1,
  648. 'type' => $data['type']
  649. ];
  650. $num=$data['num'];
  651. $result=Link::where($where)->orderBy('sort')->limit($num)->get();
  652. if(!empty($result)){
  653. return Result::success($result);
  654. }else{
  655. return Result::error("本网站暂无此类型友情链接",0);
  656. }
  657. }
  658. /**
  659. * 网站首页数据统计, 管理员
  660. * @return void
  661. */
  662. public function getAdminIndex(array $data): array
  663. {
  664. var_dump("用户类型:",$data['type_id']);
  665. switch ($data['type_id']){
  666. case 4:
  667. $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;');
  668. return Result::success($result);
  669. break;
  670. case 10000:
  671. $res = [];
  672. //网站
  673. $res['website']['count'] = 0;
  674. $res['website']['growth_rate'] = 0;
  675. //资讯
  676. $res['article']['count'] = 0;
  677. $res['article']['growth_rate'] = 0;
  678. //导航池
  679. $res['category']['count'] = 0;
  680. $res['category']['growth_rate'] = 0;
  681. //近一月数据
  682. $res['monthArticle']= [];
  683. //用户类型
  684. $res['userType'] = [];
  685. $res['website']['count'] = Website::where([])->count();
  686. $res['article']['count'] = Article::whereNotIn('status',['404'])->count();
  687. $res['category']['count'] = Category::where([])->count();
  688. $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;');
  689. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  690. return Result::success($res);
  691. }
  692. return [];
  693. }
  694. /**
  695. * 获取模板类型
  696. * @return void
  697. */
  698. public function getTemplateClass(array $data): array
  699. {
  700. $where = [];
  701. if(isset($data['name']) && $data['name']){
  702. array_push($where,['name','like','%'.$data['name'].'%']);
  703. }
  704. $result = TemplateClass::where($where)->orderBy('sort','asc')->get();
  705. if(empty($result)){
  706. return Result::error("没有模板类型",0);
  707. }else{
  708. return Result::success($result);
  709. }
  710. }
  711. /**
  712. * 添加模板类型
  713. * @param
  714. * @return void
  715. */
  716. public function addTemplateClass(array $data): array
  717. {
  718. $insertData = [
  719. 'name'=>$data['name']
  720. ];
  721. $result = TemplateClass::insertGetId($insertData);
  722. if(empty($result)){
  723. return Result::error("创建失败",0);
  724. }else{
  725. return Result::success(["id"=>$result]);
  726. }
  727. }
  728. /**
  729. * 更新模板
  730. * @param array $data
  731. * @return array
  732. */
  733. public function upTemplateClass(array $data): array
  734. {
  735. $where = [
  736. 'id'=>$data['id']
  737. ];
  738. $insertData = [
  739. 'name'=>$data['name']
  740. ];
  741. $result = TemplateClass::where($where)->update($insertData);
  742. if(empty($result)){
  743. return Result::error("更新失败",0);
  744. }else{
  745. return Result::success();
  746. }
  747. }
  748. /**
  749. * 删除模板
  750. * @param array $data
  751. * @return array
  752. */
  753. public function delTemplateClass(array $data): array
  754. {
  755. $where = [
  756. 'id'=>$data['id']
  757. ];
  758. $result = TemplateClass::where($where)->delete();
  759. if(empty($result)){
  760. return Result::error("删除失败",0);
  761. }else{
  762. return Result::success();
  763. }
  764. }
  765. /**
  766. * 获取分类下的模板
  767. * @param array $data
  768. * @return array
  769. */
  770. public function getTemplate(array $data): array
  771. {
  772. $page = $data['page'];
  773. $pageSize = $data['pageSize'];
  774. $where = [];
  775. if(isset($data['template_class_id']) && $data['template_class_id']){
  776. array_push($where,['template_class_id','=',$data['template_class_id']]);
  777. }
  778. $result = Template::where($where)
  779. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  780. $count = Template::where($where)->count();
  781. if (empty($result)) {
  782. return Result::error("没有数据",0);
  783. }
  784. $data = [
  785. 'rows'=>$result->toArray(),
  786. 'count'=>$count
  787. ];
  788. return Result::success($data);
  789. }
  790. /**
  791. * 创建模板
  792. * @param
  793. * @return void
  794. */
  795. public function addTemplate(array $data): array
  796. {
  797. $insertData = [
  798. 'template_name'=>$data['template_name'],
  799. 'template_img'=>json_encode($data['template_img']),
  800. 'template_class_id'=>$data['template_class_id'],
  801. ];
  802. $result = Template::insertGetId($insertData);
  803. if(empty($result)){
  804. return Result::error("创建模板失败",0);
  805. }else{
  806. return Result::success(["id"=>$result]);
  807. }
  808. }
  809. /**
  810. * 更新模板
  811. * @param array $data
  812. * @return array
  813. */
  814. public function upTemplate(array $data): array
  815. {
  816. $where = [
  817. 'id'=>$data['id']
  818. ];
  819. $insertData = [
  820. 'template_name'=>$data['template_name'],
  821. 'template_img'=>json_encode($data['template_img']),
  822. 'template_class_id'=>$data['template_class_id'],
  823. ];
  824. $result = Template::where($where)->update($insertData);
  825. if(empty($result)){
  826. return Result::error("更新模板失败",0);
  827. }else{
  828. return Result::success();
  829. }
  830. }
  831. /**
  832. * 删除模板
  833. * @param array $data
  834. * @return array
  835. */
  836. public function delTemplate(array $data): array
  837. {
  838. $where = [
  839. 'id'=>$data['id']
  840. ];
  841. $result = Template::where($where)->delete();
  842. if(empty($result)){
  843. return Result::error("删除模板失败",0);
  844. }else{
  845. return Result::success();
  846. }
  847. }
  848. /**
  849. * 搜索网站
  850. * @param array $data
  851. * @return array
  852. */
  853. public function websiteList(array $data): array
  854. {
  855. $where = [];
  856. if(isset($data['keyword']) && !empty($data['keyword'])){
  857. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  858. }
  859. $result = Website::where($where)->get();
  860. if($result){
  861. return Result::success($result);
  862. }else{
  863. return Result::error("没有网站",0);
  864. }
  865. }
  866. public function addWebsiteCategory(array $data): array
  867. {
  868. $website_id = $data['website_id'];
  869. $category_arr_id = $data['category_arr_id'];
  870. $categoryList = Category::whereIn('id',$category_arr_id)->get();
  871. $categoryListIds = [];
  872. if($categoryList){
  873. foreach ($categoryList->toArray() as $val){
  874. array_push($categoryListIds,$val['id']);
  875. }
  876. }
  877. $arr = [];
  878. if($categoryListIds){
  879. foreach ($categoryListIds as $v){
  880. $ids = $this->getUnderlingUIds(intval($v));
  881. $ids_arr = explode(",", $ids);
  882. array_push($arr,$ids_arr);
  883. }
  884. }
  885. $mergedArray = [];
  886. foreach ($arr as $subarray) {
  887. $mergedArray = array_merge($mergedArray, $subarray);
  888. }
  889. var_dump("所有:",$arr,$mergedArray);
  890. //查询出所有的分类进行分割插入 组装数据
  891. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  892. $categoryListData = $categoryListData->toArray();
  893. $insertData = [];
  894. if($categoryListData){
  895. foreach ($categoryListData as $key=>$value){
  896. $insertData[$key]['website_id'] = $website_id;
  897. $insertData[$key]['name'] = $value['name'];
  898. $insertData[$key]['sort'] = $value['sort'];
  899. $insertData[$key]['pid'] = $value['pid'];
  900. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  901. $insertData[$key]['seo_title'] = $value['seo_title'];
  902. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  903. $insertData[$key]['seo_description'] = $value['seo_description'];
  904. $insertData[$key]['alias'] = $value['name'];
  905. $insertData[$key]['category_id'] = $value['id'];
  906. }
  907. }
  908. $result = WebsiteCategory::insert($insertData);
  909. var_dump("插入数据状态:",$result);
  910. if($result){
  911. return Result::success($result);
  912. }else{
  913. return Result::error("创建失败",0);
  914. }
  915. }
  916. /**
  917. * 删除网站导航
  918. * @param array $data
  919. * @return array
  920. */
  921. public function delWebsiteCategory(array $data): array
  922. {
  923. $website_id = $data['website_id']??0;
  924. $category_id = $data['category_id']??0;
  925. $ids = $this->getUnderlingUIds(intval($category_id));
  926. $ids_arr = explode(",", $ids);
  927. $result = WebsiteCategory::where(['website_id'=>$website_id])->whereIn("category_id",$ids_arr)->delete();
  928. if($result){
  929. return Result::success($result);
  930. }else{
  931. return Result::error("删除失败",0);
  932. }
  933. }
  934. /**
  935. * 获取网站导航
  936. * @param array $data
  937. * @return array
  938. */
  939. public function getAdminWebsiteCategory(array $data): array
  940. {
  941. $where = [
  942. 'website_id'=>$data['website_id'],
  943. 'pid'=>0
  944. ];
  945. $result = WebsiteCategory::where($where)->get();
  946. if($result){
  947. return Result::success($result);
  948. }else{
  949. return Result::error("查询失败",0);
  950. }
  951. }
  952. /**
  953. * 更新网站导航
  954. * @param array $data
  955. * @return array
  956. */
  957. public function upWebsiteCategory(array $data): array
  958. {
  959. Db::beginTransaction();
  960. try{
  961. //合并栏目id
  962. $reqIds = array_merge($data['old_category_arr_id'],$data['new_category_arr_id']);
  963. //对比old 数组差异化,把差异化的删除
  964. $result = WebsiteCategory::where(['website_id'=>$data['website_id'],'pid'=>0])->get();
  965. $result = $result->toArray();
  966. $categoryIds = [];
  967. if($result){
  968. foreach ($result as $val){
  969. array_push($categoryIds,$val['category_id']);
  970. }
  971. }
  972. //和原始数据对比取交际
  973. $reqidsIntersect = array_intersect($reqIds,$categoryIds);
  974. //再取差集 进行对比
  975. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds),array_diff($categoryIds,$reqidsIntersect));
  976. var_dump("差集:",$differenceIDS);
  977. $arr_ids = [];
  978. if(count($differenceIDS)>0){
  979. foreach ($differenceIDS as $vv){
  980. $idV = $this->getUnderlingUIds(intval($vv));
  981. $ids_arrV = explode(",", $idV);
  982. array_push($arr_ids,$ids_arrV);
  983. }
  984. }
  985. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  986. //有差异 删除
  987. if(count($del_ids)>0){
  988. WebsiteCategory::where(['website_id'=>$data['website_id']])->whereIn("category_id",$del_ids)->delete();
  989. }
  990. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  991. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect),array_diff($reqidsIntersect,$reqIds));
  992. var_dump("要存储的:",$insertIDS);
  993. //新的数组重新创建
  994. if(count($insertIDS)>0){
  995. $arr = [];
  996. $categoryListIds = $insertIDS;
  997. if($categoryListIds){
  998. foreach ($categoryListIds as $v){
  999. $ids = $this->getUnderlingUIds(intval($v));
  1000. $ids_arr = explode(",", $ids);
  1001. array_push($arr,$ids_arr);
  1002. }
  1003. }
  1004. $mergedArray = [];
  1005. foreach ($arr as $subarray) {
  1006. $mergedArray = array_merge($mergedArray, $subarray);
  1007. }
  1008. var_dump("要插入的ID:",$mergedArray);
  1009. //查询出所有的分类进行分割插入 组装数据
  1010. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  1011. $categoryListData = $categoryListData->toArray();
  1012. $insertData = [];
  1013. if($categoryListData){
  1014. foreach ($categoryListData as $key=>$value){
  1015. $insertData[$key]['website_id'] = $data['website_id'];
  1016. $insertData[$key]['name'] = $value['name'];
  1017. $insertData[$key]['sort'] = $value['sort'];
  1018. $insertData[$key]['pid'] = $value['pid'];
  1019. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  1020. $insertData[$key]['seo_title'] = $value['seo_title'];
  1021. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  1022. $insertData[$key]['seo_description'] = $value['seo_description'];
  1023. $insertData[$key]['alias'] = $value['name'];
  1024. $insertData[$key]['category_id'] = $value['id'];
  1025. }
  1026. }
  1027. WebsiteCategory::insert($insertData);
  1028. }
  1029. Db::commit();
  1030. } catch(\Throwable $ex){
  1031. Db::rollBack();
  1032. var_dump($ex->getMessage());
  1033. return Result::error("修改失败",0);
  1034. }
  1035. return Result::success();
  1036. }
  1037. /**
  1038. * 获取网站列表
  1039. * @param array $data
  1040. * @return array
  1041. */
  1042. public function getWebsiteCategoryList(array $data): array
  1043. {
  1044. $where = [];
  1045. if(isset($data['keyword']) && !empty($data['keyword'])){
  1046. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  1047. }
  1048. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  1049. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  1050. }
  1051. $result = Website::where($where)
  1052. ->with(["websiteCategory"=>function ($query) {
  1053. $query->where(['pid'=>0])->select('website_id','name','alias','category_id');
  1054. }])
  1055. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1056. ->get();
  1057. $count = Website::where($where)->count();
  1058. if (empty($result)) {
  1059. return Result::error("没有数据",0);
  1060. }
  1061. $data = [
  1062. 'rows'=>$result->toArray(),
  1063. 'count'=>$count
  1064. ];
  1065. if($result){
  1066. return Result::success($data);
  1067. }else{
  1068. return Result::error("查询失败",0);
  1069. }
  1070. }
  1071. /**
  1072. * 删除网站下的所有导航
  1073. * @param array $data
  1074. * @return array
  1075. */
  1076. public function delWebsiteAllCategory(array $data): array
  1077. {
  1078. $website_id = $data['website_id'];
  1079. $result = WebsiteCategory::where(['website_id'=>$website_id])->delete();
  1080. if($result){
  1081. return Result::success($result);
  1082. }else{
  1083. return Result::error("删除失败",0);
  1084. }
  1085. }
  1086. /**
  1087. * 获取网站下的某一个导航
  1088. * @param array $data
  1089. * @return array
  1090. */
  1091. public function getWebsiteCategoryOnes(array $data): array
  1092. {
  1093. $website_id = $data['website_id'];
  1094. $category_id = $data['category_id'];
  1095. $result = WebsiteCategory::where(['website_category.website_id'=>$website_id,'website_category.category_id'=>$category_id])
  1096. ->first();
  1097. if($result){
  1098. return Result::success($result);
  1099. }else{
  1100. return Result::error("查询失败",0);
  1101. }
  1102. }
  1103. /**
  1104. * 更新网闸下的某一个导航
  1105. * @param array $data
  1106. * @return array
  1107. */
  1108. public function upWebsiteCategoryones(array $data): array
  1109. {
  1110. $where = [
  1111. 'website_id'=>$data['website_id'],
  1112. 'category_id'=>$data['category_id'],
  1113. ];
  1114. $result = WebsiteCategory::where($where)->update($data);
  1115. if($result){
  1116. return Result::success($result);
  1117. }else{
  1118. return Result::error("更新失败",0);
  1119. }
  1120. }
  1121. /**
  1122. * 获取网站下的所有导航(包含子导航)
  1123. * @param array $data
  1124. * @return array
  1125. */
  1126. public function getWebsiteAllCategory(array $data): array
  1127. {
  1128. $where = [];
  1129. if(isset($data['website_id']) && !empty($data['website_id'])){
  1130. array_push($where,['website_category.website_id','=',$data['website_id']]);
  1131. }
  1132. if(isset($data['name']) && !empty($data['name'])){
  1133. array_push($where,['website_category.name','like','%'.$data['name'].'%']);
  1134. }
  1135. if(isset($data['alias']) && !empty($data['alias'])){
  1136. array_push($where,['website_category.alias','like','%'.$data['alias'].'%']);
  1137. }
  1138. if(isset($data['department_id']) && !empty($data['department_id'])){
  1139. array_push($where,['category.department_id','=',$data['department_id']]);
  1140. }
  1141. if(isset($data['city_id']) && !empty($data['city_id'])){
  1142. array_push($where,['category.city_id','=',$data['city_id']]);
  1143. }
  1144. $result = WebsiteCategory::where($where)
  1145. ->leftJoin("category",'website_category.category_id','category.id')
  1146. ->leftJoin("department",'category.department_id','department.id')
  1147. ->leftJoin("district",'category.city_id','district.id')
  1148. ->select("website_category.*","department.name as department_name","district.name as city_name")
  1149. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1150. ->get();
  1151. $count = WebsiteCategory::where($where)
  1152. ->leftJoin("category",'website_category.category_id','category.id')
  1153. ->leftJoin("department",'category.department_id','department.id')
  1154. ->leftJoin("district",'category.city_id','district.id')
  1155. ->count();
  1156. if (empty($result)) {
  1157. return Result::error("没有数据",0);
  1158. }
  1159. $data = [
  1160. 'rows'=>$result->toArray(),
  1161. 'count'=>$count
  1162. ];
  1163. if($result){
  1164. return Result::success($data);
  1165. }else{
  1166. return Result::error("查询失败",0);
  1167. }
  1168. if($result){
  1169. return Result::success($result);
  1170. }else{
  1171. return Result::error("查询失败",0);
  1172. }
  1173. }
  1174. /**
  1175. * 递归查询数据
  1176. * @param $id
  1177. * @param $ids
  1178. * @return string
  1179. */
  1180. public function getUnderlingUIds($id, $ids='')
  1181. {
  1182. $back = Category::where(['pid'=>$id])->get();
  1183. $back = $back->toArray();
  1184. if (!empty($back) && is_array($back)) {
  1185. foreach ($back as $v) {
  1186. //防止当前人的ID重复去查询,形成恶性循环
  1187. if ($v['id'] == $id) {
  1188. continue;
  1189. }
  1190. $back2 = Category::where(['pid'=>$id])->count('id');
  1191. if ($back2 > 0) {
  1192. $ids = $this->getUnderlingUIds($v['id'],$ids);
  1193. } else {
  1194. $ids .= ','.$v['id'];
  1195. }
  1196. }
  1197. }
  1198. $ids = $id.','.$ids.',';
  1199. $ids = str_replace(',,', ",", $ids);
  1200. $ids = trim($ids, ',');
  1201. return $ids;
  1202. }
  1203. /**
  1204. * 检测网站名称是否重复
  1205. * @param array $data
  1206. * @return array
  1207. */
  1208. public function checkWebsiteName(array $data): array
  1209. {
  1210. if(isset($data['id'])){
  1211. $data[] = ['id',"!=",$data['id']];
  1212. unset($data['id']);
  1213. }
  1214. $websiteInfo = Website::query()->where($data)->first();
  1215. if (empty($websiteInfo)) {
  1216. return Result::error("找不到网站",0);
  1217. }
  1218. return Result::success($websiteInfo->toArray());
  1219. }
  1220. /**
  1221. * 检测网站url是否重复
  1222. * @param array $data
  1223. * @return array
  1224. */
  1225. public function checkWebsiteUrl(array $data): array
  1226. {
  1227. $whereData = [];
  1228. if(isset($data['id'])){
  1229. $whereData = [['id',"!=",$data['id']]];
  1230. unset($data['id']);
  1231. }
  1232. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1233. if (empty($websiteInfo)) {
  1234. return Result::error("找不到URL",0);
  1235. }
  1236. return Result::success($websiteInfo->toArray());
  1237. }
  1238. /**
  1239. * 获取网站底部基础信息
  1240. * @param array $data
  1241. * @return array
  1242. */
  1243. public function getWebsiteFootInfo(array $data): array
  1244. {
  1245. if(isset($data['website_id']) && !empty($data['website_id'])){
  1246. $website_head = Website::where('id',$data['website_id'])
  1247. ->where('status',1)
  1248. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1249. ->first();
  1250. if (empty($website_head)) {
  1251. return Result::error("找不到网站",0);
  1252. }
  1253. }else{
  1254. return Result::error("参数错误",0);
  1255. }
  1256. $website_foot = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1257. if (empty($website_foot)) {
  1258. return Result::error("暂无底部基础信息",0);
  1259. }
  1260. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1261. $result = [
  1262. 'website_foot'=>$website_foot,
  1263. 'website_head'=>$website_head
  1264. ];
  1265. return Result::success($result);
  1266. }
  1267. /**
  1268. * 获取网站底部导航
  1269. * @param array $data
  1270. * @return array
  1271. */
  1272. public function getWebsiteFooterCategory(array $data): array
  1273. {
  1274. if(isset($data['website_id']) && !empty($data['website_id'])){
  1275. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1276. if (empty($website)) {
  1277. return Result::error("找不到网站",0);
  1278. }
  1279. }else{
  1280. return Result::error("参数错误",0);
  1281. }
  1282. $result = FooterCategory::where('website_id',$data['website_id'])->get();
  1283. if (empty($result)) {
  1284. return Result::error("暂无底部导航",0);
  1285. }
  1286. return Result::success($result->toArray());
  1287. }
  1288. /**
  1289. * 获取网站底部导航列表
  1290. * @param array $data
  1291. * @return array
  1292. */
  1293. public function getWebsiteFooterCategoryList(array $data): array
  1294. {
  1295. if(isset($data['website_id']) && !empty($data['website_id'])){
  1296. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1297. if (empty($website)) {
  1298. return Result::error("找不到网站",0);
  1299. }
  1300. }else{
  1301. return Result::error("参数错误",0);
  1302. }
  1303. $footercategory = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1304. // '底部导航类型 0:内容型;1:列表型;',
  1305. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1306. return Result::error("底部导航id错误",0);
  1307. }else{
  1308. $query = FooterContent::where('fcat_id',$data['fcat_id']);
  1309. if($query->count() == 0){
  1310. return Result::error("暂无底部导航列表",0);
  1311. }elseif($query->count() == 1){
  1312. $result = $query->first();
  1313. }else{
  1314. $result = $query->get();
  1315. }
  1316. }
  1317. return Result::success($result);
  1318. }
  1319. /**
  1320. * 获取网站底部导航
  1321. * @param array $data
  1322. * @return array
  1323. */
  1324. public function getWebsiteFooterCategoryInfo(array $data): array
  1325. {
  1326. if(isset($data['website_id']) && !empty($data['website_id'])){
  1327. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1328. if (empty($website)) {
  1329. return Result::error("找不到网站",0);
  1330. }
  1331. }else{
  1332. return Result::error("参数错误",0);
  1333. }
  1334. if (isset($data['type']) && $data['type'] == 0) {
  1335. $fcatid = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1336. if (empty($fcatid)) {
  1337. return Result::error("底部导航id错误",0);
  1338. }
  1339. $result = FooterContent::where('fcat_id',$data['fcat_id'])->first();
  1340. }else{
  1341. $result = FooterContent::where('id',$data['fcat_id'])->first();
  1342. }
  1343. if (empty($result)) {
  1344. return Result::error("暂无底部导航内容",0);
  1345. }
  1346. return Result::success($result);
  1347. }
  1348. /*
  1349. * 搜索网站二级导航
  1350. * @param array $data
  1351. * @return array
  1352. * */
  1353. public function selectWebsiteCategory(array $data): array
  1354. {
  1355. if(isset($data['website_id']) &&!empty($data['website_id'])){
  1356. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1357. if (empty($website)) {
  1358. return Result::error("暂无该网站",0);
  1359. }
  1360. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id')->all();
  1361. // return Result::success($category);
  1362. if (empty($category)) {
  1363. return Result::error("暂无此导航",0);
  1364. }
  1365. $query = Category::whereIn('id', $category);
  1366. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1367. $cityid = District::where('id', $data['cityid'])->first();
  1368. if (empty($cityid)) {
  1369. return Result::error("暂无此城市", 0);
  1370. } else {
  1371. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1372. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1373. if (empty($city_category)) {
  1374. return Result::error("暂无此城市下的导航", 0);
  1375. }
  1376. $result['catid'] = $city_category;
  1377. $city = 1;
  1378. // var_dump("城市====================",$result);
  1379. }
  1380. }
  1381. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1382. $departmentid = Department::where('id', $data['department_id'])->first();
  1383. if (empty($departmentid)) {
  1384. return Result::error("暂无此部门", 0);
  1385. } else {
  1386. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1387. if (empty($depart_category)) {
  1388. return Result::error("暂无此部门下的导航", 0);
  1389. }
  1390. $result['catid'] = $depart_category;
  1391. // var_dump("职能部门*******************",$result);
  1392. $department = 1;
  1393. }
  1394. }
  1395. if(!empty($city) && !empty($department)){
  1396. // var_dump("城市和职能部门----------------------",$result);
  1397. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1398. if (empty($sel_category)) {
  1399. return Result::error("暂无此城市和职能部门下的导航", 0);
  1400. }
  1401. $result['catid'] = $sel_category;
  1402. }
  1403. if (empty($result)) {
  1404. return Result::error("暂无导航",0);
  1405. }
  1406. // department_id
  1407. }else{
  1408. return Result::error("参数错误",0);
  1409. }
  1410. return Result::success($result);
  1411. }
  1412. /**
  1413. * 获取网站栏目seo
  1414. * @param array $data
  1415. * @return array
  1416. */
  1417. public function getWebsiteCategoryHead(array $data): array
  1418. {
  1419. if(isset($data['website_id']) && !empty($data['website_id'])){
  1420. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1421. if (empty($website)) {
  1422. return Result::error("找不到网站",0);
  1423. }
  1424. }else{
  1425. return Result::error("参数错误",0);
  1426. }
  1427. if (isset($data['catid']) && !empty($data['catid'])) {
  1428. $result = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->first();
  1429. }
  1430. if (empty($result)) {
  1431. return Result::error("暂无导航",0);
  1432. }
  1433. $result['website_name'] = $website['website_name'] ?? '';
  1434. $result['suffix'] = $website['suffix'] ?? '';
  1435. return Result::success($result);
  1436. }
  1437. /*
  1438. * 获取某个栏目
  1439. * @param array $data
  1440. * @return array
  1441. * */
  1442. public function getOneWebsiteCategory(array $data): array
  1443. {
  1444. if(isset($data['website_id']) &&!empty($data['website_id'])){
  1445. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1446. if (empty($website)) {
  1447. return Result::error("暂无该网站",0);
  1448. }
  1449. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['category_id'])->first();
  1450. if (empty($category)) {
  1451. return Result::error("暂无此导航",0);
  1452. }
  1453. $category['children_count'] = WebsiteCategory::where('website_id',$data['website_id'])->where('pid',$data['category_id'])->count();
  1454. $parent = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$category['pid'])->select('category_id as parent_id','alias as parent_name','aLIas_pinyin')->first();
  1455. $category['parent_id'] = $parent['parent_id']?? '';
  1456. $category['parent_pinyin'] = $parent['aLIas_pinyin']?? '';
  1457. $category['parent_name'] = $parent['parent_name']?? '';
  1458. return Result::success($category);
  1459. }else{
  1460. return Result::error("参数错误",0);
  1461. }
  1462. }
  1463. // 自助建站--fr-------------------
  1464. /**
  1465. * 获取并搜索 网站模板信息
  1466. * @param array $data
  1467. * @return array
  1468. */
  1469. public function getWebsiteintel(array $data): array
  1470. {
  1471. //查询所有网站模板信息
  1472. $query = Website::where('website.status', 1)
  1473. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1474. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1475. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1476. ->select(
  1477. "website_template_info.id",
  1478. "template_class.name",
  1479. "website.id as website_id",
  1480. "website.website_name",
  1481. "website.website_url",
  1482. "website_template_info.action_id",
  1483. "website_template_info.created_at",
  1484. "website_template_info.updated_at",
  1485. "website_template_info.page_type",
  1486. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1487. "template.template_name"
  1488. );
  1489. //若存在条件;则在$query的基础上进行筛选
  1490. if(isset($data['website_name']) && !empty($data['website_name'])){
  1491. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1492. }
  1493. if(isset($data['status']) && !empty($data['status'])){
  1494. $query->where('website_template_info.status', $data['status']);
  1495. }
  1496. $count = $query->count();
  1497. $query->limit($data['pageSize'])
  1498. ->offset(($data['page'] - 1) * $data['pageSize'])
  1499. ->orderBy("website_template_info.updated_at", "desc");
  1500. $rep = $query->get();
  1501. if($rep->count()==0){
  1502. return Result::error("没有查找到相关数据",0);
  1503. } else {
  1504. $rep->each(function ($item) {
  1505. if (!empty($item->page_type)) {
  1506. $pageTypeArray = json_decode($item->page_type, true);
  1507. if (is_array($pageTypeArray)) {
  1508. $item->page_type = $pageTypeArray;
  1509. }
  1510. }
  1511. });
  1512. $result = [
  1513. "rows" => $rep->toArray(),
  1514. "count" => $count
  1515. ];
  1516. return Result::success($result);
  1517. }
  1518. // return Result::success($result);
  1519. }
  1520. /**
  1521. * 添加网站基础信息
  1522. * @param array $data
  1523. * @return array
  1524. */
  1525. public function addWebsiteTemplateintel(array $data): array
  1526. {
  1527. $website = Website::where('website.id',$data['website_id'])->first();
  1528. if(empty($website)){
  1529. return Result::error("请输入正确的网站id!", 0);
  1530. }
  1531. if(empty($website['website_column_id'])){
  1532. return Result::error("请先关联导航池!", 0);
  1533. }
  1534. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1535. $foot_type = FooterCategory::where('website_id',$data['website_id'])->pluck('type')->toArray();
  1536. if (empty($foot_type)) {
  1537. return Result::error("请先关联底部导航池!", 0);
  1538. } elseif (in_array(1, $foot_type)) {
  1539. $foot_type = 1;
  1540. } else {
  1541. $foot_type = 0;
  1542. }
  1543. $result['foot_type'] = $foot_type;
  1544. // 友情链接类型'1:图片 2:文字 3:底部';
  1545. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1546. $missingTypes = [];
  1547. if (!in_array(1, $types)) {
  1548. $missingTypes[] = "文字链接";
  1549. }
  1550. if (!in_array(2, $types)) {
  1551. $missingTypes[] = "图片链接";
  1552. }
  1553. if (!in_array(3, $types)) {
  1554. $missingTypes[] = "底部链接";
  1555. }
  1556. if (!empty($missingTypes)) {
  1557. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1558. return Result::error($errorMessage, 0);
  1559. }
  1560. if(isset($data['page_type'])){
  1561. // 删除重复元素并重新索引数组
  1562. $data['page_type'] = array_unique($data['page_type']);
  1563. $data['page_type'] = array_values($data['page_type']);
  1564. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1565. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1566. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1567. if (empty($info)) {
  1568. // 将数组转换为 JSON 字符串
  1569. $data['page_type'] = json_encode($data['page_type'])??'';
  1570. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1571. $data['action_id'] = 1;
  1572. try {
  1573. $result = WebsiteTemplateInfo::insertGetId($data);
  1574. } catch (\Exception $e) {
  1575. if ($e->getCode() == 22001) {
  1576. $errorMessage = $e->getMessage();
  1577. return Result::error("请检查表单,某个字段超出了长度限制!");
  1578. } else {
  1579. // 处理其他类型的数据库错误
  1580. return Result::error("插入操作失败: " . $e->getMessage());
  1581. }
  1582. return Result::error("添加失败!", 0);
  1583. }
  1584. } else {
  1585. return Result::error("该网站已经构建过了!", 0);
  1586. }
  1587. } else {
  1588. return Result::error("请先选择首页和底部导航详情页!", 0);
  1589. }
  1590. }
  1591. if(empty($result)){
  1592. return Result::error("添加失败!",0);
  1593. }else{
  1594. return Result::success($result);
  1595. }
  1596. }
  1597. /**
  1598. * 获取网站基础信息
  1599. *
  1600. * @return array
  1601. */
  1602. public function getWebsiteTemplateintel(array $data): array
  1603. {
  1604. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1605. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1606. ->leftJoin('website','website.id','website_template_info.website_id')
  1607. ->select(
  1608. "website_template_info.*",
  1609. "footer_category.type",
  1610. "website.website_name",
  1611. "website.id as website_id"
  1612. )
  1613. ->first();
  1614. if(empty($result['website_name'])){
  1615. return Result::error("请输入正确的网站id!", 0);
  1616. }
  1617. if($result){
  1618. $result['page_type'] = json_decode($result['page_type'],true);
  1619. return Result::success($result);
  1620. }else{
  1621. return Result::error("请先添加网站基础信息!",0);
  1622. }
  1623. }
  1624. /**
  1625. * 修改网站基础信息
  1626. *
  1627. * @return array
  1628. */
  1629. public function upWebsiteTemplateintel(array $data): array
  1630. {
  1631. $web = Website::where('website.id',$data['website_id'])->first();
  1632. if(empty($web)){
  1633. return Result::error("请输入正确的网站id!", 0);
  1634. }
  1635. $where = ['website_id'=>$data['website_id']];
  1636. $result = WebsiteTemplateInfo::where($where)->first();
  1637. if(empty($result)){
  1638. return Result::error("请先添加网站基础信息!",0);
  1639. }else{
  1640. unset($data['website_id']);
  1641. // 删除重复元素
  1642. $data['page_type'] = array_unique($data['page_type']);
  1643. // 重新索引数组
  1644. $data['page_type'] = array_values($data['page_type']);
  1645. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1646. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1647. // 将数组转换为 JSON 字符串
  1648. $data['page_type'] = json_encode($data['page_type'])??'';
  1649. try {
  1650. $result = WebsiteTemplateInfo::where($where)->update($data);
  1651. } catch (\Exception $e) {
  1652. if ($e->getCode() == 22001) {
  1653. $errorMessage = $e->getMessage();
  1654. return Result::error("请检查表单,某个字段超出了长度限制!");
  1655. } else {
  1656. // 处理其他类型的数据库错误
  1657. return Result::error("修改操作失败: " . $e->getMessage());
  1658. }
  1659. return Result::error("修改失败!", 0);
  1660. }
  1661. }else{
  1662. return Result::error("请先选择首页和底部导航详情页!",0);
  1663. }
  1664. }
  1665. if(empty($result)){
  1666. return Result::error("修改失败!",0);
  1667. }else{
  1668. return Result::success($result);
  1669. }
  1670. }
  1671. /**
  1672. * 获取所有网站风格信息
  1673. *
  1674. * @return array
  1675. */
  1676. public function getAllTemplateClass(array $data): array
  1677. {
  1678. $result = TemplateClass::all();
  1679. if(empty($result)){
  1680. return Result::error("没有查找到相关数据!",0);
  1681. }else{
  1682. return Result::success($result);
  1683. }
  1684. }
  1685. /**
  1686. * 搜索并获取网站模板信息
  1687. * @param array $data
  1688. * @return array
  1689. */
  1690. public function getWebsiteTemplateList(array $data): array
  1691. {
  1692. $website = Website::where('website.id',$data['website_id'])->first();
  1693. if(empty($website)){
  1694. return Result::error("请输入正确的网站id!",0);
  1695. }else{
  1696. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1697. if(empty($page_type)){
  1698. return Result::error("此网站还未添加基础信息!",0);
  1699. } else{
  1700. $where = json_decode($page_type['page_type'], true);
  1701. // $where = $data["page_type"];
  1702. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1703. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1704. if(empty($template_class)){
  1705. return Result::error("请输入正确的模板风格id!",0);
  1706. }else{
  1707. // 获取指定风格下的模板
  1708. $rep = Template::where('template_class_id', $data['template_class_id'])
  1709. ->where(function ($query) use ($where) {
  1710. foreach ($where as $value) {
  1711. $query->whereJsonContains('template_img', ['value' => $value]);
  1712. }
  1713. })
  1714. ->limit($data['pageSize'])
  1715. ->offset(($data['page'] - 1) * $data['pageSize'])
  1716. ->orderBy("template.updated_at", "desc")
  1717. ->get();
  1718. $count = Template::where('template_class_id', $data['template_class_id'])
  1719. ->where(function ($query) use ($where) {
  1720. foreach ($where as $value) {
  1721. $query->whereJsonContains('template_img', ['value' => $value]);
  1722. }
  1723. })->count();
  1724. }
  1725. }else{
  1726. //获取所有模板
  1727. $rep = Template::where(function ($query) use ($where) {
  1728. foreach ($where as $value) {
  1729. $query->whereJsonContains('template_img', ['value' => $value]);
  1730. }
  1731. })
  1732. ->limit($data['pageSize'])
  1733. ->offset(($data['page'] - 1) * $data['pageSize'])
  1734. ->orderBy("template.updated_at", "desc")
  1735. ->get();
  1736. $count = Template::where(function ($query) use ($where) {
  1737. foreach ($where as $value) {
  1738. $query->whereJsonContains('template_img', ['value' => $value]);
  1739. }
  1740. })->count();
  1741. }
  1742. if(empty($rep) || $rep->count()==0){
  1743. return Result::error("没有查找到相关数据!",0);
  1744. }else{
  1745. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1746. // $rep->each(function ($item) use ($where) {
  1747. // $template_img = json_decode($item->template_img, true);
  1748. // if(is_array($template_img)){
  1749. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1750. // return isset($img['value']) && in_array($img['value'], $where);
  1751. // });
  1752. // }
  1753. // $item->template_img = $filtered_img;
  1754. // });
  1755. // $result = [
  1756. // "rows" => $rep->toArray(),
  1757. // "count" => $count
  1758. // ];
  1759. $rep->each(function ($item) use ($where) {
  1760. $template_img = json_decode($item->template_img, true);
  1761. if (is_array($template_img)) {
  1762. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1763. return isset($img['value']) && in_array($img['value'], $where);
  1764. });
  1765. // 对过滤后的数组进行重新排序
  1766. $sorted_img = array_values($filtered_img);
  1767. $item->template_img = $sorted_img;
  1768. } else {
  1769. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1770. }
  1771. });
  1772. $result = [
  1773. "rows" => $rep->toArray(),
  1774. "count" => $count
  1775. ];
  1776. }
  1777. }
  1778. }
  1779. return Result::success($result);
  1780. }
  1781. /**
  1782. * 添加网站模板
  1783. * @param array $data
  1784. */
  1785. public function addWebsiteTemplateclassintel(array $data): array
  1786. {
  1787. $template = Template::where('id',$data['template_id'])->first();
  1788. $web = Website::where('id',$data['website_id'])->first();
  1789. if(empty($template)){
  1790. return Result::error("请输入正确的模板id",0);
  1791. }
  1792. if(empty($web)){
  1793. return Result::error("请输入正确的网站id",0);
  1794. }
  1795. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1796. if(empty($result)){
  1797. return Result::error("添加失败",0);
  1798. }else{
  1799. return Result::success($result);
  1800. }
  1801. }
  1802. /**
  1803. * 获取网站模板信息
  1804. * @param array $data
  1805. */
  1806. public function getWebsiteTemplateclassintel(array $data): array
  1807. {
  1808. // return Result::success($data);
  1809. $template = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1810. if(empty($template)){
  1811. return Result::error("请输入正确的搭建网站id",0);
  1812. }
  1813. $page_type = json_decode($template['page_type'], true);
  1814. // 获取指定网站下的基础信息和模板及风格信息
  1815. $result = WebsiteTemplateInfo::where([
  1816. ['website_template_info.website_id',$data['website_id']],
  1817. ['template_id','!=',null]
  1818. ])
  1819. ->leftJoin('template','template.id','website_template_info.template_id')
  1820. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1821. ->where(function ($query) use ($page_type) {
  1822. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1823. foreach ($page_type as $pageType) {
  1824. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1825. }
  1826. })
  1827. ->select(
  1828. 'template.template_name',
  1829. 'template.template_img',
  1830. 'template.id as tid',
  1831. 'template_class.name',
  1832. 'template_class.id as class_id',
  1833. 'website_template_info.page_type',
  1834. 'website_template_info.website_id')
  1835. ->first();
  1836. if(empty($result)){
  1837. return Result::error("没有查找到相关数据",0);
  1838. }
  1839. // return Result::success($result);
  1840. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1841. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1842. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1843. foreach ($template_img as $key => $value) {
  1844. if (!in_array($value['value'], $page_type)) {
  1845. unset($template_img[$key]);
  1846. }
  1847. }
  1848. // return Result::success($result);
  1849. $result['template_img'] = array_values($template_img);
  1850. $result['page_type'] = $page_type;
  1851. // $templateList = $this->getWebsiteTemplateList($data);
  1852. // $count = $templateList['data']['count']??'';
  1853. // $pages = $count / $data['pageSize'];
  1854. // $found = false;
  1855. // while ($pages && !$found) {
  1856. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1857. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1858. // if ($index !== false) {
  1859. // $found = true;
  1860. // $result['page'] = $pages;
  1861. // $pages = 0;
  1862. // // echo "查询到的 template 在第 $page 页";
  1863. // }
  1864. // $pages--;
  1865. // }
  1866. return Result::success($result);
  1867. }
  1868. /**
  1869. * 获取网站模板下的板块信息
  1870. * @param array $data
  1871. */
  1872. public function getWebsiteSectorList(array $data): array
  1873. {
  1874. $website = Website::where('id',$data['website_id'])->first();
  1875. if(empty($website)){
  1876. return Result::error("请输入正确的网站id",0);
  1877. }
  1878. $template = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1879. if(empty($template['template_id'])){
  1880. return Result::error("请先添加网站模板",0);
  1881. }
  1882. $query = Sector::where('template_id',$template['template_id']);
  1883. if($query->count() == 0){
  1884. return Result::error("没有查找到相关板块数据",0);
  1885. }elseif($query->count() == 1){
  1886. $sector = $query->first();
  1887. }else{
  1888. $sector = $query->get();
  1889. }
  1890. $result['page_type'] = $template['page_type'];
  1891. $result['tid'] = $template['template_id'];
  1892. $result = [
  1893. "tid" => $template['template_id'],
  1894. "page_type" => $template['page_type'],
  1895. "sector" => $sector
  1896. ];
  1897. return Result::success($result);
  1898. }
  1899. // 自助建站---------------fr---------------
  1900. // 网站底基础信息--fr-------------------
  1901. /**
  1902. * 添加网站底基础信息
  1903. * @param array $data
  1904. */
  1905. public function addWebFootInfo(array $data): array
  1906. {
  1907. $website = Website::where('id',$data['website_id'])->first();
  1908. if(empty($website)){
  1909. return Result::error("请输入正确的网站id!", 0);
  1910. }
  1911. $info = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1912. if(!empty($info)){
  1913. return Result::error("该网站已经添加过了!", 0);
  1914. }
  1915. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->insertGetId($data);
  1916. if(empty($result)){
  1917. return Result::error("请先添加网站基础信息!",0);
  1918. }else{
  1919. return Result::success($result);
  1920. }
  1921. }
  1922. /**
  1923. * 获取网站底基础信息
  1924. * @param array $data
  1925. */
  1926. public function getWebFootInfo(array $data): array
  1927. {
  1928. $website = Website::where('id',$data['website_id'])->first();
  1929. if(empty($website)){
  1930. return Result::error("请输入正确的网站id!", 0);
  1931. }
  1932. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1933. if(empty($result)){
  1934. return Result::error("请先添加网站基础信息!",0);
  1935. }
  1936. return Result::success($result);
  1937. }
  1938. /**
  1939. * 修改网站底基础信息
  1940. * @param array $data
  1941. */
  1942. public function upWebFootInfo(array $data): array
  1943. {
  1944. // return Result::success($data);
  1945. $website = Website::where('id',$data['website_id'])->first();
  1946. if(empty($website)){
  1947. return Result::error("请输入正确的网站id!", 0);
  1948. }
  1949. $web = $data['website_id'];
  1950. unset($data['website_id']);
  1951. $where = ['website_id'=>$web];
  1952. // return Result::success($data);
  1953. $result = WebsiteTemplateInfo::where($where)->first();
  1954. if(empty($result)){
  1955. $data['website_id'] = $web;
  1956. $result = WebsiteTemplateInfo::insertGetId($data);
  1957. // return Result::error("请先添加网站基础信息!",0);
  1958. } else{
  1959. $result = WebsiteTemplateInfo::where($where)->update($data);
  1960. }
  1961. // $where = ['website_id'=>$data['website_id']];
  1962. // unset($data['website_id']);
  1963. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  1964. if(empty($result)){
  1965. return Result::error("修改失败!",0);
  1966. }
  1967. return Result::success($result);
  1968. }
  1969. /*
  1970. * 获取父级/子级栏目------路由
  1971. * @param array $data
  1972. * @return array
  1973. * */
  1974. public function getWebsiteParentCategory(array $data): array
  1975. {
  1976. // return Result::success($data);
  1977. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1978. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1979. }
  1980. if (empty($website)) {
  1981. return Result::error("暂无该网站", 0);
  1982. }
  1983. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  1984. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  1985. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  1986. ->where('pid', 0)
  1987. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  1988. ->get() // 添加 get() 方法获取结果集合
  1989. ->all();
  1990. // 提前查询所有父级栏目,避免在 map 中多次查询
  1991. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  1992. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  1993. ->where('pid', 0)
  1994. ->select('category_id', 'aLIas_pinyin')
  1995. ->get()
  1996. ->keyBy('category_id');
  1997. // 直接查询子栏目信息
  1998. $child = WebsiteCategory::where('website_id', $data['website_id'])
  1999. ->where('pid', '!=', 0)
  2000. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  2001. ->get();
  2002. // 使用集合的 map 方法处理子栏目信息
  2003. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2004. $alias = $item->alias;
  2005. $category_id = $item->category_id;
  2006. $alias_pinyin = $item->aLIas_pinyin;
  2007. $parent = $parentCategories->get($item->pid);
  2008. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2009. $result = [
  2010. 'name' => $alias,
  2011. 'cid' => $category_id
  2012. ];
  2013. if (!empty($alias_pinyin) && !empty($parentAlias)) {
  2014. $result['path'] = "/{$parentAlias}/{$alias_pinyin}/";
  2015. }
  2016. return $result;
  2017. })->values()->all();
  2018. if (empty($category)) {
  2019. return Result::error("暂无此导航", 0);
  2020. }
  2021. return Result::success($category);
  2022. }
  2023. /**
  2024. * 获取网站栏目信息
  2025. * @param array $data
  2026. */
  2027. public function getWebsiteFootAll(array $data): array
  2028. {
  2029. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2030. if (empty($website)) {
  2031. return Result::error("暂无该网站",0);
  2032. }
  2033. // 1:图片 2:文字 3:底部
  2034. $result['foot_cate'] = FooterCategory::where('website_id',$data['website_id'])->get()->all();
  2035. $result['link_img'] = Link::where('website_id',$data['website_id'])->where('type',1)->where('status',1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2036. $result['link_text'] = Link::where('website_id',$data['website_id'])->where('type',2)->where('status',1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2037. $result['link_foot'] = Link::where('website_id',$data['website_id'])->where('type',3)->where('status',1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2038. $result['foot_info'] = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  2039. if(empty($result)){
  2040. return Result::error("暂无此网站信息",0);
  2041. }
  2042. return Result::success($result);
  2043. }
  2044. /**
  2045. * 获取网站顶部信息
  2046. * @param array $data
  2047. */
  2048. public function getWebsiteHead(array $data): array
  2049. {
  2050. $result = Website::where('id',$data['website_id'])->where('status',1)->select('website_name','logo')->first();
  2051. if (empty($result)) {
  2052. return Result::error("暂无该网站",0);
  2053. }
  2054. return Result::success($result);
  2055. }
  2056. /**
  2057. * 路由匹配
  2058. * @param array $data
  2059. */
  2060. public function getWebsiteRoute(array $data): array
  2061. {
  2062. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2063. if (empty($website)) {
  2064. return Result::error("暂无该网站",0);
  2065. }
  2066. if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
  2067. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2068. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2069. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2070. $join->on('pc.pid', '=', 'website_category.category_id')
  2071. ->where('pc.website_id', '=', $data['website_id']);
  2072. })
  2073. ->select('website_category.category_id','website_category.type', 'website_category.alias', DB::raw('CASE WHEN pc.category_id IS NOT NULL THEN 1 ELSE 0 END as children_count'))
  2074. ->first();
  2075. }
  2076. if(isset($data['foot_pinyin']) &&!empty($data['foot_pinyin'])){
  2077. $result = FooterCategory::where('website_id',$data['website_id'])->where('name_pinyin',$data['foot_pinyin'])->first(['id']);
  2078. }
  2079. if (!isset($result) || empty($result)) {
  2080. return Result::error("暂无该导航",0);
  2081. }
  2082. return Result::success($result);
  2083. }
  2084. /**
  2085. * 获取网站栏目信息
  2086. * @param array $data
  2087. */
  2088. public function getWebsiteAllinfo(array $data): array
  2089. {
  2090. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2091. if (empty($website)) {
  2092. return Result::error("暂无该网站",0);
  2093. }
  2094. if(isset($data['website_id']) && !empty($data['website_id'])){
  2095. $website_head = Website::where('id',$data['website_id'])
  2096. ->where('status',1)
  2097. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description',
  2098. 'suffix', 'website_url','ad_key','api_url','login_url','weblog_url')
  2099. ->first();
  2100. if (empty($website_head)) {
  2101. return Result::error("找不到网站",0);
  2102. }
  2103. }else{
  2104. return Result::error("参数错误",0);
  2105. }
  2106. $website_foot['foot_info'] = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  2107. if (empty($website_foot)) {
  2108. return Result::error("暂无底部基础信息",0);
  2109. }
  2110. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  2111. $result['website_head'] =$website_head;
  2112. $result['website_foot'] = $website_foot;
  2113. // return Result::success($result);
  2114. // 1:图片 2:文字 3:底部
  2115. $result['website_foot']['foot_cate'] = FooterCategory::where('website_id',$data['website_id'])->get()->all();
  2116. $result['website_foot']['link_img'] = Link::where('website_id',$data['website_id'])->where('type',1)->where('status',1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2117. $result['website_foot']['link_text'] = Link::where('website_id',$data['website_id'])->where('type',2)->where('status',1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2118. $result['website_foot']['link_foot'] = Link::where('website_id',$data['website_id'])->where('type',3)->where('status',1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2119. if(empty($result)){
  2120. return Result::error("暂无此网站信息",0);
  2121. }
  2122. return Result::success($result);
  2123. }
  2124. }