为 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:
yml2213
2026-06-10 09:12:27 +08:00
parent c517349db6
commit b377f8350b
23 changed files with 174 additions and 159 deletions
@@ -40,7 +40,7 @@ func (h *Handler) List(c *gin.Context) {
return
}
page, pageSize := parsePagination(c)
result, err := h.service.List(userID, page, pageSize)
result, err := h.service.List(c.Request.Context(), userID, page, pageSize)
if err != nil {
writeNotificationError(c, err)
return
@@ -59,7 +59,7 @@ func (h *Handler) MarkRead(c *gin.Context) {
response.BadRequest(c, "ID 不正确")
return
}
if err := h.service.MarkRead(userID, id); err != nil {
if err := h.service.MarkRead(c.Request.Context(), userID, id); err != nil {
writeNotificationError(c, err)
return
}
@@ -1,6 +1,7 @@
package notification
import (
"context"
"time"
"hfb_sys/backend/internal/model"
@@ -25,14 +26,14 @@ func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
var total int64
if err := r.db.Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
if err := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
return nil, err
}
offset := (page - 1) * pageSize
var rows []model.Notification
if err := r.db.Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
return nil, err
}
items := make([]NotificationDTO, 0, len(rows))
@@ -42,9 +43,9 @@ func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult,
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
}
func (r *Repository) MarkRead(userID uint64, id uint64) error {
func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64) error {
now := time.Now()
return r.db.Model(&model.Notification{}).
return r.db.WithContext(ctx).Model(&model.Notification{}).
Where("id = ? AND user_id = ?", id, userID).
Update("read_at", now).Error
}
@@ -1,6 +1,9 @@
package notification
import "errors"
import (
"context"
"errors"
)
var ErrDependencyUnavailable = errors.New("dependency unavailable")
@@ -12,16 +15,16 @@ func NewService(repo *Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}
return s.repo.List(userID, page, pageSize)
return s.repo.List(ctx, userID, page, pageSize)
}
func (s *Service) MarkRead(userID uint64, id uint64) error {
func (s *Service) MarkRead(ctx context.Context, userID uint64, id uint64) error {
if s.repo == nil {
return ErrDependencyUnavailable
}
return s.repo.MarkRead(userID, id)
return s.repo.MarkRead(ctx, userID, id)
}