LinkService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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']) && !empty($data['title'])){
  17. array_push($where,['link.title','like','%'.$data['title'].'%']);
  18. }
  19. if(isset($data['website_id']) && !empty($data['website_id'])){
  20. array_push($where,['link.website_id','=',$data['website_id']]);
  21. }
  22. $result = [];
  23. // return Result::success($where);
  24. if(!empty($where)){
  25. $query = Link::where($where);
  26. $count = $query->count();
  27. }else{
  28. $query = Link::query();
  29. $count = $query->count();
  30. }
  31. // 重新构建查询,避免 $query 已经被修改
  32. $rep = $query
  33. ->leftJoin("website", "website.id", "link.website_id")
  34. ->select("link.*", "website.website_name")
  35. ->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])
  36. ->orderBy("link.updated_at", "desc")
  37. ->get();
  38. $result = [
  39. 'rows'=>$rep,
  40. 'count'=>$count
  41. ];
  42. if(empty($result)){
  43. return Result::error("没有查到数据");
  44. }
  45. return $result?Result::success($result):Result::error("没有查到数据");
  46. }
  47. /**
  48. * @param array $data
  49. * @return array
  50. */
  51. public function createLink(array $data): array
  52. {
  53. $result = Link::insertGetId($data);
  54. if (empty($result)) {
  55. return Result::error("创建失败", 0);
  56. } else {
  57. return Result::success(["id" => $result]);
  58. }
  59. }
  60. /**
  61. * @param array $data
  62. * @return array
  63. */
  64. public function updateLink(array $data): array
  65. {
  66. //website_name` = '三农市场网', `status_name` =
  67. unset($data['website_name']);
  68. unset($data['status_name']);
  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($result);
  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. }