LinkService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $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. }