LinkService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Link;
  4. use Hyperf\RpcServer\Annotation\RpcService;
  5. use App\Tools\Result;
  6. #[RpcService(name: "LinkService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  7. class LinkService implements LinkServiceInterface
  8. {
  9. /**
  10. * @param array $data
  11. * @return array
  12. */
  13. public function getLinkList(array $data): array
  14. {
  15. $where = [];
  16. if(isset($data['title']) && $data['title']!=''){
  17. array_push($where, ['link.title','like','%'.$data['title'].'%']);
  18. }
  19. if(isset($data['website_id']) && $data['website_id']!=''){
  20. array_push($where, ['link.website_id','=',$data['website_id']]);
  21. }
  22. $rep = Link::where($where)
  23. ->leftJoin("website","website.id","link.website_id")
  24. ->select("link.*","website.website_name")
  25. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("sort","desc")->get();
  26. $count = Link::where($where)
  27. ->leftJoin("website","website.id","link.website_id")
  28. ->count();
  29. $result = [
  30. 'rows'=>$rep,
  31. 'count'=>$count
  32. ];
  33. return $result?Result::success($result):Result::error("没有查到数据");
  34. }
  35. /**
  36. * @param array $data
  37. * @return array
  38. */
  39. public function createLink(array $data): array
  40. {
  41. $result = Link::insertGetId($data);
  42. if (empty($result)) {
  43. return Result::error("创建失败", 0);
  44. } else {
  45. return Result::success(["id" => $result]);
  46. }
  47. }
  48. /**
  49. * @param array $data
  50. * @return array
  51. */
  52. public function updateLink(array $data): array
  53. {
  54. $result = Link::where('id', $data['id'])->update($data);
  55. if (empty($result)) {
  56. return Result::error("更新失败", 0);
  57. } else {
  58. return Result::success();
  59. }
  60. }
  61. /**
  62. * @param int $id
  63. * @return array
  64. */
  65. public function delLink(array $data): array
  66. {
  67. $result = Link::where('id', $data['id'])->delete();
  68. if (empty($result)) {
  69. return Result::error("删除失败", 0);
  70. } else {
  71. return Result::success();
  72. }
  73. }
  74. /**
  75. * 获取友情链接详情
  76. * @param array $data
  77. * @return array
  78. */
  79. public function getLinkInfo(array $data): array
  80. {
  81. $result = Link::where('id', $data['id'])->first();
  82. if (empty($result)) {
  83. return Result::error("获取友情链接详情失败", 0);
  84. } else {
  85. return Result::success($result);
  86. }
  87. }
  88. }