为 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:
@@ -51,7 +51,7 @@ func (h *Handler) Status(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status, err := h.service.Status(userID)
|
||||
status, err := h.service.Status(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
writeRealnameError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package realname
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -17,15 +18,15 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) FindByUserID(userID uint64) (*model.UserRealname, error) {
|
||||
func (r *Repository) FindByUserID(ctx context.Context, userID uint64) (*model.UserRealname, error) {
|
||||
var record model.UserRealname
|
||||
if err := r.db.Where("user_id = ?", userID).First(&record).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&record).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SaveResult(userID uint64, provider string, result ProviderResult) (*model.UserRealname, error) {
|
||||
func (r *Repository) SaveResult(ctx context.Context, userID uint64, provider string, result ProviderResult) (*model.UserRealname, error) {
|
||||
record := model.UserRealname{
|
||||
UserID: userID,
|
||||
Provider: provider,
|
||||
@@ -39,7 +40,7 @@ func (r *Repository) SaveResult(userID uint64, provider string, result ProviderR
|
||||
FailReason: result.FailReason,
|
||||
}
|
||||
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}},
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
@@ -70,5 +71,5 @@ func (r *Repository) SaveResult(userID uint64, provider string, result ProviderR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.FindByUserID(userID)
|
||||
return r.FindByUserID(ctx, userID)
|
||||
}
|
||||
|
||||
@@ -71,14 +71,14 @@ func (s *Service) Start(ctx context.Context, userID uint64, name string, idNo st
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.SaveResult(userID, s.provider.Name(), result)
|
||||
return s.repo.SaveResult(ctx, userID, s.provider.Name(), result)
|
||||
}
|
||||
|
||||
func (s *Service) Status(userID uint64) (PublicStatus, error) {
|
||||
func (s *Service) Status(ctx context.Context, userID uint64) (PublicStatus, error) {
|
||||
if s.repo == nil {
|
||||
return PublicStatus{}, ErrDependencyUnavailable
|
||||
}
|
||||
record, err := s.repo.FindByUserID(userID)
|
||||
record, err := s.repo.FindByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return PublicStatus{Status: "unverified"}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user