LinkService.php 2.9 KB

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