Role.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\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. * 创建表单
  30. *
  31. * @return \Illuminate\Http\JsonResponse
  32. */
  33. public function createForm()
  34. {
  35. // 获取商家信息
  36. $merchant = $this->request->merchant();
  37. // 调用 formToData 方法将表单转换为数据并返回成功响应
  38. return app('json')->success(formToData($this->repository->form((int)$merchant->type_id)));
  39. }
  40. /**
  41. * 更新表单
  42. *
  43. * @param int $id 表单 ID
  44. * @return \Illuminate\Http\JsonResponse
  45. */
  46. public function updateForm($id)
  47. {
  48. // 判断商家是否存在该表单
  49. if (!$this->repository->merExists($this->request->merId(), $id))
  50. return app('json')->fail('数据不存在');
  51. // 获取商家信息
  52. $merchant = $this->request->merchant();
  53. // 调用 updateForm 方法更新表单并将结果转换为数据并返回成功响应
  54. return app('json')->success(formToData($this->repository->updateForm((int)$merchant->type_id, $id)));
  55. }
  56. /**
  57. * 获取列表
  58. *
  59. * @return \think\response\Json
  60. */
  61. public function getList()
  62. {
  63. // 获取分页参数
  64. [$page, $limit] = $this->getPage();
  65. // 调用 repository 的 search 方法获取数据并返回 JSON 格式的成功响应
  66. return app('json')->success($this->repository->search($this->request->merId(), [], $page, $limit));
  67. }
  68. /**
  69. * 切换状态
  70. *
  71. * @param int $id 商户 ID
  72. * @return \think\response\Json
  73. */
  74. public function switchStatus($id)
  75. {
  76. // 获取状态参数
  77. $status = $this->request->param('status');
  78. // 判断商户是否存在
  79. if (!$this->repository->merExists($this->request->merId(), $id))
  80. return app('json')->fail('数据不存在');
  81. // 更新商户状态
  82. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  83. // 返回 JSON 格式的成功响应
  84. return app('json')->success('编辑成功');
  85. }
  86. /**
  87. * 根据ID删除数据
  88. *
  89. * @param int $id 数据ID
  90. * @return \think\response\Json 返回JSON格式的响应结果
  91. */
  92. public function delete($id)
  93. {
  94. // 判断数据是否存在
  95. if (!$this->repository->merExists($this->request->merId(), $id))
  96. return app('json')->fail('数据不存在');
  97. // 删除数据
  98. $this->repository->delete($id);
  99. // 返回成功响应
  100. return app('json')->success('删除成功');
  101. }
  102. /**
  103. * 创建新数据
  104. *
  105. * @param RoleValidate $validate 验证器实例
  106. * @return \think\response\Json 返回JSON格式的响应结果
  107. */
  108. public function create(RoleValidate $validate)
  109. {
  110. // 获取参数并验证
  111. $data = $this->checkParam($validate);
  112. $data['mer_id'] = $this->request->merId();
  113. // 创建新数据
  114. $this->repository->create($data);
  115. return app('json')->success('添加成功');
  116. }
  117. /**
  118. * 更新角色信息
  119. *
  120. * @param int $id 角色ID
  121. * @param RoleValidate $validate 角色验证器
  122. * @return \think\response\Json 返回JSON格式的响应结果
  123. */
  124. public function update($id, RoleValidate $validate)
  125. {
  126. // 获取参数并进行验证
  127. $data = $this->checkParam($validate);
  128. // 判断角色是否存在
  129. if (!$this->repository->merExists($this->request->merId(), $id))
  130. return app('json')->fail('数据不存在');
  131. // 更新角色信息
  132. $this->repository->update($id, $data);
  133. // 返回成功响应
  134. return app('json')->success('编辑成功');
  135. }
  136. /**
  137. * 检查参数并进行验证
  138. *
  139. * @param RoleValidate $validate 角色验证器
  140. * @return array 返回验证通过的参数数组
  141. */
  142. private function checkParam(RoleValidate $validate)
  143. {
  144. $data = $this->request->params(['role_name', ['rules', []], ['status', 0]]);
  145. // 进行验证
  146. $validate->check($data);
  147. // 返回验证通过的参数数组
  148. return $data;
  149. }
  150. }