Commit a6615589 authored by Cui's avatar Cui

Merge branch 'dev' into 'master'

Dev

See merge request library/utils!4
parents 24da18ae d915f380
......@@ -4,5 +4,5 @@ require (
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04
github.com/sirupsen/logrus v1.3.0
github.com/valyala/fasthttp v1.0.0
gitlab.qingclass.cn/library/jpush v0.0.2
gitlab.qingclass.cn/library/jpush v0.0.3
)
......@@ -10,8 +10,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.0.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
gitlab.qingclass.cn/library/jpush v0.0.2 h1:GzRWzf/L38wvIxSyWniWwGTse7CrVh6lofa/ZBlGNks=
gitlab.qingclass.cn/library/jpush v0.0.2/go.mod h1:h4wvcwtBQSlZKJhpE2FwcXFLIZREfB7fBNYJSCWdGhE=
gitlab.qingclass.cn/library/jpush v0.0.3 h1:wMeADXoapAWWjjHUCqxMHVRPUakIa9e3A+6MGHJtOAc=
gitlab.qingclass.cn/library/jpush v0.0.3/go.mod h1:h4wvcwtBQSlZKJhpE2FwcXFLIZREfB7fBNYJSCWdGhE=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
......@@ -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
}
......@@ -35,6 +35,14 @@ func AppMessageSender(am AppMessage, appKey, masterSecret string, iosProduction
androidNotification.Title = am.Title
iosNotification := push.NewIosNotification(am.Title)
alert := make(map[string]string)
alert["title"] = am.Title
// alert["subtitle"] = am.SubTitle
alert["body"] = am.SubTitle
iosNotification.Alert = alert
iosNotification.Badge = "+1"
notification.Android = androidNotification
......
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