WebsiteService.php 17 KB

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