Files
go-sms-V2/internal/protocol/sgip/submit.go
T

122 lines
3.9 KiB
Go

// submit.go SGIP 1.2 Submit/Report 包体(Day 33)。
package sgip
import (
"encoding/binary"
"fmt"
)
// SubmitReq SGIP 提交请求(核心字段布局按 1.2 规范)。
type SubmitReq struct {
SPNumber string // SP 接入号 ≤21
ChargeNum string // 计费号码(通常同 UserNumber,可为空)
UserNumbers []string // 接收号码(基础实现每包 1 个)
CorpID string // 企业代码 5B
ServiceType string // 业务类型 10B
FeeType uint8 // FeeType 1B
FeeValue string // 6B
Content []byte
ReportFlag uint8 // 1 要求状态报告
}
// submitFixedLen Submit 定长前缀(至 MessageLength 字段):
// SP21+Charge21+Count1+User21+Corp5+Svc10+FeeType1+FeeValue6+Given6+
// Agent1+MOMT1+Priority1+ReportFlag1+TPpid1+TPudhi1+Coding1+MsgType1 = 101,
// 之后 MessageLength 4B。
const (
submitFixedLen = 101
userOffset = 43 // 21+21+1
msgLenOffset = submitFixedLen
)
// Marshal 编码 Submit 包体。
func (s *SubmitReq) Marshal() []byte {
users := s.UserNumbers
if len(users) == 0 {
users = []string{""}
}
b := make([]byte, submitFixedLen+4+len(s.Content)+8)
copy(b[0:21], s.SPNumber)
copy(b[21:42], s.ChargeNum)
b[42] = uint8(len(users))
copy(b[userOffset:userOffset+21], users[0])
copy(b[64:69], s.CorpID)
copy(b[69:79], s.ServiceType)
b[79] = s.FeeType
copy(b[80:86], s.FeeValue)
copy(b[86:92], s.FeeValue) // GivenValue 同 FeeValue
b[92] = 0 // AgentFlag
b[93] = 2 // MOrelatetoMTFlag(先发短信)
b[94] = 0 // Priority
b[95] = s.ReportFlag
b[96], b[97] = 0, 0 // TP_pid / TP_udhi
b[98] = 8 // MessageCoding:UCS2
b[99] = 0 // MessageType
binary.BigEndian.PutUint32(b[msgLenOffset:msgLenOffset+4], uint32(len(s.Content)))
copy(b[submitFixedLen+4:], s.Content)
return b
}
// ParseSubmit 解析 Submit 包体(网关/模拟器侧)。
func ParseSubmit(body []byte) (sp, user, corp string, content []byte, reportFlag uint8, err error) {
if len(body) < submitFixedLen+4 {
return "", "", "", nil, 0, fmt.Errorf("sgip: submit 包体 %d 字节不足", submitFixedLen+4)
}
sp = trimZero(body[0:21])
user = trimZero(body[userOffset : userOffset+21])
corp = trimZero(body[64:69])
reportFlag = body[95]
n := int(binary.BigEndian.Uint32(body[msgLenOffset : msgLenOffset+4]))
if len(body) < submitFixedLen+4+n {
return "", "", "", nil, 0, fmt.Errorf("sgip: submit content %d 字节越界", n)
}
return sp, user, corp, body[submitFixedLen+4 : submitFixedLen+4+n], reportFlag, nil
}
// Report SGIP 状态报告:SubmitSequenceNumber 12B + ReportType 1 + UserNumber 21 +
// State 1 + ErrorCode 1 + Reserve 8 = 44B。
type Report struct {
SubmitSequence Sequence // 对应提交的三段流水号
ReportType uint8 // 0=发送结果 1=推送
UserNumber string
State uint8 // 0=成功送达
ErrorCode uint8
}
// ReportBodyLen Report 包体长度。
const ReportBodyLen = 12 + 1 + 21 + 1 + 1 + 8
// StateDelivered 送达成功。
const StateDelivered uint8 = 0
// MarshalReport 编码 Report 包体。
func MarshalReport(r *Report) []byte {
b := make([]byte, ReportBodyLen)
binary.BigEndian.PutUint32(b[0:4], r.SubmitSequence.NodeID)
binary.BigEndian.PutUint32(b[4:8], r.SubmitSequence.DateTime)
binary.BigEndian.PutUint32(b[8:12], r.SubmitSequence.Seq)
b[12] = r.ReportType
copy(b[13:34], r.UserNumber)
b[34] = r.State
b[35] = r.ErrorCode
return b
}
// ParseReport 解析 Report 包体。
func ParseReport(body []byte) (*Report, error) {
if len(body) < ReportBodyLen {
return nil, fmt.Errorf("sgip: report 包体 %d 字节不足 %d", len(body), ReportBodyLen)
}
return &Report{
SubmitSequence: Sequence{
NodeID: binary.BigEndian.Uint32(body[0:4]),
DateTime: binary.BigEndian.Uint32(body[4:8]),
Seq: binary.BigEndian.Uint32(body[8:12]),
},
ReportType: body[12],
UserNumber: trimZero(body[13:34]),
State: body[34],
ErrorCode: body[35],
}, nil
}