SystemRoles.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/21
  6. * @time: 17:39
  7. */
  8. namespace app\controller\admin\system;
  9. use app\common\AdminBaseController;
  10. use app\Request;
  11. use app\services\system\admin\SystemMenusServices;
  12. use app\services\system\admin\SystemRoleServices;
  13. use qiniu\services\CacheService;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use think\exception\ValidateException;
  18. class SystemRoles extends AdminBaseController
  19. {
  20. public function __construct(Request $request, SystemRoleServices $service)
  21. {
  22. parent::__construct($request);
  23. $this->service = $service;
  24. $this->request->filter(['addslashes', 'trim']);
  25. $this->createParams = [
  26. ['role_name', ''],
  27. ['status', 0],
  28. ['checked_menus', []]
  29. ];
  30. $this->saveDeal = $this->updateDeal = function (&$data) {
  31. if (!$data['role_name']) throw new ValidateException('请输入身份名称');
  32. if (!is_array($data['checked_menus']) || !count($data['checked_menus']))
  33. throw new ValidateException('请选择最少一个权限');
  34. $data['rules'] = implode(',', $data['checked_menus']);
  35. };
  36. $this->searchable = [
  37. ['status', ''],
  38. ['role_name', ''],
  39. ];
  40. }
  41. public function index()
  42. {
  43. $where = $this->request->getMore($this->searchable);
  44. $where['type'] = 0;
  45. $where['level'] = $this->adminInfo['level'] + 1;
  46. return $this->success($this->service->getRoleList($where));
  47. }
  48. public function read($id)
  49. {
  50. $info = $this->service->get($id);
  51. if (!$info)
  52. return $this->error('数据不存在');
  53. /** @var SystemMenusServices $services */
  54. $services = app()->make(SystemMenusServices::class);
  55. $info['menus'] = $services->getMenus($this->adminInfo['level'] == 0 ? [] : $this->adminInfo['roles'], 1, 0);
  56. return $this->success('ok', $info->toArray());
  57. }
  58. public function save()
  59. {
  60. $data = $this->request->postMore($this->createParams);
  61. $data['level'] = $this->adminInfo['level'] + 1;
  62. $res = $this->service->create($data);
  63. if ($res) return $this->success('添加成功');
  64. return $this->error('添加失败');
  65. }
  66. /**
  67. * 修改状态
  68. * @param $id
  69. * @param $status
  70. * @return mixed
  71. * @throws DataNotFoundException
  72. * @throws DbException
  73. * @throws ModelNotFoundException
  74. */
  75. public function setStatus($id, $status)
  76. {
  77. if (!$id) {
  78. return $this->error('缺少参数');
  79. }
  80. $role = $this->service->get($id);
  81. if (!$role) {
  82. return $this->error('没有查到此身份');
  83. }
  84. $role->status = $status;
  85. if ($role->save()) {
  86. CacheService::clear();
  87. return $this->success('修改成功');
  88. } else {
  89. return $this->error('修改失败');
  90. }
  91. }
  92. }