UserGroup.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\controller\admin\user;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\user\UserGroupRepository;
  5. use app\validate\admin\UserGroupValidate;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use think\App;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. /**
  12. * Class UserGroup
  13. * @package app\controller\admin\user
  14. * @author zfy
  15. * @day 2020-05-07
  16. */
  17. class UserGroup extends BaseController
  18. {
  19. /**
  20. * @var UserGroupRepository
  21. */
  22. protected $repository;
  23. /**
  24. * UserGroup constructor.
  25. * @param App $app
  26. * @param UserGroupRepository $repository
  27. */
  28. public function __construct(App $app, UserGroupRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @return mixed
  35. * @throws DataNotFoundException
  36. * @throws DbException
  37. * @throws ModelNotFoundException
  38. * @author zfy
  39. * @day 2020-05-07
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. return app('json')->success($this->repository->getList([], $page, $limit));
  45. }
  46. /**
  47. * @return mixed
  48. * @throws FormBuilderException
  49. * @author zfy
  50. * @day 2020-05-07
  51. */
  52. public function createForm()
  53. {
  54. return app('json')->success(formToData($this->repository->form()));
  55. }
  56. /**
  57. * @param UserGroupValidate $validate
  58. * @return mixed
  59. * @author zfy
  60. * @day 2020-05-07
  61. */
  62. public function create(UserGroupValidate $validate)
  63. {
  64. $data = $this->checkParams($validate);
  65. $this->repository->create($data);
  66. return app('json')->success('添加成功');
  67. }
  68. /**
  69. * @param $id
  70. * @return mixed
  71. * @throws DbException
  72. * @throws FormBuilderException
  73. * @throws DataNotFoundException
  74. * @throws ModelNotFoundException
  75. * @author zfy
  76. * @day 2020-05-07
  77. */
  78. public function updateForm($id)
  79. {
  80. if (!$this->repository->exists($id))
  81. return app('json')->fail('数据不存在');
  82. return app('json')->success(formToData($this->repository->updateForm($id)));
  83. }
  84. /**
  85. * @param $id
  86. * @param UserGroupValidate $validate
  87. * @return mixed
  88. * @throws DbException
  89. * @author zfy
  90. * @day 2020-05-07
  91. */
  92. public function update($id, UserGroupValidate $validate)
  93. {
  94. $data = $this->checkParams($validate);
  95. if (!$this->repository->exists($id))
  96. return app('json')->fail('数据不存在');
  97. $this->repository->update($id, $data);
  98. return app('json')->success('编辑成功');
  99. }
  100. /**
  101. * @param $id
  102. * @return mixed
  103. * @throws DbException
  104. * @author zfy
  105. * @day 2020-05-07
  106. */
  107. public function delete($id)
  108. {
  109. if (!$this->repository->exists($id))
  110. return app('json')->fail('数据不存在');
  111. $this->repository->delete($id);
  112. return app('json')->success('删除成功');
  113. }
  114. /**
  115. * @param UserGroupValidate $validate
  116. * @return array
  117. * @author zfy
  118. * @day 2020-05-07
  119. */
  120. protected function checkParams(UserGroupValidate $validate)
  121. {
  122. $data = $this->request->params(['group_name']);
  123. $validate->check($data);
  124. return $data;
  125. }
  126. }