UserRelation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/30
  6. */
  7. namespace app\controller\api\user;
  8. use app\common\repositories\store\order\StoreCartRepository;
  9. use crmeb\basic\BaseController;
  10. use app\common\repositories\user\UserRelationRepository as repository;
  11. use think\App;
  12. use think\facade\Db;
  13. class UserRelation extends BaseController
  14. {
  15. /**
  16. * @var repository
  17. */
  18. protected $repository;
  19. /**
  20. * UserRelation constructor.
  21. * @param App $app
  22. * @param repository $repository
  23. */
  24. public function __construct(App $app, repository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * @return mixed
  31. * @author Qinii
  32. */
  33. public function create()
  34. {
  35. $params = $this->request->params(['type_id','type']);
  36. if(!$params['type_id'] || !$params['type'])
  37. return app('json')->fail('缺少必备参数');
  38. if(!$this->repository->fieldExists($params))
  39. return app('json')->fail('信息不存在');
  40. if($this->repository->getUserRelation($params,$this->request->uid()))
  41. return app('json')->fail('您已经收藏过了');
  42. $params['uid'] = $this->request->uid();
  43. $this->repository->create($params);
  44. return app('json')->success('收藏成功');
  45. }
  46. /**
  47. * @return mixed
  48. * @author Qinii
  49. */
  50. public function productList()
  51. {
  52. [$page, $limit] = $this->getPage();
  53. $where = ['uid'=>$this->request->uid()];
  54. $keyword = $this->request->params(['keywords']);
  55. $where['keyword'] =$keyword['keywords'];
  56. return app('json')->success($this->repository->search($where, $page,$limit));
  57. }
  58. /**
  59. * @return mixed
  60. * @author Qinii
  61. */
  62. public function merchantList()
  63. {
  64. [$page, $limit] = $this->getPage();
  65. $where = ['uid'=>$this->request->uid()];
  66. $keyword = $this->request->params(['keywords']);
  67. $where['keyword'] =$keyword['keywords'];
  68. return app('json')->success($this->repository->searchMer($where, $page,$limit));
  69. }
  70. public function businessSchoolList(){
  71. [$page, $limit] = $this->getPage();
  72. $where = ['uid'=>$this->request->uid()];
  73. if($this->source=='yhc' && $this->merId){
  74. $mer_id=$this->merId;
  75. $query =Db::name("user_relation")->field('type_id,type,create_time')->whereIn('type',[1,11])->where('uid',$where['uid'])->order("create_time",'desc');
  76. $count = $query->count();
  77. $list = $query->page($page, $limit)->select()->each(function ($item) use ($mer_id) {
  78. if($item['type']==11){
  79. $item['business_school']=Db::name('business_school')->where('mer_id',$mer_id)->where('id',$item['type_id'])->field('id,name,image,type,reading,create_time')->find();
  80. }
  81. return $item;
  82. });
  83. return compact('count', 'list');
  84. }
  85. }
  86. /**
  87. * @return mixed
  88. * @author Qinii
  89. */
  90. public function delete()
  91. {
  92. $params = $this->request->params(['type_id','type']);
  93. if(!$this->repository->getUserRelation($params,$this->request->uid()))
  94. return app('json')->fail('信息不存在');
  95. $params['uid'] = $this->request->uid();
  96. $this->repository->destory($params);
  97. return app('json')->success('删除成功');
  98. }
  99. /**
  100. * @return mixed
  101. * @author Qinii
  102. */
  103. public function batchDelete()
  104. {
  105. $params = $this->request->params(['type_id','type']);
  106. $params['uid'] = $this->request->uid();
  107. if (!count($params['type_id']))
  108. return app('json')->fail('缺少必备参数');
  109. foreach ($params['type_id'] as $type_id){
  110. $this->repository->destory([
  111. 'type_id' => $type_id,
  112. 'type' => $params['type']
  113. ]);
  114. }
  115. return app('json')->success('删除成功');
  116. }
  117. /**
  118. * @return mixed
  119. * @author Qinii
  120. */
  121. public function batchCreate()
  122. {
  123. $params = $this->request->params(['type_id','type']);
  124. if(!count($params['type_id']) || !$params['type'])
  125. return app('json')->fail('缺少必备参数');
  126. $this->repository->batchCreate($this->request->uid(),$params);
  127. return app('json')->success('收藏成功');
  128. }
  129. /**
  130. * @return mixed
  131. * @author Qinii
  132. */
  133. public function batchCreateRemoveCart()
  134. {
  135. $params = $this->request->params(['type_id']);
  136. $params['type'] = 1;
  137. if (!count($params['type_id']))
  138. return app('json')->fail('缺少必备参数');
  139. $this->repository->batchCreate($this->request->uid(),$params);
  140. $ids = Db::name('store_cart')->where('uid', $this->request->uid())->whereIn('product_id', $params['type_id'])->column('cart_id');
  141. app()->make(StoreCartRepository::class)->batchDelete($ids,$this->request->uid());
  142. return app('json')->success('收藏成功');
  143. }
  144. }