Role.php 3.7 KB

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