muyu-apiserver/gateway/internal/logic/auth/getUserInfoLogic.go

81 lines
1.9 KiB
Go
Raw Normal View History

package auth
import (
"context"
"muyu-apiserver/gateway/internal/svc"
"muyu-apiserver/gateway/internal/types"
"muyu-apiserver/pkg/ctxdata"
"muyu-apiserver/rpc/system/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
return &GetUserInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
type UserInfoWithMenus struct {
types.UserInfo
MenuPaths []string `json:"menuPaths"`
}
func (l *GetUserInfoLogic) GetUserInfo() (*UserInfoWithMenus, error) {
userId := ctxdata.GetUserId(l.ctx)
rpcResp, err := l.svcCtx.SystemRpc.GetUser(l.ctx, &pb.GetUserReq{
UserId: userId,
})
if err != nil {
return nil, err
}
// Get user's role menus
var menuPaths []string
if rpcResp.RoleId != "" {
role, _ := l.svcCtx.SystemRpc.GetRole(l.ctx, &pb.GetRoleReq{RoleId: rpcResp.RoleId})
if role != nil && len(role.MenuIds) > 0 {
menuList, _ := l.svcCtx.SystemRpc.ListMenu(l.ctx, &pb.ListMenuReq{})
if menuList != nil {
menuIdSet := make(map[string]bool)
for _, id := range role.MenuIds {
menuIdSet[id] = true
}
for _, m := range menuList.List {
if menuIdSet[m.MenuId] && m.Path != "" {
menuPaths = append(menuPaths, m.Path)
}
}
}
}
}
return &UserInfoWithMenus{
UserInfo: types.UserInfo{
UserId: rpcResp.UserId,
Username: rpcResp.Username,
RealName: rpcResp.RealName,
Phone: rpcResp.Phone,
Email: rpcResp.Email,
Avatar: rpcResp.Avatar,
Status: rpcResp.Status,
RoleId: rpcResp.RoleId,
RoleName: rpcResp.RoleName,
LastLoginTime: rpcResp.LastLoginTime,
CreatedAt: rpcResp.CreatedAt,
UpdatedAt: rpcResp.UpdatedAt,
},
MenuPaths: menuPaths,
}, nil
}