UserRelation.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 crmeb\basic\BaseController;
  13. use app\common\repositories\user\UserRelationRepository as repository;
  14. use think\App;
  15. class UserRelation extends BaseController
  16. {
  17. /**
  18. * @var repository
  19. */
  20. protected $repository;
  21. /**
  22. * UserRelation constructor.
  23. * @param App $app
  24. * @param repository $repository
  25. */
  26. public function __construct(App $app, repository $repository)
  27. {
  28. parent::__construct($app);
  29. $this->repository = $repository;
  30. }
  31. /**
  32. * @return mixed
  33. * @author Qinii
  34. */
  35. public function create()
  36. {
  37. $params = $this->request->params(['type_id', 'type']);
  38. if (!$params['type_id'] || !$params['type'])
  39. return app('json')->fail('参数丢失');
  40. if (!in_array($params['type'], [1, 10]))
  41. return app('json')->fail('参数错误');
  42. if (!$this->repository->fieldExists($params))
  43. return app('json')->fail('信息丢失');
  44. if ($this->repository->getUserRelation($params, $this->request->uid()))
  45. return app('json')->fail('您已经关注过了');
  46. $params['uid'] = $this->request->uid();
  47. $this->repository->create($params);
  48. return app('json')->success('关注成功');
  49. }
  50. /**
  51. * @return mixed
  52. * @author Qinii
  53. */
  54. public function productList()
  55. {
  56. [$page, $limit] = $this->getPage();
  57. $where = ['uid'=>$this->request->uid(),'type'=>1];
  58. return app('json')->success($this->repository->search($where, $page,$limit));
  59. }
  60. /**
  61. * @return mixed
  62. * @author Qinii
  63. */
  64. public function merchantList()
  65. {
  66. [$page, $limit] = $this->getPage();
  67. $where = ['uid'=>$this->request->uid(),'type'=>10];
  68. return app('json')->success($this->repository->search($where, $page,$limit));
  69. }
  70. /**
  71. * @return mixed
  72. * @author Qinii
  73. */
  74. public function delete()
  75. {
  76. $params = $this->request->params(['type_id','type']);
  77. if(!$this->repository->getUserRelation($params,$this->request->uid()))
  78. return app('json')->fail('信息不存在');
  79. $params['uid'] = $this->request->uid();
  80. $this->repository->destory($params);
  81. return app('json')->success('已取消收藏');
  82. }
  83. /**
  84. * @return mixed
  85. * @author Qinii
  86. */
  87. public function batchCreate()
  88. {
  89. $params = $this->request->params(['type_id','type']);
  90. if(!count($params['type_id']) || !$params['type'])
  91. return app('json')->fail('缺少必备参数');
  92. $this->repository->batchCreate($this->request->uid(),$params);
  93. return app('json')->success('收藏成功');
  94. }
  95. }