package model import ( "kpt-pasture/util" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) const ( PermissionBtnAdd = "permission:btn:add" PermissionBtnEdit = "permission:btn:edit" PermissionBtnDelete = "permission:btn:delete" ) var PermissionBtnMap = map[pasturePb.ButtonType_Kind]string{ pasturePb.ButtonType_Add: PermissionBtnAdd, pasturePb.ButtonType_Edit: PermissionBtnEdit, pasturePb.ButtonType_Delete: PermissionBtnDelete, } type SystemMenu struct { Id int64 `json:"id"` Name string `json:"name"` Title string `json:"title"` Path string `json:"path"` Icon string `json:"icon"` Component string `json:"component"` Redirect string `json:"redirect"` Auths string `json:"auths"` MenuType pasturePb.MenuType_Kind `json:"menuType"` ButtonType pasturePb.ButtonType_Kind `json:"buttonType"` ParentId int64 `json:"parentId"` Rank int32 `json:"rank"` ExtraIcon string `json:"extraIcon"` EnterTransition string `json:"enterTransition"` ActivePath string `json:"activePath"` FrameSrc string `json:"frameSrc"` FrameLoading pasturePb.IsShow_Kind `json:"frameLoading"` Keepalive pasturePb.IsShow_Kind `json:"keepalive"` HiddenTag pasturePb.IsShow_Kind `json:"hiddenTag"` ShowLink pasturePb.IsShow_Kind `json:"showLink"` ShowParent pasturePb.IsShow_Kind `json:"showParent"` IsShow pasturePb.IsShow_Kind `json:"isShow"` IsDelete pasturePb.IsShow_Kind `json:"isDelete"` CreatedAt int64 `json:"createdAt"` UpdatedAt int64 `json:"updatedAt"` } func (s *SystemMenu) TableName() string { return "system_menu" } func (s *SystemMenu) MenuUpdate(req *pasturePb.SearchMenuRequest) { s.Name = req.Name s.Path = req.Path s.MenuType = req.MenuType s.Title = req.Title s.ParentId = int64(req.ParentId) s.FrameSrc = req.FrameSrc s.FrameLoading = util.BooleanToIsShow(req.FrameLoading) s.Keepalive = util.BooleanToIsShow(req.Keepalive) s.HiddenTag = util.BooleanToIsShow(req.HiddenTag) s.ShowLink = util.BooleanToIsShow(req.ShowLink) s.ShowParent = util.BooleanToIsShow(req.ShowParent) s.Icon = req.Icon s.Component = req.Component s.Redirect = req.Redirect s.Auths = req.Auths s.Rank = req.Rank s.ExtraIcon = req.ExtraIcon s.EnterTransition = req.EnterTransition s.ActivePath = req.ActivePath s.IsShow = req.IsShow } // NewSystemMenu AddMenuRequest func NewSystemMenu(req *pasturePb.SearchMenuRequest) *SystemMenu { return &SystemMenu{ Name: req.Name, Title: req.Title, Path: req.Path, Icon: req.Icon, Component: req.Component, Redirect: req.Redirect, Auths: req.Auths, MenuType: req.MenuType, ParentId: int64(req.ParentId), Rank: req.Rank, ExtraIcon: req.ExtraIcon, EnterTransition: req.EnterTransition, ActivePath: req.ActivePath, FrameSrc: req.FrameSrc, FrameLoading: util.BooleanToIsShow(req.FrameLoading), Keepalive: util.BooleanToIsShow(req.Keepalive), HiddenTag: util.BooleanToIsShow(req.HiddenTag), ShowLink: util.BooleanToIsShow(req.ShowLink), ShowParent: util.BooleanToIsShow(req.ShowParent), IsShow: pasturePb.IsShow_Ok, IsDelete: pasturePb.IsShow_Ok, } } type SystemMenuSlice []*SystemMenu func (s SystemMenuSlice) ToPB() []*pasturePb.SearchMenuRequest { res := make([]*pasturePb.SearchMenuRequest, len(s)) for i, menu := range s { var frameLoading, keepalive, hiddenTag, showLink, showParent bool if menu.FrameLoading == pasturePb.IsShow_Ok { frameLoading = true } if menu.Keepalive == pasturePb.IsShow_Ok { keepalive = true } if menu.HiddenTag == pasturePb.IsShow_Ok { hiddenTag = true } if menu.ShowLink == pasturePb.IsShow_Ok { showLink = true } if menu.ShowParent == pasturePb.IsShow_Ok { showParent = true } res[i] = &pasturePb.SearchMenuRequest{ Id: int32(menu.Id), Name: menu.Name, ParentId: int32(menu.ParentId), MenuType: menu.MenuType, Title: menu.Title, Path: menu.Path, IsShow: menu.IsShow, Component: menu.Component, Icon: menu.Icon, Redirect: menu.Redirect, Auths: menu.Auths, Rank: menu.Rank, ExtraIcon: menu.ExtraIcon, EnterTransition: menu.EnterTransition, ActivePath: menu.ActivePath, FrameSrc: menu.FrameSrc, FrameLoading: frameLoading, Keepalive: keepalive, HiddenTag: hiddenTag, ShowLink: showLink, ShowParent: showParent, CreatedAtFormat: time.Unix(menu.CreatedAt, 0).Local().Format(LayoutTime), UpdatedAtFormat: time.Unix(menu.UpdatedAt, 0).Local().Format(LayoutTime), } } return res } func (s SystemMenuSlice) ToTree() []*pasturePb.MenuTree { if len(s) == 0 { return make([]*pasturePb.MenuTree, 0) } // 1. 一次性创建所有节点并建立ID映射 nodeMap := make(map[int32]*pasturePb.MenuTree, len(s)) allNodes := make([]*pasturePb.MenuTree, len(s)) for i, v := range s { node := &pasturePb.MenuTree{ Id: int32(v.Id), Path: v.Path, ParentId: int32(v.ParentId), Name: v.Name, ButtonType: v.ButtonType, MenuType: v.MenuType, Meta: &pasturePb.MenuMeta{ Icon: v.Icon, Title: v.Title, Rank: v.Rank, Roles: []string{}, ShowLink: v.ShowLink == pasturePb.IsShow_Ok, Auths: make([]string, 0), }, Children: make([]*pasturePb.MenuTree, 0), } allNodes[i] = node nodeMap[node.Id] = node } // 2. 构建树结构 rootNodes := make([]*pasturePb.MenuTree, 0, len(s)/2) // 假设约一半是根节点 for _, node := range allNodes { if node.ParentId == 0 { rootNodes = append(rootNodes, node) } else { if parent, exists := nodeMap[node.ParentId]; exists { parent.Children = append(parent.Children, node) } } } // 3. 处理特殊节点类型 for _, node := range nodeMap { if node.MenuType == pasturePb.MenuType_Button && node.ButtonType > pasturePb.ButtonType_Invalid { if parent, ok := nodeMap[node.ParentId]; ok && parent.Meta != nil { parent.Meta.Auths = append(parent.Meta.Auths, PermissionBtnMap[node.ButtonType]) } } } return rootNodes } type MenuItem struct { Path string `json:"path"` Name string `json:"name"` Meta MenuMeta `json:"meta"` Children []*MenuItem `json:"children"` } type MenuMeta struct { Icon string `json:"icon"` Title string `json:"title"` Rank int32 `json:"rank"` ShowLink bool `json:"showLink"` Hidden bool `json:"hidden"` }