CollectorService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\OldModel\Article as OldArticle;
  4. use App\Model\Article;
  5. use App\Model\Web;
  6. use Hyperf\DbConnection\Db;
  7. use Hyperf\RpcServer\Annotation\RpcService;
  8. use App\Tools\Result;
  9. #[RpcService(name: "CollectorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  10. class CollectorService implements CollectorServiceInterface
  11. {
  12. /**
  13. * 添加网站
  14. * @param array $data
  15. * @return array|mixed
  16. */
  17. public function addWeb(array $data): array
  18. {
  19. $where = [
  20. 'name' => $data['name']
  21. ];
  22. $isweb = Web::where($where)->first();
  23. if(empty($isweb)){
  24. date_default_timezone_set('Asia/Shanghai');
  25. $time = time();
  26. $catetime = date('Y-m-d H:i:s', $time);
  27. $data['created_at'] = $catetime;
  28. $web = Web::insert($data);
  29. }else{
  30. return Result::error('此网站已存在,不可重复添加!');
  31. }
  32. if(empty($web)){
  33. return Result::error('添加失败');
  34. }
  35. return Result::success('添加成功');
  36. }
  37. /**
  38. * 获取并搜索网站
  39. * @param array $data
  40. * @return array|mixed
  41. */
  42. public function getWeb(array $data): array
  43. {
  44. if(isset($data['keyWord'])){
  45. $where = [
  46. ['name','like','%'.$data['keyWord'].'%']
  47. ];
  48. $webss = Web::where($where)->first();
  49. if(empty($webss)){
  50. return Result::error('未查找到相关网站!');
  51. }
  52. }else{
  53. $web = Web::get();
  54. }
  55. if(empty($web)){
  56. return Result::error('您还未添加网站,请先去添加!');
  57. }
  58. return Result::success($web);
  59. }
  60. /**
  61. * 修改网站
  62. * @param array $data
  63. * @return array|mixed
  64. */
  65. public function upWeb(array $data): array
  66. {
  67. $web = Web::where('id',$data['id'])->first();
  68. if(empty($web)){
  69. return Result::error('请输入正确的网站id!');
  70. }else{
  71. $id = Web::where('id',$data['id'])->update($data);
  72. if(empty($id)){
  73. return Result::error('无法修改!');
  74. }
  75. }
  76. return Result::success($id);
  77. }
  78. /**
  79. * 删除网站
  80. * @param array $data
  81. * @return array|mixed
  82. */
  83. public function delWeb(array $data): array
  84. {
  85. $web = Web::where('id',$data['id'])->first();
  86. if(empty($web)){
  87. return Result::error('请输入正确的网站id!');
  88. }else{
  89. $id = Web::where('id',$data['id'])->delete();
  90. if(empty($id)){
  91. return Result::error('无法删除!');
  92. }
  93. }
  94. return Result::success($id);
  95. }
  96. }