SystemRole.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\{SystemMenus, SystemRole as RoleModel};
  5. use crmeb\services\{UtilService as Util, JsonService as Json};
  6. use think\facade\Route as Url;
  7. /**
  8. * 身份管理 控制器
  9. * Class SystemRole
  10. * @package app\admin\controller\setting
  11. */
  12. class SystemRole extends AuthController
  13. {
  14. /**
  15. * 显示资源列表
  16. *
  17. * @return \think\Response
  18. */
  19. public function index()
  20. {
  21. $where = Util::getMore([
  22. ['status', ''],
  23. ['role_name', ''],
  24. ], $this->request);
  25. $where['level'] = $this->adminInfo['level'];
  26. $this->assign('where', $where);
  27. $this->assign(RoleModel::systemPage($where));
  28. return $this->fetch();
  29. }
  30. /**
  31. * 显示创建资源表单页.
  32. *
  33. * @return \think\Response
  34. */
  35. public function create()
  36. {
  37. // if(0 == 0){
  38. // }else{
  39. // dump($this->adminInfo['level']);
  40. // }
  41. $menus = $this->adminInfo['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->adminInfo['roles']);
  42. $this->assign(['menus' => json($menus)->getContent(), 'saveUrl' => Url::buildUrl('save')]);
  43. return $this->fetch();
  44. }
  45. /**
  46. * 保存新建的资源
  47. *
  48. * @param \think\Request $request
  49. * @return \think\Response
  50. */
  51. public function save()
  52. {
  53. $data = Util::postMore([
  54. 'role_name',
  55. ['status', 0],
  56. ['checked_menus', [], '', 'rules']
  57. ]);
  58. if (!$data['role_name']) return Json::fail('请输入身份名称');
  59. if (!is_array($data['rules']) || !count($data['rules']))
  60. return Json::fail('请选择最少一个权限');
  61. foreach ($data['rules'] as $v) {
  62. $pid = SystemMenus::where('id', $v)->value('pid');
  63. if (!in_array($pid, $data['rules'])) $data['rules'][] = $pid;
  64. }
  65. $data['rules'] = implode(',', $data['rules']);
  66. $data['level'] = $this->adminInfo['level'] + 1;
  67. if (!RoleModel::create($data)) return Json::fail('添加身份失败!');
  68. return Json::successful('添加身份成功!');
  69. }
  70. /**
  71. * 显示指定的资源
  72. *
  73. * @param int $id
  74. * @return \think\Response
  75. */
  76. public function read($id)
  77. {
  78. //
  79. }
  80. /**
  81. * 显示编辑资源表单页.
  82. *
  83. * @param int $id
  84. * @return \think\Response
  85. */
  86. public function edit($id)
  87. {
  88. //
  89. $role = RoleModel::get($id);
  90. $menus = $this->adminInfo['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->adminInfo['roles']);
  91. $this->assign(['role' => $role->toJson(), 'menus' => json($menus)->getContent(), 'updateUrl' => Url::buildUrl('update', array('id' => $id))]);
  92. return $this->fetch();
  93. }
  94. /**
  95. * 保存更新的资源
  96. *
  97. * @param \think\Request $request
  98. * @param int $id
  99. * @return \think\Response
  100. */
  101. public function update($id)
  102. {
  103. $data = Util::postMore([
  104. 'role_name',
  105. ['status', 0],
  106. ['checked_menus', [], '', 'rules']
  107. ]);
  108. if (!$data['role_name']) return Json::fail('请输入身份名称');
  109. if (!is_array($data['rules']) || !count($data['rules']))
  110. return Json::fail('请选择最少一个权限');
  111. foreach ($data['rules'] as $v) {
  112. $pid = SystemMenus::where('id', $v)->value('pid');
  113. if (!in_array($pid, $data['rules'])) $data['rules'][] = $pid;
  114. }
  115. $data['rules'] = implode(',', $data['rules']);
  116. if (!RoleModel::edit($data, $id)) return Json::fail('修改失败!');
  117. return Json::successful('修改成功!');
  118. }
  119. /**
  120. * 删除指定资源
  121. *
  122. * @param int $id
  123. * @return \think\Response
  124. */
  125. public function delete($id)
  126. {
  127. if (!RoleModel::del($id))
  128. return Json::fail(RoleModel::getErrorInfo('删除失败,请稍候再试!'));
  129. else
  130. return Json::successful('删除成功!');
  131. }
  132. }