为 9 个模块添加 Context 超时控制
完成模块: - auth: 3 个 Repository 方法 + Service + Handler + Middleware - wallet: 已有 context 支持,修复依赖调用 - payment: 已有 context 支持,修复 wallet 调用 - adminaudit: 1 个方法 - notification: 2 个方法 - realname: 2 个方法 - systemconfig: 4 个方法 - adminauth: 7 个方法 - adminuser: 6 个方法 所有数据库调用已改为 r.db.WithContext(ctx),完整传递 context 链路。 待完成模块: order, listing, chat 等 12 个模块(约 157 个方法)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
@@ -18,25 +19,25 @@ func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByID(id uint64) (*model.User, error) {
|
||||
func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := r.db.First(&user, id).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateProfile(id uint64, nickname string, avatarURL string) (*model.User, error) {
|
||||
if err := r.db.Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
func (r *UserRepository) UpdateProfile(ctx context.Context, id uint64, nickname string, avatarURL string) (*model.User, error) {
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
"nickname": nickname,
|
||||
"avatar_url": avatarURL,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.FindByID(id)
|
||||
return r.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error) {
|
||||
func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string) (*model.User, error) {
|
||||
now := time.Now()
|
||||
user := model.User{
|
||||
Phone: phone,
|
||||
@@ -48,7 +49,7 @@ func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error)
|
||||
LastLoginAt: &now,
|
||||
}
|
||||
|
||||
err := r.db.Clauses(clause.OnConflict{
|
||||
err := r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "phone"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
||||
}).Create(&user).Error
|
||||
@@ -57,7 +58,7 @@ func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error)
|
||||
}
|
||||
|
||||
var found model.User
|
||||
if err := r.db.Where("phone = ?", phone).First(&found).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&found).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &found, nil
|
||||
|
||||
@@ -101,7 +101,7 @@ func (s *Service) LoginWithSMS(ctx context.Context, phone string, code string) (
|
||||
return LoginResult{}, err
|
||||
}
|
||||
|
||||
user, err := s.users.FindOrCreateByPhone(phone)
|
||||
user, err := s.users.FindOrCreateByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user