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