Files
sundynix-micro-be/app/user/api/internal/logic/user/getUserInfoLogic.go
T
2026-04-27 00:02:18 +08:00

56 lines
1.3 KiB
Go

// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package user
import (
"context"
"encoding/json"
"fmt"
"sundynix-micro-go/app/user/api/internal/svc"
"sundynix-micro-go/app/user/api/internal/types"
pb "sundynix-micro-go/app/user/rpc/user"
"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,
}
}
func (l *GetUserInfoLogic) GetUserInfo() (resp *types.LoginResp, err error) {
// 从JWT claims中获取userId
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
if userId == "" || userId == "<nil>" {
return nil, fmt.Errorf("用户未登录")
}
userResp, err := l.svcCtx.UserRpc.GetUserById(l.ctx, &pb.GetUserByIdReq{
Id: userId,
})
if err != nil {
l.Errorf("获取用户信息失败: %v", err)
return nil, fmt.Errorf("获取用户信息失败")
}
// 将proto消息转换为map以便JSON序列化
userJSON, _ := json.Marshal(userResp.User)
var userInfo interface{}
_ = json.Unmarshal(userJSON, &userInfo)
return &types.LoginResp{
UserInfo: userInfo,
}, nil
}