UserGroup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\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. class UserGroup extends BaseController
  27. {
  28. /**
  29. * @var UserGroupRepository
  30. */
  31. protected $repository;
  32. /**
  33. * UserGroup constructor.
  34. * @param App $app
  35. * @param UserGroupRepository $repository
  36. */
  37. public function __construct(App $app, UserGroupRepository $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. return app('json')->success($this->repository->getList([], $page, $limit));
  54. }
  55. /**
  56. * @return mixed
  57. * @throws FormBuilderException
  58. * @author xaboy
  59. * @day 2020-05-07
  60. */
  61. public function createForm()
  62. {
  63. return app('json')->success(formToData($this->repository->form()));
  64. }
  65. /**
  66. * @param UserGroupValidate $validate
  67. * @return mixed
  68. * @author xaboy
  69. * @day 2020-05-07
  70. */
  71. public function create(UserGroupValidate $validate)
  72. {
  73. $data = $this->checkParams($validate);
  74. $this->repository->create($data);
  75. return app('json')->success('添加成功');
  76. }
  77. /**
  78. * @param $id
  79. * @return mixed
  80. * @throws DbException
  81. * @throws FormBuilderException
  82. * @throws DataNotFoundException
  83. * @throws ModelNotFoundException
  84. * @author xaboy
  85. * @day 2020-05-07
  86. */
  87. public function updateForm($id)
  88. {
  89. if (!$this->repository->exists($id))
  90. return app('json')->fail('数据不存在');
  91. return app('json')->success(formToData($this->repository->updateForm($id)));
  92. }
  93. /**
  94. * @param $id
  95. * @param UserGroupValidate $validate
  96. * @return mixed
  97. * @throws DbException
  98. * @author xaboy
  99. * @day 2020-05-07
  100. */
  101. public function update($id, UserGroupValidate $validate)
  102. {
  103. $data = $this->checkParams($validate);
  104. if (!$this->repository->exists($id))
  105. return app('json')->fail('数据不存在');
  106. $this->repository->update($id, $data);
  107. return app('json')->success('编辑成功');
  108. }
  109. /**
  110. * @param $id
  111. * @return mixed
  112. * @throws DbException
  113. * @author xaboy
  114. * @day 2020-05-07
  115. */
  116. public function delete($id)
  117. {
  118. if (!$this->repository->exists($id))
  119. return app('json')->fail('数据不存在');
  120. $this->repository->delete($id);
  121. return app('json')->success('删除成功');
  122. }
  123. /**
  124. * @param UserGroupValidate $validate
  125. * @return array
  126. * @author xaboy
  127. * @day 2020-05-07
  128. */
  129. protected function checkParams(UserGroupValidate $validate)
  130. {
  131. $data = $this->request->params(['group_name']);
  132. $validate->check($data);
  133. return $data;
  134. }
  135. }