Role.php 4.4 KB

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