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.
121 lines
2.9 KiB
121 lines
2.9 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 UpdateProfileLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 更新个人资料
|
|
func NewUpdateProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProfileLogic {
|
|
return &UpdateProfileLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateProfileLogic) UpdateProfile(req *types.UpdateProfileRequest) (resp *types.GetProfileResponse, err error) {
|
|
// 从上下文中获取当前用户ID
|
|
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)
|
|
}
|
|
|
|
// 更新用户基本信息
|
|
if req.Username != "" {
|
|
user.Username = req.Username
|
|
}
|
|
if req.Phone != "" {
|
|
user.Phone = req.Phone
|
|
}
|
|
|
|
// 更新用户表
|
|
err = model.Update(l.ctx, l.svcCtx.DB, user)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("更新用户信息失败: %v", err)
|
|
}
|
|
|
|
// 查询或创建个人资料
|
|
profile, err := model.FindProfileByUserId(l.ctx, l.svcCtx.DB, userId)
|
|
if err == model.ErrNotFound {
|
|
// 创建新的个人资料
|
|
profile = &model.Profile{
|
|
UserId: userId,
|
|
Avatar: req.Avatar,
|
|
Bio: req.Bio,
|
|
}
|
|
_, err = model.InsertProfile(l.ctx, l.svcCtx.DB, profile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建个人资料失败: %v", err)
|
|
}
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("查询个人资料失败: %v", err)
|
|
} else {
|
|
// 更新现有个人资料
|
|
if req.Avatar != "" {
|
|
profile.Avatar = req.Avatar
|
|
}
|
|
if req.Bio != "" {
|
|
profile.Bio = req.Bio
|
|
}
|
|
err = model.UpdateProfile(l.ctx, l.svcCtx.DB, profile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("更新个人资料失败: %v", err)
|
|
}
|
|
}
|
|
|
|
// 重新查询用户信息
|
|
user, err = model.FindOne(l.ctx, l.svcCtx.DB, userId)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("查询用户失败: %v", err)
|
|
}
|
|
|
|
// 查询个人资料
|
|
profile, _ = model.FindProfileByUserId(l.ctx, l.svcCtx.DB, userId)
|
|
|
|
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
|
|
}
|
|
|