3.权限管理器
3.1 权限列表展示功能
1)原型效果
2)接口说明
功能描述: 查询所有权限集合
服务路径: /api/permissions
服务方法:Get
请求参数:无
响应数据格式:
{
"code": 1,
"data": [
{
"id": 1236916745927790556,
"code": "btn-user-delete",
"title": "删除用户权限",
"icon": "",
"perms": "sys:user:delete",
"url": "/api/user",
"method": "DELETE",
"name": "",
"pid": 1236916745927790575,
"orderNum": 100,
"type": 3,
"status": 1,
"createTime": "2020-01-08T07:42:50.000+00:00",
"updateTime": null,
"deleted": 1
},
{
"id": 1473855535827783680,
"code": "",
"title": "测试-01",
"icon": "el-icon-user-solid",
"perms": "",
"url": null,
"method": "",
"name": "test-01",
"pid": 0,
"orderNum": 555,
"type": 1,
"status": 1,
"createTime": "2021-12-23T03:18:36.000+00:00",
"updateTime": "2021-12-23T03:18:36.000+00:00",
"deleted": 1
}
//...............
]
}
代码实现
1. 表现层
@ApiOperation(value = "权限列表展示功能")
@GetMapping("/permissions")
public R<ArrayList<SysPermission>> getPermissions(){
return service.getPermissions();
}
2. 服务层
R<ArrayList<SysPermission>> getPermissions();
@Override
public R<ArrayList<SysPermission>> getPermissions() {
ArrayList<SysPermission> permisson_data = sysPermissionMapper.getPermissions();
return R.ok(permisson_data);
}
3. Dao层
ArrayList permisson_data = sysPermissionMapper.getPermissions();
ArrayList<SysPermission> getPermissions();
<select id="getPermissions" resultType="com.jixu.stock.pojo.entity.SysPermission">
select id, code, title, icon, perms, url, method, name, pid, order_num, type, status, create_time, update_time, deleted
from sys_permission;
</select>
3.3 添加权限时回显权限树
1)原型效果
2)接口说明
功能描述: 添加权限时回显权限树,仅仅显示目录和菜单
服务路径: /api/permissions/tree
服务方法:Get
请求参数:无
响应数据格式:
保证数据顺序;
{
"code": 1,
"data": [
{
"id": "0",
"title": "顶级菜单",
"level": 0
},
{
"id": "1236916745927790564",
"title": "组织管理",
"level": 1
},
{
"id": "1236916745927790560",
"title": "菜单权限管理",
"level": 2
},
{
"id": "1236916745927790575",
"title": "用户管理",
"level": 2
},
{
"id": "1236916745927790578",
"title": "角色管理",
"level": 2
},
{
"id": "1236916745927790569",
"title": "系统管理",
"level": 1
},
{
"id": "1236916745927790558",
"title": "接口管理",
"level": 2
},
{
"id": "1236916745927790571",
"title": "SQL监控",
"level": 2
},
{
"id": "1236916745927790589",
"title": "日志管理",
"level": 2
},
{
"id": "1247842736313339904",
"title": "测试菜单",
"level": 1
},
{
"id": "1247871697634332672",
"title": "测试子菜单",
"level": 2
},
{
"id": "1247877926632951808",
"title": "测试子菜单1",
"level": 2
},
{
"id": "1247880664557162496",
"title": "测试菜单2",
"level": 1
},
{
"id": "1247881176622960640",
"title": "测试子菜单2",
"level": 2
},
{
"id": "1247881904334704640",
"title": "测试子菜单3",
"level": 2
},
{
"id": "1469201551976435712",
"title": "任务管理",
"level": 1
},
{
"id": "1473855535827783680",
"title": "测试-01",
"level": 1
}
]
}
代码实现
1. 表现层
@ApiOperation(value = "添加权限时回显权限树")
@GetMapping("/permissions/tree")
public R<ArrayList<HashMap>> getPermissionsTree(){
return service.getPermissionsTree();
}
2. 服务层
R<ArrayList<HashMap>> getPermissionsTree();
@Override
public R<ArrayList<HashMap>> getPermissionsTree() {
// 获取全部数据
ArrayList<PermissonTreeDomian> permisson_data = sysPermissionMapper.getPermissionTree();
// 组装数据进行递归
HashMap<Long, PermissonTreeDomian> data = new HashMap<>();
for (PermissonTreeDomian permissonDatum : permisson_data) {
Long id = permissonDatum.getId();
HashMap<Long, PermissonTreeDomian> map = new HashMap<>();
map.put(id , permissonDatum);
data.put(id ,permissonDatum);
}
// 进行递归
ArrayList<HashMap> treeList = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
map.put("id",0);
map.put("title","顶级菜单");
map.put("level",0);
treeList.add(map);
TreeUtil.buildPermissionTree(data, Long.valueOf(0),1,treeList);
return R.ok(treeList);
}
3. Dao层
// 获取全部数据
ArrayList permisson_data = sysPermissionMapper.getPermissionTree();
ArrayList<PermissonTreeDomian> getPermissionTree();
<select id="getPermissionTree" resultType="com.jixu.stock.pojo.domain.PermissonTreeDomian">
select id,title,pid
from sys_permission;
</select>
4. 工具类
public static List<HashMap> buildPermissionTree(Map<Long, PermissonTreeDomian> permissionMap, Long parentId , Integer count,List<HashMap> treeList) {
// 创建列表用于保存数据
// 循环查询是否有符合条件的数据
for (Map.Entry<Long, PermissonTreeDomian> entry : permissionMap.entrySet()) {
PermissonTreeDomian permissionDomain = entry.getValue();
// 如何Id== pid 则获取到了
if (parentId.equals(permissionDomain.getPid())) { // 假设PermissonTreeDomian类中有一个getPid()方法返回父ID
String title = permissionDomain.getTitle();
Long id = permissionDomain.getId();
HashMap<String, Object> map = new HashMap<>();
map.put("id" , id);
map.put("title" , title);
map.put("count" , count);
treeList.add(map);
buildPermissionTree(permissionMap, permissionDomain.getId(),count+1,treeList);
}
}
return treeList;
}
5. 实体类
@Data
public class PermissonTreeDomian {
private Long id;
private Long pid;
private String title;
}