• 递归生成后台菜单数据


    先从数据库中查询出所有有效的菜单数据

    [
        {
            "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
            }]
        }]
    }]
  • 相关阅读:
    Java_Habse_add
    Java_Habse_shell
    android Studio 出现:Unable to resolve dependency for ':app@debug/compileClasspath'
    微信小程序云函数中有以下未安装的依赖,如果未安装即全量上传
    Bittorrent Protocol Specification v1.0 中文
    BT客户端实现 Peer协议设计
    NGINX 配置 SSL 双向认证
    openssl、x509、crt、cer、key、csr、ssl、tls 这些都是什么鬼?
    ssl双向认证和单向认证原理
    网络服务器之HTTPS服务
  • 原文地址:https://www.cnblogs.com/root0/p/15132607.html
Copyright © 2020-2023  润新知