WebsiteService.php 43 KB

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