代码拉取完成,页面将自动刷新
package service
import (
"cmp"
"slices"
"strconv"
"strings"
"gitee.com/linqwen/momo/app/rbac/model"
"gitee.com/linqwen/momo/base"
)
type menuService struct {
base.BaseService[model.MenuEntity]
}
func NewMenuService() *menuService {
return &menuService{
BaseService: *base.NewBaseService(RbacDb, model.MenuEntity{}),
}
}
func (bs menuService) GetUserMenus(userId int64) ([]model.MenuEntity, error) {
var roles []model.RoleEntity
var menus []model.MenuEntity
var user model.UserEntity
fields := []string{"id", "parent_id", "code", "name", "alias", "path", "category", "action", "is_open", "sort"}
// 获取用户信息
if err := bs.DB.Where("id = ?", userId).First(&user).Error; err != nil {
return nil, err
}
// 获取用户的角色ID列表
roleIds := strings.Split(user.RoleIds, ",")
if err := bs.DB.Where("id IN (?)", roleIds).Find(&roles).Error; err != nil {
return nil, err
}
// 获取所有角色对应的菜单ID
menuIdSet := make(map[string]struct{})
for _, role := range roles {
if role.RoleAlias == "administrator" {
// 获取所有菜单项
if err := bs.DB.Select(fields).Find(&menus).Error; err != nil {
return nil, err
}
// 生成树形结构
//menuTree := buildMenuTree(menus)
menuTree := buildMenuForest(menus, menus, true)
return menuTree, nil
}
menuIds := strings.Split(role.MenuIds, ",")
for _, menuId := range menuIds {
menuIdSet[menuId] = struct{}{}
}
}
// 获取所有菜单项
if err := bs.DB.Select(fields).Find(&menus).Error; err != nil {
return nil, err
}
// 生成树形结构
//menuTree := buildMenuTree(menus)
roleMenus := make([]model.MenuEntity, 0)
for _, menu := range menus {
_, ok := menuIdSet[strconv.FormatInt(menu.Id, 10)]
if ok {
roleMenus = append(roleMenus, menu)
}
}
menuTree := buildMenuForest(menus, roleMenus, true)
return menuTree, nil
}
func buildMenuForest(allMenus []model.MenuEntity, roleMenus []model.MenuEntity, isMenuOnly bool) []model.MenuEntity {
allMenuMap := make(map[int64]model.MenuEntity)
for _, menu := range allMenus {
allMenuMap[menu.Id] = menu
}
userRoutes := make(map[int64]model.MenuEntity)
for _, roleMenu := range roleMenus {
recursion(allMenuMap, userRoutes, roleMenu, isMenuOnly)
}
menuTree := buildForest(userRoutes, 0)
resultTree := make([]model.MenuEntity, 0)
for _, item := range menuTree {
resultTree = append(resultTree, *item)
}
return resultTree
}
func recursion(allMenus map[int64]model.MenuEntity, routes map[int64]model.MenuEntity, roleMenu model.MenuEntity, menuOnly bool) {
_, ok := routes[roleMenu.Id]
if !ok && (!menuOnly || roleMenu.Action == 0) {
routes[roleMenu.Id] = roleMenu
}
if roleMenu.ParentId > 0 {
parentMenu, ok := allMenus[roleMenu.ParentId]
if ok {
recursion(allMenus, routes, parentMenu, menuOnly)
}
}
}
func buildForest(nodes map[int64]model.MenuEntity, parentId int64) []*model.MenuEntity {
nodeTree := make([]*model.MenuEntity, 0)
nodeSortFunc := func(a, b *model.MenuEntity) int {
if a.Sort == b.Sort {
return cmp.Compare(a.Id, b.Id)
} else {
return cmp.Compare(b.Sort, a.Sort)
}
}
for _, node := range nodes {
if node.ParentId == parentId {
node.Children = buildForest(nodes, node.Id)
node.HasChildren = len(node.Children) > 0
slices.SortFunc(node.Children, nodeSortFunc)
nodeTree = append(nodeTree, &node)
}
}
slices.SortFunc(nodeTree, nodeSortFunc)
return nodeTree
}
func (bs menuService) GetAllMenus(isMenuOnly bool) ([]model.MenuEntity, error) {
var menus []model.MenuEntity
fields := []string{"id", "parent_id", "code", "name", "alias", "path", "category", "action", "is_open", "sort"}
// 查询所有菜单项
if err := bs.DB.Select(fields).Find(&menus).Error; err != nil {
return nil, err
}
// 构建并返回树形结构
result := buildMenuForest(menus, menus, isMenuOnly)
return result, nil
}
func (bs menuService) GetUserCodes(userId int64) ([]model.MenuEntity, error) {
var roles []model.RoleEntity
var menus []model.MenuEntity
var user model.UserEntity
fields := []string{"id", "parent_id", "code", "name", "alias", "path", "category", "action", "is_open", "sort"}
// 获取用户信息
if err := bs.DB.Where("id = ?", userId).First(&user).Error; err != nil {
return nil, err
}
// 获取用户的角色ID列表
roleIds := strings.Split(user.RoleIds, ",")
if err := bs.DB.Where("id IN (?)", roleIds).Find(&roles).Error; err != nil {
return nil, err
}
// 获取所有角色对应的菜单ID
menuIdSet := make(map[string]struct{})
for _, role := range roles {
if role.RoleAlias == "administrator" {
// 获取所有菜单项
if err := bs.DB.Select(fields).Find(&menus).Error; err != nil {
return nil, err
}
// 生成树形结构
//menuTree := buildMenuTree(menus)
menuTree := buildMenuForest(menus, menus, false)
return menuTree, nil
}
menuIds := strings.Split(role.MenuIds, ",")
for _, menuId := range menuIds {
menuIdSet[menuId] = struct{}{}
}
}
// 获取所有菜单项
if err := bs.DB.Select(fields).Find(&menus).Error; err != nil {
return nil, err
}
// 生成树形结构
//menuTree := buildMenuTree(menus)
roleMenus := make([]model.MenuEntity, 0)
for _, menu := range menus {
_, ok := menuIdSet[strconv.FormatInt(menu.Id, 10)]
if ok {
roleMenus = append(roleMenus, menu)
}
}
menuTree := buildMenuForest(menus, roleMenus, false)
return menuTree, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。