WebsiteService.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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. ->get()->all();
  496. }else{
  497. $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
  498. // return Result::success($where);
  499. $result = AdPlace::where($where)
  500. ->where('ad_place.website_id',$data['website_id'])
  501. ->leftJoin("ad", function ($join) use ($now) {
  502. $join->on("ad.pid", "=", "ad_place.id")
  503. ->where('ad.status', 1)
  504. ->where('ad.fromtime', '<=', $now)
  505. ->where('ad.totime', '>=', $now);
  506. })
  507. ->select(
  508. 'ad_place.name as place_name',
  509. 'ad_place.thumb',
  510. 'ad_place.ad_tag',
  511. 'ad_place.introduce',
  512. 'ad.name as ad_name',
  513. 'ad.image_src',
  514. 'ad.image_url',
  515. 'ad.image_alt')
  516. ->get()->all();
  517. // return Result::error("请选择广告位!",0);
  518. }
  519. if(empty($result)){
  520. return Result::error("此广告位不存在!",0);
  521. }
  522. return Result::success($result);
  523. }
  524. /**
  525. *
  526. * @param array $data
  527. * @return array
  528. */
  529. /**网站行政职能搜索 */
  530. public function selectWebsiteDepartment(array $data): array
  531. {
  532. $depart = Department::where('pid',0)->orderBy('id','asc')->get();
  533. if(isset($data['keyword']) && !empty($data['keyword'])){
  534. $departments= Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  535. if(empty($departments)){
  536. $result['message']="未查询到与此相关职能部门";
  537. }else{
  538. $count = Department::where('name','like',"%{$data['keyword']}%")->count();
  539. $m = [
  540. 'department'=>$depart,
  541. 'type'=>$departments,
  542. 'count'=>$count
  543. ];
  544. }
  545. $result['sele'] = $m;
  546. return Result::success($result['sele']);
  547. }
  548. $result=$depart;
  549. return Result::success($result);
  550. }
  551. /**
  552. * 搜索地区
  553. * @param array $data
  554. * @return array
  555. */
  556. public function selectWebsiteArea(array $data): array
  557. {
  558. // return Result::success($data);
  559. $pid = $data['pid'] ?? 0;
  560. $result = District::leftJoin('district as district2','district.pid','=','district2.id')
  561. ->where('district.pid',$pid)
  562. ->where('district.status',1)
  563. ->select('district.*','district2.name as pid_name')
  564. ->orderBy('district.code')->get();
  565. if(empty($result)){
  566. return Result::error("未查询到此地区",0);
  567. }else{
  568. return Result::success($result);
  569. }
  570. }
  571. /**
  572. * 获取栏目
  573. * @param array $data
  574. * @return array
  575. */
  576. public function getWebsiteModelCategory(array $data): array
  577. {
  578. // return Result::success($data);
  579. $website_id = [
  580. 'website_id' => $data['website_id'],
  581. ];
  582. // 初始化 $pid 数组
  583. // $pid = [];
  584. // 以下注释掉的代码是之前的逻辑,用于获取非顶级分类的 pid
  585. $pidQuery = WebsiteCategory::where($website_id)
  586. ->where('pid', '!=', 0)
  587. ->orderBy('sort')
  588. ->select('pid', 'category_id', 'alias');
  589. $pid = $pidQuery->pluck('pid');
  590. $pid = array_values(array_unique($pid->toArray()));
  591. // 构建查询语句
  592. $query = WebsiteCategory::where($website_id)
  593. ->where('pid', $data['pid'])
  594. ->offset($data['placeid'])
  595. ->limit($data['num'])
  596. ->orderBy('sort');
  597. // 如果 $pid 数组不为空,添加 CASE WHEN 条件
  598. if (!empty($pid)) {
  599. $placeholders = implode(',', array_fill(0, count($pid), '?'));
  600. $query->selectRaw("website_category.*, CASE WHEN website_category.category_id IN ($placeholders) THEN 1 ELSE 0 END AS children_count", $pid);
  601. } else {
  602. // 如果 $pid 数组为空,不添加 CASE WHEN 条件,添加字段 children_count 并赋值为 0
  603. $query->select('website_category.*', DB::raw('0 as children_count'));
  604. }
  605. // 执行查询
  606. $placeid = $data['placeid'] - 1;
  607. $result = $query->offset($placeid)->limit($data['num'])->get();
  608. if (!empty($result)) {
  609. $pidResults = $pidQuery->get();
  610. if(isset($data['type']) && $data['type'] == 1){
  611. $result = $result->map(function ($item) use ($pidResults) {
  612. $children = $pidResults->where('pid', $item->category_id)->map(function ($child) {
  613. if(!empty($child)){
  614. return $child;
  615. }
  616. });
  617. // 重置索引,使 key 值从 0 开始
  618. $item->children = $children->values();
  619. return $item;
  620. });
  621. }else{
  622. $pidMap = $pidResults->keyBy('pid');
  623. $result->each(function ($record) use ($pidMap) {
  624. if ($pidMap->has($record->category_id)) {
  625. $pidResult = $pidMap->get($record->category_id);
  626. $record->chilid_id = $pidResult->category_id;
  627. $record->chilid_alias = $pidResult->alias;
  628. }
  629. });
  630. }
  631. return Result::success($result);
  632. } else {
  633. return Result::error("本网站暂无栏目", 0);
  634. }
  635. }
  636. /**
  637. * 获取友情链接
  638. * @param array $data
  639. * @return array
  640. */
  641. public function selectWebsiteLinks(array $data): array
  642. {
  643. $where = [
  644. 'website_id' => $data['website_id'],
  645. 'status' => 1,
  646. 'type' => $data['type']
  647. ];
  648. $num=$data['num'];
  649. $result=Link::where($where)->orderBy('sort')->limit($num)->get();
  650. if(!empty($result)){
  651. return Result::success($result);
  652. }else{
  653. return Result::error("本网站暂无此类型友情链接",0);
  654. }
  655. }
  656. /**
  657. * 网站首页数据统计, 管理员
  658. * @return void
  659. */
  660. public function getAdminIndex(array $data): array
  661. {
  662. var_dump("用户类型:",$data['type_id']);
  663. switch ($data['type_id']){
  664. case 4:
  665. $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;');
  666. return Result::success($result);
  667. break;
  668. case 10000:
  669. $res = [];
  670. //网站
  671. $res['website']['count'] = 0;
  672. $res['website']['growth_rate'] = 0;
  673. //资讯
  674. $res['article']['count'] = 0;
  675. $res['article']['growth_rate'] = 0;
  676. //导航池
  677. $res['category']['count'] = 0;
  678. $res['category']['growth_rate'] = 0;
  679. //近一月数据
  680. $res['monthArticle']= [];
  681. //用户类型
  682. $res['userType'] = [];
  683. $res['website']['count'] = Website::where([])->count();
  684. $res['article']['count'] = Article::whereNotIn('status',['404'])->count();
  685. $res['category']['count'] = Category::where([])->count();
  686. $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;');
  687. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  688. return Result::success($res);
  689. }
  690. return [];
  691. }
  692. /**
  693. * 获取模板类型
  694. * @return void
  695. */
  696. public function getTemplateClass(array $data): array
  697. {
  698. $where = [];
  699. if(isset($data['name']) && $data['name']){
  700. array_push($where,['name','like','%'.$data['name'].'%']);
  701. }
  702. $result = TemplateClass::where($where)->orderBy('sort','asc')->get();
  703. if(empty($result)){
  704. return Result::error("没有模板类型",0);
  705. }else{
  706. return Result::success($result);
  707. }
  708. }
  709. /**
  710. * 添加模板类型
  711. * @param
  712. * @return void
  713. */
  714. public function addTemplateClass(array $data): array
  715. {
  716. $insertData = [
  717. 'name'=>$data['name']
  718. ];
  719. $result = TemplateClass::insertGetId($insertData);
  720. if(empty($result)){
  721. return Result::error("创建失败",0);
  722. }else{
  723. return Result::success(["id"=>$result]);
  724. }
  725. }
  726. /**
  727. * 更新模板
  728. * @param array $data
  729. * @return array
  730. */
  731. public function upTemplateClass(array $data): array
  732. {
  733. $where = [
  734. 'id'=>$data['id']
  735. ];
  736. $insertData = [
  737. 'name'=>$data['name']
  738. ];
  739. $result = TemplateClass::where($where)->update($insertData);
  740. if(empty($result)){
  741. return Result::error("更新失败",0);
  742. }else{
  743. return Result::success();
  744. }
  745. }
  746. /**
  747. * 删除模板
  748. * @param array $data
  749. * @return array
  750. */
  751. public function delTemplateClass(array $data): array
  752. {
  753. $where = [
  754. 'id'=>$data['id']
  755. ];
  756. $result = TemplateClass::where($where)->delete();
  757. if(empty($result)){
  758. return Result::error("删除失败",0);
  759. }else{
  760. return Result::success();
  761. }
  762. }
  763. /**
  764. * 获取分类下的模板
  765. * @param array $data
  766. * @return array
  767. */
  768. public function getTemplate(array $data): array
  769. {
  770. $page = $data['page'];
  771. $pageSize = $data['pageSize'];
  772. $where = [];
  773. if(isset($data['template_class_id']) && $data['template_class_id']){
  774. array_push($where,['template_class_id','=',$data['template_class_id']]);
  775. }
  776. $result = Template::where($where)
  777. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  778. $count = Template::where($where)->count();
  779. if (empty($result)) {
  780. return Result::error("没有数据",0);
  781. }
  782. $data = [
  783. 'rows'=>$result->toArray(),
  784. 'count'=>$count
  785. ];
  786. return Result::success($data);
  787. }
  788. /**
  789. * 创建模板
  790. * @param
  791. * @return void
  792. */
  793. public function addTemplate(array $data): array
  794. {
  795. $insertData = [
  796. 'template_name'=>$data['template_name'],
  797. 'template_img'=>json_encode($data['template_img']),
  798. 'template_class_id'=>$data['template_class_id'],
  799. ];
  800. $result = Template::insertGetId($insertData);
  801. if(empty($result)){
  802. return Result::error("创建模板失败",0);
  803. }else{
  804. return Result::success(["id"=>$result]);
  805. }
  806. }
  807. /**
  808. * 更新模板
  809. * @param array $data
  810. * @return array
  811. */
  812. public function upTemplate(array $data): array
  813. {
  814. $where = [
  815. 'id'=>$data['id']
  816. ];
  817. $insertData = [
  818. 'template_name'=>$data['template_name'],
  819. 'template_img'=>json_encode($data['template_img']),
  820. 'template_class_id'=>$data['template_class_id'],
  821. ];
  822. $result = Template::where($where)->update($insertData);
  823. if(empty($result)){
  824. return Result::error("更新模板失败",0);
  825. }else{
  826. return Result::success();
  827. }
  828. }
  829. /**
  830. * 删除模板
  831. * @param array $data
  832. * @return array
  833. */
  834. public function delTemplate(array $data): array
  835. {
  836. $where = [
  837. 'id'=>$data['id']
  838. ];
  839. $result = Template::where($where)->delete();
  840. if(empty($result)){
  841. return Result::error("删除模板失败",0);
  842. }else{
  843. return Result::success();
  844. }
  845. }
  846. /**
  847. * 搜索网站
  848. * @param array $data
  849. * @return array
  850. */
  851. public function websiteList(array $data): array
  852. {
  853. $where = [];
  854. if(isset($data['keyword']) && !empty($data['keyword'])){
  855. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  856. }
  857. $result = Website::where($where)->get();
  858. if($result){
  859. return Result::success($result);
  860. }else{
  861. return Result::error("没有网站",0);
  862. }
  863. }
  864. public function addWebsiteCategory(array $data): array
  865. {
  866. $website_id = $data['website_id'];
  867. $category_arr_id = $data['category_arr_id'];
  868. $categoryList = Category::whereIn('id',$category_arr_id)->get();
  869. $categoryListIds = [];
  870. if($categoryList){
  871. foreach ($categoryList->toArray() as $val){
  872. array_push($categoryListIds,$val['id']);
  873. }
  874. }
  875. $arr = [];
  876. if($categoryListIds){
  877. foreach ($categoryListIds as $v){
  878. $ids = $this->getUnderlingUIds(intval($v));
  879. $ids_arr = explode(",", $ids);
  880. array_push($arr,$ids_arr);
  881. }
  882. }
  883. $mergedArray = [];
  884. foreach ($arr as $subarray) {
  885. $mergedArray = array_merge($mergedArray, $subarray);
  886. }
  887. var_dump("所有:",$arr,$mergedArray);
  888. //查询出所有的分类进行分割插入 组装数据
  889. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  890. $categoryListData = $categoryListData->toArray();
  891. $insertData = [];
  892. if($categoryListData){
  893. foreach ($categoryListData as $key=>$value){
  894. $insertData[$key]['website_id'] = $website_id;
  895. $insertData[$key]['name'] = $value['name'];
  896. $insertData[$key]['sort'] = $value['sort'];
  897. $insertData[$key]['pid'] = $value['pid'];
  898. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  899. $insertData[$key]['seo_title'] = $value['seo_title'];
  900. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  901. $insertData[$key]['seo_description'] = $value['seo_description'];
  902. $insertData[$key]['alias'] = $value['name'];
  903. $insertData[$key]['category_id'] = $value['id'];
  904. }
  905. }
  906. $result = WebsiteCategory::insert($insertData);
  907. var_dump("插入数据状态:",$result);
  908. if($result){
  909. return Result::success($result);
  910. }else{
  911. return Result::error("创建失败",0);
  912. }
  913. }
  914. /**
  915. * 删除网站导航
  916. * @param array $data
  917. * @return array
  918. */
  919. public function delWebsiteCategory(array $data): array
  920. {
  921. $website_id = $data['website_id']??0;
  922. $category_id = $data['category_id']??0;
  923. $ids = $this->getUnderlingUIds(intval($category_id));
  924. $ids_arr = explode(",", $ids);
  925. $result = WebsiteCategory::where(['website_id'=>$website_id])->whereIn("category_id",$ids_arr)->delete();
  926. if($result){
  927. return Result::success($result);
  928. }else{
  929. return Result::error("删除失败",0);
  930. }
  931. }
  932. /**
  933. * 获取网站导航
  934. * @param array $data
  935. * @return array
  936. */
  937. public function getAdminWebsiteCategory(array $data): array
  938. {
  939. $where = [
  940. 'website_id'=>$data['website_id'],
  941. 'pid'=>0
  942. ];
  943. $result = WebsiteCategory::where($where)->get();
  944. if($result){
  945. return Result::success($result);
  946. }else{
  947. return Result::error("查询失败",0);
  948. }
  949. }
  950. /**
  951. * 更新网站导航
  952. * @param array $data
  953. * @return array
  954. */
  955. public function upWebsiteCategory(array $data): array
  956. {
  957. Db::beginTransaction();
  958. try{
  959. //合并栏目id
  960. $reqIds = array_merge($data['old_category_arr_id'],$data['new_category_arr_id']);
  961. //对比old 数组差异化,把差异化的删除
  962. $result = WebsiteCategory::where(['website_id'=>$data['website_id'],'pid'=>0])->get();
  963. $result = $result->toArray();
  964. $categoryIds = [];
  965. if($result){
  966. foreach ($result as $val){
  967. array_push($categoryIds,$val['category_id']);
  968. }
  969. }
  970. //和原始数据对比取交际
  971. $reqidsIntersect = array_intersect($reqIds,$categoryIds);
  972. //再取差集 进行对比
  973. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds),array_diff($categoryIds,$reqidsIntersect));
  974. var_dump("差集:",$differenceIDS);
  975. $arr_ids = [];
  976. if(count($differenceIDS)>0){
  977. foreach ($differenceIDS as $vv){
  978. $idV = $this->getUnderlingUIds(intval($vv));
  979. $ids_arrV = explode(",", $idV);
  980. array_push($arr_ids,$ids_arrV);
  981. }
  982. }
  983. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  984. //有差异 删除
  985. if(count($del_ids)>0){
  986. WebsiteCategory::where(['website_id'=>$data['website_id']])->whereIn("category_id",$del_ids)->delete();
  987. }
  988. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  989. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect),array_diff($reqidsIntersect,$reqIds));
  990. var_dump("要存储的:",$insertIDS);
  991. //新的数组重新创建
  992. if(count($insertIDS)>0){
  993. $arr = [];
  994. $categoryListIds = $insertIDS;
  995. if($categoryListIds){
  996. foreach ($categoryListIds as $v){
  997. $ids = $this->getUnderlingUIds(intval($v));
  998. $ids_arr = explode(",", $ids);
  999. array_push($arr,$ids_arr);
  1000. }
  1001. }
  1002. $mergedArray = [];
  1003. foreach ($arr as $subarray) {
  1004. $mergedArray = array_merge($mergedArray, $subarray);
  1005. }
  1006. var_dump("要插入的ID:",$mergedArray);
  1007. //查询出所有的分类进行分割插入 组装数据
  1008. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  1009. $categoryListData = $categoryListData->toArray();
  1010. $insertData = [];
  1011. if($categoryListData){
  1012. foreach ($categoryListData as $key=>$value){
  1013. $insertData[$key]['website_id'] = $data['website_id'];
  1014. $insertData[$key]['name'] = $value['name'];
  1015. $insertData[$key]['sort'] = $value['sort'];
  1016. $insertData[$key]['pid'] = $value['pid'];
  1017. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  1018. $insertData[$key]['seo_title'] = $value['seo_title'];
  1019. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  1020. $insertData[$key]['seo_description'] = $value['seo_description'];
  1021. $insertData[$key]['alias'] = $value['name'];
  1022. $insertData[$key]['category_id'] = $value['id'];
  1023. }
  1024. }
  1025. WebsiteCategory::insert($insertData);
  1026. }
  1027. Db::commit();
  1028. } catch(\Throwable $ex){
  1029. Db::rollBack();
  1030. var_dump($ex->getMessage());
  1031. return Result::error("修改失败",0);
  1032. }
  1033. return Result::success();
  1034. }
  1035. /**
  1036. * 获取网站列表
  1037. * @param array $data
  1038. * @return array
  1039. */
  1040. public function getWebsiteCategoryList(array $data): array
  1041. {
  1042. $where = [];
  1043. if(isset($data['keyword']) && !empty($data['keyword'])){
  1044. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  1045. }
  1046. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  1047. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  1048. }
  1049. $result = Website::where($where)
  1050. ->with(["websiteCategory"=>function ($query) {
  1051. $query->where(['pid'=>0])->select('website_id','name','alias','category_id');
  1052. }])
  1053. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1054. ->get();
  1055. $count = Website::where($where)->count();
  1056. if (empty($result)) {
  1057. return Result::error("没有数据",0);
  1058. }
  1059. $data = [
  1060. 'rows'=>$result->toArray(),
  1061. 'count'=>$count
  1062. ];
  1063. if($result){
  1064. return Result::success($data);
  1065. }else{
  1066. return Result::error("查询失败",0);
  1067. }
  1068. }
  1069. /**
  1070. * 删除网站下的所有导航
  1071. * @param array $data
  1072. * @return array
  1073. */
  1074. public function delWebsiteAllCategory(array $data): array
  1075. {
  1076. $website_id = $data['website_id'];
  1077. $result = WebsiteCategory::where(['website_id'=>$website_id])->delete();
  1078. if($result){
  1079. return Result::success($result);
  1080. }else{
  1081. return Result::error("删除失败",0);
  1082. }
  1083. }
  1084. /**
  1085. * 获取网站下的某一个导航
  1086. * @param array $data
  1087. * @return array
  1088. */
  1089. public function getWebsiteCategoryOnes(array $data): array
  1090. {
  1091. $website_id = $data['website_id'];
  1092. $category_id = $data['category_id'];
  1093. $result = WebsiteCategory::where(['website_category.website_id'=>$website_id,'website_category.category_id'=>$category_id])
  1094. ->first();
  1095. if($result){
  1096. return Result::success($result);
  1097. }else{
  1098. return Result::error("查询失败",0);
  1099. }
  1100. }
  1101. /**
  1102. * 更新网闸下的某一个导航
  1103. * @param array $data
  1104. * @return array
  1105. */
  1106. public function upWebsiteCategoryones(array $data): array
  1107. {
  1108. $where = [
  1109. 'website_id'=>$data['website_id'],
  1110. 'category_id'=>$data['category_id'],
  1111. ];
  1112. $result = WebsiteCategory::where($where)->update($data);
  1113. if($result){
  1114. return Result::success($result);
  1115. }else{
  1116. return Result::error("更新失败",0);
  1117. }
  1118. }
  1119. /**
  1120. * 获取网站下的所有导航(包含子导航)
  1121. * @param array $data
  1122. * @return array
  1123. */
  1124. public function getWebsiteAllCategory(array $data): array
  1125. {
  1126. $where = [];
  1127. if(isset($data['website_id']) && !empty($data['website_id'])){
  1128. array_push($where,['website_category.website_id','=',$data['website_id']]);
  1129. }
  1130. if(isset($data['name']) && !empty($data['name'])){
  1131. array_push($where,['website_category.name','like','%'.$data['name'].'%']);
  1132. }
  1133. if(isset($data['alias']) && !empty($data['alias'])){
  1134. array_push($where,['website_category.alias','like','%'.$data['alias'].'%']);
  1135. }
  1136. if(isset($data['department_id']) && !empty($data['department_id'])){
  1137. array_push($where,['category.department_id','=',$data['department_id']]);
  1138. }
  1139. if(isset($data['city_id']) && !empty($data['city_id'])){
  1140. array_push($where,['category.city_id','=',$data['city_id']]);
  1141. }
  1142. $result = WebsiteCategory::where($where)
  1143. ->leftJoin("category",'website_category.category_id','category.id')
  1144. ->leftJoin("department",'category.department_id','department.id')
  1145. ->leftJoin("district",'category.city_id','district.id')
  1146. ->select("website_category.*","department.name as department_name","district.name as city_name")
  1147. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1148. ->get();
  1149. $count = WebsiteCategory::where($where)
  1150. ->leftJoin("category",'website_category.category_id','category.id')
  1151. ->leftJoin("department",'category.department_id','department.id')
  1152. ->leftJoin("district",'category.city_id','district.id')
  1153. ->count();
  1154. if (empty($result)) {
  1155. return Result::error("没有数据",0);
  1156. }
  1157. $data = [
  1158. 'rows'=>$result->toArray(),
  1159. 'count'=>$count
  1160. ];
  1161. if($result){
  1162. return Result::success($data);
  1163. }else{
  1164. return Result::error("查询失败",0);
  1165. }
  1166. if($result){
  1167. return Result::success($result);
  1168. }else{
  1169. return Result::error("查询失败",0);
  1170. }
  1171. }
  1172. /**
  1173. * 递归查询数据
  1174. * @param $id
  1175. * @param $ids
  1176. * @return string
  1177. */
  1178. public function getUnderlingUIds($id, $ids='')
  1179. {
  1180. $back = Category::where(['pid'=>$id])->get();
  1181. $back = $back->toArray();
  1182. if (!empty($back) && is_array($back)) {
  1183. foreach ($back as $v) {
  1184. //防止当前人的ID重复去查询,形成恶性循环
  1185. if ($v['id'] == $id) {
  1186. continue;
  1187. }
  1188. $back2 = Category::where(['pid'=>$id])->count('id');
  1189. if ($back2 > 0) {
  1190. $ids = $this->getUnderlingUIds($v['id'],$ids);
  1191. } else {
  1192. $ids .= ','.$v['id'];
  1193. }
  1194. }
  1195. }
  1196. $ids = $id.','.$ids.',';
  1197. $ids = str_replace(',,', ",", $ids);
  1198. $ids = trim($ids, ',');
  1199. return $ids;
  1200. }
  1201. /**
  1202. * 检测网站名称是否重复
  1203. * @param array $data
  1204. * @return array
  1205. */
  1206. public function checkWebsiteName(array $data): array
  1207. {
  1208. if(isset($data['id'])){
  1209. $data[] = ['id',"!=",$data['id']];
  1210. unset($data['id']);
  1211. }
  1212. $websiteInfo = Website::query()->where($data)->first();
  1213. if (empty($websiteInfo)) {
  1214. return Result::error("找不到网站",0);
  1215. }
  1216. return Result::success($websiteInfo->toArray());
  1217. }
  1218. /**
  1219. * 检测网站url是否重复
  1220. * @param array $data
  1221. * @return array
  1222. */
  1223. public function checkWebsiteUrl(array $data): array
  1224. {
  1225. $whereData = [];
  1226. if(isset($data['id'])){
  1227. $whereData = [['id',"!=",$data['id']]];
  1228. unset($data['id']);
  1229. }
  1230. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1231. if (empty($websiteInfo)) {
  1232. return Result::error("找不到URL",0);
  1233. }
  1234. return Result::success($websiteInfo->toArray());
  1235. }
  1236. /**
  1237. * 获取网站底部基础信息
  1238. * @param array $data
  1239. * @return array
  1240. */
  1241. public function getWebsiteFootInfo(array $data): array
  1242. {
  1243. if(isset($data['website_id']) && !empty($data['website_id'])){
  1244. $website_head = Website::where('id',$data['website_id'])
  1245. ->where('status',1)
  1246. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description', 'suffix', 'website_url')
  1247. ->first();
  1248. if (empty($website_head)) {
  1249. return Result::error("找不到网站",0);
  1250. }
  1251. }else{
  1252. return Result::error("参数错误",0);
  1253. }
  1254. $website_foot = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1255. if (empty($website_foot)) {
  1256. return Result::error("暂无底部基础信息",0);
  1257. }
  1258. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  1259. $result = [
  1260. 'website_foot'=>$website_foot,
  1261. 'website_head'=>$website_head
  1262. ];
  1263. return Result::success($result);
  1264. }
  1265. /**
  1266. * 获取网站底部导航
  1267. * @param array $data
  1268. * @return array
  1269. */
  1270. public function getWebsiteFooterCategory(array $data): array
  1271. {
  1272. if(isset($data['website_id']) && !empty($data['website_id'])){
  1273. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1274. if (empty($website)) {
  1275. return Result::error("找不到网站",0);
  1276. }
  1277. }else{
  1278. return Result::error("参数错误",0);
  1279. }
  1280. $result = FooterCategory::where('website_id',$data['website_id'])->get();
  1281. if (empty($result)) {
  1282. return Result::error("暂无底部导航",0);
  1283. }
  1284. return Result::success($result->toArray());
  1285. }
  1286. /**
  1287. * 获取网站底部导航列表
  1288. * @param array $data
  1289. * @return array
  1290. */
  1291. public function getWebsiteFooterCategoryList(array $data): array
  1292. {
  1293. if(isset($data['website_id']) && !empty($data['website_id'])){
  1294. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1295. if (empty($website)) {
  1296. return Result::error("找不到网站",0);
  1297. }
  1298. }else{
  1299. return Result::error("参数错误",0);
  1300. }
  1301. $footercategory = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1302. // '底部导航类型 0:内容型;1:列表型;',
  1303. if (!isset($footercategory['type']) || $footercategory['type'] == 0) {
  1304. return Result::error("底部导航id错误",0);
  1305. }else{
  1306. $query = FooterContent::where('fcat_id',$data['fcat_id']);
  1307. if($query->count() == 0){
  1308. return Result::error("暂无底部导航列表",0);
  1309. }elseif($query->count() == 1){
  1310. $result = $query->first();
  1311. }else{
  1312. $result = $query->get();
  1313. }
  1314. }
  1315. return Result::success($result);
  1316. }
  1317. /**
  1318. * 获取网站底部导航
  1319. * @param array $data
  1320. * @return array
  1321. */
  1322. public function getWebsiteFooterCategoryInfo(array $data): array
  1323. {
  1324. if(isset($data['website_id']) && !empty($data['website_id'])){
  1325. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1326. if (empty($website)) {
  1327. return Result::error("找不到网站",0);
  1328. }
  1329. }else{
  1330. return Result::error("参数错误",0);
  1331. }
  1332. if (isset($data['type']) && $data['type'] == 0) {
  1333. $fcatid = FooterCategory::where('website_id',$data['website_id'])->where('id',$data['fcat_id'])->first();
  1334. if (empty($fcatid)) {
  1335. return Result::error("底部导航id错误",0);
  1336. }
  1337. $result = FooterContent::where('fcat_id',$data['fcat_id'])->first();
  1338. }else{
  1339. $result = FooterContent::where('id',$data['fcat_id'])->first();
  1340. }
  1341. if (empty($result)) {
  1342. return Result::error("暂无底部导航内容",0);
  1343. }
  1344. return Result::success($result);
  1345. }
  1346. /*
  1347. * 搜索网站二级导航
  1348. * @param array $data
  1349. * @return array
  1350. * */
  1351. public function selectWebsiteCategory(array $data): array
  1352. {
  1353. if(isset($data['website_id']) &&!empty($data['website_id'])){
  1354. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1355. if (empty($website)) {
  1356. return Result::error("暂无该网站",0);
  1357. }
  1358. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id')->all();
  1359. // return Result::success($category);
  1360. if (empty($category)) {
  1361. return Result::error("暂无此导航",0);
  1362. }
  1363. $query = Category::whereIn('id', $category);
  1364. if (isset($data['cityid']) && !empty($data['cityid'])) {
  1365. $cityid = District::where('id', $data['cityid'])->first();
  1366. if (empty($cityid)) {
  1367. return Result::error("暂无此城市", 0);
  1368. } else {
  1369. $city_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->pluck('id');
  1370. // $where[] = ['JSON_CONTAINS(city_arr_id, ?)', $data['cityid']];
  1371. if (empty($city_category)) {
  1372. return Result::error("暂无此城市下的导航", 0);
  1373. }
  1374. $result['catid'] = $city_category;
  1375. $city = 1;
  1376. // var_dump("城市====================",$result);
  1377. }
  1378. }
  1379. if (isset($data['department_id']) && !empty($data['department_id'])) {
  1380. $departmentid = Department::where('id', $data['department_id'])->first();
  1381. if (empty($departmentid)) {
  1382. return Result::error("暂无此部门", 0);
  1383. } else {
  1384. $depart_category = $query->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1385. if (empty($depart_category)) {
  1386. return Result::error("暂无此部门下的导航", 0);
  1387. }
  1388. $result['catid'] = $depart_category;
  1389. // var_dump("职能部门*******************",$result);
  1390. $department = 1;
  1391. }
  1392. }
  1393. if(!empty($city) && !empty($department)){
  1394. // var_dump("城市和职能部门----------------------",$result);
  1395. $sel_category = $query->whereRaw('JSON_CONTAINS(city_arr_id,?)', [$data['cityid']])->whereRaw('JSON_CONTAINS(department_arr_id,?)', [$data['department_id']])->pluck('id');
  1396. if (empty($sel_category)) {
  1397. return Result::error("暂无此城市和职能部门下的导航", 0);
  1398. }
  1399. $result['catid'] = $sel_category;
  1400. }
  1401. if (empty($result)) {
  1402. return Result::error("暂无导航",0);
  1403. }
  1404. // department_id
  1405. }else{
  1406. return Result::error("参数错误",0);
  1407. }
  1408. return Result::success($result);
  1409. }
  1410. /**
  1411. * 获取网站栏目seo
  1412. * @param array $data
  1413. * @return array
  1414. */
  1415. public function getWebsiteCategoryHead(array $data): array
  1416. {
  1417. if(isset($data['website_id']) && !empty($data['website_id'])){
  1418. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1419. if (empty($website)) {
  1420. return Result::error("找不到网站",0);
  1421. }
  1422. }else{
  1423. return Result::error("参数错误",0);
  1424. }
  1425. if (isset($data['catid']) && !empty($data['catid'])) {
  1426. $result = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->first();
  1427. }
  1428. if (empty($result)) {
  1429. return Result::error("暂无导航",0);
  1430. }
  1431. $result['website_name'] = $website['website_name'] ?? '';
  1432. $result['suffix'] = $website['suffix'] ?? '';
  1433. return Result::success($result);
  1434. }
  1435. /*
  1436. * 获取某个栏目
  1437. * @param array $data
  1438. * @return array
  1439. * */
  1440. public function getOneWebsiteCategory(array $data): array
  1441. {
  1442. if(isset($data['website_id']) &&!empty($data['website_id'])){
  1443. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  1444. if (empty($website)) {
  1445. return Result::error("暂无该网站",0);
  1446. }
  1447. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['category_id'])->first();
  1448. if (empty($category)) {
  1449. return Result::error("暂无此导航",0);
  1450. }
  1451. $category['children_count'] = WebsiteCategory::where('website_id',$data['website_id'])->where('pid',$data['category_id'])->count();
  1452. $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();
  1453. $category['parent_id'] = $parent['parent_id']?? '';
  1454. $category['parent_pinyin'] = $parent['aLIas_pinyin']?? '';
  1455. $category['parent_name'] = $parent['parent_name']?? '';
  1456. return Result::success($category);
  1457. }else{
  1458. return Result::error("参数错误",0);
  1459. }
  1460. }
  1461. // 自助建站--fr-------------------
  1462. /**
  1463. * 获取并搜索 网站模板信息
  1464. * @param array $data
  1465. * @return array
  1466. */
  1467. public function getWebsiteintel(array $data): array
  1468. {
  1469. //查询所有网站模板信息
  1470. $query = Website::where('website.status', 1)
  1471. ->leftJoin("website_template_info", "website_template_info.website_id", "website.id")
  1472. ->leftJoin("template", "template.id", "website_template_info.template_id")
  1473. ->leftJoin("template_class", "template_class.id", "template.template_class_id")
  1474. ->select(
  1475. "website_template_info.id",
  1476. "template_class.name",
  1477. "website.id as website_id",
  1478. "website.website_name",
  1479. "website.website_url",
  1480. "website_template_info.action_id",
  1481. "website_template_info.created_at",
  1482. "website_template_info.updated_at",
  1483. "website_template_info.page_type",
  1484. DB::raw("COALESCE(website_template_info.status, 0) as template_status"),
  1485. "template.template_name"
  1486. );
  1487. //若存在条件;则在$query的基础上进行筛选
  1488. if(isset($data['website_name']) && !empty($data['website_name'])){
  1489. $query->where('website.website_name', 'like', '%' . $data['website_name'] . '%');
  1490. }
  1491. if(isset($data['status']) && !empty($data['status'])){
  1492. $query->where('website_template_info.status', $data['status']);
  1493. }
  1494. $count = $query->count();
  1495. $query->limit($data['pageSize'])
  1496. ->offset(($data['page'] - 1) * $data['pageSize'])
  1497. ->orderBy("website_template_info.updated_at", "desc");
  1498. $rep = $query->get();
  1499. if($rep->count()==0){
  1500. return Result::error("没有查找到相关数据",0);
  1501. } else {
  1502. $rep->each(function ($item) {
  1503. if (!empty($item->page_type)) {
  1504. $pageTypeArray = json_decode($item->page_type, true);
  1505. if (is_array($pageTypeArray)) {
  1506. $item->page_type = $pageTypeArray;
  1507. }
  1508. }
  1509. });
  1510. $result = [
  1511. "rows" => $rep->toArray(),
  1512. "count" => $count
  1513. ];
  1514. return Result::success($result);
  1515. }
  1516. // return Result::success($result);
  1517. }
  1518. /**
  1519. * 添加网站基础信息
  1520. * @param array $data
  1521. * @return array
  1522. */
  1523. public function addWebsiteTemplateintel(array $data): array
  1524. {
  1525. $website = Website::where('website.id',$data['website_id'])->first();
  1526. if(empty($website)){
  1527. return Result::error("请输入正确的网站id!", 0);
  1528. }
  1529. if(empty($website['website_column_id'])){
  1530. return Result::error("请先关联导航池!", 0);
  1531. }
  1532. // 获取此网站的底部导航 类型 type: 0:内容型底部导航(只有一条详情内容);1:列表型底部导航(可以有多条详情内容)
  1533. $foot_type = FooterCategory::where('website_id',$data['website_id'])->pluck('type')->toArray();
  1534. if (empty($foot_type)) {
  1535. return Result::error("请先关联底部导航池!", 0);
  1536. } elseif (in_array(1, $foot_type)) {
  1537. $foot_type = 1;
  1538. } else {
  1539. $foot_type = 0;
  1540. }
  1541. $result['foot_type'] = $foot_type;
  1542. // 友情链接类型'1:图片 2:文字 3:底部';
  1543. $types = Link::where('website_id', $data['website_id'])->pluck('type')->toArray();
  1544. $missingTypes = [];
  1545. if (!in_array(1, $types)) {
  1546. $missingTypes[] = "文字链接";
  1547. }
  1548. if (!in_array(2, $types)) {
  1549. $missingTypes[] = "图片链接";
  1550. }
  1551. if (!in_array(3, $types)) {
  1552. $missingTypes[] = "底部链接";
  1553. }
  1554. if (!empty($missingTypes)) {
  1555. $errorMessage = "请先添加 " . implode(" 和 ", $missingTypes) . "!";
  1556. return Result::error($errorMessage, 0);
  1557. }
  1558. if(isset($data['page_type'])){
  1559. // 删除重复元素并重新索引数组
  1560. $data['page_type'] = array_unique($data['page_type']);
  1561. $data['page_type'] = array_values($data['page_type']);
  1562. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1563. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1564. $info = WebsiteTemplateInfo::where('website_id', $data['website_id'])->first();
  1565. if (empty($info)) {
  1566. // 将数组转换为 JSON 字符串
  1567. $data['page_type'] = json_encode($data['page_type'])??'';
  1568. // 操作状态:1:填写完成基础信息;2:选择完成模板;0:未构建
  1569. $data['action_id'] = 1;
  1570. try {
  1571. $result = WebsiteTemplateInfo::insertGetId($data);
  1572. } catch (\Exception $e) {
  1573. if ($e->getCode() == 22001) {
  1574. $errorMessage = $e->getMessage();
  1575. return Result::error("请检查表单,某个字段超出了长度限制!");
  1576. } else {
  1577. // 处理其他类型的数据库错误
  1578. return Result::error("插入操作失败: " . $e->getMessage());
  1579. }
  1580. return Result::error("添加失败!", 0);
  1581. }
  1582. } else {
  1583. return Result::error("该网站已经构建过了!", 0);
  1584. }
  1585. } else {
  1586. return Result::error("请先选择首页和底部导航详情页!", 0);
  1587. }
  1588. }
  1589. if(empty($result)){
  1590. return Result::error("添加失败!",0);
  1591. }else{
  1592. return Result::success($result);
  1593. }
  1594. }
  1595. /**
  1596. * 获取网站基础信息
  1597. *
  1598. * @return array
  1599. */
  1600. public function getWebsiteTemplateintel(array $data): array
  1601. {
  1602. $result = WebsiteTemplateInfo::where('website_template_info.website_id',$data['website_id'])
  1603. ->leftJoin('footer_category','footer_category.website_id','website_template_info.website_id')
  1604. ->leftJoin('website','website.id','website_template_info.website_id')
  1605. ->select(
  1606. "website_template_info.*",
  1607. "footer_category.type",
  1608. "website.website_name",
  1609. "website.id as website_id"
  1610. )
  1611. ->first();
  1612. if(empty($result['website_name'])){
  1613. return Result::error("请输入正确的网站id!", 0);
  1614. }
  1615. if($result){
  1616. $result['page_type'] = json_decode($result['page_type'],true);
  1617. return Result::success($result);
  1618. }else{
  1619. return Result::error("请先添加网站基础信息!",0);
  1620. }
  1621. }
  1622. /**
  1623. * 修改网站基础信息
  1624. *
  1625. * @return array
  1626. */
  1627. public function upWebsiteTemplateintel(array $data): array
  1628. {
  1629. $web = Website::where('website.id',$data['website_id'])->first();
  1630. if(empty($web)){
  1631. return Result::error("请输入正确的网站id!", 0);
  1632. }
  1633. $where = ['website_id'=>$data['website_id']];
  1634. $result = WebsiteTemplateInfo::where($where)->first();
  1635. if(empty($result)){
  1636. return Result::error("请先添加网站基础信息!",0);
  1637. }else{
  1638. unset($data['website_id']);
  1639. // 删除重复元素
  1640. $data['page_type'] = array_unique($data['page_type']);
  1641. // 重新索引数组
  1642. $data['page_type'] = array_values($data['page_type']);
  1643. // 数组中同时包含值为 1(首页) 和值为 7 (底部导航详情页)的元素
  1644. if (in_array(1, $data['page_type']) && in_array(7, $data['page_type'])) {
  1645. // 将数组转换为 JSON 字符串
  1646. $data['page_type'] = json_encode($data['page_type'])??'';
  1647. try {
  1648. $result = WebsiteTemplateInfo::where($where)->update($data);
  1649. } catch (\Exception $e) {
  1650. if ($e->getCode() == 22001) {
  1651. $errorMessage = $e->getMessage();
  1652. return Result::error("请检查表单,某个字段超出了长度限制!");
  1653. } else {
  1654. // 处理其他类型的数据库错误
  1655. return Result::error("修改操作失败: " . $e->getMessage());
  1656. }
  1657. return Result::error("修改失败!", 0);
  1658. }
  1659. }else{
  1660. return Result::error("请先选择首页和底部导航详情页!",0);
  1661. }
  1662. }
  1663. if(empty($result)){
  1664. return Result::error("修改失败!",0);
  1665. }else{
  1666. return Result::success($result);
  1667. }
  1668. }
  1669. /**
  1670. * 获取所有网站风格信息
  1671. *
  1672. * @return array
  1673. */
  1674. public function getAllTemplateClass(array $data): array
  1675. {
  1676. $result = TemplateClass::all();
  1677. if(empty($result)){
  1678. return Result::error("没有查找到相关数据!",0);
  1679. }else{
  1680. return Result::success($result);
  1681. }
  1682. }
  1683. /**
  1684. * 搜索并获取网站模板信息
  1685. * @param array $data
  1686. * @return array
  1687. */
  1688. public function getWebsiteTemplateList(array $data): array
  1689. {
  1690. $website = Website::where('website.id',$data['website_id'])->first();
  1691. if(empty($website)){
  1692. return Result::error("请输入正确的网站id!",0);
  1693. }else{
  1694. $page_type = WebsiteTemplateInfo::where('website_id',$data['website_id'])->select('page_type')->first();
  1695. if(empty($page_type)){
  1696. return Result::error("此网站还未添加基础信息!",0);
  1697. } else{
  1698. $where = json_decode($page_type['page_type'], true);
  1699. // $where = $data["page_type"];
  1700. if(isset($data['template_class_id']) && !empty($data['template_class_id'])){
  1701. $template_class = TemplateClass::where('id',$data['template_class_id'])->first();
  1702. if(empty($template_class)){
  1703. return Result::error("请输入正确的模板风格id!",0);
  1704. }else{
  1705. // 获取指定风格下的模板
  1706. $rep = Template::where('template_class_id', $data['template_class_id'])
  1707. ->where(function ($query) use ($where) {
  1708. foreach ($where as $value) {
  1709. $query->whereJsonContains('template_img', ['value' => $value]);
  1710. }
  1711. })
  1712. ->limit($data['pageSize'])
  1713. ->offset(($data['page'] - 1) * $data['pageSize'])
  1714. ->orderBy("template.updated_at", "desc")
  1715. ->get();
  1716. $count = Template::where('template_class_id', $data['template_class_id'])
  1717. ->where(function ($query) use ($where) {
  1718. foreach ($where as $value) {
  1719. $query->whereJsonContains('template_img', ['value' => $value]);
  1720. }
  1721. })->count();
  1722. }
  1723. }else{
  1724. //获取所有模板
  1725. $rep = Template::where(function ($query) use ($where) {
  1726. foreach ($where as $value) {
  1727. $query->whereJsonContains('template_img', ['value' => $value]);
  1728. }
  1729. })
  1730. ->limit($data['pageSize'])
  1731. ->offset(($data['page'] - 1) * $data['pageSize'])
  1732. ->orderBy("template.updated_at", "desc")
  1733. ->get();
  1734. $count = Template::where(function ($query) use ($where) {
  1735. foreach ($where as $value) {
  1736. $query->whereJsonContains('template_img', ['value' => $value]);
  1737. }
  1738. })->count();
  1739. }
  1740. if(empty($rep) || $rep->count()==0){
  1741. return Result::error("没有查找到相关数据!",0);
  1742. }else{
  1743. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1744. // $rep->each(function ($item) use ($where) {
  1745. // $template_img = json_decode($item->template_img, true);
  1746. // if(is_array($template_img)){
  1747. // $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1748. // return isset($img['value']) && in_array($img['value'], $where);
  1749. // });
  1750. // }
  1751. // $item->template_img = $filtered_img;
  1752. // });
  1753. // $result = [
  1754. // "rows" => $rep->toArray(),
  1755. // "count" => $count
  1756. // ];
  1757. $rep->each(function ($item) use ($where) {
  1758. $template_img = json_decode($item->template_img, true);
  1759. if (is_array($template_img)) {
  1760. $filtered_img = array_filter($template_img, function ($img) use ($where) {
  1761. return isset($img['value']) && in_array($img['value'], $where);
  1762. });
  1763. // 对过滤后的数组进行重新排序
  1764. $sorted_img = array_values($filtered_img);
  1765. $item->template_img = $sorted_img;
  1766. } else {
  1767. $item->template_img = []; // 如果无法解码为数组,则设置为空数组
  1768. }
  1769. });
  1770. $result = [
  1771. "rows" => $rep->toArray(),
  1772. "count" => $count
  1773. ];
  1774. }
  1775. }
  1776. }
  1777. return Result::success($result);
  1778. }
  1779. /**
  1780. * 添加网站模板
  1781. * @param array $data
  1782. */
  1783. public function addWebsiteTemplateclassintel(array $data): array
  1784. {
  1785. $template = Template::where('id',$data['template_id'])->first();
  1786. $web = Website::where('id',$data['website_id'])->first();
  1787. if(empty($template)){
  1788. return Result::error("请输入正确的模板id",0);
  1789. }
  1790. if(empty($web)){
  1791. return Result::error("请输入正确的网站id",0);
  1792. }
  1793. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->update(['template_id'=>$data['template_id'],'action_id'=>2]);
  1794. if(empty($result)){
  1795. return Result::error("添加失败",0);
  1796. }else{
  1797. return Result::success($result);
  1798. }
  1799. }
  1800. /**
  1801. * 获取网站模板信息
  1802. * @param array $data
  1803. */
  1804. public function getWebsiteTemplateclassintel(array $data): array
  1805. {
  1806. // return Result::success($data);
  1807. $template = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1808. if(empty($template)){
  1809. return Result::error("请输入正确的搭建网站id",0);
  1810. }
  1811. $page_type = json_decode($template['page_type'], true);
  1812. // 获取指定网站下的基础信息和模板及风格信息
  1813. $result = WebsiteTemplateInfo::where([
  1814. ['website_template_info.website_id',$data['website_id']],
  1815. ['template_id','!=',null]
  1816. ])
  1817. ->leftJoin('template','template.id','website_template_info.template_id')
  1818. ->leftJoin('template_class','template_class.id','template.template_class_id')
  1819. ->where(function ($query) use ($page_type) {
  1820. // 假设 $data['page_type'] 是一个数组,包含需要匹配的 page_type 值
  1821. foreach ($page_type as $pageType) {
  1822. $query->orWhereJsonContains('template.template_img', ['value' => $pageType]);
  1823. }
  1824. })
  1825. ->select(
  1826. 'template.template_name',
  1827. 'template.template_img',
  1828. 'template.id as tid',
  1829. 'template_class.name',
  1830. 'template_class.id as class_id',
  1831. 'website_template_info.page_type',
  1832. 'website_template_info.website_id')
  1833. ->first();
  1834. if(empty($result)){
  1835. return Result::error("没有查找到相关数据",0);
  1836. }
  1837. // return Result::success($result);
  1838. // 过滤 template_img 字段 只获取 基础信息中 包含的模板图片
  1839. $template_img = $result['template_img'] !== null ? json_decode($result['template_img'], true) : [];
  1840. $page_type = $result['page_type'] !== null ? json_decode($result['page_type'], true) : [];
  1841. foreach ($template_img as $key => $value) {
  1842. if (!in_array($value['value'], $page_type)) {
  1843. unset($template_img[$key]);
  1844. }
  1845. }
  1846. // return Result::success($result);
  1847. $result['template_img'] = array_values($template_img);
  1848. $result['page_type'] = $page_type;
  1849. // $templateList = $this->getWebsiteTemplateList($data);
  1850. // $count = $templateList['data']['count']??'';
  1851. // $pages = $count / $data['pageSize'];
  1852. // $found = false;
  1853. // while ($pages && !$found) {
  1854. // $templates = $this->getWebsiteTemplateList($pages,$data['pageSize']);
  1855. // $index = array_search($template['template_id'], array_column($templates['rows'], 'id'));
  1856. // if ($index !== false) {
  1857. // $found = true;
  1858. // $result['page'] = $pages;
  1859. // $pages = 0;
  1860. // // echo "查询到的 template 在第 $page 页";
  1861. // }
  1862. // $pages--;
  1863. // }
  1864. return Result::success($result);
  1865. }
  1866. /**
  1867. * 获取网站模板下的板块信息
  1868. * @param array $data
  1869. */
  1870. public function getWebsiteSectorList(array $data): array
  1871. {
  1872. $website = Website::where('id',$data['website_id'])->first();
  1873. if(empty($website)){
  1874. return Result::error("请输入正确的网站id",0);
  1875. }
  1876. $template = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1877. if(empty($template['template_id'])){
  1878. return Result::error("请先添加网站模板",0);
  1879. }
  1880. $query = Sector::where('template_id',$template['template_id']);
  1881. if($query->count() == 0){
  1882. return Result::error("没有查找到相关板块数据",0);
  1883. }elseif($query->count() == 1){
  1884. $sector = $query->first();
  1885. }else{
  1886. $sector = $query->get();
  1887. }
  1888. $result['page_type'] = $template['page_type'];
  1889. $result['tid'] = $template['template_id'];
  1890. $result = [
  1891. "tid" => $template['template_id'],
  1892. "page_type" => $template['page_type'],
  1893. "sector" => $sector
  1894. ];
  1895. return Result::success($result);
  1896. }
  1897. // 自助建站---------------fr---------------
  1898. // 网站底基础信息--fr-------------------
  1899. /**
  1900. * 添加网站底基础信息
  1901. * @param array $data
  1902. */
  1903. public function addWebFootInfo(array $data): array
  1904. {
  1905. $website = Website::where('id',$data['website_id'])->first();
  1906. if(empty($website)){
  1907. return Result::error("请输入正确的网站id!", 0);
  1908. }
  1909. $info = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1910. if(!empty($info)){
  1911. return Result::error("该网站已经添加过了!", 0);
  1912. }
  1913. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->insertGetId($data);
  1914. if(empty($result)){
  1915. return Result::error("请先添加网站基础信息!",0);
  1916. }else{
  1917. return Result::success($result);
  1918. }
  1919. }
  1920. /**
  1921. * 获取网站底基础信息
  1922. * @param array $data
  1923. */
  1924. public function getWebFootInfo(array $data): array
  1925. {
  1926. $website = Website::where('id',$data['website_id'])->first();
  1927. if(empty($website)){
  1928. return Result::error("请输入正确的网站id!", 0);
  1929. }
  1930. $result = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  1931. if(empty($result)){
  1932. return Result::error("请先添加网站基础信息!",0);
  1933. }
  1934. return Result::success($result);
  1935. }
  1936. /**
  1937. * 修改网站底基础信息
  1938. * @param array $data
  1939. */
  1940. public function upWebFootInfo(array $data): array
  1941. {
  1942. // return Result::success($data);
  1943. $website = Website::where('id',$data['website_id'])->first();
  1944. if(empty($website)){
  1945. return Result::error("请输入正确的网站id!", 0);
  1946. }
  1947. $web = $data['website_id'];
  1948. unset($data['website_id']);
  1949. $where = ['website_id'=>$web];
  1950. // return Result::success($data);
  1951. $result = WebsiteTemplateInfo::where($where)->first();
  1952. if(empty($result)){
  1953. $data['website_id'] = $web;
  1954. $result = WebsiteTemplateInfo::insertGetId($data);
  1955. // return Result::error("请先添加网站基础信息!",0);
  1956. } else{
  1957. $result = WebsiteTemplateInfo::where($where)->update($data);
  1958. }
  1959. // $where = ['website_id'=>$data['website_id']];
  1960. // unset($data['website_id']);
  1961. // $result = WebsiteTemplateInfo::updateOrInsert($where, $data);
  1962. if(empty($result)){
  1963. return Result::error("修改失败!",0);
  1964. }
  1965. return Result::success($result);
  1966. }
  1967. /*
  1968. * 获取父级/子级栏目------路由
  1969. * @param array $data
  1970. * @return array
  1971. * */
  1972. public function getWebsiteParentCategory(array $data): array
  1973. {
  1974. // return Result::success($data);
  1975. if (isset($data['website_id']) && !empty($data['website_id'])) {
  1976. $website = Website::where('id', $data['website_id'])->where('status', 1)->first();
  1977. }
  1978. if (empty($website)) {
  1979. return Result::error("暂无该网站", 0);
  1980. }
  1981. // 在 Hyperf 中,查询构建器(Builder)没有 `map` 方法,`map` 方法是集合(Collection)的方法。
  1982. // 因此,需要先调用 `get()` 方法获取查询结果集合,再使用 `map` 方法进行处理。
  1983. $category['parent'] = WebsiteCategory::where('website_id', $data['website_id'])
  1984. ->where('pid', 0)
  1985. ->selectRaw("category_id as cid, alias as name, CONCAT('/', '', aLIas_pinyin, '/') as path")
  1986. ->get() // 添加 get() 方法获取结果集合
  1987. ->all();
  1988. // 提前查询所有父级栏目,避免在 map 中多次查询
  1989. // 提前查询所有父级栏目信息并以 category_id 作为键存储
  1990. $parentCategories = WebsiteCategory::where('website_id', $data['website_id'])
  1991. ->where('pid', 0)
  1992. ->select('category_id', 'aLIas_pinyin')
  1993. ->get()
  1994. ->keyBy('category_id');
  1995. // 直接查询子栏目信息
  1996. $child = WebsiteCategory::where('website_id', $data['website_id'])
  1997. ->where('pid', '!=', 0)
  1998. ->select('alias', 'category_id', 'aLIas_pinyin', 'pid')
  1999. ->get();
  2000. // 使用集合的 map 方法处理子栏目信息
  2001. $category['child'] = $child->map(function ($item) use ($parentCategories) {
  2002. $alias = $item->alias;
  2003. $category_id = $item->category_id;
  2004. $alias_pinyin = $item->aLIas_pinyin;
  2005. $parent = $parentCategories->get($item->pid);
  2006. $parentAlias = $parent ? $parent->aLIas_pinyin : '';
  2007. $result = [
  2008. 'name' => $alias,
  2009. 'cid' => $category_id
  2010. ];
  2011. if (!empty($alias_pinyin) && !empty($parentAlias)) {
  2012. $result['path'] = "/{$parentAlias}/{$alias_pinyin}/";
  2013. }
  2014. return $result;
  2015. })->values()->all();
  2016. if (empty($category)) {
  2017. return Result::error("暂无此导航", 0);
  2018. }
  2019. return Result::success($category);
  2020. }
  2021. /**
  2022. * 获取网站栏目信息
  2023. * @param array $data
  2024. */
  2025. public function getWebsiteFootAll(array $data): array
  2026. {
  2027. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2028. if (empty($website)) {
  2029. return Result::error("暂无该网站",0);
  2030. }
  2031. // 1:图片 2:文字 3:底部
  2032. $result['foot_cate'] = FooterCategory::where('website_id',$data['website_id'])->get()->all();
  2033. $result['link_img'] = Link::where('website_id',$data['website_id'])->where('type',1)->where('status',1)->limit($data['link_imgnum'])->orderBy('sort')->get()->all();
  2034. $result['link_text'] = Link::where('website_id',$data['website_id'])->where('type',2)->where('status',1)->limit($data['link_textnum'])->orderBy('sort')->get()->all();
  2035. $result['link_foot'] = Link::where('website_id',$data['website_id'])->where('type',3)->where('status',1)->limit($data['link_footnum'])->orderBy('sort')->get()->all();
  2036. $result['foot_info'] = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  2037. if(empty($result)){
  2038. return Result::error("暂无此网站信息",0);
  2039. }
  2040. return Result::success($result);
  2041. }
  2042. /**
  2043. * 获取网站顶部信息
  2044. * @param array $data
  2045. */
  2046. public function getWebsiteHead(array $data): array
  2047. {
  2048. $result = Website::where('id',$data['website_id'])->where('status',1)->select('website_name','logo')->first();
  2049. if (empty($result)) {
  2050. return Result::error("暂无该网站",0);
  2051. }
  2052. return Result::success($result);
  2053. }
  2054. /**
  2055. * 路由匹配
  2056. * @param array $data
  2057. */
  2058. public function getWebsiteRoute(array $data): array
  2059. {
  2060. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2061. if (empty($website)) {
  2062. return Result::error("暂无该网站",0);
  2063. }
  2064. if (isset($data['pinyin']) &&!empty($data['pinyin'])) {
  2065. $result = WebsiteCategory::where('website_category.website_id', $data['website_id'])
  2066. ->where('website_category.aLIas_pinyin', $data['pinyin'])
  2067. ->leftJoin('website_category as pc', function ($join) use ($data) {
  2068. $join->on('pc.pid', '=', 'website_category.category_id')
  2069. ->where('pc.website_id', '=', $data['website_id']);
  2070. })
  2071. ->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'))
  2072. ->first();
  2073. }
  2074. if(isset($data['foot_pinyin']) &&!empty($data['foot_pinyin'])){
  2075. $result = FooterCategory::where('website_id',$data['website_id'])->where('name_pinyin',$data['foot_pinyin'])->first(['id']);
  2076. }
  2077. if (!isset($result) || empty($result)) {
  2078. return Result::error("暂无该导航",0);
  2079. }
  2080. return Result::success($result);
  2081. }
  2082. /**
  2083. * 获取网站栏目信息
  2084. * @param array $data
  2085. */
  2086. public function getWebsiteAllinfo(array $data): array
  2087. {
  2088. $website = Website::where('id',$data['website_id'])->where('status',1)->first();
  2089. if (empty($website)) {
  2090. return Result::error("暂无该网站",0);
  2091. }
  2092. if(isset($data['website_id']) && !empty($data['website_id'])){
  2093. $website_head = Website::where('id',$data['website_id'])
  2094. ->where('status',1)
  2095. ->select('id', 'website_name', 'logo', 'title', 'keywords', 'description',
  2096. 'suffix', 'website_url','ad_key','api_url','login_url','weblog_url')
  2097. ->first();
  2098. if (empty($website_head)) {
  2099. return Result::error("找不到网站",0);
  2100. }
  2101. }else{
  2102. return Result::error("参数错误",0);
  2103. }
  2104. $website_foot['foot_info'] = WebsiteTemplateInfo::where('website_id',$data['website_id'])->first();
  2105. if (empty($website_foot)) {
  2106. return Result::error("暂无底部基础信息",0);
  2107. }
  2108. $website_head['website_url'] = $website_head['website_url'] ? json_decode($website_head['website_url']) : [];
  2109. $result['website_head'] =$website_head;
  2110. $result['website_foot'] = $website_foot;
  2111. // return Result::success($result);
  2112. // 1:图片 2:文字 3:底部
  2113. $result['website_foot']['foot_cate'] = FooterCategory::where('website_id',$data['website_id'])->get()->all();
  2114. $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();
  2115. $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();
  2116. $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();
  2117. if(empty($result)){
  2118. return Result::error("暂无此网站信息",0);
  2119. }
  2120. return Result::success($result);
  2121. }
  2122. }