Role.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\home\controller;
  15. use app\base\BaseController;
  16. use app\home\model\AdminGroup;
  17. use app\home\validate\GroupCheck;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Role extends BaseController
  22. {
  23. public function index()
  24. {
  25. if (request()->isAjax()) {
  26. $param = get_params();
  27. $where = array();
  28. if (!empty($param['keywords'])) {
  29. $where[] = ['id|title|desc', 'like', '%' . $param['keywords'] . '%'];
  30. }
  31. $list = Db::name('AdminGroup')->where($where)->order('create_time asc')->select();
  32. return to_assign(0, '', $list);
  33. } else {
  34. return view();
  35. }
  36. }
  37. //添加&编辑
  38. public function add()
  39. {
  40. $param = get_params();
  41. if (request()->isAjax()) {
  42. $ruleData = isset($param['rule']) ? $param['rule'] : '';
  43. $layoutData = isset($param['layout']) ? $param['layout'] : '';
  44. $menuData = isset($param['mobile_menu']) ? $param['mobile_menu'] : '';
  45. $barData = isset($param['mobile_bar']) ? $param['mobile_bar'] : '';
  46. if($ruleData==0){
  47. return to_assign(1, '权限节点至少选择一个');
  48. }
  49. if($layoutData==0){
  50. return to_assign(1, '首页展示模块至少选择一个');
  51. }
  52. $param['rules'] = implode(',', $ruleData);
  53. $param['layouts'] = implode(',', $layoutData);
  54. if(empty($menuData)){
  55. $param['mobile_menu'] = '';
  56. }
  57. else{
  58. $param['mobile_menu'] = implode(',', $menuData);
  59. }
  60. if(empty($barData)){
  61. $param['mobile_bar'] = '';
  62. }
  63. else{
  64. $param['mobile_bar'] = implode(',', $barData);
  65. }
  66. if (!empty($param['id']) && $param['id'] > 0) {
  67. try {
  68. validate(GroupCheck::class)->scene('edit')->check($param);
  69. } catch (ValidateException $e) {
  70. // 验证失败 输出错误信息
  71. return to_assign(1, $e->getError());
  72. }
  73. //为了系统安全id为1的系统所有者管理组不允许修改
  74. if ($param['id'] == 1) {
  75. return to_assign(1, '为了系统安全,该管理组不允许修改');
  76. }
  77. Db::name('AdminGroup')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  78. add_log('edit', $param['id'], $param);
  79. } else {
  80. try {
  81. validate(GroupCheck::class)->scene('add')->check($param);
  82. } catch (ValidateException $e) {
  83. // 验证失败 输出错误信息
  84. return to_assign(1, $e->getError());
  85. }
  86. $gid = Db::name('AdminGroup')->strict(false)->field(true)->insertGetId($param);
  87. add_log('add', $gid, $param);
  88. }
  89. //清除菜单\权限缓存
  90. clear_cache('adminMenu');
  91. clear_cache('MobileRules');
  92. return to_assign();
  93. } else {
  94. $id = isset($param['id']) ? $param['id'] : 0;
  95. $rule = admin_rule();
  96. $layouts = get_config('layout');
  97. $mobile_bar = Db::name('MobileBar')->where([['status','=',1]])->field('id,url,title,icon')->select()->toArray();
  98. $mobile_menu = Db::name('MobileTypes')->where(['status'=>1])->select()->toArray();
  99. if ($id > 0) {
  100. $rules = admin_group_info($id);
  101. $role_rule = create_tree_list(0, $rule, $rules);
  102. $role = Db::name('AdminGroup')->where(['id' => $id])->find();
  103. View::assign('role', $role);
  104. $layout_selected = explode(',', $role['layouts']);
  105. foreach ($layouts as $key =>&$vo) {
  106. if (!empty($layout_selected) and in_array($vo['id'], $layout_selected)) {
  107. $vo['checked'] = true;
  108. } else {
  109. $vo['checked'] = false;
  110. }
  111. }
  112. $mobile_bar_selected = explode(',', $role['mobile_bar']);
  113. foreach ($mobile_bar as $key =>&$vo) {
  114. if (!empty($mobile_bar_selected) and in_array($vo['id'], $mobile_bar_selected)) {
  115. $vo['checked'] = true;
  116. } else {
  117. $vo['checked'] = false;
  118. }
  119. }
  120. $mobile_menu_selected = explode(',', $role['mobile_menu']);
  121. foreach ($mobile_menu as &$row) {
  122. $list = Db::name('MobileMenu')->where([['types','=',$row['id']],['status','=',1]])->select()->toArray();
  123. foreach ($list as $key =>&$vo) {
  124. if (!empty($mobile_menu_selected) and in_array($vo['id'], $mobile_menu_selected)) {
  125. $vo['checked'] = true;
  126. } else {
  127. $vo['checked'] = false;
  128. }
  129. }
  130. $row['list'] = $list;
  131. }
  132. } else {
  133. $role_rule = create_tree_list(0, $rule, []);
  134. foreach ($layouts as $key =>&$vo) {
  135. $vo['checked'] = false;
  136. }
  137. foreach ($mobile_bar as $key =>&$vo) {
  138. $vo['checked'] = false;
  139. }
  140. foreach ($mobile_menu as &$row) {
  141. $list = Db::name('MobileMenu')->where([['types','=',$row['id']],['status','=',1]])->select()->toArray();
  142. foreach ($list as $key =>&$vo) {
  143. $vo['checked'] = false;
  144. }
  145. $row['list'] = $list;
  146. }
  147. }
  148. View::assign('role_rule', $role_rule);
  149. View::assign('layout', $layouts);
  150. View::assign('mobile_bar', $mobile_bar);
  151. View::assign('mobile_menu', $mobile_menu);
  152. View::assign('id', $id);
  153. return view();
  154. }
  155. }
  156. //删除
  157. public function delete()
  158. {
  159. if (request()->isDelete()) {
  160. $id = get_params("id");
  161. if ($id == 1) {
  162. return to_assign(1, "该组是系统所有者,无法删除");
  163. }
  164. $count = Db::name('PositionGroup')->where(["group_id" => $id])->count();
  165. if ($count > 0) {
  166. return to_assign(1, "该权限组还在使用,请去除使用者关联再删除");
  167. }
  168. if (Db::name('AdminGroup')->delete($id) !== false) {
  169. add_log('delete', $id, []);
  170. return to_assign(0, "删除权限组成功");
  171. } else {
  172. return to_assign(1, "删除失败");
  173. }
  174. } else {
  175. return to_assign(1, "错误的请求");
  176. }
  177. }
  178. }