132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
package submail
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sort"
|
|
|
|
"gitea.ddegame.cn/open/servicebase/pkg/utils"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type SubmailClient struct {
|
|
ToMobile string
|
|
TplId string
|
|
Vars map[string]string
|
|
}
|
|
|
|
type SubmailResponse struct {
|
|
Status string `json:"status"`
|
|
SendId string `json:"send_id"`
|
|
Fee int `json:"fee"`
|
|
SmsCredits string `json:"sms_credits"`
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
// 用Submail发送短信
|
|
func (s *SubmailClient) SendMessageBySubmail() (submailRes SubmailResponse) {
|
|
|
|
if len(s.ToMobile) == 0 || len(s.TplId) == 0 {
|
|
panic("缺少手机号或模板ID")
|
|
}
|
|
|
|
parameters := make(map[string]string)
|
|
parameters["appid"] = viper.GetString("submail.sms.appId")
|
|
parameters["to"] = s.ToMobile
|
|
parameters["project"] = s.TplId
|
|
parameters["sign_type"] = viper.GetString("submail.sms.signType")
|
|
|
|
if len(s.Vars) > 0 {
|
|
varbyte, err := json.Marshal(s.Vars)
|
|
if err != nil {
|
|
return
|
|
}
|
|
parameters["vars"] = string(varbyte)
|
|
}
|
|
|
|
keys := make([]string, 0, len(parameters)-1)
|
|
|
|
for k := range parameters {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
//对key按asc升序
|
|
sort.Strings(keys)
|
|
|
|
//待签名字符串
|
|
waitSignString := ""
|
|
for _, value := range keys {
|
|
waitSignString += value + "=" + parameters[value] + "&"
|
|
}
|
|
strlen := len(waitSignString)
|
|
if strlen > 0 {
|
|
waitSignString = waitSignString[:strlen-1]
|
|
}
|
|
|
|
waitSignString += "&signature=" + viper.GetString("submail.sms.appKey")
|
|
|
|
res, err1 := utils.HttpPost(viper.GetString("submail.sms.url"), waitSignString)
|
|
|
|
if err1 != nil {
|
|
return
|
|
}
|
|
|
|
_ = json.Unmarshal([]byte(res), &submailRes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 用Submail发送国际短信
|
|
func (s *SubmailClient) SendInternationalMessageBySubmail() (submailRes SubmailResponse) {
|
|
|
|
if len(s.ToMobile) == 0 || len(s.TplId) == 0 {
|
|
panic("缺少手机号或模板ID")
|
|
}
|
|
|
|
parameters := make(map[string]string)
|
|
parameters["appid"] = viper.GetString("submail.international.appId")
|
|
parameters["to"] = s.ToMobile
|
|
parameters["project"] = s.TplId
|
|
parameters["sign_type"] = viper.GetString("submail.international.signType")
|
|
|
|
if len(s.Vars) > 0 {
|
|
varbyte, err := json.Marshal(s.Vars)
|
|
if err != nil {
|
|
return
|
|
}
|
|
parameters["vars"] = string(varbyte)
|
|
}
|
|
|
|
keys := make([]string, 0, len(parameters)-1)
|
|
|
|
for k := range parameters {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
//对key按asc升序
|
|
sort.Strings(keys)
|
|
|
|
//待签名字符串
|
|
waitSignString := ""
|
|
for _, value := range keys {
|
|
waitSignString += value + "=" + parameters[value] + "&"
|
|
}
|
|
strlen := len(waitSignString)
|
|
if strlen > 0 {
|
|
waitSignString = waitSignString[:strlen-1]
|
|
}
|
|
|
|
waitSignString += "&signature=" + viper.GetString("submail.international.appKey")
|
|
|
|
res, err1 := utils.HttpPost(viper.GetString("submail.international.url"), waitSignString)
|
|
|
|
if err1 != nil {
|
|
return
|
|
}
|
|
|
|
_ = json.Unmarshal([]byte(res), &submailRes)
|
|
return
|
|
}
|