先从数据库中查询出所有有效的菜单数据
[
{
"permission_id": 6,
"title": "首页",
"url": "/",
"icon": "el-icon-s-home",
"weight": 100,
"is_menu": 1,
"parent_id": null
},
{
"permission_id": 29,
"title": "当日用户留存",
"url": "/liucun",
"icon": "",
"weight": 100,
"is_menu": 1,
"parent_id": 28
},
{
"permission_id": 2,
"title": "权限管理",
"url": "/author",
"icon": "el-icon-s-home",
"weight": 100,
"is_menu": 1,
"parent_id": 1
},
{
"permission_id": 28,
"title": "BI数据列表",
"url": "/bi",
"icon": "el-icon-s-home",
"weight": 90,
"is_menu": 1,
"parent_id": null
},
{
"permission_id": 4,
"title": "角色管理",
"url": "/group",
"icon": "el-icon-s-home",
"weight": 90,
"is_menu": 1,
"parent_id": 1
},
{
"permission_id": 25,
"title": "测试三级菜单",
"url": "/test",
"icon": "",
"weight": 10,
"is_menu": 1,
"parent_id": 2
},
{
"permission_id": 9,
"title": "添加角色",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 4
},
{
"permission_id": 10,
"title": "修改角色",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 4
},
{
"permission_id": 11,
"title": "删除角色",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 4
},
{
"permission_id": 12,
"title": "添加管理员",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 5
},
{
"permission_id": 13,
"title": "修改管理员",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 5
},
{
"permission_id": 14,
"title": "删除管理员",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 5
},
{
"permission_id": 1,
"title": "系统管理",
"url": "system",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 1,
"parent_id": null
},
{
"permission_id": 15,
"title": "添加顶级节点",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 2
},
{
"permission_id": 16,
"title": "添加子节点",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 2
},
{
"permission_id": 17,
"title": "编辑节点",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 2
},
{
"permission_id": 5,
"title": "管理员管理",
"url": "/admin",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 1,
"parent_id": 1
},
{
"permission_id": 18,
"title": "删除节点",
"url": "",
"icon": "el-icon-s-home",
"weight": 10,
"is_menu": 0,
"parent_id": 2
}
]
递归函数
with open('data.json', mode='r', encoding='utf-8') as f:
permissions = json.load(f)
def create_tree_list(pid, permission_list):
tree_list = []
for permission in permission_list:
if permission.get('parent_id') == pid:
children = create_tree_list(permission.get('permission_id'), permission_list)
if children:
permission['children'] = children
tree_list.append(permission)
return tree_list
print(create_tree_list(None, permissions))
得到的数据结果
[{
'permission_id': 6,
'title': '首页',
'url': '/',
'icon': 'el-icon-s-home',
'weight': 100,
'is_menu': 1,
'parent_id': None
}, {
'permission_id': 28,
'title': 'BI数据列表',
'url': '/bi',
'icon': 'el-icon-s-home',
'weight': 90,
'is_menu': 1,
'parent_id': None,
'children': [{
'permission_id': 29,
'title': '当日用户留存',
'url': '/liucun',
'icon': '',
'weight': 100,
'is_menu': 1,
'parent_id': 28
}]
}, {
'permission_id': 1,
'title': '系统管理',
'url': 'system',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 1,
'parent_id': None,
'children': [{
'permission_id': 2,
'title': '权限管理',
'url': '/author',
'icon': 'el-icon-s-home',
'weight': 100,
'is_menu': 1,
'parent_id': 1,
'children': [{
'permission_id': 25,
'title': '测试三级菜单',
'url': '/test',
'icon': '',
'weight': 10,
'is_menu': 1,
'parent_id': 2
}, {
'permission_id': 15,
'title': '添加顶级节点',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 2
}, {
'permission_id': 16,
'title': '添加子节点',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 2
}, {
'permission_id': 17,
'title': '编辑节点',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 2
}, {
'permission_id': 18,
'title': '删除节点',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 2
}]
}, {
'permission_id': 4,
'title': '角色管理',
'url': '/group',
'icon': 'el-icon-s-home',
'weight': 90,
'is_menu': 1,
'parent_id': 1,
'children': [{
'permission_id': 9,
'title': '添加角色',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 4
}, {
'permission_id': 10,
'title': '修改角色',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 4
}, {
'permission_id': 11,
'title': '删除角色',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 4
}]
}, {
'permission_id': 5,
'title': '管理员管理',
'url': '/admin',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 1,
'parent_id': 1,
'children': [{
'permission_id': 12,
'title': '添加管理员',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 5
}, {
'permission_id': 13,
'title': '修改管理员',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 5
}, {
'permission_id': 14,
'title': '删除管理员',
'url': '',
'icon': 'el-icon-s-home',
'weight': 10,
'is_menu': 0,
'parent_id': 5
}]
}]
}]