58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.10.1
|
|
|
|
package userProfile
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"sundynix-micro-go/app/plant/api/internal/svc"
|
|
"sundynix-micro-go/app/plant/api/internal/types"
|
|
"sundynix-micro-go/app/plant/rpc/plant"
|
|
systemPb "sundynix-micro-go/app/system/rpc/system"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdateUserProfileLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 更新用户资料
|
|
func NewUpdateUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserProfileLogic {
|
|
return &UpdateUserProfileLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdateUserProfileLogic) UpdateUserProfile(req *types.UpdateProfileReq) error {
|
|
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
|
|
|
// 1. 更新植物用户资料(昵称、头像)
|
|
if _, err := l.svcCtx.PlantRpc.UpdateUserProfile(l.ctx, &plant.UpdateProfileReq{
|
|
UserId: userId,
|
|
NickName: req.Nickname,
|
|
AvatarId: req.AvatarId,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 2. 同步更新系统 user 表(与旧项目保持一致:name + avatar_id 双写)
|
|
if req.Nickname != "" || req.AvatarId != "" {
|
|
if _, err := l.svcCtx.UserRpc.UpdateUser(l.ctx, &systemPb.UpdateUserReq{
|
|
Id: userId,
|
|
NickName: req.Nickname,
|
|
AvatarId: req.AvatarId,
|
|
}); err != nil {
|
|
// 非致命错误,记录日志但不回滚(植物资料已更新)
|
|
l.Logger.Errorf("sync system user failed: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|