56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
// default.go Day 42:默认链装配(固定顺序:黑名单→频次→签名→退订→地区)。
|
|
|
|
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go-sms/internal/model/entity"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Options 链装配选项(零值=最小可用:仅黑名单本地库+频次内存计数+签名存在性)。
|
|
type Options struct {
|
|
ThirdBlacklist ThirdPartyBlacklist
|
|
FreqStore FreqStore // nil=内存(生产建议 NewRedisFreqStore)
|
|
SignAllow SignAllowlist
|
|
UnsubRequired bool
|
|
UnsubKeywords []string
|
|
RegionResolver RegionResolver
|
|
BlockedRegions []string
|
|
}
|
|
|
|
// NewDefaultChain 默认五类链(硬拦截在前:黑名单→频次→签名→退订→地区)。
|
|
func NewDefaultChain(db *gorm.DB, rdb redis.UniversalClient, rec Recorder, opt Options) *Chain {
|
|
c := NewChain(rec,
|
|
NewBlacklist(db, rdb, opt.ThirdBlacklist),
|
|
NewFrequency(db, opt.FreqStore),
|
|
NewSign(opt.SignAllow),
|
|
NewUnsubscribe(opt.UnsubRequired, opt.UnsubKeywords),
|
|
NewRegion(opt.RegionResolver, opt.BlockedRegions),
|
|
)
|
|
c.db = db // Day51:NewScope 依赖(与拦截器同库)
|
|
return c
|
|
}
|
|
|
|
// DBRecorder 拦截原因落库 send_block_log(同步写,拦截为少数路径)。
|
|
type DBRecorder struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
// NewDBRecorder 构造。
|
|
func NewDBRecorder(db *gorm.DB) *DBRecorder { return &DBRecorder{DB: db} }
|
|
|
|
// Record 落库(失败仅吞错——拦截结论已定,落库失败不反向影响发送判定)。
|
|
func (d *DBRecorder) Record(ctx context.Context, r *Req, b *Block) {
|
|
if d == nil || d.DB == nil || b == nil {
|
|
return
|
|
}
|
|
_ = d.DB.WithContext(ctx).Create(&entity.SendBlockLog{
|
|
TaskNo: r.TaskNo, AccountID: r.AccountID, Mobile: r.Mobile,
|
|
Interceptor: b.Interceptor, Code: b.Code, Msg: b.Msg,
|
|
}).Error
|
|
}
|