go-wxhelper/service/menu/utils.go
2024-01-19 12:06:30 +08:00

55 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
var meta menu.ItemMeta
meta.Title = record.Name
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
}