go 循环遍历子菜单

go 循环遍历子菜单

转载请注明来源:https://janrs.com/f597


菜单表结构

CREATE TABLE `permission_menu` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'primary id',
  `parent_id` int(10) DEFAULT 0 COMMENT 'parent id',
  `type` tinyint(2) DEFAULT 2 COMMENT 'menu type[1=dir2=menu3=button]',
  `name` varchar(100) DEFAULT '' COMMENT 'name',
  `icon` varchar(100) DEFAULT '' COMMENT 'icon',
  `sort` int(10) DEFAULT 0 COMMENT 'sort',
  `perm_code` text DEFAULT '' COMMENT 'permission code',
  `path` varchar(100) DEFAULT '' COMMENT 'route path',
  `component` varchar(200) DEFAULT '' COMMENT 'frontend component',
  `redirect` varchar(255) DEFAULT '' COMMENT 'redirect url',
  `meta` text DEFAULT '' COMMENT 'meta info',
  `selected` text DEFAULT '' COMMENT 'select routed',
  `params` varchar(200) DEFAULT '' COMMENT 'route params',
  `is_ext` tinyint(1) DEFAULT 2 COMMENT 'whether it is an external link[1=yes2=no]',
  `is_cache` tinyint(1) DEFAULT 2 COMMENT 'cache or not[1=yes2=no]',
  `is_show` tinyint(1) DEFAULT 2 COMMENT 'whether to display[1=yes2=no]',
  `is_disable` tinyint(1) DEFAULT 2 COMMENT 'disable or not[1=yes2=no]',
  `create_time` int(10) DEFAULT 0 COMMENT 'create time''',
  `update_time` int(10) DEFAULT 0 COMMENT 'update time',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='Menu Table';

基于 gorm 定义结构体

// Menu Table
type Menu struct {
    // primary id
    ID *uint32 `json:"id" gorm:"column:id;primaryKey;type:int(10);unique;autoIncrement;comment:primary id"`
    // parent id
    ParentId *uint32 `json:"parent_id" gorm:"column:parent_id;type:int(10);default:0;comment:parent id"`
    // menu type
    Type *uint8 `json:"type" gorm:"column:type;type:tinyint(2);default:2;comment:menu type[1=dir2=menu3=button]"`
    // name
    Name *string `json:"name" gorm:"column:name;type:varchar(100);default:'';comment:name"`
    // icon
    Icon *string `json:"icon" gorm:"column:icon;type:varchar(100);default:'';comment:icon"`
    // sort
    Sort *uint8 `json:"sort" gorm:"column:sort;type:int(10);default:0;comment:sort"`
    // permission code
    PermCode *string `json:"perm_code" gorm:"column:perm_code;type:text;default:'';comment:permission code"`
    // permission/route path
    Path *string `json:"path" gorm:"column:path;type:varchar(100);default:'';comment:route path"`
    // frontend component
    Component *string `json:"component" gorm:"column:component;type:varchar(200);default:'';comment:frontend component"`
    // redirect
    Redirect *string `json:"redirect" gorm:"column:redirect;type:varchar(255);default:'';comment:redirect url"`
    // Meta info
    Meta *string `json:"meta" gorm:"column:meta;type:text;default:'';comment:meta info"`
    // selected route
    Selected *string `json:"selected" gorm:"column:selected;type:text;default:'';comment:select routed"`
    // route params
    Params *string `json:"params" gorm:"column:params;type:varchar(200);default:'';comment:route params"`
    // whether it is an external link
    IsExt *uint8 `json:"is_ext" gorm:"column:is_ext;type:tinyint(1);default:2;comment:whether it is an external link[1=yes2=no]"`
    // cache or not
    IsCache *uint8 `json:"is_cache" gorm:"column:is_cache;type:tinyint(1);default:2;comment:cache or not[1=yes2=no]"`
    // whether to display
    IsShow *uint8 `json:"is_show" gorm:"column:is_show;type:tinyint(1);default:2;comment:whether to display[1=yes2=no]"`
    // disable or not
    IsDisable *uint8 `json:"is_disable" gorm:"column:is_disable;type:tinyint(1);default:2;comment:disable or not[1=yes2=no]"`
    //create time / update time
    CreateTime int64 `json:"create_time" gorm:"column:create_time;type:int(10);default:0;comment:create time'"`
    UpdateTime int64 `json:"update_time" gorm:"column:update_time;type:int(10);default:0;comment:update time"`
    // children menu
    Children []*Menu `json:"children" gorm:"foreignKey:parent_id;references:id"`
}

gorm 获取列表数据


// List Query Menu List
func (r *MenuRepo) List() ([]*permission.Menu, error) {

    var menus []*permission.Menu
    if err := r.db().Find(&menus).Error; err != nil {
        return nil, err
    }
    return menus, nil

}

创建遍历函数


// getChildrenMenu Get Children Menus List
func (l *MenuLogic) getChildrenMenu(menus []*permission.Menu, parentId uint32) []*permission.Menu {

    var children []*permission.Menu
    for _, menu := range menus {
        if *menu.ParentId == parentId {
            menu.Children = l.getChildrenMenu(menus, *menu.ID)
            children = append(children, menu)
        }
    }
    return children

}

遍历数据


上面查询出来的数据为全部的菜单列表数据,没有分级。
以下代码将遍历数据并且构造出子菜单列表


// List Query Menu List
func (l *MenuLogic) List() *public.HandleLogic {

    menus, err := l.menuRepo.List()
    if err != nil {
        api.Global.Logger.Error("Get Menu List Failed.[Error=>]" + err.Error())
        return l.LFail()
    }

    var topLevelMenus []*permission.Menu

    for _, menu := range menus {
        if *menu.ParentId == 0 {
            menu.Children = l.getChildrenMenu(menus, *menu.ID)
            topLevelMenus = append(topLevelMenus, menu)
        }
    }
    return l.LSuccessData(topLevelMenus)

}

返回的数据如下

{
    "code": 200,
    "message": "Operate Success",
    "data": [
        {
            "id": 6,
            "parent_id": 0,
            "type": 2,
            "name": "test222",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "dd",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702564804,
            "update_time": 1702566816,
            "children": [
                {
                    "id": 13,
                    "parent_id": 6,
                    "type": 1,
                    "name": "test6",
                    "icon": "icon",
                    "sort": 2,
                    "perm_code": "perm_code",
                    "path": "",
                    "component": "component",
                    "redirect": "redirect",
                    "meta": "meta",
                    "selected": "selected",
                    "params": "params",
                    "is_ext": 1,
                    "is_cache": 1,
                    "is_show": 1,
                    "is_disable": 1,
                    "create_time": 1702567487,
                    "update_time": 1702567487,
                    "children": [
                        {
                            "id": 12,
                            "parent_id": 13,
                            "type": 1,
                            "name": "test5",
                            "icon": "icon",
                            "sort": 2,
                            "perm_code": "perm_code",
                            "path": "",
                            "component": "component",
                            "redirect": "redirect",
                            "meta": "meta",
                            "selected": "selected",
                            "params": "params",
                            "is_ext": 1,
                            "is_cache": 1,
                            "is_show": 1,
                            "is_disable": 1,
                            "create_time": 1702567243,
                            "update_time": 1702567243,
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "id": 7,
            "parent_id": 0,
            "type": 1,
            "name": "test",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "component",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702566518,
            "update_time": 1702566518,
            "children": null
        },
        {
            "id": 8,
            "parent_id": 0,
            "type": 1,
            "name": "test1",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "component",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702567233,
            "update_time": 1702567233,
            "children": null
        },
        {
            "id": 9,
            "parent_id": 0,
            "type": 1,
            "name": "test2",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "component",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702567236,
            "update_time": 1702567236,
            "children": null
        },
        {
            "id": 10,
            "parent_id": 0,
            "type": 1,
            "name": "test3",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "component",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702567238,
            "update_time": 1702567238,
            "children": null
        },
        {
            "id": 11,
            "parent_id": 0,
            "type": 1,
            "name": "test4",
            "icon": "icon",
            "sort": 2,
            "perm_code": "perm_code",
            "path": "",
            "component": "component",
            "redirect": "redirect",
            "meta": "meta",
            "selected": "selected",
            "params": "params",
            "is_ext": 1,
            "is_cache": 1,
            "is_show": 1,
            "is_disable": 1,
            "create_time": 1702567240,
            "update_time": 1702567240,
            "children": null
        }
    ]
}

转载请注明来源:https://janrs.com/f597