220 lines
9.0 KiB
Go
220 lines
9.0 KiB
Go
// Package send Day 41(阶段 4 起步):发送域——Submit DTO+发送校验器+计费预估。
|
||
// Day 44 群发/个性/文件服务与 Day 46 HTTP API 提交接口复用本包校验。
|
||
package send
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
"strings"
|
||
"unicode/utf8"
|
||
|
||
"go-sms/internal/model/entity"
|
||
errs "go-sms/internal/pkg/errors"
|
||
"go-sms/internal/util"
|
||
)
|
||
|
||
// 发送类型(与 MessageTask.SendType 对齐)。
|
||
const (
|
||
SendTypeBatch int8 = 1 // 群发
|
||
SendTypePersonal int8 = 2 // 个性(变量)
|
||
SendTypeTimer int8 = 3 // 定时
|
||
SendTypeFile int8 = 4 // 文件
|
||
)
|
||
|
||
// MaxMobiles 单次提交号码上限。
|
||
const MaxMobiles = 10000
|
||
|
||
// MaxTextLen 内容上限(字符;账号级上限 Day42 拦截器接入,平台级先 1000)。
|
||
const MaxTextLen = 1000
|
||
|
||
// SubmitReq 发送提交 DTO(HTTP API/内部服务共用)。
|
||
type SubmitReq struct {
|
||
TaskNo string `json:"task_no"` // 幂等键(缺省服务端生成)
|
||
AccountID int64 `json:"account_id"`
|
||
SendType int8 `json:"send_type"` // 1群发 2个性 3定时 4文件
|
||
TemplateID int64 `json:"template_id"` // 模板短信必填
|
||
Mobiles []string `json:"mobiles"` // 号码列表(个性短信=变量表,Day44)
|
||
Content string `json:"content"` // 直发内容;模板短信可为空
|
||
Variables map[string]string `json:"variables"` // 模板短信变量(群发单值)
|
||
Sign string `json:"sign"` // 签名(如「公司名」或含于内容)
|
||
SendAt string `json:"send_at"` // 定时(YYYY-MM-DD HH:mm;立即发送为空)
|
||
Extend string `json:"extend"` //扩展码透传
|
||
CallData string `json:"call_data"` // Day 46:用户回传数据(≤64,回执推送带回)
|
||
Business string `json:"business"` // Day 49:sms(默认)/video 视频短信(biz_type 路由;账号须开通)
|
||
Source int8 `json:"-"` // Day 60:受理来源打标(内部入口设置,不收外部输入;0→API 归一)
|
||
PresetMsgID int64 `json:"-"` // 内部:预置首条 DetailID(入向长短信聚合组ID:片级 Resp/Deliver 回执/明细三口径同 ID;0=雪花默认;不收外部输入)
|
||
// Day 70:计费口径分流(批次 2344043 少计 1 条根修)——CMPP 入向的真实
|
||
// 上游片数与 GBK 出向拆分在 ASCII 混合内容下背离,fee≡上游真实片数
|
||
// (决策②本意)须按来源编码分流;两字段仅内部链路设置,不收外部输入。
|
||
ContentEncoding int8 `json:"-"` // 0=默认(GBK 口径,出向 SplitContent 同构);1=UCS2(MsgFmt=8)
|
||
FeeSegments int `json:"-"` // 计费段数覆写(入向长短信聚合=客户端 pk_total=运营商计费依据;0=按口径计算)
|
||
}
|
||
|
||
// 计费口径编码(Day 70)。
|
||
const (
|
||
ContentEncodingDefault int8 = 0 // GBK/出向拆分口径(ASCII 1B/GBK 2B,134B 预算)
|
||
ContentEncodingUCS2 int8 = 1 // UCS2 运营商口径(每码元 2B,70 码元单片/67 码元每片)
|
||
)
|
||
|
||
// 业务类型(Day 49 biz_type 路由;与技术方案 Kafka 消息 Business 字段对齐)。
|
||
const (
|
||
BusinessSMS = "sms"
|
||
BusinessVideo = "video"
|
||
)
|
||
|
||
// 任务受理来源(Day 60 · D24-25 下行明细页筛选维度;message_task.source)。
|
||
const (
|
||
TaskSourceAPI int8 = 1 // 外部 HTTP API(openapi sendMessageMass/One)
|
||
TaskSourceCMPP int8 = 2 // CMPP 协议接入(access.Submit)
|
||
TaskSourceSMPP int8 = 3 // SMPP 协议接入(access.SubmitSM)
|
||
TaskSourceWeb int8 = 4 // Web 控制台(/send/submit)
|
||
)
|
||
|
||
// NormalizeTaskSource 打标归一(0/非法→API 默认;入口显式传入,不收外部请求体)。
|
||
func NormalizeTaskSource(s int8) int8 {
|
||
if s < TaskSourceAPI || s > TaskSourceWeb {
|
||
return TaskSourceAPI
|
||
}
|
||
return s
|
||
}
|
||
|
||
// Estimate 计费预估(供 D17 群发工作台实时显示;与后端 SmsLength 同源)。
|
||
type Estimate struct {
|
||
ValidCount int `json:"valid_count"` // 合格号码数
|
||
Chars int `json:"chars"` // 字符数
|
||
Segments int `json:"segments"` // 单号码计费条数
|
||
TotalFee int `json:"total_fee"` // 总条数=ValidCount×Segments
|
||
}
|
||
|
||
// Validate 发送校验(拒绝路径逐条返回 ISC:400):
|
||
// 号码非空/合法/去重上限 · 类型合法 · 内容(直发非空≤MaxTextLen/模板短信渲染非空) ·
|
||
// 变量(直发禁变量/模板缺参拒绝) · 签名(非空,建议【】包裹;白名单校验 Day42 签名拦截器)。
|
||
// tpl 为模板短信时必传(通过态),nil 表示直发。
|
||
func (r *SubmitReq) Validate(tpl *entity.TemplateSMS) (*Estimate, error) {
|
||
// ---- 号码 ----
|
||
if len(r.Mobiles) == 0 {
|
||
return nil, errs.InvalidParam.WithMsg("号码列表为空")
|
||
}
|
||
if len(r.Mobiles) > MaxMobiles {
|
||
return nil, errs.InvalidParam.WithMsg(fmt.Sprintf("号码 %d 超单次上限 %d", len(r.Mobiles), MaxMobiles))
|
||
}
|
||
seen := make(map[string]struct{}, len(r.Mobiles))
|
||
valid := make([]string, 0, len(r.Mobiles))
|
||
for _, m := range r.Mobiles {
|
||
n := util.NormalizeMobile(m)
|
||
if !util.IsValidMainlandMobile(n) {
|
||
return nil, errs.InvalidParam.WithMsg("非法手机号: " + m)
|
||
}
|
||
if _, dup := seen[n]; !dup {
|
||
seen[n] = struct{}{}
|
||
valid = append(valid, n)
|
||
}
|
||
}
|
||
|
||
// ---- 类型 ----
|
||
if r.SendType == 0 {
|
||
r.SendType = SendTypeBatch
|
||
}
|
||
// ---- 业务类型(Day 49 biz_type 路由):空规范化为 sms;账号开通校验在
|
||
// Submit 落库前查 AccountConfig;video 内容合帧校验在内容合并后。----
|
||
switch r.Business = strings.TrimSpace(r.Business); r.Business {
|
||
case "":
|
||
r.Business = BusinessSMS
|
||
case BusinessSMS, BusinessVideo:
|
||
default:
|
||
return nil, errs.InvalidParam.WithMsg("business 仅支持 sms/video: " + r.Business)
|
||
}
|
||
if r.SendType < SendTypeBatch || r.SendType > SendTypeFile {
|
||
return nil, errs.InvalidParam.WithMsg(fmt.Sprintf("发送类型 %d 非法", r.SendType))
|
||
}
|
||
if r.SendType == SendTypeTimer && r.SendAt == "" {
|
||
return nil, errs.InvalidParam.WithMsg("定时发送须指定 send_at")
|
||
}
|
||
|
||
// ---- 签名(可选:Day41 验收二变——UI 去独立签名框,内容=签名+正文一体;
|
||
// sign 传空时内容须自带签名(如【公司名】),白名单校验 Day42 拦截器从内容提取) ----
|
||
if len([]rune(r.Sign)) > 32 {
|
||
return nil, errs.InvalidParam.WithMsg("签名超 32 字")
|
||
}
|
||
|
||
// ---- 内容与变量 ----
|
||
content := r.Content
|
||
if r.TemplateID > 0 {
|
||
if tpl == nil {
|
||
return nil, errs.InvalidParam.WithMsg("模板不存在")
|
||
}
|
||
if tpl.Status != 1 {
|
||
return nil, errs.InvalidParam.WithMsg("模板未过审,不可发送")
|
||
}
|
||
rendered, err := renderVars(tpl.Content, r.Variables)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
content = rendered
|
||
if vars := util.ExtractVariables(r.Sign); len(vars) > 0 {
|
||
return nil, errs.InvalidParam.WithMsg("签名含变量占位符 " + strings.Join(vars, ",") + ",签名须为固定文本")
|
||
}
|
||
} else {
|
||
if content == "" {
|
||
return nil, errs.InvalidParam.WithMsg("内容为空")
|
||
}
|
||
}
|
||
// Day41 验收 #2/#3 需求:签名与内容合并计入短信内容(原样拼接,零包装),
|
||
// 字数/条数/长度上限全部按合并口径。
|
||
merged := r.Sign + content
|
||
if vars := util.ExtractVariables(merged); len(vars) > 0 && r.TemplateID == 0 {
|
||
return nil, errs.InvalidParam.WithMsg("直发内容含变量占位符 " + strings.Join(vars, ",") + ",请走模板短信")
|
||
}
|
||
if n := len([]rune(merged)); n > MaxTextLen {
|
||
return nil, errs.InvalidParam.WithMsg(fmt.Sprintf("签名+内容 %d 字超上限 %d", n, MaxTextLen))
|
||
}
|
||
// Day 49:视频短信合帧要素——文字+视频 URL(URL 可来自模板变量渲染)。
|
||
if r.Business == BusinessVideo && !strings.Contains(merged, "http://") && !strings.Contains(merged, "https://") {
|
||
return nil, errs.InvalidParam.WithMsg("视频短信内容须包含视频 URL(http/https,文字+URL 合帧)")
|
||
}
|
||
|
||
// ---- 预估(合并口径;Day 70 按来源编码分流) ----
|
||
// FeeSegments>0:上游已知段数直接采信(入向长短信聚合=客户端 pk_total,
|
||
// 运营商即按此计费——比重算任何口径都准);UCS2 来源按 UCS2 口径;
|
||
// 默认 GBK 口径(出向 SplitContent 同构=我方真实拆分片数)。
|
||
var chars, seg int
|
||
switch {
|
||
case r.FeeSegments > 0:
|
||
chars, seg = utf8.RuneCountInString(merged), r.FeeSegments
|
||
case r.ContentEncoding == ContentEncodingUCS2:
|
||
chars, seg = util.SmsLengthUCS2(merged)
|
||
default:
|
||
chars, seg = util.SmsLength(merged)
|
||
}
|
||
return &Estimate{ValidCount: len(valid), Chars: chars, Segments: seg, TotalFee: len(valid) * seg}, nil
|
||
}
|
||
|
||
// renderVars 模板渲染(缺参拒绝;多余变量忽略)。
|
||
func renderVars(tplContent string, vars map[string]string) (string, error) {
|
||
for _, name := range util.ExtractVariables(tplContent) {
|
||
if _, ok := vars[name]; !ok {
|
||
return "", errs.InvalidParam.WithMsg("缺少模板变量 {" + name + "}")
|
||
}
|
||
}
|
||
out := tplContent
|
||
for k, v := range vars {
|
||
out = strings.ReplaceAll(out, "{"+k+"}", v)
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
// SortedMobiles 归一去重后的有序号码(测试与落库批次用)。
|
||
func SortedMobiles(mobiles []string) []string {
|
||
seen := make(map[string]struct{}, len(mobiles))
|
||
out := make([]string, 0, len(mobiles))
|
||
for _, m := range mobiles {
|
||
n := util.NormalizeMobile(m)
|
||
if _, ok := seen[n]; !ok {
|
||
seen[n] = struct{}{}
|
||
out = append(out, n)
|
||
}
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
}
|