Browse Source

增加腾讯云调用方法

master v0.2.0
guzeng 3 years ago
parent
commit
79d0a701a4
2 changed files with 96 additions and 0 deletions
  1. +76
    -0
      tencent.client.go
  2. +20
    -0
      tencent.client_test.go

+ 76
- 0
tencent.client.go View File

@ -0,0 +1,76 @@
package smsrpc
import (
"encoding/json"
"errors"
"log"
)
/**
* 发送腾讯云短信
* @secretId 腾讯云密钥id
* @secretKey 腾讯云密钥key
* @signname 签名
* @templateId 模板id
* @mobiles 手机号多个用逗号隔开
* @params 参数值
*/
func SendTencentSms(secretId, secretKey, smsAppId string, signname, templateId string, mobiles, params []string, url ...string) ([]map[string]interface{}, error) {
if secretId == "" || secretKey == "" || signname == "" || templateId == "" {
return nil, errors.New("参数错误")
}
if len(mobiles) < 1 || len(params) < 1 {
return nil, errors.New("参数错误")
}
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
mobile, err := json.Marshal(mobiles)
if err != nil {
return nil, err
}
param, err := json.Marshal(params)
if err != nil {
return nil, err
}
arg := TencentParam{secretId, secretKey, smsAppId, signname, string(mobile), templateId, string(param)}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.SendTencentMsg(req, res)
if err != nil {
return nil, err
}
res_data_de, err := GetResData(res)
log.Println(res_data_de, err)
if err != nil {
return nil, err
}
if res_data_de == "" {
return nil, nil
}
var res_arr []map[string]interface{}
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return res_arr, nil
}

+ 20
- 0
tencent.client_test.go View File

@ -0,0 +1,20 @@
package smsrpc
import (
"testing"
)
func Test_SendTencentSms(t *testing.T) {
secretId := "AKIDGQL0OVLuKTMyKPDk11kCOJyMSHqY0C7l"
secretKey := "OWOzJM9mGbiaBVYKp30g5C2cvwg9LHwK"
smsAppId := "1400536841"
signname := "优品猫"
templateId := "1166786"
mobiles := []string{"18607565510"}
params := []string{"290901", "10"}
ret, err := SendTencentSms(secretId, secretKey, smsAppId, signname, templateId, mobiles, params)
t.Log(ret)
t.Log(err)
}

Loading…
Cancel
Save