UserLabel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\admin\user;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\user\UserLabelRepository;
  14. use app\validate\admin\UserLabelValidate;
  15. use FormBuilder\Exception\FormBuilderException;
  16. use think\App;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class UserLabel
  22. * @package app\controller\admin\user
  23. * @author xaboy
  24. * @day 2020-05-07
  25. */
  26. class UserLabel extends BaseController
  27. {
  28. /**
  29. * @var UserLabelRepository
  30. */
  31. protected $repository;
  32. /**
  33. * UserGroup constructor.
  34. * @param App $app
  35. * @param UserLabelRepository $repository
  36. */
  37. public function __construct(App $app, UserLabelRepository $repository)
  38. {
  39. parent::__construct($app);
  40. $this->repository = $repository;
  41. }
  42. /**
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-05-07
  49. */
  50. public function lst()
  51. {
  52. [$page, $limit] = $this->getPage();
  53. $where = $this->request->params(['type', 'all']);
  54. $where['mer_id'] = $this->request->merId();
  55. return app('json')->success($this->repository->getList($where, $page, $limit));
  56. }
  57. /**
  58. * @return mixed
  59. * @throws FormBuilderException
  60. * @author xaboy
  61. * @day 2020-05-07
  62. */
  63. public function createForm()
  64. {
  65. return app('json')->success(formToData($this->repository->form()));
  66. }
  67. /**
  68. * @param UserLabelValidate $validate
  69. * @return mixed
  70. * @author xaboy
  71. * @day 2020-05-07
  72. */
  73. public function create(UserLabelValidate $validate)
  74. {
  75. $data = $this->checkParams($validate);
  76. $data['mer_id'] = $this->request->merId();
  77. if ($this->repository->existsName($data['label_name'], $data['mer_id'], 0))
  78. return app('json')->fail('标签名已存在');
  79. $this->repository->create($data);
  80. return app('json')->success('添加成功');
  81. }
  82. /**
  83. * @param $id
  84. * @return mixed
  85. * @throws DbException
  86. * @throws FormBuilderException
  87. * @throws DataNotFoundException
  88. * @throws ModelNotFoundException
  89. * @author xaboy
  90. * @day 2020-05-07
  91. */
  92. public function updateForm($id)
  93. {
  94. if (!$this->repository->exists($id, $this->request->merId()))
  95. return app('json')->fail('数据不存在');
  96. return app('json')->success(formToData($this->repository->updateForm($id)));
  97. }
  98. /**
  99. * @param $id
  100. * @param UserLabelValidate $validate
  101. * @return mixed
  102. * @throws DbException
  103. * @author xaboy
  104. * @day 2020-05-07
  105. */
  106. public function update($id, UserLabelValidate $validate)
  107. {
  108. $data = $this->checkParams($validate);
  109. $merId = $this->request->merId();
  110. if (!$this->repository->exists($id, $merId))
  111. return app('json')->fail('数据不存在');
  112. if ($this->repository->existsName($data['label_name'], $merId, 0, $id))
  113. return app('json')->fail('标签名已存在');
  114. $this->repository->update($id, $data);
  115. return app('json')->success('编辑成功');
  116. }
  117. /**
  118. * @param $id
  119. * @return mixed
  120. * @throws DbException
  121. * @author xaboy
  122. * @day 2020-05-07
  123. */
  124. public function delete($id)
  125. {
  126. if (!$this->repository->exists($id, $this->request->merId()))
  127. return app('json')->fail('数据不存在');
  128. $this->repository->delete($id);
  129. return app('json')->success('删除成功');
  130. }
  131. /**
  132. * @param UserLabelValidate $validate
  133. * @return array
  134. * @author xaboy
  135. * @day 2020-05-07
  136. */
  137. protected function checkParams(UserLabelValidate $validate)
  138. {
  139. $data = $this->request->params(['label_name']);
  140. $validate->check($data);
  141. return $data;
  142. }
  143. }