Initial commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package fengniao
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"stat/common/httputil"
|
||||
"stat/common/log"
|
||||
"stat/common/utils"
|
||||
)
|
||||
|
||||
type requestBody struct {
|
||||
MercId string `json:"mercId"`
|
||||
Secret string `json:"secret"`
|
||||
TemplateId string `json:"templateId"`
|
||||
SmsCode string `json:"smsCode"`
|
||||
Mobile string `json:"mobile"`
|
||||
SignType string `json:"signType"` // 固定sha1
|
||||
Sign string `json:"sign"` // 签名
|
||||
}
|
||||
|
||||
type msg struct {
|
||||
Status string `json:"status"`
|
||||
TaskId string `json:"taskId"`
|
||||
}
|
||||
|
||||
type responseBody struct {
|
||||
Code int `json:"code"`
|
||||
Msg msg `json:"msg"`
|
||||
Err string `json:"err"`
|
||||
}
|
||||
|
||||
type FengNiao struct {
|
||||
Url string
|
||||
MercId string
|
||||
Secret string
|
||||
Tmpl string
|
||||
}
|
||||
|
||||
// Send 发送, 目前蜂鸟貌似只支持数字
|
||||
func (f *FengNiao) Send(mobile string, code string) error {
|
||||
data := fmt.Sprintf("mercId=%s&templateId=%s&smsCode=%s&mobile=%s&secret=%s", f.MercId, f.Tmpl, code, mobile, f.Secret)
|
||||
sha := sha1.New()
|
||||
_, err := sha.Write([]byte(data))
|
||||
if err != nil {
|
||||
log.Error("fengniao send error", log.Any("mobile", mobile), log.Any("templateId", f.Tmpl), log.E(err))
|
||||
return err
|
||||
}
|
||||
|
||||
args, _ := utils.JSONStruct2Map(&requestBody{
|
||||
MercId: f.MercId,
|
||||
Secret: f.Secret,
|
||||
TemplateId: f.Tmpl,
|
||||
SmsCode: code,
|
||||
SignType: "sha1",
|
||||
Mobile: mobile,
|
||||
Sign: fmt.Sprintf("%x", sha.Sum(nil)),
|
||||
})
|
||||
rp := responseBody{}
|
||||
statusCode, err := httputil.Client().POSTJsonWithJResp(&rp, f.Url, nil, args)
|
||||
if err != nil {
|
||||
log.Error("fengniao POSTJsonWithResp err", log.E(err))
|
||||
return err
|
||||
}
|
||||
if statusCode != http.StatusOK {
|
||||
log.Error("fengniao status code not 200")
|
||||
return errors.New("fengniao statusCode not 200")
|
||||
}
|
||||
if rp.Code != 200 {
|
||||
log.Error("fengniao code not 200", log.Any("errdesc", rp.Err))
|
||||
err = errors.New(rp.Err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"stat/common/sms/fengniao"
|
||||
"stat/common/sms/yunpian"
|
||||
"stat/common/ysphone"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Provider = int
|
||||
|
||||
const (
|
||||
tmplChinaId = "5ecfc70ab6a62a37c00baf74" //大陆模版
|
||||
interNationId = "5ecfbfc3b6a62a37c00ba945" //国际模版
|
||||
chinaTmpl = "【PFYH】您的验证码是:%s。请不要把验证码泄露给其他人。"
|
||||
interTmpl = "[YueHui] Your verification code is: %s Please do not disclose the verification code to others."
|
||||
tmplYunPian = "【泡芙官网】您的验证码是:%s。请不要把验证码泄漏给其他人。"
|
||||
)
|
||||
|
||||
const (
|
||||
FengNiao Provider = 1 //蜂鸟
|
||||
YunPian Provider = 2 //云片
|
||||
)
|
||||
|
||||
func sendByFengNiao(mobile string, code string) error {
|
||||
newMobile := ysphone.FormatPhoneNumber(mobile)
|
||||
var tmplId string
|
||||
if strings.HasPrefix(newMobile, "+86") {
|
||||
tmplId = tmplChinaId
|
||||
} else {
|
||||
tmplId = interNationId
|
||||
}
|
||||
return (&fengniao.FengNiao{
|
||||
Url: "https://rise.sudukj.com/api/sms/sendSms",
|
||||
MercId: "10103",
|
||||
Secret: "a7934623f508122fab27373fdee0e410",
|
||||
Tmpl: tmplId,
|
||||
}).Send(newMobile, code)
|
||||
}
|
||||
|
||||
func sendByYunPian(mobile string, code string) error {
|
||||
return (&yunpian.YunPian{
|
||||
Apikey: "a2c52f92f570d839e203ce0c41eb4bc5",
|
||||
Tmpl: tmplYunPian,
|
||||
}).Send(mobile, code)
|
||||
}
|
||||
|
||||
// Send 发送短信
|
||||
func Send(p Provider, mobile string, code string) error {
|
||||
switch p {
|
||||
case FengNiao:
|
||||
return sendByFengNiao(mobile, code)
|
||||
case YunPian:
|
||||
return sendByYunPian(mobile, code)
|
||||
default:
|
||||
return sendByFengNiao(mobile, code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
// log.Init(log.Options{Level: "debug"})
|
||||
err := Send(FengNiao, "+85581359280", "221156")
|
||||
t.Log(err)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package yunpian
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
|
||||
ypclnt "github.com/yunpian/yunpian-go-sdk/sdk"
|
||||
)
|
||||
|
||||
//YunPian 云片短信
|
||||
type YunPian struct {
|
||||
Apikey string
|
||||
Tmpl string
|
||||
}
|
||||
|
||||
//Send 发送验证码
|
||||
func (yun *YunPian) Send(mobile string, code string) error {
|
||||
text := fmt.Sprintf(yun.Tmpl, code)
|
||||
clnt := ypclnt.New(yun.Apikey)
|
||||
param := ypclnt.NewParam(2)
|
||||
param[ypclnt.MOBILE] = mobile
|
||||
param[ypclnt.TEXT] = text
|
||||
resp := clnt.Sms().SingleSend(param)
|
||||
if resp == nil {
|
||||
log.Error("YunPian send error", log.Any("mobile", mobile), log.Any("code", code))
|
||||
return errors.New("SingleSend error")
|
||||
}
|
||||
if !resp.IsSucc() {
|
||||
log.Error("YunPian send fail:", log.Any("resp", resp))
|
||||
return errors.New("SingleSend fail")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user