You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
1.8 KiB

package profile
import (
"context"
"fmt"
"github.com/youruser/base/internal/svc"
"github.com/youruser/base/internal/types"
"github.com/youruser/base/model"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProfileLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取个人信息
func NewGetProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProfileLogic {
return &GetProfileLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetProfileLogic) GetProfile() (resp *types.GetProfileResponse, err error) {
// 从上下文中获取当前用户ID(假设通过 Auth 中间件设置)
userIdValue := l.ctx.Value("userId")
if userIdValue == nil {
return nil, fmt.Errorf("未获取到用户信息,请先登录")
}
userId, ok := userIdValue.(int64)
if !ok {
return nil, fmt.Errorf("用户ID格式错误")
}
// 查询用户信息
user, err := model.FindOne(l.ctx, l.svcCtx.DB, userId)
if err != nil {
if err == model.ErrNotFound {
return nil, fmt.Errorf("用户不存在")
}
return nil, fmt.Errorf("查询用户失败: %v", err)
}
// 查询个人资料
profile, err := model.FindProfileByUserId(l.ctx, l.svcCtx.DB, userId)
if err != nil && err != model.ErrNotFound {
return nil, fmt.Errorf("查询个人资料失败: %v", err)
}
// 如果没有个人资料,使用默认值
avatar := ""
bio := ""
if profile != nil {
avatar = profile.Avatar
bio = profile.Bio
}
resp = &types.GetProfileResponse{
Id: user.Id,
Username: user.Username,
Email: user.Email,
Phone: user.Phone,
Avatar: avatar,
Bio: bio,
Status: int(user.Status),
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
}
return resp, nil
}