UserRelation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\order\StoreOrderRepository;
  13. use app\common\repositories\store\StorePercentageRepository;
  14. use app\common\repositories\user\UserRepository;
  15. use crmeb\basic\BaseController;
  16. use app\common\repositories\user\UserRelationRepository as repository;
  17. use think\App;
  18. use think\console\command\Make;
  19. class UserRelation extends BaseController
  20. {
  21. /**
  22. * @var repository
  23. */
  24. protected $repository;
  25. /**
  26. * UserRelation constructor.
  27. * @param App $app
  28. * @param repository $repository
  29. */
  30. public function __construct(App $app, repository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * @return mixed
  37. * @author Qinii
  38. */
  39. public function create(StorePercentageRepository $store_percentage, UserRepository $repository, StoreOrderRepository $order_epository)
  40. {
  41. $params = $this->request->params(['type_id', 'type']);
  42. $params['uid'] = $this->request->uid();
  43. if (!$params['type_id'])
  44. return app('json')->fail('参数丢失');
  45. if (!in_array($params['type'], [0,1,2,3,4,10]))
  46. return app('json')->fail('参数错误');
  47. if (!$this->repository->fieldExists($params))
  48. return app('json')->fail('数据不存在');
  49. if ($this->repository->getUserRelation($params,$this->request->uid()))
  50. return app('json')->fail('您已经关注过了');
  51. $params['uid'] = $this->request->uid();
  52. $this->repository->create($params);
  53. //关注店铺返利
  54. $user = $repository->get($params['uid']);
  55. if($user->order_id){
  56. $order_info = $order_epository->findOrCreate(array('order_id'=>$user->order_id));
  57. $percentage_info = $store_percentage->getWhere(array('passivity_user_id'=>$params['uid'],'deduction_type'=>5));
  58. if(empty($percentage_info)){
  59. //一级关注店铺折扣百分比
  60. $store_percentage->deduction_percentage(5,$user->uid,$user->order_id);
  61. $p_user = $repository->get($order_info->uid);
  62. if($p_user->order_id){
  63. $store_percentage->deduction_percentage(6,$user->uid,$p_user->order_id);
  64. }
  65. }
  66. }
  67. return app('json')->success('关注成功');
  68. }
  69. /**
  70. * @return mixed
  71. * @author Qinii
  72. */
  73. public function productList()
  74. {
  75. [$page, $limit] = $this->getPage();
  76. $where = ['uid'=>$this->request->uid(),'type'=>1];
  77. return app('json')->success($this->repository->search($where, $page,$limit));
  78. }
  79. /**
  80. * @return mixed
  81. * @author Qinii
  82. */
  83. public function merchantList()
  84. {
  85. [$page, $limit] = $this->getPage();
  86. $where = ['uid'=>$this->request->uid(),'type'=>10];
  87. return app('json')->success($this->repository->search($where, $page,$limit));
  88. }
  89. /**
  90. * TODO 收藏列表的删除
  91. * @return \think\response\Json
  92. * @author Qinii
  93. * @day 7/12/21
  94. */
  95. public function lstDelete()
  96. {
  97. $params = $this->request->params(['type_id','type']);
  98. $params['uid'] = $this->request->uid();
  99. if(!$this->repository->getWhere($params))
  100. return app('json')->fail('信息不存在');
  101. $this->repository->destory($params,1);
  102. return app('json')->success('已取消关注');
  103. }
  104. /**
  105. * TODO 商品详情中的取消收藏
  106. * @return \think\response\Json
  107. * @author Qinii
  108. * @day 7/12/21
  109. */
  110. public function delete()
  111. {
  112. $params = $this->request->params(['type_id','type']);
  113. if (!$this->repository->getUserRelation($params,$this->request->uid()))
  114. return app('json')->fail('信息不存在');
  115. $this->repository->destory($params);
  116. return app('json')->success('已取消关注');
  117. }
  118. /**
  119. * @return mixed
  120. * @author Qinii
  121. */
  122. public function batchCreate()
  123. {
  124. $params = $this->request->params(['type_id','type']);
  125. if(!count($params['type_id']) || !in_array($params['type'], [1,10]))
  126. return app('json')->fail('请选择商品');
  127. $this->repository->batchCreate($this->request->uid(),$params);
  128. return app('json')->success('收藏成功');
  129. }
  130. }