hotime/dri/ddsms/ddsms.go

94 lines
2.1 KiB
Go
Raw Normal View History

2017-10-27 09:14:15 +00:00
package ddsms
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
2018-11-06 14:57:53 +00:00
//"fmt"
2017-10-27 09:14:15 +00:00
)
2022-03-12 17:48:54 +00:00
type dingdongyun struct {
2017-10-27 09:14:15 +00:00
ApiKey string
YzmUrl string
TzUrl string
}
2022-03-12 17:48:54 +00:00
var DDY = dingdongyun{}
func (that *dingdongyun) Init(apikey string) {
that.ApiKey = apikey
that.YzmUrl = "https://api.dingdongcloud.com/v2/sms/captcha/send.json"
that.TzUrl = "https://api.dingdongcloud.com/v2/sms/notice/send.json"
2017-10-27 09:14:15 +00:00
}
2022-03-12 17:48:54 +00:00
// SendYZM 发送短信验证码 code验证码如123456 返回true表示发送成功flase表示发送失败
func (that *dingdongyun) SendYZM(umoblie string, tpt string, data map[string]string) (bool, error) {
2017-10-27 09:14:15 +00:00
for k, v := range data {
tpt = strings.Replace(tpt, "{"+k+"}", v, -1)
}
2022-03-12 17:48:54 +00:00
return that.send(that.YzmUrl, umoblie, tpt)
2017-10-27 09:14:15 +00:00
}
2022-03-12 17:48:54 +00:00
// SendTz 发送通知
func (that *dingdongyun) SendTz(umoblie []string, tpt string, data map[string]string) (bool, error) {
2017-10-27 09:14:15 +00:00
for k, v := range data {
2021-10-21 14:52:40 +00:00
tpt = strings.Replace(tpt, "{"+k+"}", v, -1)
2017-10-27 09:14:15 +00:00
}
umobleStr := ""
for i, v := range umoblie {
if i == 0 {
umobleStr = v
continue
}
umobleStr += "," + v
}
2022-03-12 17:48:54 +00:00
return that.send(that.TzUrl, umobleStr, tpt)
2017-10-27 09:14:15 +00:00
}
//发送短信
2022-03-12 17:48:54 +00:00
func (that *dingdongyun) send(mUrl string, umoblie string, content string) (bool, error) {
2017-10-27 09:14:15 +00:00
2022-03-12 17:48:54 +00:00
data_send_sms_yzm := url.Values{"apikey": {that.ApiKey}, "mobile": {umoblie}, "content": {content}}
res, err := that.httpsPostForm(mUrl, data_send_sms_yzm)
2017-10-27 09:14:15 +00:00
if err != nil && res == "" {
return false, errors.New("连接错误")
}
var msg interface{}
err1 := json.Unmarshal([]byte(res), &msg)
if err1 != nil {
return false, errors.New("json解析错误")
}
2018-11-06 14:57:53 +00:00
//fmt.Println(msg)
2017-10-27 09:14:15 +00:00
resmsg := msg.(map[string]interface{})
rcode := int(resmsg["code"].(float64))
result := resmsg["msg"].(string)
if rcode != 1 {
return false, errors.New(result)
}
return true, nil
}
//调用url发送短信的连接
2022-03-12 17:48:54 +00:00
func (that *dingdongyun) httpsPostForm(url string, data url.Values) (string, error) {
2017-10-27 09:14:15 +00:00
resp, err := http.PostForm(url, data)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}