LinkService.php 2.9 KB

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