UserGroup.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\UserGroupRepository;
  14. use app\validate\admin\UserGroupValidate;
  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 UserGroup
  22. * @package app\controller\admin\user
  23. * @author xaboy
  24. * @day 2020-05-07
  25. * 用户分组
  26. */
  27. class UserGroup extends BaseController
  28. {
  29. /**
  30. * @var UserGroupRepository
  31. */
  32. protected $repository;
  33. /**
  34. * UserGroup constructor.
  35. * @param App $app
  36. * @param UserGroupRepository $repository
  37. */
  38. public function __construct(App $app, UserGroupRepository $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. return app('json')->success($this->repository->getList([], $page, $limit));
  56. }
  57. /**
  58. * 创建表单
  59. * @return mixed
  60. * @throws FormBuilderException
  61. * @author xaboy
  62. * @day 2020-05-07
  63. */
  64. public function createForm()
  65. {
  66. return app('json')->success(formToData($this->repository->form()));
  67. }
  68. /**
  69. * 创建
  70. * @param UserGroupValidate $validate
  71. * @return mixed
  72. * @author xaboy
  73. * @day 2020-05-07
  74. */
  75. public function create(UserGroupValidate $validate)
  76. {
  77. $data = $this->checkParams($validate);
  78. $this->repository->create($data);
  79. return app('json')->success('添加成功');
  80. }
  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))
  95. return app('json')->fail('数据不存在');
  96. return app('json')->success(formToData($this->repository->updateForm($id)));
  97. }
  98. /**
  99. * 修改
  100. * @param $id
  101. * @param UserGroupValidate $validate
  102. * @return mixed
  103. * @throws DbException
  104. * @author xaboy
  105. * @day 2020-05-07
  106. */
  107. public function update($id, UserGroupValidate $validate)
  108. {
  109. $data = $this->checkParams($validate);
  110. if (!$this->repository->exists($id))
  111. return app('json')->fail('数据不存在');
  112. $this->repository->update($id, $data);
  113. return app('json')->success('编辑成功');
  114. }
  115. /**
  116. * 删除
  117. * @param $id
  118. * @return mixed
  119. * @throws DbException
  120. * @author xaboy
  121. * @day 2020-05-07
  122. */
  123. public function delete($id)
  124. {
  125. if (!$this->repository->exists($id))
  126. return app('json')->fail('数据不存在');
  127. $this->repository->delete($id);
  128. return app('json')->success('删除成功');
  129. }
  130. /**
  131. * 验证数据
  132. * @param UserGroupValidate $validate
  133. * @return array
  134. * @author xaboy
  135. * @day 2020-05-07
  136. */
  137. protected function checkParams(UserGroupValidate $validate)
  138. {
  139. $data = $this->request->params(['group_name']);
  140. $validate->check($data);
  141. return $data;
  142. }
  143. }