246 lines
7.7 KiB
Go
246 lines
7.7 KiB
Go
// send_handler.go Day 42:/send/precheck——发送预检(拦截器链真入口)。
|
|
// D18 个性短信工作台提交前逐号预检(DryRun:频次只读不占配额,拦截不落库);
|
|
// Day 46 提交主链复用同一 Chain(非 DryRun,命中落 send_block_log)。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/errors"
|
|
jwtpkg "go-sms/internal/pkg/jwt"
|
|
"go-sms/internal/pkg/response"
|
|
"go-sms/internal/service/interceptor"
|
|
"go-sms/internal/service/send"
|
|
"go-sms/internal/service/template"
|
|
)
|
|
|
|
// registerSendRoutes 注册发送域端点(chain/submit/fileImport 可 nil——未装配时对应端点不注册)。
|
|
func registerSendRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker,
|
|
tplSvc *template.Service, chain *interceptor.Chain, submit *send.SubmitService,
|
|
fileImport *send.FileImportService) {
|
|
h := &sendHandler{tplSvc: tplSvc, chain: chain, submitSvc: submit, importSvc: fileImport}
|
|
gate := func(obj, act string) gin.HandlerFunc {
|
|
return middleware.RequirePermission(perm, obj, act)
|
|
}
|
|
protected.POST("/send/precheck", gate("message", "write"), h.precheck)
|
|
// Day 43 修复循环:最小提交链(受理落库+审核分流;限流/扣费/下发 Day45/46)。
|
|
if submit != nil {
|
|
protected.POST("/send/submit", gate("message", "write"), h.submit)
|
|
}
|
|
// Day 44:号码文件导入(TXT/CSV/XLSX→解析校验去重→MinIO 落对象)。
|
|
if fileImport != nil {
|
|
protected.POST("/files/numbers/import", gate("message", "write"), h.importFile)
|
|
}
|
|
}
|
|
|
|
type sendHandler struct {
|
|
tplSvc *template.Service // 模板渲染(模板短信预检)
|
|
chain *interceptor.Chain // 拦截器链(预检 DryRun)
|
|
submitSvc *send.SubmitService // 提交受理(Day 43 修复循环)
|
|
importSvc *send.FileImportService // 号码文件导入(Day 44)
|
|
}
|
|
|
|
// importFile POST /files/numbers/import(multipart 字段 file;可选 account_id 缺省取 JWT)。
|
|
func (h *sendHandler) importFile(c *gin.Context) {
|
|
fh, err := c.FormFile("file")
|
|
if err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "缺少文件(file 字段)")
|
|
return
|
|
}
|
|
v, _ := c.Get(middleware.ContextKeyClaims)
|
|
claims, _ := v.(*middleware.Claims)
|
|
accountID := int64(0)
|
|
if claims != nil {
|
|
accountID = claims.AccountID
|
|
}
|
|
if formID := c.PostForm("account_id"); formID != "" {
|
|
if id, perr := strconv.ParseInt(formID, 10, 64); perr == nil {
|
|
accountID = id
|
|
}
|
|
}
|
|
if accountID <= 0 {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "account_id 缺失")
|
|
return
|
|
}
|
|
f, err := fh.Open()
|
|
if err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "文件读取失败: "+err.Error())
|
|
return
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
res, err := h.importSvc.Import(c.Request.Context(), f, fh.Filename, fh.Size, accountID)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, res)
|
|
}
|
|
|
|
// BlockItem 单号码拦截结果。
|
|
type BlockItem struct {
|
|
Mobile string `json:"mobile"`
|
|
Interceptor string `json:"interceptor"`
|
|
Code string `json:"code"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
// PrecheckResp 预检结果。
|
|
type PrecheckResp struct {
|
|
Total int `json:"total"`
|
|
Passed int `json:"passed"`
|
|
Blocked []BlockItem `json:"blocked"` // 空=全过
|
|
}
|
|
|
|
// precheck POST /send/precheck(SubmitReq;模板短信自动渲染)→ 逐号过拦截链(DryRun)。
|
|
func (h *sendHandler) precheck(c *gin.Context) {
|
|
var req send.SubmitReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
var tpl *entity.TemplateSMS
|
|
if req.TemplateID > 0 {
|
|
if h.tplSvc == nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "模板服务未装配")
|
|
return
|
|
}
|
|
t, err := h.tplSvc.GetByID(c.Request.Context(), req.TemplateID)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
tpl = t
|
|
}
|
|
// 基础校验(号码合法/类型/内容/变量)+渲染,复用 Day41 校验器同源逻辑。
|
|
if _, err := req.Validate(tpl); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
content := req.Content
|
|
if tpl != nil {
|
|
rendered, err := h.tplSvc.Render(tpl, req.Variables)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
content = rendered
|
|
}
|
|
resp := PrecheckResp{Total: len(req.Mobiles), Blocked: []BlockItem{}}
|
|
if h.chain == nil { // 未装配拦截链:基础校验即通过
|
|
resp.Passed = resp.Total
|
|
response.Success(c, resp)
|
|
return
|
|
}
|
|
// Day51 R3:批量预检同样经请求级快照(白名单/黑名单 IN 批查,策略跨号复用)。
|
|
sc := h.chain.NewScope(c.Request.Context(), req.AccountID, req.Mobiles)
|
|
for _, m := range req.Mobiles {
|
|
b := h.chain.Check(c.Request.Context(), &interceptor.Req{
|
|
TaskNo: req.TaskNo, AccountID: req.AccountID, Mobile: m,
|
|
Content: content, SendType: req.SendType, DryRun: true, Scope: sc,
|
|
})
|
|
if b == nil {
|
|
resp.Passed++
|
|
continue
|
|
}
|
|
resp.Blocked = append(resp.Blocked, BlockItem{
|
|
Mobile: m, Interceptor: b.Interceptor, Code: b.Code, Msg: b.Msg})
|
|
}
|
|
response.Success(c, resp)
|
|
}
|
|
|
|
// SubmitResp 提交受理响应。
|
|
type SubmitResp struct {
|
|
TaskNo string `json:"task_no"`
|
|
TaskID int64 `json:"task_id"`
|
|
AuditStatus int8 `json:"audit_status"`
|
|
AuditHint string `json:"audit_hint"`
|
|
ValidCount int `json:"valid_count"`
|
|
Segments int `json:"segments"`
|
|
TotalFee int `json:"total_fee"`
|
|
ScheduleAt string `json:"schedule_at,omitempty"` // RFC3339(定时任务)
|
|
}
|
|
|
|
// auditHint 审核态文案(响应提示;0 待人工/1 过/2 拒/3 免审)。
|
|
func auditHint(s int8) string {
|
|
switch s {
|
|
case 1:
|
|
return "审核通过,等待调度"
|
|
case 2:
|
|
return "审核拒绝(命中敏感词)"
|
|
case 3:
|
|
return "免审受理,等待调度"
|
|
default:
|
|
return "已受理,待人工审核(48h 未审自动退回)"
|
|
}
|
|
}
|
|
|
|
// submit POST /send/submit——受理发送任务(Day 43 修复循环:最小提交链;
|
|
// 校验→计费→余额只读校验→落任务+明细→审核分流;限流/扣费/下发 Day45/46)。
|
|
func (h *sendHandler) submit(c *gin.Context) {
|
|
var req send.SubmitReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
// 账号:请求未带则取 JWT(管理端代提交可显式指定;客户自提交缺省本人)。
|
|
req.Source = send.TaskSourceWeb // Day 60:Web 控制台提交打标(内部字段,请求体不可覆写)
|
|
if req.AccountID == 0 {
|
|
if v, ok := c.Get(middleware.ContextKeyClaims); ok {
|
|
if claims, ok := v.(*jwtpkg.Claims); ok {
|
|
req.AccountID = claims.AccountID
|
|
}
|
|
}
|
|
}
|
|
if req.AccountID == 0 {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "缺少账号(account_id)")
|
|
return
|
|
}
|
|
var tpl *entity.TemplateSMS
|
|
if req.TemplateID > 0 {
|
|
if h.tplSvc == nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "模板服务未装配")
|
|
return
|
|
}
|
|
t, err := h.tplSvc.GetByID(c.Request.Context(), req.TemplateID)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
tpl = t
|
|
}
|
|
// 渲染内容(直发=原样;模板=Render),与 estimate/precheck 同源。
|
|
if _, err := req.Validate(tpl); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
content := req.Content
|
|
if tpl != nil {
|
|
rendered, err := h.tplSvc.Render(tpl, req.Variables)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
content = rendered
|
|
}
|
|
res, err := h.submitSvc.Submit(c.Request.Context(), &req, tpl, content, c.ClientIP())
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
resp := SubmitResp{
|
|
TaskNo: res.Task.TaskNo, TaskID: res.Task.ID,
|
|
AuditStatus: res.Task.AuditStatus, AuditHint: auditHint(res.Task.AuditStatus),
|
|
ValidCount: res.Estimate.ValidCount, Segments: res.Estimate.Segments,
|
|
TotalFee: res.Estimate.TotalFee,
|
|
}
|
|
if res.Task.ScheduleAt != nil {
|
|
resp.ScheduleAt = res.Task.ScheduleAt.Format(time.RFC3339)
|
|
}
|
|
response.Success(c, resp)
|
|
}
|