fix(chat): 提升二维码 OCR 稳定性
This commit is contained in:
@@ -1,27 +1,81 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
hub *chathub.Hub
|
||||
redis *redis.Client
|
||||
db *gorm.DB
|
||||
hub *chathub.Hub
|
||||
redis *redis.Client
|
||||
logger *zap.Logger
|
||||
ocrHTTPClient *http.Client
|
||||
ocrGate *ocrRequestGate
|
||||
ocrSleep func(context.Context, time.Duration) error
|
||||
}
|
||||
|
||||
const (
|
||||
defaultSupportRoleCode = "cs"
|
||||
)
|
||||
|
||||
func NewRepository(db *gorm.DB, hub *chathub.Hub, redis *redis.Client) *Repository {
|
||||
return &Repository{db: db, hub: hub, redis: redis}
|
||||
type RepositoryOption func(*Repository)
|
||||
|
||||
func WithLogger(logger *zap.Logger) RepositoryOption {
|
||||
return func(repo *Repository) {
|
||||
if logger != nil {
|
||||
repo.logger = logger
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withOCRHTTPClient(client *http.Client) RepositoryOption {
|
||||
return func(repo *Repository) {
|
||||
if client != nil {
|
||||
repo.ocrHTTPClient = client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withOCRGate(gate *ocrRequestGate) RepositoryOption {
|
||||
return func(repo *Repository) {
|
||||
if gate != nil {
|
||||
repo.ocrGate = gate
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withOCRSleep(sleep func(context.Context, time.Duration) error) RepositoryOption {
|
||||
return func(repo *Repository) {
|
||||
if sleep != nil {
|
||||
repo.ocrSleep = sleep
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB, hub *chathub.Hub, redis *redis.Client, options ...RepositoryOption) *Repository {
|
||||
repo := &Repository{
|
||||
db: db,
|
||||
hub: hub,
|
||||
redis: redis,
|
||||
logger: zap.NewNop(),
|
||||
ocrHTTPClient: ocrHTTPClient,
|
||||
ocrGate: sharedOCRRequestGate,
|
||||
ocrSleep: sleepWithContext,
|
||||
}
|
||||
for _, option := range options {
|
||||
option(repo)
|
||||
}
|
||||
return repo
|
||||
}
|
||||
func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatConversation, error) {
|
||||
var existing model.ChatConversation
|
||||
|
||||
Reference in New Issue
Block a user