LinkService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. //website_name` = '三农市场网', `status_name` =
  63. unset($data['website_name']);
  64. unset($data['status_name']);
  65. $result = Link::where('id', $data['id'])->update($data);
  66. if (empty($result)) {
  67. return Result::error("更新失败", 0);
  68. } else {
  69. return Result::success();
  70. }
  71. }
  72. /**
  73. * @param array $data
  74. * @return array
  75. */
  76. public function delLink(array $data): array
  77. {
  78. // return Result::success($data);
  79. $result = Link::where('id', $data['id'])->delete();
  80. // return Result::success($result);
  81. // var_dump($result);
  82. if (empty($result)) {
  83. return Result::error("删除失败", 0);
  84. } else {
  85. return Result::success($result);
  86. }
  87. }
  88. /**
  89. * 获取友情链接详情
  90. * @param array $data
  91. * @return array
  92. */
  93. public function getLinkInfo(array $data): array
  94. {
  95. $result = Link::where('id', $data['id'])->first();
  96. if (empty($result)) {
  97. return Result::error("获取友情链接详情失败", 0);
  98. } else {
  99. return Result::success($result);
  100. }
  101. }
  102. }