feat(用户管理): 添加用户备注字段并优化用户信息更新逻辑

为User类型添加remark字段以支持用户备注功能
重构updateUserBasicInfoLogic使用事务处理用户信息更新
优化错误处理和日志记录
This commit is contained in:
shanshanzhong 2026-01-13 19:04:59 -08:00
parent 01ccd44e84
commit 869d7fbe59
3 changed files with 91 additions and 79 deletions

View File

@ -28,6 +28,7 @@ type (
EnableTradeNotify bool `json:"enable_trade_notify"` EnableTradeNotify bool `json:"enable_trade_notify"`
LastLoginTime int64 `json:"last_login_time"` LastLoginTime int64 `json:"last_login_time"`
MemberStatus string `json:"member_status"` MemberStatus string `json:"member_status"`
Remark string `json:"remark"`
AuthMethods []UserAuthMethod `json:"auth_methods"` AuthMethods []UserAuthMethod `json:"auth_methods"`
UserDevices []UserDevice `json:"user_devices"` UserDevices []UserDevice `json:"user_devices"`
CreatedAt int64 `json:"created_at"` CreatedAt int64 `json:"created_at"`

View File

@ -2,6 +2,7 @@ package user
import ( import (
"context" "context"
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
@ -13,6 +14,7 @@ import (
"github.com/perfect-panel/server/pkg/tool" "github.com/perfect-panel/server/pkg/tool"
"github.com/perfect-panel/server/pkg/xerr" "github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors" "github.com/pkg/errors"
"gorm.io/gorm"
) )
type UpdateUserBasicInfoLogic struct { type UpdateUserBasicInfoLogic struct {
@ -43,6 +45,7 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Invalid Image Size") return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Invalid Image Size")
} }
err = l.svcCtx.UserModel.Transaction(l.ctx, func(tx *gorm.DB) error {
if userInfo.Balance != req.Balance { if userInfo.Balance != req.Balance {
change := req.Balance - userInfo.Balance change := req.Balance - userInfo.Balance
balanceLog := log.Balance{ balanceLog := log.Balance{
@ -54,15 +57,14 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
} }
content, _ := balanceLog.Marshal() content, _ := balanceLog.Marshal()
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{ err = tx.Create(&log.SystemLog{
Type: log.TypeBalance.Uint8(), Type: log.TypeBalance.Uint8(),
Date: time.Now().Format(time.DateOnly), Date: time.Now().Format(time.DateOnly),
ObjectID: userInfo.Id, ObjectID: userInfo.Id,
Content: string(content), Content: string(content),
}) }).Error
if err != nil { if err != nil {
l.Errorw("[UpdateUserBasicInfoLogic] Insert Balance Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId)) return err
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Balance Log Error")
} }
userInfo.Balance = req.Balance userInfo.Balance = req.Balance
} }
@ -85,15 +87,14 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
} }
content, _ := giftLog.Marshal() content, _ := giftLog.Marshal()
// Add gift amount change log // Add gift amount change log
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{ err = tx.Create(&log.SystemLog{
Type: log.TypeGift.Uint8(), Type: log.TypeGift.Uint8(),
Date: time.Now().Format(time.DateOnly), Date: time.Now().Format(time.DateOnly),
ObjectID: userInfo.Id, ObjectID: userInfo.Id,
Content: string(content), Content: string(content),
}) }).Error
if err != nil { if err != nil {
l.Errorw("[UpdateUserBasicInfoLogic] Insert Balance Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId)) return err
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Balance Log Error")
} }
userInfo.GiftAmount = req.GiftAmount userInfo.GiftAmount = req.GiftAmount
} }
@ -108,15 +109,14 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
} }
content, _ := commentLog.Marshal() content, _ := commentLog.Marshal()
err = l.svcCtx.LogModel.Insert(l.ctx, &log.SystemLog{ err = tx.Create(&log.SystemLog{
Type: log.TypeCommission.Uint8(), Type: log.TypeCommission.Uint8(),
Date: time.Now().Format(time.DateOnly), Date: time.Now().Format(time.DateOnly),
ObjectID: userInfo.Id, ObjectID: userInfo.Id,
Content: string(content), Content: string(content),
}) }).Error
if err != nil { if err != nil {
l.Errorw("[UpdateUserBasicInfoLogic] Insert Commission Log Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId)) return err
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "Insert Commission Log Error")
} }
userInfo.Commission = req.Commission userInfo.Commission = req.Commission
} }
@ -128,16 +128,26 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
if req.Password != "" { if req.Password != "" {
if userInfo.Id == 2 && isDemo { if userInfo.Id == 2 && isDemo {
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode") return errors.New("Demo mode does not allow modification of the admin user password")
} }
userInfo.Password = tool.EncodePassWord(req.Password) userInfo.Password = tool.EncodePassWord(req.Password)
userInfo.Algo = "default" userInfo.Algo = "default"
} }
err = l.svcCtx.UserModel.Update(l.ctx, userInfo) err = l.svcCtx.UserModel.Update(l.ctx, userInfo, tx)
if err != nil {
return err
}
return nil
})
if err != nil { if err != nil {
l.Errorw("[UpdateUserBasicInfoLogic] Update User Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId)) l.Errorw("[UpdateUserBasicInfoLogic] Update User Error:", logger.Field("err", err.Error()), logger.Field("userId", req.UserId))
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "Update User Error") if err.Error() == "Demo mode does not allow modification of the admin user password" {
return errors.Wrapf(xerr.NewErrCodeMsg(503, "Demo mode does not allow modification of the admin user password"), "UpdateUserBasicInfo failed: cannot update admin user password in demo mode")
}
return errors.Wrapf(xerr.NewErrCodeMsg(xerr.DatabaseUpdateError, fmt.Sprintf("Database update error: %v", err)), "Update User Error")
} }
return nil return nil

View File

@ -2604,6 +2604,7 @@ type User struct {
EnableTradeNotify bool `json:"enable_trade_notify"` EnableTradeNotify bool `json:"enable_trade_notify"`
LastLoginTime int64 `json:"last_login_time"` LastLoginTime int64 `json:"last_login_time"`
MemberStatus string `json:"member_status"` MemberStatus string `json:"member_status"`
Remark string `json:"remark"`
AuthMethods []UserAuthMethod `json:"auth_methods"` AuthMethods []UserAuthMethod `json:"auth_methods"`
UserDevices []UserDevice `json:"user_devices"` UserDevices []UserDevice `json:"user_devices"`
CreatedAt int64 `json:"created_at"` CreatedAt int64 `json:"created_at"`