UserRelation.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\controller\api\user;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\user\UserRelationRepository as repository;
  5. use think\App;
  6. class UserRelation extends BaseController
  7. {
  8. /**
  9. * @var repository
  10. */
  11. protected $repository;
  12. /**
  13. * UserRelation constructor.
  14. * @param App $app
  15. * @param repository $repository
  16. */
  17. public function __construct(App $app, repository $repository)
  18. {
  19. parent::__construct($app);
  20. $this->repository = $repository;
  21. }
  22. /**
  23. * @return mixed
  24. * @author Qinii
  25. */
  26. public function create()
  27. {
  28. $params = $this->request->params(['type_id', 'type']);
  29. $params['uid'] = $this->request->uid();
  30. if (!$params['type_id'])
  31. return app('json')->fail('参数丢失');
  32. if (!in_array($params['type'], [0,1,2,3,4,10]))
  33. return app('json')->fail('参数错误');
  34. if (!$this->repository->fieldExists($params))
  35. return app('json')->fail('数据不存在');
  36. if ($this->repository->getUserRelation($params,$this->request->uid()))
  37. return app('json')->fail('您已经关注过了');
  38. $params['uid'] = $this->request->uid();
  39. $this->repository->create($params);
  40. return app('json')->success('关注成功');
  41. }
  42. /**
  43. * @return mixed
  44. * @author Qinii
  45. */
  46. public function productList()
  47. {
  48. [$page, $limit] = $this->getPage();
  49. $where = ['uid'=>$this->request->uid(),'type'=>1];
  50. return app('json')->success($this->repository->search($where, $page,$limit));
  51. }
  52. /**
  53. * @return mixed
  54. * @author Qinii
  55. */
  56. public function merchantList()
  57. {
  58. [$page, $limit] = $this->getPage();
  59. $where = ['uid'=>$this->request->uid(),'type'=>10];
  60. return app('json')->success($this->repository->search($where, $page,$limit));
  61. }
  62. /**
  63. * TODO 收藏列表的删除
  64. * @return \think\response\Json
  65. * @author Qinii
  66. * @day 7/12/21
  67. */
  68. public function lstDelete()
  69. {
  70. $params = $this->request->params(['type_id','type']);
  71. $params['uid'] = $this->request->uid();
  72. if(!$this->repository->getWhere($params))
  73. return app('json')->fail('信息不存在');
  74. $this->repository->destory($params,1);
  75. return app('json')->success('已取消收藏');
  76. }
  77. /**
  78. * TODO 商品详情中的取消收藏
  79. * @return \think\response\Json
  80. * @author Qinii
  81. * @day 7/12/21
  82. */
  83. public function delete()
  84. {
  85. $params = $this->request->params(['type_id','type']);
  86. if (!$this->repository->getUserRelation($params,$this->request->uid()))
  87. return app('json')->fail('信息不存在');
  88. $this->repository->destory($params);
  89. return app('json')->success('已取消收藏');
  90. }
  91. /**
  92. * @return mixed
  93. * @author Qinii
  94. */
  95. public function batchCreate()
  96. {
  97. $params = $this->request->params(['type_id','type']);
  98. if(!count($params['type_id']) || !in_array($params['type'], [1,10]))
  99. return app('json')->fail('缺少必备参数');
  100. if($params['type'] == 1) $params['type'] = 0;
  101. $this->repository->batchCreate($this->request->uid(),$params);
  102. return app('json')->success('收藏成功');
  103. }
  104. }