UserLabel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. */
  27. class UserLabel extends BaseController
  28. {
  29. /**
  30. * @var UserLabelRepository
  31. */
  32. protected $repository;
  33. /**
  34. * UserGroup constructor.
  35. * @param App $app
  36. * @param UserLabelRepository $repository
  37. */
  38. public function __construct(App $app, UserLabelRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * 列表
  45. * @return mixed
  46. * @throws DataNotFoundException
  47. * @throws DbException
  48. * @throws ModelNotFoundException
  49. * @author xaboy
  50. * @day 2020-05-07
  51. */
  52. public function lst()
  53. {
  54. [$page, $limit] = $this->getPage();
  55. $where = $this->request->params(['type', 'all']);
  56. $where['mer_id'] = $this->request->merId();
  57. return app('json')->success($this->repository->getList($where, $page, $limit));
  58. }
  59. /**
  60. * 创建表单
  61. * @return mixed
  62. * @throws FormBuilderException
  63. * @author xaboy
  64. * @day 2020-05-07
  65. */
  66. public function createForm()
  67. {
  68. return app('json')->success(formToData($this->repository->form()));
  69. }
  70. /**
  71. * 创建
  72. * @param UserLabelValidate $validate
  73. * @return mixed
  74. * @author xaboy
  75. * @day 2020-05-07
  76. */
  77. public function create(UserLabelValidate $validate)
  78. {
  79. $data = $this->checkParams($validate);
  80. $data['mer_id'] = $this->request->merId();
  81. if ($this->repository->existsName($data['label_name'], $data['mer_id'], 0))
  82. return app('json')->fail('标签名已存在');
  83. $this->repository->create($data);
  84. return app('json')->success('添加成功');
  85. }
  86. /**
  87. * 修改表单
  88. * @param $id
  89. * @return mixed
  90. * @throws DbException
  91. * @throws FormBuilderException
  92. * @throws DataNotFoundException
  93. * @throws ModelNotFoundException
  94. * @author xaboy
  95. * @day 2020-05-07
  96. */
  97. public function updateForm($id)
  98. {
  99. if (!$this->repository->exists($id, $this->request->merId()))
  100. return app('json')->fail('数据不存在');
  101. return app('json')->success(formToData($this->repository->updateForm($id)));
  102. }
  103. /**
  104. * 修改
  105. * @param $id
  106. * @param UserLabelValidate $validate
  107. * @return mixed
  108. * @throws DbException
  109. * @author xaboy
  110. * @day 2020-05-07
  111. */
  112. public function update($id, UserLabelValidate $validate)
  113. {
  114. $data = $this->checkParams($validate);
  115. $merId = $this->request->merId();
  116. if (!$this->repository->exists($id, $merId))
  117. return app('json')->fail('数据不存在');
  118. if ($this->repository->existsName($data['label_name'], $merId, 0, $id))
  119. return app('json')->fail('标签名已存在');
  120. $this->repository->update($id, $data);
  121. return app('json')->success('编辑成功');
  122. }
  123. /**
  124. * 删除
  125. * @param $id
  126. * @return mixed
  127. * @throws DbException
  128. * @author xaboy
  129. * @day 2020-05-07
  130. */
  131. public function delete($id)
  132. {
  133. if (!$this->repository->exists($id, $this->request->merId()))
  134. return app('json')->fail('数据不存在');
  135. $this->repository->delete($id);
  136. return app('json')->success('删除成功');
  137. }
  138. /**
  139. * 验证数据
  140. * @param UserLabelValidate $validate
  141. * @return array
  142. * @author xaboy
  143. * @day 2020-05-07
  144. */
  145. protected function checkParams(UserLabelValidate $validate)
  146. {
  147. $data = $this->request->params(['label_name']);
  148. $validate->check($data);
  149. return $data;
  150. }
  151. }