UserRelation.php 5.8 KB

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