角色处理器
2.1 分页查询当前角色信息
1)原型效果
2)接口说明
功能描述: 分页查询当前角色信息
服务路径: /api/roles
服务方法:Post
请求参数格式:
{
"pageNum":1,
"pageSize":10
}
响应数据格式:
{
"code": 1,
"data": {
"totalRows": 10,
"totalPages": 1,
"pageNum": 1,
"pageSize": 10,
"size": 10,
"rows": [
{
"id": 1237258113002901512,
"name": "超级管理员",
"description": "我是超级管理员",
"status": 1,
"createTime": "2020-01-06T15:37:45.000+00:00",
"updateTime": "2021-12-09T23:08:02.000+00:00",
"deleted": 1
},
{
"id": 1237258113002901513,
"name": "标记用户角色测试",
"description": "标记用户角色测试",
"status": 1,
"createTime": "2020-01-08T02:53:35.000+00:00",
"updateTime": "2021-12-28T10:16:21.000+00:00",
"deleted": 1
},
//.........
]
}
}
代码实现
1. 表现层
/**
* 分页查询角色信息
*/
@ApiOperation(value = "分页查询角色信息")
@PostMapping("/roles")
public R<RoleDataRespVo> getRolesData(@RequestBody HashMap<String,Object> pageMsg){
return UserService.getRolesData(pageMsg);
2. 服务层
R<RoleDataRespVo> getRolesData(HashMap<String, Object> pageMsg);
@Override
public R<RoleDataRespVo> getRolesData(HashMap<String, Object> pageMsg) {
// 设置分页参数
Integer pageNum = (Integer) pageMsg.get("pageNum");
Integer pageSize = (Integer) pageMsg.get("pageSize");
PageHelper.startPage(pageNum, pageSize);
// 获取数据
ArrayList<SysRole> allRole = sysRoleMapper.getAllRole();
PageInfo<SysRole> sysRolePageInfo = new PageInfo<>(allRole);
RoleDataRespVo roleDataRespVo = new RoleDataRespVo(sysRolePageInfo);
return R.ok(roleDataRespVo);
}
3. Dao层
ArrayList<SysRole> getAllRole();
<select id="getAllRole" resultType="com.jixu.stock.pojo.entity.SysRole">
select id,name,description
,status,create_time,update_time
,deleted
from sys_role
</select>
2.2 添加角色回显权限选项功能-1
1)原型效果
2)接口说明
功能描述: 树状结构回显权限集合,底层通过递归获取权限数据集合
服务路径: /api/permissions/tree/all
服务方法:GET
请求参数:无
响应格式:
{
"code": 1,
"data": [
{
"id": "1236916745927790564",
"title": "组织管理",
"icon": "el-icon-menu",
"path": "/org",
"name": "org",
"children": [
{
"id": "1236916745927790560",
"title": "菜单权限管理",
"icon": "el-icon-menu",
"path": "/menus",
"name": "menus",
"children": [
{
"id": "1236916745927790563",
"title": "删除菜单权限",
"icon": "",
"path": "/api/permission",
"name": "",
"children": []
},
{
"id": "1236916745927790565",
"title": "查询菜单权限列表权限",
"icon": "",
"path": "/api/permissions",
"name": "",
"children": []
},
{
"id": "1236916745927790577",
"title": "更新菜单权限",
"icon": "",
"path": "/api/permission",
"name": "",
"children": []
},
{
"id": "1236916745927790582",
"title": "新增菜单权限",
"icon": "",
"path": "/api/permission",
"name": "",
"children": []
}
]
},
{
"id": "1236916745927790575",
"title": "用户管理",
"icon": "el-icon-user-solid",
"path": "/user",
"name": "user",
"children": [
{
"id": "1236916745927790556",
"title": "删除用户权限",
"icon": "",
"path": "/api/user",
"name": "",
"children": []
},
{
"id": "1236916745927790561",
"title": "新增用户权限",
"icon": "",
"path": "/api/user",
"name": "",
"children": []
},
{
"id": "1236916745927790568",
"title": "查询用户信息列表权限",
"icon": "",
"path": "/api/users",
"name": "",
"children": []
},
{
"id": "1236916745927790579",
"title": "赋予用户角色权限",
"icon": "",
"path": "/api/user/roles",
"name": "",
"children": []
},
{
"id": "1236916745927790580",
"title": "更新用户信息权限",
"icon": "",
"path": "/api/user",
"name": "",
"children": []
}
]
},
{
"id": "1236916745927790578",
"title": "角色管理",
"icon": "el-icon-user",
"path": "/roles",
"name": "roles",
"children": [
{
"id": "1236916745927790562",
"title": "更新角色权限",
"icon": "",
"path": "/api/role",
"name": "",
"children": []
},
{
"id": "1236916745927790570",
"title": "删除角色权限",
"icon": "",
"path": "/api/role/*",
"name": "",
"children": []
},
{
"id": "1236916745927790572",
"title": "新增角色权限",
"icon": "",
"path": "/api/role",
"name": "",
"children": []
},
{
"id": "1236916745927790574",
"title": "角色详情权限",
"icon": "",
"path": "/api/role/*",
"name": "",
"children": []
},
{
"id": "1236916745927790583",
"title": "查询角色列表权限",
"icon": "",
"path": "/api/roles",
"name": "",
"children": []
}
]
}
]
},
{
//......
}
]
}
代码实现
1. 表现层
@ApiOperation(value = "添加角色回显权限选项")
@GetMapping("/permissions/tree/all")
public R<ArrayList<PermissionDomain>> getRolesPermission(){
return UserService.getRolesPermission();
}
2. 服务层
R<ArrayList<PermissionDomain>> getRolesPermission();
@Override
public R<ArrayList<PermissionDomain>> getRolesPermission() {
// 获取所有的权限信息
List<HashMap<String ,Object>> allPermission = sysPermissionMapper.getAllPermission();
// 制作映射集合
HashMap<Long, PermissionDomain> permissionMap = new HashMap<>();
// 获取树的父节点
ArrayList<PermissionDomain> permissionParentList = new ArrayList<>();
for (HashMap<String, Object> map : allPermission) {
PermissionDomain permissionDomain = new PermissionDomain();
permissionDomain.setId((Long) map.get("id"));
permissionDomain.setPid((Long) map.get("pid"));
permissionDomain.setIcon((String) map.get("icon"));
permissionDomain.setPath((String) map.get("url"));
permissionDomain.setName((String) map.get("name"));
permissionDomain.setTitle((String) map.get("title"));
permissionDomain.setChildren(new ArrayList<>());
permissionDomain.setChildren(new ArrayList<>());
Long id = (Long) map.get("id");
Long pid = (Long) map.get("pid");
permissionMap.put(id,permissionDomain);
if (pid.equals(Long.valueOf(0))){
permissionParentList.add(permissionDomain);
}
}
List<PermissionDomain> treeData = buildTree(permissionMap, Long.valueOf(0));
/**
* 迭代方式
*/
// // 用于快速查找子菜单的映射
// HashMap<Long, ArrayList<PermissionDomain>> mapList = new HashMap<>();
// /**
// * 在这里循环所有对象 , 将每个对象的pid( pid 指向于 id )也就是说一个id对应多个pid
// * 将所有pid 指向的对象 , 封装到一个列表 , 则该列表就是它的父id所对应的列表
// */
// for (HashMap<String, Object> map : allPermission) {
// Long pid = (Long) map.get("pid");
// Long id = (Long) map.get("id");
//
// // 获取主键对象
// PermissionDomain permissionObject = permissionMap.get(id);
//
// if (permissionObject != null){
// mapList.computeIfAbsent(pid , k -> new ArrayList<>()).add(permissionObject);
// }
// }
//
// for (PermissionDomain parentPermission : permissionParentList) {
// Long parentId = Long.valueOf(parentPermission.getId());
// ArrayList<PermissionDomain> childrenList = new ArrayList<>();
// // 如果父菜单包含在内
// if (mapList.containsKey(parentId)){
// // 获取父菜单所对应的子菜单
// ArrayList<PermissionDomain> permissinSonList = mapList.get(parentId);
// // 循环所有子菜单
// for (PermissionDomain permissionSon : permissinSonList) {
// Long id = permissionSon.getId();
// // 判断子菜单是否还含有下一级菜单
// ArrayList<PermissionDomain> orDefault = mapList.getOrDefault(id, new ArrayList<>());
// permissionSon.setChildren(orDefault);
//
// }
// childrenList.addAll(permissinSonList);
// }
// parentPermission.setChildren(childrenList);
// }
//
return R.ok(new ArrayList<>(treeData));
}
3. Dao层
List<HashMap<String, Object>> getAllPermission();
<select id="getAllPermission" resultType="java.util.HashMap">
select id,
code,
title,
icon,
url,
name,
pid
from sys_permission;
</select>