Verified Commit 12e7a5fb authored by Cui's avatar Cui

add sender

parent a3a23bc5
......@@ -7,9 +7,15 @@ package utils
import (
"crypto/rand"
"math"
"math/big"
r "math/rand"
)
func RandInt() int {
return int(math.Round(r.Float64() * 99999))
}
func RandInt64(min, max int64) int64 {
maxBigInt := big.NewInt(max)
i, _ := rand.Int(rand.Reader, maxBigInt)
......
/*
* @Author: cuiweiqiang
* @Date: 2019-01-15 15:06
*/
package utils
import (
"encoding/json"
"github.com/sirupsen/logrus"
"strconv"
"time"
)
const (
CUSTOMURI = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
MASSURI = "https://api.weixin.qq.com/cgi-bin/message/mass/send"
MASSPREVIEWURI = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"
TEMPLATEURI = "https://api.weixin.qq.com/cgi-bin/message/template/send"
SUBSCRIBEURI = "https://api.weixin.qq.com/cgi-bin/message/template/subscribe"
NationCode = "86"
SMSSendUri = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms"
)
type WxResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Msgid float32 `json:"msgid"`
}
func (wr *WxResponse) Tofields() (fields logrus.Fields) {
fields = make(logrus.Fields)
fields["errcode"] = wr.Errcode
fields["errmsg"] = wr.Errmsg
fields["msgid"] = wr.Msgid
return
}
type CustomerMessageInterface interface {
GetToUser() interface{}
ToFieldsCustomerMessage() logrus.Fields
}
func CustomerMessageSender(accessToken string, cmi CustomerMessageInterface, preview bool) (int, string) {
log := Logger.WithFields(logrus.Fields{
"position": "messageSender.go",
"func": "CustomerMessageSender",
"accessToken": accessToken,
})
var sendUri string
switch v := cmi.GetToUser().(type) {
case string:
sendUri = CUSTOMURI
case []string:
sendUri = MASSURI
default:
log.Print("type " + v.(string) + " is not in list")
}
if preview {
sendUri = MASSPREVIEWURI
}
uri := sendUri + "?access_token=" + accessToken
data, _ := json.Marshal(cmi)
body, _, err := FasthttpPost(uri, data)
var result WxResponse
if err != nil {
log.
WithFields(cmi.ToFieldsCustomerMessage()).
Warn("send message to user fail")
result.Errcode = -100001
result.Errmsg = err.Error()
} else {
_ = json.Unmarshal(body, &result)
log.
WithFields(result.Tofields()).
WithFields(cmi.ToFieldsCustomerMessage()).
Info("send message to user")
}
return result.Errcode, result.Errmsg
}
func TemplateMessageSender(accessToken string, tm TemplateMessage) (int, string) {
log := Logger.WithFields(logrus.Fields{
"position": "messageSender.go",
"func": "TemplateMessageSender",
"accessToken": accessToken,
})
uri := TEMPLATEURI + "?access_token=" + accessToken
data, _ := json.Marshal(tm)
body, _, err := FasthttpPost(uri, data)
var result WxResponse
if err != nil {
log.
WithFields(tm.ToFields()).
Warn("send TemplateMessage to user fail")
result.Errcode = -100001
result.Errmsg = err.Error()
} else {
_ = json.Unmarshal(body, &result)
log.
WithFields(result.Tofields()).
WithFields(tm.ToFields()).
Info("send TemplateMessage to user")
}
return result.Errcode, result.Errmsg
}
func SubscribeMessageSender(accessToken string, sm SubscribeMessage) (int, string) {
log := Logger.WithFields(logrus.Fields{
"position": "messageSender.go",
"func": "SubscribeMessageSender",
"accessToken": accessToken,
})
uri := SUBSCRIBEURI + "?access_token=" + accessToken
data, _ := json.Marshal(sm)
body, _, err := FasthttpPost(uri, data)
var result WxResponse
if err != nil {
log.
WithFields(sm.ToFields()).
Warn("send SubscribeMessage to user fail")
result.Errcode = -100001
result.Errmsg = err.Error()
} else {
_ = json.Unmarshal(body, &result)
log.
WithFields(result.Tofields()).
WithFields(sm.ToFields()).
Info("send SubscribeMessage to user")
}
return result.Errcode, result.Errmsg
}
func SmsMessageSender(sm SmsMessage, appId int, appKey string) (int, string) {
log := Logger.WithFields(logrus.Fields{
"position": "messageSender.go",
"func": "SmsMessageSender",
"accessToken": appId,
})
randInt := RandInt()
uri := SMSSendUri + "?sdkappid=" + strconv.Itoa(appId) + "&random=" + strconv.Itoa(randInt)
sm.Sign = ""
sm.Ext = ""
sm.Extend = ""
sm.Time = time.Now().Unix()
sm.Sig = SmsDoSign(appKey, []string{sm.Tel.Mobile}, sm.Time, randInt)
sm.Tel.Nationcode = NationCode
reqBody, _ := json.Marshal(sm)
body, statusCode, err := FasthttpPost(uri, reqBody)
if err != nil {
log.
WithField("error", err).
Error("SmsMessageSender Error!")
} else {
log.
WithFields(sm.ToFields()).
WithField("response", string(body)).
Info("SmsMessageSender send success !")
}
return statusCode, ""
}
This diff is collapsed.
......@@ -6,11 +6,14 @@
package utils
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"github.com/sirupsen/logrus"
"io"
"reflect"
"strconv"
)
func IOClose(fio io.ReadCloser) {
......@@ -21,10 +24,35 @@ func IOClose(fio io.ReadCloser) {
err := fio.Close()
if err != nil {
log.WithField("error", err).Error("io.ReadCloser close error")
log.
WithField("error", err).
Error("io.ReadCloser close error")
}
}
func SmsDoSign(appKey string, tels []string, time int64, rand int) string {
var err error
h := sha256.New()
if len(tels) != 0 {
var telStr string
for _, v := range tels {
telStr = telStr + v + ","
}
telStr = telStr[:len(telStr)-1]
_, err = h.Write([]byte("appkey=" + appKey + "&random=" + strconv.Itoa(rand) + "&time=" + strconv.FormatInt(time, 10) + "&mobile=" + telStr))
if err != nil {
return ""
}
} else {
_, err = h.Write([]byte("appkey=" + appKey + "&random=" + strconv.Itoa(rand) + "&time=" + strconv.FormatInt(time, 10)))
if err != nil {
return ""
}
}
return hex.EncodeToString(h.Sum(nil))
}
// Deep Copy
func Copy(toValue interface{}, fromValue interface{}) (err error) {
var (
......
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