Verified Commit d915f380 authored by Cui's avatar Cui

fix compatible with jiguang sms

parent 6923ab00
......@@ -7,12 +7,13 @@ package utils
import (
"crypto/tls"
"github.com/valyala/fasthttp"
"io"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/valyala/fasthttp"
)
func HttpGet(uri string) ([]byte, int, error) {
......@@ -70,3 +71,30 @@ func FasthttpPost(url string, bodyjson []byte) ([]byte, int, error) {
return resp.Body(), resp.StatusCode(), err
}
func FasthttpPostWithAuthorization(authorization, url string, bodyjson []byte) ([]byte, int, error) {
total := 500
req := &fasthttp.Request{}
resp := &fasthttp.Response{}
req.Header.SetMethod("POST")
req.Header.Add("Authorization", "Basic "+authorization)
req.Header.SetContentType("application/json")
req.SetBody(bodyjson)
req.SetRequestURI(url)
c := &fasthttp.Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
Dial: func(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, time.Second*10)
},
MaxConnsPerHost: total,
}
err := c.Do(req, resp)
return resp.Body(), resp.StatusCode(), err
}
package sender
import (
"encoding/base64"
"encoding/json"
"strconv"
"github.com/sirupsen/logrus"
. "gitlab.qingclass.cn/library/utils"
)
const (
JpushSMSSendUri = "https://api.sms.jpush.cn/v1/messages"
JpushCodeSMSSendUri = "https://api.sms.jpush.cn/v1/codes"
)
type SmsError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type JpushSmsResponse struct {
MsgId int `json:"msg_id,omitempty"`
Error SmsError `json:"error,omitempty"`
}
type SmsRequest struct {
Mobile string `json:"mobile"` // 手机号码
SignId string `json:"sign_id,omitempty"` // 签名ID,该字段为空则使用应用默认签名
TempId string `json:"temp_id"` // 模板ID
TempPara map[string]string `json:"temp_para,omitempty"` // 模板参数,需要替换的参数名和 value 的键值对
}
func JpushSmsMessageSender(sm SmsMessage, appKey, masterSecret string) (int, string) {
log := Logger.WithFields(logrus.Fields{
"position": "sms.go",
"func": "SmsMessageSender",
"appKey": appKey,
})
sr := ConvertParam(sm)
reqBody, _ := json.Marshal(sr)
authorization := base64.StdEncoding.EncodeToString([]byte(appKey + ":" + masterSecret))
body, statusCode, err := FasthttpPostWithAuthorization(authorization, JpushSMSSendUri, reqBody)
var res JpushSmsResponse
var errorStr string
_ = json.Unmarshal(body, &res)
if err != nil || res.MsgId == 0 {
log.
WithField("error", err).
Error("SmsMessageSender Error!")
statusCode = -1
if err != nil {
errorStr = "SmsMessageSender Error -" + err.Error()
} else {
errorStr = "SmsMessageSender Error -" + strconv.Itoa(res.Error.Code) + " " + res.Error.Message
}
} else {
log.
WithFields(sm.ToFields()).
WithField("response", string(body)).
Info("SmsMessageSender send success !")
}
return statusCode, errorStr
}
func ConvertParam(sm SmsMessage) (sr SmsRequest) {
sr.Mobile = sm.Tel.Mobile
sr.TempId = strconv.Itoa(sm.TplId)
length := len(sm.Params)
if length > 0 {
sr.TempPara = make(map[string]string)
for i := 0; i < length; i++ {
sr.TempPara[strconv.Itoa(i+1)] = sm.Params[i]
}
}
return
}
......@@ -2,10 +2,11 @@ package sender
import (
"encoding/json"
"github.com/sirupsen/logrus"
. "gitlab.qingclass.cn/library/utils"
"strconv"
"time"
"github.com/sirupsen/logrus"
. "gitlab.qingclass.cn/library/utils"
)
const (
......
......@@ -8,6 +8,8 @@ import (
. "gitlab.qingclass.cn/library/utils"
)
var TencentSmsIds = []int{228304, 230043, 277995, 292569, 292570, 292573, 330224, 330284, 330286, 330461, 103765, 104921, 110327, 228298, 228299, 228300, 228301, 228303}
func Sender(accessToken string, message MessageStruct) (code int, msg string) {
log := Logger.WithFields(logrus.Fields{
"position": "wrapper.go",
......@@ -197,7 +199,12 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
break
}
code, msg = SmsMessageSender(sm, message.SmsAppId, message.SmsAppKey)
// 区分极光短信和腾讯短信,极光短信 id
if indexOf(TencentSmsIds, sm.TplId) != -1 {
code, msg = JpushSmsMessageSender(sm, message.JAppKey, message.JAppSecret)
} else {
code, msg = SmsMessageSender(sm, message.SmsAppId, message.SmsAppKey)
}
break
case "APP":
var am AppMessage
......@@ -225,3 +232,13 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
return
}
func indexOf(slice []int, item int) int {
for i := range slice {
if slice[i] == item {
return i
}
}
return -1
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment