Sfoglia il codice sorgente

菜单&数据权限新增(展开/折叠 全选/全不选 父子联动)

RuoYi 5 anni fa
parent
commit
8acb322d49

+ 28 - 0
ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysRole.java

@@ -37,6 +37,12 @@ public class SysRole extends BaseEntity
37 37
     @Excel(name = "数据范围", readConverterExp = "1=所有数据权限,2=自定义数据权限,3=本部门数据权限,4=本部门及以下数据权限")
38 38
     private String dataScope;
39 39
 
40
+    /** 菜单树选择项是否关联显示( 0:父子不互相关联显示 1:父子互相关联显示) */
41
+    private boolean menuCheckStrictly;
42
+
43
+    /** 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示 ) */
44
+    private boolean deptCheckStrictly;
45
+
40 46
     /** 角色状态(0正常 1停用) */
41 47
     @Excel(name = "角色状态", readConverterExp = "0=正常,1=停用")
42 48
     private String status;
@@ -128,6 +134,26 @@ public class SysRole extends BaseEntity
128 134
         this.dataScope = dataScope;
129 135
     }
130 136
 
137
+    public boolean isMenuCheckStrictly()
138
+    {
139
+        return menuCheckStrictly;
140
+    }
141
+
142
+    public void setMenuCheckStrictly(boolean menuCheckStrictly)
143
+    {
144
+        this.menuCheckStrictly = menuCheckStrictly;
145
+    }
146
+
147
+    public boolean isDeptCheckStrictly()
148
+    {
149
+        return deptCheckStrictly;
150
+    }
151
+
152
+    public void setDeptCheckStrictly(boolean deptCheckStrictly)
153
+    {
154
+        this.deptCheckStrictly = deptCheckStrictly;
155
+    }
156
+
131 157
     public String getStatus()
132 158
     {
133 159
         return status;
@@ -185,6 +211,8 @@ public class SysRole extends BaseEntity
185 211
             .append("roleKey", getRoleKey())
186 212
             .append("roleSort", getRoleSort())
187 213
             .append("dataScope", getDataScope())
214
+            .append("menuCheckStrictly", isMenuCheckStrictly())
215
+            .append("deptCheckStrictly", isDeptCheckStrictly())
188 216
             .append("status", getStatus())
189 217
             .append("delFlag", getDelFlag())
190 218
             .append("createBy", getCreateBy())

+ 2 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysDeptMapper.java

@@ -25,9 +25,10 @@ public interface SysDeptMapper
25 25
      * 根据角色ID查询部门树信息
26 26
      * 
27 27
      * @param roleId 角色ID
28
+     * @param deptCheckStrictly 部门树选择项是否关联显示
28 29
      * @return 选中部门列表
29 30
      */
30
-    public List<Integer> selectDeptListByRoleId(Long roleId);
31
+    public List<Integer> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);
31 32
 
32 33
     /**
33 34
      * 根据部门ID查询信息

+ 2 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysMenuMapper.java

@@ -63,9 +63,10 @@ public interface SysMenuMapper
63 63
      * 根据角色ID查询菜单树信息
64 64
      * 
65 65
      * @param roleId 角色ID
66
+     * @param menuCheckStrictly 菜单树选择项是否关联显示
66 67
      * @return 选中菜单列表
67 68
      */
68
-    public List<Integer> selectMenuListByRoleId(Long roleId);
69
+    public List<Integer> selectMenuListByRoleId(@Param("roleId") Long roleId, @Param("menuCheckStrictly") boolean menuCheckStrictly);
69 70
 
70 71
     /**
71 72
      * 根据菜单ID查询信息

+ 7 - 3
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java

@@ -4,17 +4,17 @@ import java.util.ArrayList;
4 4
 import java.util.Iterator;
5 5
 import java.util.List;
6 6
 import java.util.stream.Collectors;
7
-
8 7
 import org.springframework.beans.factory.annotation.Autowired;
9 8
 import org.springframework.stereotype.Service;
10
-
11 9
 import com.ruoyi.common.core.constant.UserConstants;
12 10
 import com.ruoyi.common.core.exception.CustomException;
13 11
 import com.ruoyi.common.core.utils.StringUtils;
14 12
 import com.ruoyi.common.datascope.annotation.DataScope;
15 13
 import com.ruoyi.system.api.domain.SysDept;
14
+import com.ruoyi.system.api.domain.SysRole;
16 15
 import com.ruoyi.system.domain.vo.TreeSelect;
17 16
 import com.ruoyi.system.mapper.SysDeptMapper;
17
+import com.ruoyi.system.mapper.SysRoleMapper;
18 18
 import com.ruoyi.system.service.ISysDeptService;
19 19
 
20 20
 /**
@@ -28,6 +28,9 @@ public class SysDeptServiceImpl implements ISysDeptService
28 28
     @Autowired
29 29
     private SysDeptMapper deptMapper;
30 30
 
31
+    @Autowired
32
+    private SysRoleMapper roleMapper;
33
+
31 34
     /**
32 35
      * 查询部门管理数据
33 36
      * 
@@ -95,7 +98,8 @@ public class SysDeptServiceImpl implements ISysDeptService
95 98
     @Override
96 99
     public List<Integer> selectDeptListByRoleId(Long roleId)
97 100
     {
98
-        return deptMapper.selectDeptListByRoleId(roleId);
101
+        SysRole role = roleMapper.selectRoleById(roleId);
102
+        return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
99 103
     }
100 104
 
101 105
     /**

+ 7 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysMenuServiceImpl.java

@@ -13,12 +13,14 @@ import org.springframework.stereotype.Service;
13 13
 import com.ruoyi.common.core.constant.UserConstants;
14 14
 import com.ruoyi.common.core.utils.StringUtils;
15 15
 import com.ruoyi.common.security.utils.SecurityUtils;
16
+import com.ruoyi.system.api.domain.SysRole;
16 17
 import com.ruoyi.system.api.domain.SysUser;
17 18
 import com.ruoyi.system.domain.SysMenu;
18 19
 import com.ruoyi.system.domain.vo.MetaVo;
19 20
 import com.ruoyi.system.domain.vo.RouterVo;
20 21
 import com.ruoyi.system.domain.vo.TreeSelect;
21 22
 import com.ruoyi.system.mapper.SysMenuMapper;
23
+import com.ruoyi.system.mapper.SysRoleMapper;
22 24
 import com.ruoyi.system.mapper.SysRoleMenuMapper;
23 25
 import com.ruoyi.system.service.ISysMenuService;
24 26
 
@@ -36,6 +38,9 @@ public class SysMenuServiceImpl implements ISysMenuService
36 38
     private SysMenuMapper menuMapper;
37 39
 
38 40
     @Autowired
41
+    private SysRoleMapper roleMapper;
42
+
43
+    @Autowired
39 44
     private SysRoleMenuMapper roleMenuMapper;
40 45
 
41 46
     /**
@@ -124,7 +129,8 @@ public class SysMenuServiceImpl implements ISysMenuService
124 129
     @Override
125 130
     public List<Integer> selectMenuListByRoleId(Long roleId)
126 131
     {
127
-        return menuMapper.selectMenuListByRoleId(roleId);
132
+        SysRole role = roleMapper.selectRoleById(roleId);
133
+        return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
128 134
     }
129 135
 
130 136
     /**

+ 5 - 3
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysDeptMapper.xml

@@ -44,12 +44,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
44 44
 		order by d.parent_id, d.order_num
45 45
     </select>
46 46
     
47
-    <select id="selectDeptListByRoleId" parameterType="Long" resultType="Integer">
48
-		select d.dept_id, d.parent_id
47
+    <select id="selectDeptListByRoleId" resultType="Integer">
48
+		select d.dept_id
49 49
 		from sys_dept d
50 50
             left join sys_role_dept rd on d.dept_id = rd.dept_id
51 51
         where rd.role_id = #{roleId}
52
-        	and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
52
+            <if test="deptCheckStrictly">
53
+              and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
54
+            </if>
53 55
 		order by d.parent_id, d.order_num
54 56
 	</select>
55 57
     

+ 5 - 3
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysMenuMapper.xml

@@ -82,12 +82,14 @@
82 82
 		order by m.parent_id, m.order_num
83 83
 	</select>
84 84
 	
85
-	<select id="selectMenuListByRoleId" parameterType="Long" resultType="Integer">
86
-		select m.menu_id, m.parent_id
85
+	<select id="selectMenuListByRoleId" resultType="Integer">
86
+		select m.menu_id
87 87
 		from sys_menu m
88 88
             left join sys_role_menu rm on m.menu_id = rm.menu_id
89 89
         where rm.role_id = #{roleId}
90
-        	and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = #{roleId})
90
+            <if test="menuCheckStrictly">
91
+              and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = #{roleId})
92
+            </if>
91 93
 		order by m.parent_id, m.order_num
92 94
 	</select>
93 95
 	

+ 21 - 13
ruoyi-modules/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml

@@ -5,22 +5,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
5 5
 <mapper namespace="com.ruoyi.system.mapper.SysRoleMapper">
6 6
 
7 7
 	<resultMap type="SysRole" id="SysRoleResult">
8
-		<id     property="roleId"       column="role_id"        />
9
-		<result property="roleName"     column="role_name"      />
10
-		<result property="roleKey"      column="role_key"       />
11
-		<result property="roleSort"     column="role_sort"      />
12
-		<result property="dataScope"    column="data_scope"     />
13
-		<result property="status"       column="status"         />
14
-		<result property="delFlag"      column="del_flag"       />
15
-		<result property="createBy"     column="create_by"      />
16
-		<result property="createTime"   column="create_time"    />
17
-		<result property="updateBy"     column="update_by"      />
18
-		<result property="updateTime"   column="update_time"    />
19
-		<result property="remark"       column="remark"         />
8
+		<id     property="roleId"             column="role_id"               />
9
+		<result property="roleName"           column="role_name"             />
10
+		<result property="roleKey"            column="role_key"              />
11
+		<result property="roleSort"           column="role_sort"             />
12
+		<result property="dataScope"          column="data_scope"            />
13
+		<result property="menuCheckStrictly"  column="menu_check_strictly"   />
14
+		<result property="deptCheckStrictly"  column="dept_check_strictly"   />
15
+		<result property="status"             column="status"                />
16
+		<result property="delFlag"            column="del_flag"              />
17
+		<result property="createBy"           column="create_by"             />
18
+		<result property="createTime"         column="create_time"           />
19
+		<result property="updateBy"           column="update_by"             />
20
+		<result property="updateTime"         column="update_time"           />
21
+		<result property="remark"             column="remark"                />
20 22
 	</resultMap>
21 23
 	
22 24
 	<sql id="selectRoleVo">
23
-	    select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope,
25
+	    select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
24 26
             r.status, r.del_flag, r.create_time, r.remark 
25 27
         from sys_role r
26 28
 	        left join sys_user_role ur on ur.role_id = r.role_id
@@ -95,6 +97,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
95 97
  			<if test="roleKey != null and roleKey != ''">role_key,</if>
96 98
  			<if test="roleSort != null and roleSort != ''">role_sort,</if>
97 99
  			<if test="dataScope != null and dataScope != ''">data_scope,</if>
100
+            <if test="menuCheckStrictly != null">menu_check_strictly,</if>
101
+ 			<if test="deptCheckStrictly != null">dept_check_strictly,</if>
98 102
  			<if test="status != null and status != ''">status,</if>
99 103
  			<if test="remark != null and remark != ''">remark,</if>
100 104
  			<if test="createBy != null and createBy != ''">create_by,</if>
@@ -105,6 +109,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
105 109
  			<if test="roleKey != null and roleKey != ''">#{roleKey},</if>
106 110
  			<if test="roleSort != null and roleSort != ''">#{roleSort},</if>
107 111
  			<if test="dataScope != null and dataScope != ''">#{dataScope},</if>
112
+            <if test="menuCheckStrictly != null">#{menuCheckStrictly},</if>
113
+ 			<if test="deptCheckStrictly != null">#{deptCheckStrictly},</if>
108 114
  			<if test="status != null and status != ''">#{status},</if>
109 115
  			<if test="remark != null and remark != ''">#{remark},</if>
110 116
  			<if test="createBy != null and createBy != ''">#{createBy},</if>
@@ -119,6 +125,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
119 125
  			<if test="roleKey != null and roleKey != ''">role_key = #{roleKey},</if>
120 126
  			<if test="roleSort != null and roleSort != ''">role_sort = #{roleSort},</if>
121 127
  			<if test="dataScope != null and dataScope != ''">data_scope = #{dataScope},</if>
128
+            <if test="menuCheckStrictly != null">menu_check_strictly = #{menuCheckStrictly},</if>
129
+ 			<if test="deptCheckStrictly != null">dept_check_strictly = #{deptCheckStrictly},</if>
122 130
  			<if test="status != null and status != ''">status = #{status},</if>
123 131
  			<if test="remark != null">remark = #{remark},</if>
124 132
  			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>

+ 50 - 0
ruoyi-ui/src/views/system/role/index.vue

@@ -175,11 +175,16 @@
175 175
           </el-radio-group>
176 176
         </el-form-item>
177 177
         <el-form-item label="菜单权限">
178
+          <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
179
+          <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
180
+          <el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
178 181
           <el-tree
182
+            class="tree-border"
179 183
             :data="menuOptions"
180 184
             show-checkbox
181 185
             ref="menu"
182 186
             node-key="id"
187
+            :check-strictly="!form.menuCheckStrictly"
183 188
             empty-text="加载中,请稍后"
184 189
             :props="defaultProps"
185 190
           ></el-tree>
@@ -214,12 +219,17 @@
214 219
           </el-select>
215 220
         </el-form-item>
216 221
         <el-form-item label="数据权限" v-show="form.dataScope == 2">
222
+          <el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
223
+          <el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
224
+          <el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
217 225
           <el-tree
226
+            class="tree-border"
218 227
             :data="deptOptions"
219 228
             show-checkbox
220 229
             default-expand-all
221 230
             ref="dept"
222 231
             node-key="id"
232
+            :check-strictly="!form.deptCheckStrictly"
223 233
             empty-text="加载中,请稍后"
224 234
             :props="defaultProps"
225 235
           ></el-tree>
@@ -262,6 +272,10 @@ export default {
262 272
       open: false,
263 273
       // 是否显示弹出层(数据权限)
264 274
       openDataScope: false,
275
+	  menuExpand: false,
276
+      menuNodeAll: false,
277
+      deptExpand: true,
278
+      deptNodeAll: false,
265 279
       // 日期范围
266 280
       dateRange: [],
267 281
       // 状态数据字典
@@ -413,6 +427,10 @@ export default {
413 427
       if (this.$refs.menu != undefined) {
414 428
         this.$refs.menu.setCheckedKeys([]);
415 429
       }
430
+	  this.menuExpand = false,
431
+      this.menuNodeAll = false,
432
+      this.deptExpand = true,
433
+      this.deptNodeAll = false,
416 434
       this.form = {
417 435
         roleId: undefined,
418 436
         roleName: undefined,
@@ -421,6 +439,8 @@ export default {
421 439
         status: "0",
422 440
         menuIds: [],
423 441
         deptIds: [],
442
+		menuCheckStrictly: true,
443
+		deptCheckStrictly: true,
424 444
         remark: undefined
425 445
       };
426 446
       this.resetForm("form");
@@ -442,6 +462,36 @@ export default {
442 462
       this.single = selection.length!=1
443 463
       this.multiple = !selection.length
444 464
     },
465
+	// 树权限(展开/折叠)
466
+    handleCheckedTreeExpand(value, type) {
467
+      if (type == 'menu') {
468
+        let treeList = this.menuOptions;
469
+        for (let i = 0; i < treeList.length; i++) {
470
+          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
471
+        }
472
+      } else if (type == 'dept') {
473
+        let treeList = this.deptOptions;
474
+        for (let i = 0; i < treeList.length; i++) {
475
+          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
476
+        }
477
+      }
478
+    },
479
+    // 树权限(全选/全不选)
480
+    handleCheckedTreeNodeAll(value, type) {
481
+      if (type == 'menu') {
482
+        this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
483
+      } else if (type == 'dept') {
484
+        this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
485
+      }
486
+    },
487
+    // 树权限(父子联动)
488
+    handleCheckedTreeConnect(value, type) {
489
+      if (type == 'menu') {
490
+        this.form.menuCheckStrictly = value ? true: false;
491
+      } else if (type == 'dept') {
492
+        this.form.deptCheckStrictly = value ? true: false;
493
+      }
494
+    },
445 495
     /** 新增按钮操作 */
446 496
     handleAdd() {
447 497
       this.reset();

+ 1 - 1
ruoyi-ui/src/views/system/user/index.vue

@@ -209,7 +209,7 @@
209 209
           </el-col>
210 210
           <el-col :span="12">
211 211
             <el-form-item label="归属部门" prop="deptId">
212
-              <treeselect v-model="form.deptId" :options="deptOptions" :disable-branch-nodes="true" :show-count="true" placeholder="请选择归属部门" />
212
+              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
213 213
             </el-form-item>
214 214
           </el-col>
215 215
         </el-row>

+ 16 - 14
sql/ry_20200901.sql

@@ -102,26 +102,28 @@ insert into sys_post values(4, 'user', '普通员工',  4, '0', 'admin', '2018-0
102 102
 -- ----------------------------
103 103
 drop table if exists sys_role;
104 104
 create table sys_role (
105
-  role_id           bigint(20)      not null auto_increment    comment '角色ID',
106
-  role_name         varchar(30)     not null                   comment '角色名称',
107
-  role_key          varchar(100)    not null                   comment '角色权限字符串',
108
-  role_sort         int(4)          not null                   comment '显示顺序',
109
-  data_scope        char(1)         default '1'                comment '数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限)',
110
-  status            char(1)         not null                   comment '角色状态(0正常 1停用)',
111
-  del_flag          char(1)         default '0'                comment '删除标志(0代表存在 2代表删除)',
112
-  create_by         varchar(64)     default ''                 comment '创建者',
113
-  create_time       datetime                                   comment '创建时间',
114
-  update_by         varchar(64)     default ''                 comment '更新者',
115
-  update_time       datetime                                   comment '更新时间',
116
-  remark            varchar(500)    default null               comment '备注',
105
+  role_id              bigint(20)      not null auto_increment    comment '角色ID',
106
+  role_name            varchar(30)     not null                   comment '角色名称',
107
+  role_key             varchar(100)    not null                   comment '角色权限字符串',
108
+  role_sort            int(4)          not null                   comment '显示顺序',
109
+  data_scope           char(1)         default '1'                comment '数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限)',
110
+  menu_check_strictly  tinyint(1)      default 1                  comment '菜单树选择项是否关联显示',
111
+  dept_check_strictly  tinyint(1)      default 1                  comment '部门树选择项是否关联显示',
112
+  status               char(1)         not null                   comment '角色状态(0正常 1停用)',
113
+  del_flag             char(1)         default '0'                comment '删除标志(0代表存在 2代表删除)',
114
+  create_by            varchar(64)     default ''                 comment '创建者',
115
+  create_time          datetime                                   comment '创建时间',
116
+  update_by            varchar(64)     default ''                 comment '更新者',
117
+  update_time          datetime                                   comment '更新时间',
118
+  remark               varchar(500)    default null               comment '备注',
117 119
   primary key (role_id)
118 120
 ) engine=innodb auto_increment=100 comment = '角色信息表';
119 121
 
120 122
 -- ----------------------------
121 123
 -- 初始化-角色信息表数据
122 124
 -- ----------------------------
123
-insert into sys_role values('1', '超级管理员',  'admin',  1, 1, '0', '0', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '超级管理员');
124
-insert into sys_role values('2', '普通角色',    'common', 2, 2, '0', '0', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '普通角色');
125
+insert into sys_role values('1', '超级管理员',  'admin',  1, 1, 1, 1, '0', '0', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '超级管理员');
126
+insert into sys_role values('2', '普通角色',    'common', 2, 2, 1, 1, '0', '0', 'admin', '2018-03-16 11-33-00', 'ry', '2018-03-16 11-33-00', '普通角色');
125 127
 
126 128
 
127 129
 -- ----------------------------