This commit is contained in:
hoteas 2017-10-27 09:14:15 +00:00
parent 7423a7dd57
commit 3507d2ed74
2 changed files with 102 additions and 91 deletions

View File

@ -1,89 +1,90 @@
package ddsms package ddsms
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
) )
type DDY struct { type DDY struct {
ApiKey string ApiKey string
YzmUrl string YzmUrl string
TzUrl string TzUrl string
} }
func (this *DDY) Init(apikey string, uzmurl string, tzurl string) { func (this *DDY) Init(apikey string) {
this.ApiKey = apikey this.ApiKey = apikey
this.YzmUrl = uzmurl this.YzmUrl = "https://api.dingdongcloud.com/v1/sms/captcha/send"
this.TzUrl = tzurl this.TzUrl = "https://api.dingdongcloud.com/v1/sms/notice/multi_send"
} }
//发送短信验证码 code验证码如123456 返回true表示发送成功flase表示发送失败 //发送短信验证码 code验证码如123456 返回true表示发送成功flase表示发送失败
func (this *DDY) SendYZM(umoblie string, tpt string, data map[string]string) (bool, error) { func (this *DDY) SendYZM(umoblie string, tpt string, data map[string]string) (bool, error) {
for k, v := range data { for k, v := range data {
tpt = strings.Replace(tpt, "{"+k+"}", v, -1) tpt = strings.Replace(tpt, "{"+k+"}", v, -1)
} }
return this.send(this.YzmUrl, umoblie, tpt)
} return this.send(this.YzmUrl, umoblie, tpt)
}
//发送通知
func (this *DDY) SendTz(umoblie []string, tpt string, data map[string]string) (bool, error) { //发送通知
for k, v := range data { func (this *DDY) SendTz(umoblie []string, tpt string, data map[string]string) (bool, error) {
tpt = strings.Replace(tpt, "["+k+"]", v, -1) for k, v := range data {
} tpt = strings.Replace(tpt, "["+k+"]", v, -1)
umobleStr := "" }
for i, v := range umoblie { umobleStr := ""
if i == 0 { for i, v := range umoblie {
umobleStr = v if i == 0 {
continue umobleStr = v
} continue
umobleStr += "," + v }
} umobleStr += "," + v
return this.send(this.TzUrl, umobleStr, tpt) }
} return this.send(this.TzUrl, umobleStr, tpt)
}
//发送短信
func (this *DDY) send(mUrl string, umoblie string, content string) (bool, error) { //发送短信
func (this *DDY) send(mUrl string, umoblie string, content string) (bool, error) {
data_send_sms_yzm := url.Values{"apikey": {this.ApiKey}, "mobile": {umoblie}, "content": {content}}
res, err := this.httpsPostForm(mUrl, data_send_sms_yzm) data_send_sms_yzm := url.Values{"apikey": {this.ApiKey}, "mobile": {umoblie}, "content": {content}}
if err != nil && res == "" { res, err := this.httpsPostForm(mUrl, data_send_sms_yzm)
return false, errors.New("连接错误") if err != nil && res == "" {
} return false, errors.New("连接错误")
}
var msg interface{}
err1 := json.Unmarshal([]byte(res), &msg) var msg interface{}
if err1 != nil { err1 := json.Unmarshal([]byte(res), &msg)
return false, errors.New("json解析错误") if err1 != nil {
} return false, errors.New("json解析错误")
}
resmsg := msg.(map[string]interface{})
rcode := int(resmsg["code"].(float64)) resmsg := msg.(map[string]interface{})
result := resmsg["msg"].(string) rcode := int(resmsg["code"].(float64))
if rcode != 1 { result := resmsg["msg"].(string)
return false, errors.New(result) if rcode != 1 {
} return false, errors.New(result)
}
return true, nil
} return true, nil
}
//调用url发送短信的连接
func (this *DDY) httpsPostForm(url string, data url.Values) (string, error) { //调用url发送短信的连接
resp, err := http.PostForm(url, data) func (this *DDY) httpsPostForm(url string, data url.Values) (string, error) {
resp, err := http.PostForm(url, data)
if err != nil {
return "", err if err != nil {
} return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close()
if err != nil { body, err := ioutil.ReadAll(resp.Body)
return "", err if err != nil {
} return "", err
}
return string(body), nil
return string(body), nil
}
}

View File

@ -22,8 +22,18 @@ func ObjToMap(obj interface{}, e ...*Error) Map {
case map[string]interface{}: case map[string]interface{}:
v = obj.(map[string]interface{}) v = obj.(map[string]interface{})
default: default:
v = nil data, err := json.Marshal(obj)
err = errors.New("没有合适的转换对象!") if err != nil {
err = errors.New("没有合适的转换对象!" + err.Error())
v = nil
}
v = Map{}
e := json.Unmarshal(data, &v)
if e != nil {
err = errors.New("没有合适的转换对象!" + e.Error())
v = nil
}
} }
} }
if len(e) != 0 { if len(e) != 0 {