LinkService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // return Result::success($data);
  16. $where = [];
  17. if(isset($data['title']) && !empty($data['title'])){
  18. array_push($where,['link.title','like','%'.$data['title'].'%']);
  19. }
  20. if(isset($data['website_id']) && !empty($data['website_id'])){
  21. array_push($where,['link.website_id','=',$data['website_id']]);
  22. }
  23. $result = [];
  24. // return Result::success($where);
  25. if(!empty($where)){
  26. $query = Link::where($where);
  27. $count = $query->count();
  28. }else{
  29. $query = Link::query();
  30. $count = $query->count();
  31. }
  32. // 重新构建查询,避免 $query 已经被修改
  33. $rep = $query
  34. ->leftJoin("website", "website.id", "link.website_id")
  35. ->select("link.*", "website.website_name")
  36. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  37. ->orderBy("sort", "asc")
  38. ->get();
  39. // $countQuery = clone $query;
  40. // $count = $countQuery->count();
  41. $result = [
  42. 'rows'=>$rep,
  43. 'count'=>$count
  44. ];
  45. if(empty($result)){
  46. return Result::error("没有查到数据");
  47. }
  48. return $result?Result::success($result):Result::error("没有查到数据");
  49. }
  50. /**
  51. * @param array $data
  52. * @return array
  53. */
  54. public function createLink(array $data): array
  55. {
  56. $result = Link::insertGetId($data);
  57. if (empty($result)) {
  58. return Result::error("创建失败", 0);
  59. } else {
  60. return Result::success(["id" => $result]);
  61. }
  62. }
  63. /**
  64. * @param array $data
  65. * @return array
  66. */
  67. public function updateLink(array $data): array
  68. {
  69. $result = Link::where('id', $data['id'])->update($data);
  70. if (empty($result)) {
  71. return Result::error("更新失败", 0);
  72. } else {
  73. return Result::success();
  74. }
  75. }
  76. /**
  77. * @param array $data
  78. * @return array
  79. */
  80. public function delLink(array $data): array
  81. {
  82. // return Result::success($data);
  83. $result = Link::where('id', $data['id'])->delete();
  84. // return Result::success($result);
  85. // var_dump($result);
  86. if (empty($result)) {
  87. return Result::error("删除失败", 0);
  88. } else {
  89. return Result::success();
  90. }
  91. }
  92. /**
  93. * 获取友情链接详情
  94. * @param array $data
  95. * @return array
  96. */
  97. public function getLinkInfo(array $data): array
  98. {
  99. $result = Link::where('id', $data['id'])->first();
  100. if (empty($result)) {
  101. return Result::error("获取友情链接详情失败", 0);
  102. } else {
  103. return Result::success($result);
  104. }
  105. }
  106. }