|
|
|
|
package values
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"encoding/xml"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"server/call"
|
|
|
|
|
"server/config"
|
|
|
|
|
"server/util"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/liangdas/mqant/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
SmsChannelAntgst = iota + 1
|
|
|
|
|
SmsChannelBuka
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func APISmsReq(phone, code string) error {
|
|
|
|
|
switch call.GetConfigPlatform().SmsChannel {
|
|
|
|
|
case SmsChannelAntgst:
|
|
|
|
|
return AntgstRequest(phone, code)
|
|
|
|
|
case SmsChannelBuka:
|
|
|
|
|
return BukaRequest(phone, code)
|
|
|
|
|
default:
|
|
|
|
|
return errors.New("unknown channel")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AntgstPhoneCodeReq struct {
|
|
|
|
|
Numbers string `json:"number"` // 手机号
|
|
|
|
|
Sms string `json:"sms"` // 正文内容
|
|
|
|
|
AuthSecret string `json:"authSecret"` // 秘钥
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AntgstResp 短信验证返回
|
|
|
|
|
type AntgstResp struct {
|
|
|
|
|
Result struct {
|
|
|
|
|
SmsID string `json:"smsId"`
|
|
|
|
|
Number string `json:"number"`
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
Result string `json:"result"`
|
|
|
|
|
}
|
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RandomPhoneCode 随机6位验证码
|
|
|
|
|
func RandomPhoneCode() string {
|
|
|
|
|
return strconv.Itoa(rand.Intn(900000) + 100000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AntgstRequest(phone string, code string) error {
|
|
|
|
|
s := fmt.Sprintf(config.GetConfig().Web.OTP.AntgstModel, code)
|
|
|
|
|
// s = `"` + s + `"`
|
|
|
|
|
req := AntgstPhoneCodeReq{
|
|
|
|
|
Numbers: "0091" + phone, // 加上区号
|
|
|
|
|
// Sms: fmt.Sprintf(config.GetConfig().Web.OTP.AntgstModel, `"`+code+`"`),
|
|
|
|
|
Sms: s,
|
|
|
|
|
AuthSecret: config.GetConfig().Web.OTP.AntgstAccessKey + config.GetConfig().Web.OTP.AntgstAccessSecret,
|
|
|
|
|
}
|
|
|
|
|
resp := AntgstResp{}
|
|
|
|
|
err := util.HttpPost(config.GetConfig().Web.OTP.AntgstSmsReqURL, &req, &resp, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if resp.Code != 200 {
|
|
|
|
|
log.Error("req err:%v", resp)
|
|
|
|
|
return errors.New(resp.Msg)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AliSmsResp ali短信验证返回
|
|
|
|
|
type AliSmsResp struct {
|
|
|
|
|
ReturnStatus string `xml:"returnstatus"`
|
|
|
|
|
Message string `xml:"message"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AliRequest(phone string, code string) error {
|
|
|
|
|
// t := int64(common.RedisExpirePhoneCode / (60 * time.Second))
|
|
|
|
|
s := fmt.Sprintf(config.GetConfig().Web.OTP.AliSmsModel, code)
|
|
|
|
|
u := url.Values{}
|
|
|
|
|
u.Set("userid", "1")
|
|
|
|
|
u.Set("account", config.GetConfig().Web.OTP.AliSmsAccount)
|
|
|
|
|
u.Set("password", config.GetConfig().Web.OTP.AliSmsPass)
|
|
|
|
|
u.Set("mobile", "91"+phone)
|
|
|
|
|
u.Set("content", s)
|
|
|
|
|
u.Set("checkcontent", "0")
|
|
|
|
|
url := config.GetConfig().Web.OTP.AliSmsReqURL + "&" + u.Encode()
|
|
|
|
|
log.Debug("AliRequest:%v", url)
|
|
|
|
|
req, err := http.NewRequest("POST", url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
log.Debug("req:%+v", req)
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("http post call err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
ret := AliSmsResp{}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
log.Debug("response Body%v:", string(body))
|
|
|
|
|
if err := xml.Unmarshal(body, &ret); err != nil {
|
|
|
|
|
log.Error("unmarshal fail err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if ret.ReturnStatus != "Success" {
|
|
|
|
|
log.Error("err:%v", ret.Message)
|
|
|
|
|
return errors.New(ret.Message)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =====================================不卡短信
|
|
|
|
|
type BukaPhoneCodeReq struct {
|
|
|
|
|
AppID string `json:"appId"` // appId
|
|
|
|
|
Numbers string `json:"numbers"` // 手机号
|
|
|
|
|
Content string `json:"content"` // 正文内容
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BukaPhoneCodeResp struct {
|
|
|
|
|
Status string `json:"status"` // 0成功
|
|
|
|
|
Reason string `json:"reason"` // 失败原因
|
|
|
|
|
Success string `json:"success"` // 成功数
|
|
|
|
|
Fail string `json:"fail"` // 失败数
|
|
|
|
|
MsgID string `json:"msgId"` // 提交号码对应平台msgId
|
|
|
|
|
Number string `json:"number"` // 提交号码
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BukaRequest(phone string, code string) error {
|
|
|
|
|
s := fmt.Sprintf(config.GetConfig().Web.OTP.BukaModel, code)
|
|
|
|
|
req := BukaPhoneCodeReq{
|
|
|
|
|
AppID: config.GetConfig().Web.OTP.BukaAppID,
|
|
|
|
|
Numbers: "91" + phone, // 加上区号
|
|
|
|
|
Content: s,
|
|
|
|
|
}
|
|
|
|
|
url := config.GetConfig().Web.OTP.BukaUrl
|
|
|
|
|
reqStr, _ := json.Marshal(req)
|
|
|
|
|
log.Debug("Post to:%v,req:%v", url, string(reqStr))
|
|
|
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(reqStr))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
now := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
|
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
|
|
|
|
request.Header.Set("Api-Key", config.GetConfig().Web.OTP.BukaAPIKey)
|
|
|
|
|
request.Header.Set("Timestamp", now)
|
|
|
|
|
request.Header.Set("Sign", util.CalculateMD5(config.GetConfig().Web.OTP.BukaAPIKey+config.GetConfig().Web.OTP.BukaAPISecret+now))
|
|
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
log.Debug("request:%+v", request)
|
|
|
|
|
resp, err := client.Do(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("http post call err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
|
log.Debug("response Body%v:", string(body))
|
|
|
|
|
ret := BukaPhoneCodeResp{}
|
|
|
|
|
if err := json.Unmarshal(body, &ret); err != nil {
|
|
|
|
|
log.Error("unmarshal fail err:%v", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if ret.Status != "0" {
|
|
|
|
|
return fmt.Errorf("%v", ret.Reason)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|