forked from lxh/go-wxhelper
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
|
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
|
|||
|
}
|