LinkService.php 3.2 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. $result = Link::when($data, function ($query) use ($data) {
  16. if (isset($data['title']) && !empty($data['title'])) {
  17. $query->where('link.title', 'like', '%' . trim($data['title']) . '%');
  18. }
  19. if (isset($data['website_id']) && !empty($data['website_id'])) {
  20. $query->where('link.website_id', '=', $data['website_id']);
  21. }
  22. })
  23. ->leftJoin("website","website.id","link.website_id")
  24. ->select("link.*","website.website_name")
  25. ->limit($data['pageSize'])
  26. ->offset(($data['page']-1)*$data['pageSize'])
  27. ->orderBy("link.updated_at","desc")
  28. ->paginate(intval($data['pageSize']),
  29. [
  30. 'link.*',
  31. 'website.website_name'
  32. ],
  33. 'page', intval($data['page']));
  34. if(empty($result)){
  35. return Result::error("没有查到数据");
  36. }
  37. $result = [
  38. 'rows'=>$result->items(),
  39. 'count'=>$result->total()
  40. ];
  41. return Result::success($result);
  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. }