WebsiteService.php 45 KB

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