Role.php 4.1 KB

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