package submail import ( "encoding/json" "servicebase/pkg/utils" "sort" "github.com/spf13/viper" ) const ( // submailUrl = "https://api.mysubmail.com/message/xsend.json" // submailAppId = "105610" // submialAppKey = "6fec871bd91b4184cec9a6840bc55a31" // submailSignType = "normal" // // 国际短信 // internationalUrl = "https://api-v4.mysubmail.com/internationalsms/xsend" // internationalAppId = "62587" // internationalAppKey = "7b1e063c06d45b29a2490e61363981c4" ) 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 }