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
|
2024-02-01 18:01:01 +08:00
|
|
|
|
node.Name = record.Name
|
2024-01-19 12:06:30 +08:00
|
|
|
|
|
|
|
|
|
var meta menu.ItemMeta
|
2024-02-01 18:01:01 +08:00
|
|
|
|
meta.Title = record.Title
|
2024-01-19 12:06:30 +08:00
|
|
|
|
meta.Icon = record.Icon
|
2024-02-02 11:45:19 +08:00
|
|
|
|
// rank只有顶级菜单才返回rank值
|
|
|
|
|
if record.ParentId == nil || *record.ParentId == "" {
|
|
|
|
|
meta.Rank = record.Sort
|
|
|
|
|
}
|
2024-01-19 12:06:30 +08:00
|
|
|
|
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
|
|
|
|
|
}
|