WebsiteService.php 44 KB

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