Role.php 4.8 KB

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