UserRelation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\user;
  12. use app\common\repositories\store\product\ProductRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\user\UserRelationRepository as repository;
  15. use think\App;
  16. class UserRelation extends BaseController
  17. {
  18. /**
  19. * @var repository
  20. */
  21. protected $repository;
  22. /**
  23. * UserRelation constructor.
  24. * @param App $app
  25. * @param repository $repository
  26. */
  27. public function __construct(App $app, repository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 用户收藏商品或者店铺
  34. * @return mixed
  35. * @author Qinii
  36. */
  37. public function create()
  38. {
  39. $params = $this->request->params(['type_id', 'type']);
  40. $params['uid'] = $this->request->uid();
  41. if (!$params['type_id'])
  42. return app('json')->fail('请选择'. ($params['type'] == 1 ? '商品' : '商户'));
  43. if (!in_array($params['type'], [0,1,2,3,4,10]))
  44. return app('json')->fail('参数错误');
  45. if (!$this->repository->fieldExists($params))
  46. return app('json')->fail('数据不存在');
  47. if ($this->repository->getUserRelation($params,$this->request->uid()))
  48. return app('json')->fail('您已经关注过了');
  49. $this->repository->create($params);
  50. return app('json')->success('关注成功');
  51. }
  52. /**
  53. * 收藏的商品列表
  54. * @return mixed
  55. * @author Qinii
  56. */
  57. public function productList()
  58. {
  59. [$page, $limit] = $this->getPage();
  60. $where = ['uid'=>$this->request->uid(),'type'=>1];
  61. return app('json')->success($this->repository->search($where, $page,$limit));
  62. }
  63. /**
  64. * 收藏的商户列表
  65. * @return mixed
  66. * @author Qinii
  67. */
  68. public function merchantList()
  69. {
  70. [$page, $limit] = $this->getPage();
  71. $where = ['uid'=>$this->request->uid(),'type'=>10];
  72. return app('json')->success($this->repository->search($where, $page,$limit));
  73. }
  74. /**
  75. * 收藏列表的删除
  76. * @return \think\response\Json
  77. * @author Qinii
  78. * @day 7/12/21
  79. */
  80. public function batchDelete()
  81. {
  82. $ids = $this->request->param('type_id');
  83. $type = $this->request->param('type',1);
  84. if(empty($ids)) return app('json')->fail('请选择'. ($type == 1 ? '商品' : '商户'));
  85. $this->repository->batchDestory($ids,$this->request->uid(),$type);
  86. return app('json')->success('已取消关注');
  87. }
  88. /**
  89. * 商品详情中的取消收藏
  90. * @return \think\response\Json
  91. * @author Qinii
  92. * @day 7/12/21
  93. */
  94. public function delete()
  95. {
  96. $params = $this->request->params(['type_id','type']);
  97. if (!$this->repository->getUserRelation($params,$this->request->uid()))
  98. return app('json')->fail('信息不存在');
  99. if(in_array($params['type'],[0,1,2,3,4])) {
  100. $spu = $this->repository->getSpu($params);
  101. $params['type_id'] = $spu->spu_id;
  102. $params['type'] = 1;
  103. }
  104. $this->repository->batchDestory([$params['type_id']],$this->request->uid(),$params['type']);
  105. return app('json')->success('已取消关注');
  106. }
  107. /**
  108. * 批量收藏
  109. * @return mixed
  110. * @author Qinii
  111. */
  112. public function batchCreate()
  113. {
  114. $params = $this->request->params(['type_id','type']);
  115. if(!count($params['type_id']) || !in_array($params['type'], [1,10]))
  116. return app('json')->fail('请选择商品');
  117. $this->repository->batchCreate($this->request->uid(),$params);
  118. return app('json')->success('收藏成功');
  119. }
  120. }