feat: Refactor user context handling and service methods
- Updated middleware to fetch user and tenant models by ID and set them in context. - Refactored common service methods to accept userID as a parameter instead of extracting from context. - Modified content service methods to include userID as a parameter for better clarity and performance. - Adjusted coupon, creator, notification, order, tenant, user, and wallet services to utilize userID directly. - Enhanced context key constants for improved readability and maintainability.
This commit is contained in:
@@ -77,40 +77,36 @@ func (s *user) LoginWithOTP(ctx context.Context, phone, otp string) (*auth_dto.L
|
||||
|
||||
return &auth_dto.LoginResponse{
|
||||
Token: token,
|
||||
User: s.toAuthUserDTO(u),
|
||||
User: s.ToAuthUserDTO(u),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Me 获取当前用户信息
|
||||
func (s *user) Me(ctx context.Context) (*auth_dto.User, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
|
||||
uid := cast.ToInt64(userID)
|
||||
// GetModelByID 获取指定 ID 的用户model
|
||||
func (s *user) GetModelByID(ctx context.Context, userID int64) (*models.User, error) {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
u, err := query.Where(tbl.ID.Eq(uid)).First()
|
||||
u, err := query.Where(tbl.ID.Eq(userID)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
}
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
return s.toAuthUserDTO(u), nil
|
||||
// Me 获取当前用户信息
|
||||
func (s *user) Me(ctx context.Context, userID int64) (*auth_dto.User, error) {
|
||||
u, err := s.GetModelByID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.ToAuthUserDTO(u), nil
|
||||
}
|
||||
|
||||
// Update 更新用户信息
|
||||
func (s *user) Update(ctx context.Context, form *user_dto.UserUpdate) error {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
func (s *user) Update(ctx context.Context, userID int64, form *user_dto.UserUpdate) error {
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err := query.Where(tbl.ID.Eq(uid)).Updates(&models.User{
|
||||
_, err := query.Where(tbl.ID.Eq(userID)).Updates(&models.User{
|
||||
Nickname: form.Nickname,
|
||||
Avatar: form.Avatar,
|
||||
Gender: form.Gender,
|
||||
@@ -125,13 +121,7 @@ func (s *user) Update(ctx context.Context, form *user_dto.UserUpdate) error {
|
||||
}
|
||||
|
||||
// RealName 实名认证
|
||||
func (s *user) RealName(ctx context.Context, form *user_dto.RealNameForm) error {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
func (s *user) RealName(ctx context.Context, userID int64, form *user_dto.RealNameForm) error {
|
||||
// Mock Verification
|
||||
if len(form.IDCard) != 18 {
|
||||
return errorx.ErrBadRequest.WithMsg("身份证号格式错误")
|
||||
@@ -141,7 +131,7 @@ func (s *user) RealName(ctx context.Context, form *user_dto.RealNameForm) error
|
||||
}
|
||||
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
u, err := query.Where(tbl.ID.Eq(uid)).First()
|
||||
u, err := query.Where(tbl.ID.Eq(userID)).First()
|
||||
if err != nil {
|
||||
return errorx.ErrRecordNotFound
|
||||
}
|
||||
@@ -159,7 +149,7 @@ func (s *user) RealName(ctx context.Context, form *user_dto.RealNameForm) error
|
||||
|
||||
b, _ := json.Marshal(metaMap)
|
||||
|
||||
_, err = query.Where(tbl.ID.Eq(uid)).Updates(&models.User{
|
||||
_, err = query.Where(tbl.ID.Eq(userID)).Updates(&models.User{
|
||||
IsRealNameVerified: true,
|
||||
VerifiedAt: time.Now(),
|
||||
Metas: types.JSON(b),
|
||||
@@ -171,15 +161,9 @@ func (s *user) RealName(ctx context.Context, form *user_dto.RealNameForm) error
|
||||
}
|
||||
|
||||
// GetNotifications 获取通知
|
||||
func (s *user) GetNotifications(ctx context.Context, typeArg string) ([]user_dto.Notification, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
func (s *user) GetNotifications(ctx context.Context, userID int64, typeArg string) ([]user_dto.Notification, error) {
|
||||
tbl, query := models.NotificationQuery.QueryContext(ctx)
|
||||
query = query.Where(tbl.UserID.Eq(uid))
|
||||
query = query.Where(tbl.UserID.Eq(userID))
|
||||
if typeArg != "" && typeArg != "all" {
|
||||
query = query.Where(tbl.Type.Eq(typeArg))
|
||||
}
|
||||
@@ -203,7 +187,7 @@ func (s *user) GetNotifications(ctx context.Context, typeArg string) ([]user_dto
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *user) toAuthUserDTO(u *models.User) *auth_dto.User {
|
||||
func (s *user) ToAuthUserDTO(u *models.User) *auth_dto.User {
|
||||
return &auth_dto.User{
|
||||
ID: cast.ToString(u.ID),
|
||||
Phone: u.Phone,
|
||||
|
||||
Reference in New Issue
Block a user