package sms import ( "encoding/hex" "errors" "log" "net/url" "time" "git.tetele.net/tgo/helper" "github.com/tjfoc/gmsm/sm3" ) /** * 企业信息服务平台--一信通短信,健帆用 * @spCode 企业编号 * @appKey 登录的用户名 * @secretKey 接口业务--接口申请里的秘钥 * @templateId 短信模板id * @mobiles 手机号,多个用逗号隔开 * @content 短信内容 */ func SendByUms(spCode, appKey, secretKey, mobiles, templateId, content string) (bool, []byte, error) { if spCode == "" || appKey == "" || secretKey == "" || mobiles == "" || templateId == "" || content == "" { return false, []byte(""), errors.New("参数不全") } api := "https://api.ums86.com/api/sms/send" timestamp := helper.ToStr(time.Now().UnixNano() / 1e6) num_rand := helper.GetRandomNumber(7) serialNumber := timestamp + num_rand // 短信请求字段 body_data := map[string]string{ "userNumber": mobiles, "templateId": templateId, "messageContent": content, "serialNumber": serialNumber, } // 请求头封装 six_rand := helper.GetRandomString(6) request_id := timestamp + six_rand headers := map[string]string{ "x-sp-code": spCode, "x-app-key": appKey, "x-request-id": request_id, "X-Forwarded-For": "120.78.230.86", } // 签名 sign := UmsSetSign(body_data, headers, secretKey) headers["x-sign"] = sign reply, err := PostJsonData(api, body_data, headers) if err != nil { log.Println("post err", err) return false, reply, err } var reply_data map[string]interface{} err = json.Unmarshal(reply, &reply_data) if err != nil { log.Println(string(reply), err) return false, reply, err } if helper.ToStr(reply_data["code"]) != "0" { return false, reply, errors.New("短信发送失败," + helper.ToStr(reply_data["msg"])) } return true, reply, nil } // ums验签 func UmsSetSign(data, header map[string]string, secretKey string) string { str := helper.HttpBuildQuery(data) // url encode str = url.QueryEscape(str) str = header["x-sp-code"] + header["x-app-key"] + secretKey + str + header["x-request-id"] log.Println("加密前数据: ", str) // ms3加密 hash := sm3.New() hash.Write([]byte(str)) sign_byte := hash.Sum(nil) sign := hex.EncodeToString(sign_byte) log.Println("加密后数据: ", sign) return sign }