Role.php 4.2 KB

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