v1.1
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
package admin_menu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
"ycc-server/app/main/model"
|
||||
"ycc-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CreateMenuLogic struct {
|
||||
@@ -48,13 +49,13 @@ func (l *CreateMenuLogic) CreateMenu(req *types.CreateMenuReq) (resp *types.Crea
|
||||
}
|
||||
|
||||
// 3. 检查父菜单是否存在(如果不是根菜单)
|
||||
if req.Pid > 0 {
|
||||
if req.Pid != "" {
|
||||
parentMenu, err := l.svcCtx.AdminMenuModel.FindOne(l.ctx, req.Pid)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询父菜单失败, id: %d, err: %v", req.Pid, err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询父菜单失败, id: %s, err: %v", req.Pid, err)
|
||||
}
|
||||
if parentMenu == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "父菜单不存在, id: %d", req.Pid)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "父菜单不存在, id: %s", req.Pid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,18 +66,19 @@ func (l *CreateMenuLogic) CreateMenu(req *types.CreateMenuReq) (resp *types.Crea
|
||||
}
|
||||
|
||||
// 5. 创建菜单记录
|
||||
menu := &model.AdminMenu{
|
||||
Pid: req.Pid,
|
||||
Name: req.Name,
|
||||
Path: req.Path,
|
||||
Component: req.Component,
|
||||
Redirect: sql.NullString{String: req.Redirect, Valid: req.Redirect != ""},
|
||||
Status: req.Status,
|
||||
Type: typeValue,
|
||||
Sort: req.Sort,
|
||||
CreateTime: time.Now(),
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
menu := &model.AdminMenu{
|
||||
Id: uuid.NewString(),
|
||||
Pid: sql.NullString{String: req.Pid, Valid: req.Pid != ""},
|
||||
Name: req.Name,
|
||||
Path: req.Path,
|
||||
Component: req.Component,
|
||||
Redirect: sql.NullString{String: req.Redirect, Valid: req.Redirect != ""},
|
||||
Status: req.Status,
|
||||
Type: typeValue,
|
||||
Sort: req.Sort,
|
||||
CreateTime: time.Now(),
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
|
||||
// 将Meta转换为JSON字符串
|
||||
metaJson, err := json.Marshal(req.Meta)
|
||||
|
||||
@@ -27,16 +27,16 @@ func NewDeleteMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
|
||||
|
||||
func (l *DeleteMenuLogic) DeleteMenu(req *types.DeleteMenuReq) (resp *types.DeleteMenuResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.Id <= 0 {
|
||||
if req.Id == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"菜单ID必须大于0, id: %d", req.Id)
|
||||
"菜单ID不能为空, id: %s", req.Id)
|
||||
}
|
||||
|
||||
// 2. 查询菜单是否存在
|
||||
menu, err := l.svcCtx.AdminMenuModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查找菜单失败, err: %v, id: %d", err, req.Id)
|
||||
"查找菜单失败, err: %v, id: %s", err, req.Id)
|
||||
}
|
||||
|
||||
// 3. 检查是否有子菜单
|
||||
|
||||
@@ -2,8 +2,8 @@ package admin_menu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"ycc-server/app/main/api/internal/svc"
|
||||
"ycc-server/app/main/api/internal/types"
|
||||
@@ -38,14 +38,14 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户ID失败, %+v", err)
|
||||
}
|
||||
|
||||
// 使用MapReduceVoid并发获取用户角色
|
||||
var roleIds []int64
|
||||
// 使用MapReduceVoid并发获取用户角色(UUID 字符串)
|
||||
var roleIds []string
|
||||
var permissions []*struct {
|
||||
RoleId int64
|
||||
RoleId string
|
||||
}
|
||||
|
||||
type UserRoleResult struct {
|
||||
RoleId int64
|
||||
RoleId string
|
||||
}
|
||||
|
||||
err = mr.MapReduceVoid(
|
||||
@@ -67,7 +67,7 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
},
|
||||
func(pipe <-chan *UserRoleResult, cancel func(error)) {
|
||||
for item := range pipe {
|
||||
permissions = append(permissions, &struct{ RoleId int64 }{RoleId: item.RoleId})
|
||||
permissions = append(permissions, &struct{ RoleId string }{RoleId: item.RoleId})
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -79,14 +79,14 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
roleIds = append(roleIds, permission.RoleId)
|
||||
}
|
||||
|
||||
// 使用MapReduceVoid并发获取角色菜单
|
||||
var menuIds []int64
|
||||
// 使用MapReduceVoid并发获取角色菜单(UUID 字符串)
|
||||
var menuIds []string
|
||||
var roleMenus []*struct {
|
||||
MenuId int64
|
||||
MenuId string
|
||||
}
|
||||
|
||||
type RoleMenuResult struct {
|
||||
MenuId int64
|
||||
MenuId string
|
||||
}
|
||||
|
||||
err = mr.MapReduceVoid(
|
||||
@@ -108,7 +108,7 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
},
|
||||
func(pipe <-chan *RoleMenuResult, cancel func(error)) {
|
||||
for item := range pipe {
|
||||
roleMenus = append(roleMenus, &struct{ MenuId int64 }{MenuId: item.MenuId})
|
||||
roleMenus = append(roleMenus, &struct{ MenuId string }{MenuId: item.MenuId})
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -122,8 +122,8 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
|
||||
// 使用MapReduceVoid并发获取菜单
|
||||
type AdminMenuStruct struct {
|
||||
Id int64
|
||||
Pid int64
|
||||
Id string
|
||||
Pid sql.NullString
|
||||
Name string
|
||||
Path string
|
||||
Component string
|
||||
@@ -199,8 +199,7 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
return ""
|
||||
}()
|
||||
|
||||
menuId := strconv.FormatInt(menu.Id, 10)
|
||||
menuMap[menuId] = types.GetMenuAllResp{
|
||||
menuMap[menu.Id] = types.GetMenuAllResp{
|
||||
Name: menu.Name,
|
||||
Path: menu.Path,
|
||||
Redirect: redirect,
|
||||
@@ -211,14 +210,17 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
}
|
||||
}
|
||||
|
||||
// 按ParentId将菜单分组
|
||||
menuGroups := lo.GroupBy(menus, func(item *AdminMenuStruct) int64 {
|
||||
return item.Pid
|
||||
// 按ParentId将菜单分组(字符串键)
|
||||
menuGroups := lo.GroupBy(menus, func(item *AdminMenuStruct) string {
|
||||
if item.Pid.Valid {
|
||||
return item.Pid.String
|
||||
}
|
||||
return "0"
|
||||
})
|
||||
|
||||
// 递归构建菜单树
|
||||
var buildMenuTree func(parentId int64) []types.GetMenuAllResp
|
||||
buildMenuTree = func(parentId int64) []types.GetMenuAllResp {
|
||||
// 递归构建菜单树(字符串键)
|
||||
var buildMenuTree func(parentId string) []types.GetMenuAllResp
|
||||
buildMenuTree = func(parentId string) []types.GetMenuAllResp {
|
||||
children := make([]types.GetMenuAllResp, 0)
|
||||
|
||||
childMenus, ok := menuGroups[parentId]
|
||||
@@ -232,8 +234,7 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
})
|
||||
|
||||
for _, childMenu := range childMenus {
|
||||
menuId := strconv.FormatInt(childMenu.Id, 10)
|
||||
if menu, exists := menuMap[menuId]; exists && childMenu.Status == 1 {
|
||||
if menu, exists := menuMap[childMenu.Id]; exists && childMenu.Status == 1 {
|
||||
// 递归构建子菜单
|
||||
menu.Children = buildMenuTree(childMenu.Id)
|
||||
children = append(children, menu)
|
||||
@@ -243,8 +244,8 @@ func (l *GetMenuAllLogic) GetMenuAll(req *types.GetMenuAllReq) (resp *[]types.Ge
|
||||
return children
|
||||
}
|
||||
|
||||
// 从根菜单开始构建(ParentId为0的是根菜单)
|
||||
menuTree := buildMenuTree(0)
|
||||
// 从根菜单开始构建(ParentId为"0"的是根菜单)
|
||||
menuTree := buildMenuTree("0")
|
||||
|
||||
return &menuTree, nil
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListReq) (resp []types.
|
||||
}
|
||||
|
||||
// 将菜单按ID存入map
|
||||
menuMap := make(map[int64]types.MenuListItem)
|
||||
menuMap := make(map[string]types.MenuListItem)
|
||||
for _, menu := range menus {
|
||||
var meta map[string]interface{}
|
||||
err := json.Unmarshal([]byte(menu.Meta), &meta)
|
||||
@@ -69,7 +69,7 @@ func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListReq) (resp []types.
|
||||
}
|
||||
item := types.MenuListItem{
|
||||
Id: menu.Id,
|
||||
Pid: menu.Pid,
|
||||
Pid: menu.Pid.String,
|
||||
Name: menu.Name,
|
||||
Path: menu.Path,
|
||||
Component: menu.Component,
|
||||
@@ -86,13 +86,13 @@ func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListReq) (resp []types.
|
||||
|
||||
// 构建父子关系
|
||||
for _, menu := range menus {
|
||||
if menu.Pid > 0 {
|
||||
if menu.Pid.Valid && menu.Pid.String != "0" {
|
||||
// 找到父菜单
|
||||
if parent, exists := menuMap[menu.Pid]; exists {
|
||||
if parent, exists := menuMap[menu.Pid.String]; exists {
|
||||
// 添加当前菜单到父菜单的子菜单列表
|
||||
children := append(parent.Children, menuMap[menu.Id])
|
||||
parent.Children = children
|
||||
menuMap[menu.Pid] = parent
|
||||
menuMap[menu.Pid.String] = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (l *GetMenuListLogic) GetMenuList(req *types.GetMenuListReq) (resp []types.
|
||||
// 提取顶级菜单(ParentId为0)到响应列表
|
||||
result := make([]types.MenuListItem, 0)
|
||||
for _, menu := range menus {
|
||||
if menu.Pid == 0 {
|
||||
if menu.Pid.Valid && menu.Pid.String == "0" {
|
||||
result = append(result, menuMap[menu.Id])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,13 +45,13 @@ func (l *UpdateMenuLogic) UpdateMenu(req *types.UpdateMenuReq) (resp *types.Upda
|
||||
}
|
||||
|
||||
// 3. 检查父菜单是否存在(如果不是根菜单)
|
||||
if req.Pid > 0 {
|
||||
parentMenu, err := l.svcCtx.AdminMenuModel.FindOne(l.ctx, req.Pid)
|
||||
if req.Pid != nil && *req.Pid != "0" {
|
||||
parentMenu, err := l.svcCtx.AdminMenuModel.FindOne(l.ctx, *req.Pid)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询父菜单失败, id: %d, err: %v", req.Pid, err)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询父菜单失败, id: %s, err: %v", *req.Pid, err)
|
||||
}
|
||||
if parentMenu == nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "父菜单不存在, id: %d", req.Pid)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "父菜单不存在, id: %s", *req.Pid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (l *UpdateMenuLogic) UpdateMenu(req *types.UpdateMenuReq) (resp *types.Upda
|
||||
|
||||
// 5. 更新菜单信息
|
||||
|
||||
menu.Pid = req.Pid
|
||||
menu.Pid = sql.NullString{String: *req.Pid, Valid: req.Pid != nil}
|
||||
menu.Name = req.Name
|
||||
menu.Path = req.Path
|
||||
menu.Component = req.Component
|
||||
|
||||
Reference in New Issue
Block a user