UserGroup.php 3.5 KB

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