WebsiteService.php 41 KB

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