go-wxhelper/service/menu/utils.go

56 lines
1.1 KiB
Go
Raw Normal View History

2024-01-19 12:06:30 +08:00
package menu
import (
"sort"
"strings"
"wechat-robot/model/vo/menu"
)
// toTree
// @description: 递归生成菜单树
// @param records
// @param pid
// @return tree
func toTree(records []menuRecordItem, pid string) (tree []menu.Item) {
for _, record := range records {
// 判断数据父级Id和传入的父级Id是否相等相等就处理出来
var deal bool
if pid == "" {
deal = record.ParentId == nil
} else {
deal = record.ParentId != nil && *record.ParentId == pid
}
if deal {
var node menu.Item
node.Id = record.Id
node.Path = record.Path
node.Name = record.Name
2024-01-19 12:06:30 +08:00
var meta menu.ItemMeta
meta.Title = record.Title
2024-01-19 12:06:30 +08:00
meta.Icon = record.Icon
meta.Rank = record.Sort
if record.RoleCode != "" {
meta.Roles = strings.Split(record.RoleCode, ",")
}
if record.AuthCode != "" {
meta.Auths = strings.Split(record.AuthCode, ",")
}
node.Meta = meta
// 处理子级
node.Children = toTree(records, record.Id)
// 往数组塞数据
tree = append(tree, node)
}
}
// 排序
sort.SliceStable(tree, func(i, j int) bool {
return tree[i].Meta.Rank < tree[j].Meta.Rank
})
return
}