WebsiteService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\TemplateClass;
  4. use App\Model\Template;
  5. use App\Model\WebsiteRole;
  6. use App\Model\WebsiteRoleUser;
  7. use App\Model\Website;
  8. use App\Model\WebsiteColumn;
  9. use Hyperf\RpcServer\Annotation\RpcService;
  10. use App\Tools\Result;
  11. use App\Model\WebsiteCategory;
  12. use function PHPUnit\Framework\isNull;
  13. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  14. class WebsiteService implements WebsiteServiceInterface
  15. {
  16. /**
  17. * @param string $keyword
  18. * @param int $page
  19. * @param int $pageSize
  20. * @return array
  21. */
  22. public function getWebsitetList(array $data): array
  23. {
  24. $where = [];
  25. if(isset($data['keyword']) && !empty($data['keyword'])){
  26. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  27. }
  28. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  29. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  30. }
  31. if(isset($data['city_id']) && !empty($data['city_id'])){
  32. array_push($where,['website.city_id','=',$data['city_id']]);
  33. }
  34. $result = Website::where($where)
  35. ->leftJoin("website_column","website.website_column_id","website_column.id")
  36. ->leftJoin("district","district.id","website.city_id")
  37. ->select("website.*","website_column.column_name","district.name as city_name")
  38. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->get();
  39. $count = Website::where($where)->count();
  40. if (empty($result)) {
  41. return Result::error("没有数据",0);
  42. }
  43. $data = [
  44. 'rows'=>$result->toArray(),
  45. 'count'=>$count
  46. ];
  47. return Result::success($data);
  48. }
  49. /**
  50. * @param array $data
  51. * @return array
  52. */
  53. public function createWebsite(array $data): array
  54. {
  55. var_dump("网站数据:",$data);
  56. $insertData = [
  57. 'website_name'=>$data['website_name'],
  58. 'logo'=>$data['logo']??'',
  59. 'website_url'=>$data['website_url']??'',
  60. 'city_id'=>$data['city_id']??0,
  61. 'website_column_id'=>$data['website_column_id'],
  62. 'title'=>$data['title']??'',
  63. 'keywords'=>$data['keywords']??'',
  64. 'description'=>$data['description']??'',
  65. 'status'=>$data['status']??0,
  66. 'website_column_arr_id'=>$data['website_column_arr_id'],
  67. 'city_arr_id'=>$data['city_arr_id']??[0],
  68. 'template_id' =>$data['template_id']??0,
  69. ];
  70. $result = Website::insertGetId($insertData);
  71. if(empty($result)){
  72. return Result::error("创建失败",0);
  73. }else{
  74. return Result::success(["id"=>$result]);
  75. }
  76. }
  77. /**
  78. * @param int $id
  79. * @param array $data
  80. * @return array
  81. */
  82. public function updateWebsite(int $id,array $data): array
  83. {
  84. $insertData = [
  85. 'website_name'=>$data['website_name'],
  86. 'logo'=>$data['logo']??'',
  87. 'website_url'=>$data['website_url']??'',
  88. 'city_id'=>$data['city_id']??0,
  89. 'website_column_id'=>$data['website_column_id'],
  90. 'title'=>$data['title']??'',
  91. 'keywords'=>$data['keywords']??'',
  92. 'description'=>$data['description']??'',
  93. 'status'=>$data['status']??0,
  94. 'website_column_arr_id'=>$data['website_column_arr_id'],
  95. 'city_arr_id'=>$data['city_arr_id']??[0],
  96. 'template_id' =>$data['template_id']??0,
  97. ];
  98. $result = Website::where('id',$id)->update($insertData);
  99. var_dump("更新站点",$result);
  100. if(empty($result)){
  101. return Result::error("更新失败",0);
  102. }else{
  103. return Result::success();
  104. }
  105. }
  106. /**
  107. * @param int $id
  108. * @return array
  109. */
  110. public function delWebsite(int $id): array
  111. {
  112. $result = Website::where('id',$id )->delete();
  113. var_dump("删除站点",$result);
  114. if(empty($result)){
  115. return Result::error("删除失败",0);
  116. }else{
  117. return Result::success();
  118. }
  119. }
  120. /**
  121. * @param int $id
  122. * @return array
  123. */
  124. public function getWebsiteInfo(int $id): array
  125. {
  126. $where = [
  127. ['website.id','=',$id]
  128. ];
  129. $result = Website::where($where )
  130. ->leftJoin("template","template.id","website.template_id")
  131. ->select("website.*","template.template_name","template.template_img")
  132. ->first();
  133. if(empty($result)){
  134. return Result::error("数据不存在",0);
  135. }else{
  136. return Result::success($result->toArray());
  137. }
  138. }
  139. /**
  140. * 查询所有的站点栏目
  141. * @return array
  142. */
  143. public function getWebsiteColumn(array $data): array
  144. {
  145. $result = WebsiteColumn::where($data)->get();
  146. if(empty($result)){
  147. return Result::error("数据不存在",0);
  148. }else{
  149. return Result::success($result->toArray());
  150. }
  151. }
  152. /**
  153. * @param string $keyword
  154. * @param int $page
  155. * @param int $pageSize
  156. * @return array
  157. */
  158. public function getWebsiteColumnList(string $keyword,int $page,int $pageSize):array
  159. {
  160. $where = [
  161. ['website_column.column_name','like','%'.$keyword.'%']
  162. ];
  163. $result = WebsiteColumn::where($where)
  164. ->leftJoin("website_column as wc","website_column.pid","wc.id")
  165. ->select("website_column.*","wc.column_name as parent_column_name")
  166. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  167. $count = WebsiteColumn::where($where)->count();
  168. if (empty($result)) {
  169. return Result::error("没有数据",0);
  170. }
  171. $data = [
  172. 'rows'=>$result->toArray(),
  173. 'count'=>$count
  174. ];
  175. return Result::success($data);
  176. }
  177. /**
  178. * @param array $data
  179. * @return array
  180. */
  181. public function createWebsiteColumn(array $data): array
  182. {
  183. $insertData = [
  184. 'column_name'=>$data['column_name'],
  185. 'pid'=>$data['pid']??'',
  186. 'column_arr_id'=>$data['column_arr_id']??[0],
  187. 'sort'=>$data['sort']??0,
  188. 'remark'=>$data['remark']??'',
  189. ];
  190. $result = WebsiteColumn::insertGetId($insertData);
  191. if(empty($result)){
  192. return Result::error("创建失败",0);
  193. }else{
  194. return Result::success(["id"=>$result]);
  195. }
  196. }
  197. /**
  198. * @param int $id
  199. * @param array $data
  200. * @return array
  201. */
  202. public function updateWebsiteColumn(int $id,array $data): array
  203. {
  204. $insertData = [
  205. 'column_name'=>$data['column_name'],
  206. 'pid'=>$data['pid']??'',
  207. 'column_arr_id'=>$data['column_arr_id']??[0],
  208. 'sort'=>$data['sort']??0,
  209. 'remark'=>$data['remark']??'',
  210. ];
  211. $result = WebsiteColumn::where('id',$id)->update($insertData);
  212. if(empty($result)){
  213. return Result::error("更新失败",0);
  214. }else{
  215. return Result::success();
  216. }
  217. }
  218. /**
  219. * @param int $id
  220. * @return array
  221. */
  222. public function delWebsiteColumn(int $id): array
  223. {
  224. $result = WebsiteColumn::where('id',$id )->delete();
  225. if(empty($result)){
  226. return Result::error("删除失败",0);
  227. }else{
  228. return Result::success();
  229. }
  230. }
  231. /**
  232. * @param string $keyword
  233. * @param int $page
  234. * @param int $pageSize
  235. * @return array
  236. */
  237. public function getWebsiteRoleList(string $keyword,int $page,int $pageSize,int $websiteId):array
  238. {
  239. $where = [
  240. ['role.role_name','like','%'.$keyword.'%'],
  241. ['website_role.website_id','=',$websiteId],
  242. ];
  243. $result = WebsiteRole::where($where)
  244. ->leftJoin("role","role.id","website_role.role_id")
  245. ->select("role.*","website_role.type","website_role.role_id","website_role.id as website_role_id","website_role.website_id")
  246. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  247. $count = WebsiteRole::where($where) ->leftJoin("role","role.id","website_role.role_id")->count();
  248. if (empty($result)) {
  249. return Result::error("没有数据",0);
  250. }
  251. $data = [
  252. 'rows'=>$result->toArray(),
  253. 'count'=>$count
  254. ];
  255. return Result::success($data);
  256. }
  257. /**
  258. * @param array $data
  259. * @return array
  260. */
  261. public function createWebsiteRole(array $data): array
  262. {
  263. $insertData = [
  264. 'website_id'=>$data['website_id'],
  265. 'role_id'=>$data['role_id']??''
  266. ];
  267. $info = WebsiteRole::where($insertData)->first();
  268. if($info){
  269. return Result::error("不能重复添加角色",0);
  270. }
  271. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  272. $insertData['type'] = $data['type']??'';
  273. $result = WebsiteRole::insertGetId($insertData);
  274. if(empty($result)){
  275. return Result::error("创建失败",0);
  276. }else{
  277. return Result::success(["id"=>$result]);
  278. }
  279. }
  280. /**
  281. * 暂时用不上
  282. * @param int $id
  283. * @param array $data
  284. * @return array
  285. */
  286. public function updateWebsiteRole(int $id,array $data): array
  287. {
  288. $insertData = [
  289. 'website_id'=>$data['website_id'],
  290. 'type'=>$data['type']??'',
  291. ];
  292. $result = WebsiteRole::where('id',$id)->update($insertData);
  293. if(empty($result)){
  294. return Result::error("更新失败",0);
  295. }else{
  296. return Result::success();
  297. }
  298. }
  299. /**
  300. * @param int $id
  301. * @return array
  302. */
  303. public function delWebsiteRole(int $id): array
  304. {
  305. $result = WebsiteRole::where('id',$id )->delete();
  306. if(empty($result)){
  307. return Result::error("删除失败",0);
  308. }else{
  309. return Result::success();
  310. }
  311. }
  312. /**
  313. * @param string $keyword
  314. * @param int $page
  315. * @param int $pageSize
  316. * @return array
  317. */
  318. public function getWebsiteRoleUserList(string $keyword,int $page,int $pageSize,int $websiteId,int $roleId):array
  319. {
  320. $where = [
  321. ['website_role_user.website_id','=',$websiteId],
  322. ['website_role_user.role_id','=',$roleId],
  323. ];
  324. $count = WebsiteRoleUser::where($where)->count();
  325. $where[] = ['u.user_name','like','%'.$keyword.'%'];
  326. $result = WebsiteRoleUser::where($where)
  327. ->leftJoin("user as u","website_role_user.user_id","u.id")
  328. ->leftJoin("website as w","website_role_user.website_id","u.id")
  329. ->leftJoin("role as r","website_role_user.role_id","r.id")
  330. ->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')
  331. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  332. if (empty($result)) {
  333. return Result::error("没有数据",0);
  334. }
  335. $data = [
  336. 'rows'=>$result->toArray(),
  337. 'count'=>$count
  338. ];
  339. return Result::success($data);
  340. }
  341. /**
  342. * @param array $data
  343. * @return array
  344. */
  345. public function createWebsiteRoleUser(array $data): array
  346. {
  347. $insertData = [
  348. 'website_id'=>$data['website_id'],
  349. 'user_id'=>$data['user_id']??'',
  350. ];
  351. $info = WebsiteRoleUser::where($insertData)->first();
  352. if($info){
  353. return Result::error("不能重复添加角色用户",0);
  354. }
  355. $insertData['role_id'] = $data['role_id']??'';
  356. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  357. $insertData['type'] = $data['type']??'';
  358. $result = WebsiteRoleUser::insertGetId($insertData);
  359. if(empty($result)){
  360. return Result::error("创建失败",0);
  361. }else{
  362. return Result::success(["id"=>$result]);
  363. }
  364. }
  365. /**
  366. * @param int $id
  367. * @param array $data
  368. * @return array
  369. */
  370. public function updateWebsiteRoleUser(int $id,array $data): array
  371. {
  372. $insertData = [
  373. 'website_id'=>$data['website_id'],
  374. 'type'=>$data['type']??'',
  375. 'type_id'=>$data['type_id']??'',
  376. 'role_id'=>$data['role_id']??'',
  377. ];
  378. $result = WebsiteRoleUser::where('id',$id)->update($insertData);
  379. if(empty($result)){
  380. return Result::error("更新失败",0);
  381. }else{
  382. return Result::success();
  383. }
  384. }
  385. /**
  386. * @param int $id
  387. * @return array
  388. */
  389. public function delWebsiteRoleUser(int $id): array
  390. {
  391. $result = WebsiteRoleUser::where('id',$id )->delete();
  392. if(empty($result)){
  393. return Result::error("删除失败",0);
  394. }else{
  395. return Result::success();
  396. }
  397. }
  398. /**
  399. * 根据域名获取网站 站点id
  400. * @param array $data
  401. * @return array
  402. */
  403. public function getWebsiteId(array $data): array
  404. {
  405. $where = [
  406. 'website_url'=>$data['website_url']
  407. ];
  408. $result = Website::where($where)->first();
  409. if(empty($result)){
  410. return Result::error("查询站点失败",0);
  411. }else{
  412. return Result::success($result);
  413. }
  414. }
  415. /**
  416. * 查询网站下面的导航
  417. * @param array $data
  418. * @return array
  419. */
  420. public function getWebsiteCategory(array $data): array
  421. {
  422. $where = [
  423. 'website_id'=>$data['website_id'],
  424. 'pid'=>0
  425. ];
  426. $result = WebsiteCategory::where($where)->orderBy('sort','asc')->get();
  427. if(empty($result)){
  428. return Result::error("查询站点栏目失败",0);
  429. }else{
  430. return Result::success($result);
  431. }
  432. }
  433. /**
  434. * 网站首页数据统计, 管理员
  435. * @return void
  436. */
  437. public function getAdminIndex(array $data): array
  438. {
  439. return [];
  440. }
  441. /**
  442. * 获取模板类型
  443. * @return void
  444. */
  445. public function getTemplateClass(array $data): array
  446. {
  447. $result = TemplateClass::orderBy('sort','asc')->get();
  448. if(empty($result)){
  449. return Result::error("没有模板类型",0);
  450. }else{
  451. return Result::success($result);
  452. }
  453. }
  454. /**
  455. * 添加模板类型
  456. * @param
  457. * @return void
  458. */
  459. public function addTemplateClass(array $data): array
  460. {
  461. $insertData = [
  462. 'name'=>$data['name']
  463. ];
  464. $result = TemplateClass::insertGetId($insertData);
  465. if(empty($result)){
  466. return Result::error("创建失败",0);
  467. }else{
  468. return Result::success(["id"=>$result]);
  469. }
  470. }
  471. /**
  472. * 更新模板
  473. * @param array $data
  474. * @return array
  475. */
  476. public function upTemplateClass(array $data): array
  477. {
  478. $where = [
  479. 'id'=>$data['id']
  480. ];
  481. $insertData = [
  482. 'name'=>$data['name']
  483. ];
  484. $result = TemplateClass::where($where)->update($insertData);
  485. if(empty($result)){
  486. return Result::error("更新失败",0);
  487. }else{
  488. return Result::success();
  489. }
  490. }
  491. /**
  492. * 删除模板
  493. * @param array $data
  494. * @return array
  495. */
  496. public function delTemplateClass(array $data): array
  497. {
  498. $where = [
  499. 'id'=>$data['id']
  500. ];
  501. $result = TemplateClass::where($where)->delete();
  502. if(empty($result)){
  503. return Result::error("删除失败",0);
  504. }else{
  505. return Result::success();
  506. }
  507. }
  508. /**
  509. * 获取分类下的模板
  510. * @param array $data
  511. * @return array
  512. */
  513. public function getTemplate(array $data): array
  514. {
  515. $page = $data['page'];
  516. $pageSize = $data['pageSize'];
  517. $where = [
  518. 'template_class_id'=> $data['template_class_id']
  519. ];
  520. $result = Template::where($where)
  521. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  522. $count = Template::where($where)->count();
  523. if (empty($result)) {
  524. return Result::error("没有数据",0);
  525. }
  526. $data = [
  527. 'rows'=>$result->toArray(),
  528. 'count'=>$count
  529. ];
  530. return Result::success($data);
  531. }
  532. /**
  533. * 创建模板
  534. * @param
  535. * @return void
  536. */
  537. public function addTemplate(array $data): array
  538. {
  539. $insertData = [
  540. 'template_name'=>$data['template_name'],
  541. 'template_img'=>json_encode($data['template_img']),
  542. 'template_class_id'=>$data['template_class_id'],
  543. ];
  544. $result = Template::insertGetId($insertData);
  545. if(empty($result)){
  546. return Result::error("创建模板失败",0);
  547. }else{
  548. return Result::success(["id"=>$result]);
  549. }
  550. }
  551. /**
  552. * 更新模板
  553. * @param array $data
  554. * @return array
  555. */
  556. public function upTemplate(array $data): array
  557. {
  558. $where = [
  559. 'id'=>$data['id']
  560. ];
  561. $insertData = [
  562. 'template_name'=>$data['template_name'],
  563. 'template_img'=>json_encode($data['template_img']),
  564. 'template_class_id'=>$data['template_class_id'],
  565. ];
  566. $result = Template::where($where)->update($insertData);
  567. if(empty($result)){
  568. return Result::error("更新模板失败",0);
  569. }else{
  570. return Result::success();
  571. }
  572. }
  573. /**
  574. * 删除模板
  575. * @param array $data
  576. * @return array
  577. */
  578. public function delTemplate(array $data): array
  579. {
  580. $where = [
  581. 'id'=>$data['id']
  582. ];
  583. $result = Template::where($where)->delete();
  584. if(empty($result)){
  585. return Result::error("删除模板失败",0);
  586. }else{
  587. return Result::success();
  588. }
  589. }
  590. }