Role.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\system\auth;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\auth\RoleRepository;
  14. use app\validate\admin\RoleValidate;
  15. use Exception;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\response\Json;
  22. class Role extends BaseController
  23. {
  24. protected $repository;
  25. public function __construct(App $app, RoleRepository $repository)
  26. {
  27. parent::__construct($app);
  28. $this->repository = $repository;
  29. }
  30. /**
  31. * @return mixed
  32. * @throws FormBuilderException
  33. * @author xaboy
  34. * @day 2020-04-09
  35. */
  36. public function createForm()
  37. {
  38. return app('json')->success(formToData($this->repository->form()));
  39. }
  40. /**
  41. * @param int $id
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws FormBuilderException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-04-09
  49. */
  50. public function updateForm($id)
  51. {
  52. if (!$this->repository->exists($id))
  53. return app('json')->fail('数据不存在');
  54. return app('json')->success(formToData($this->repository->updateForm(false, $id)));
  55. }
  56. /**
  57. * @return mixed
  58. * @throws DataNotFoundException
  59. * @throws DbException
  60. * @throws ModelNotFoundException
  61. * @author xaboy
  62. * @day 2020-04-09
  63. */
  64. public function getList()
  65. {
  66. [$page, $limit] = $this->getPage();
  67. return app('json')->success($this->repository->search(0, [], $page, $limit));
  68. }
  69. /**
  70. * 获取单个
  71. * @param int $id
  72. * @return Json
  73. * @throws DataNotFoundException
  74. * @throws DbException
  75. * @throws ModelNotFoundException
  76. * @author 张先生
  77. * @date 2020-03-26
  78. */
  79. public function get( $id)
  80. {
  81. $data = $this->repository->get($id);
  82. return app('json')->success($data);
  83. }
  84. /**
  85. * @param int $id
  86. * @return mixed
  87. * @throws DbException
  88. * @author xaboy
  89. * @day 2020-04-09
  90. */
  91. public function switchStatus($id)
  92. {
  93. $status = $this->request->param('status');
  94. if (!$this->repository->merExists(0, $id))
  95. return app('json')->fail('数据不存在');
  96. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  97. return app('json')->success('编辑成功');
  98. }
  99. /**
  100. * 删除单个
  101. * @param int $id
  102. * @return Json
  103. * @throws Exception
  104. * @author 张先生
  105. * @date 2020-03-30
  106. */
  107. public function delete($id)
  108. {
  109. if (!$this->repository->merExists(0, $id))
  110. return app('json')->fail('数据不存在');
  111. $this->repository->delete($id);
  112. return app('json')->success('删除成功');
  113. }
  114. /**
  115. * 创建
  116. * @param RoleValidate $validate
  117. * @return mixed
  118. * @author 张先生
  119. * @date 2020-03-30
  120. */
  121. public function create(RoleValidate $validate)
  122. {
  123. $data = $this->checkParam($validate);
  124. $data['mer_id'] = 0;
  125. $this->repository->create($data);
  126. return app('json')->success('添加成功');
  127. }
  128. /**
  129. * 更新
  130. * @param int $id id
  131. * @param RoleValidate $validate
  132. * @return mixed
  133. * @throws DbException
  134. * @author 张先生
  135. * @date 2020-03-30
  136. */
  137. public function update($id, RoleValidate $validate)
  138. {
  139. $data = $this->checkParam($validate);
  140. if (!$this->repository->merExists(0, $id))
  141. return app('json')->fail('数据不存在');
  142. $this->repository->update($id, $data);
  143. return app('json')->success('编辑成功');
  144. }
  145. /**
  146. * 添加和修改参数验证
  147. * @param RoleValidate $validate 验证规则
  148. * @return mixed
  149. * @author 张先生
  150. * @date 2020-03-30
  151. */
  152. private function checkParam(RoleValidate $validate)
  153. {
  154. $data = $this->request->params(['role_name', ['rules', []], ['status', 0]]);
  155. $validate->check($data);
  156. return $data;
  157. }
  158. }