Python教程

layuimini父子菜单-python实现

本文主要是介绍layuimini父子菜单-python实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

现在项目使用了laymini的后台框架,非常不错,美中不足,没有构建菜单的python示例,遂自己写了一个,供大家参考:

数据库表结构

CREATE TABLE `system_menu` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `pid` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '父ID',
  `title` varchar(100) NOT NULL DEFAULT '' COMMENT '名称',
  `icon` varchar(100) NOT NULL DEFAULT '' COMMENT '菜单图标',
  `href` varchar(100) NOT NULL DEFAULT '' COMMENT '链接',
  `target` varchar(20) NOT NULL DEFAULT '_self' COMMENT '链接打开方式',
  `sort` int(11) DEFAULT '0' COMMENT '菜单排序',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态(0:禁用,1:启用)',
  `remark` varchar(255) DEFAULT NULL COMMENT '备注信息',
  `create_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
  `update_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
  `delete_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`),
  KEY `title` (`title`),
  KEY `href` (`href`)
) ENGINE=InnoDB AUTO_INCREMENT=250 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='系统菜单表';

python 示例

from flask import jsonify, request, Blueprint, json, make_response
from flask_login import login_required, current_user,login_user,logout_user,utils

from app import common
from app import loginManager

from app.models.systemmenu import SystemMenu

def init_api(app):

    @app.route('/xxx/api/v1.0/menus', methods=['GET']) 
    def getMenu():
        systemMenu = SystemMenu()
        menuList = systemMenu.getByStatus(1)

        if menuList:
            homeinfo={"title":"首页","href":"page/welcome-1.html?t=1"}
            logoinfo={"title":"LAYUI MINI","image":"images/logo.png","href": ""} 

            menuList = buildMenuChild(0, menuList)

            # 合成菜单
            menuAllList = dict()
            menuAllList["homeInfo"] = homeinfo
            menuAllList["logoInfo"] = logoinfo
            menuAllList["menuInfo"] = menuList

            result = make_response(jsonify(menuAllList))
        else:
            result = None

        return result

    # 递归创建菜单列表
    def buildMenuChild(pid,menulist):
        treeList = dict()
        node = []
        for item in menulist:
            if (item.pid == pid):
                nodetmp = {"title":item.title,"icon":item.icon,"href":item.href,"target":item.target}
                child = buildMenuChild(item.id,menulist)
                if (child != {}):
                    nodetmp.update({"child":child})

                node.append(nodetmp)
                    
                treeList = node
                
        return treeList
            

 

这篇关于layuimini父子菜单-python实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!