Browse Source

增加创蓝短信发送接口

master v1.1.0
guzeng 1 year ago
parent
commit
88657cb9ad
6 changed files with 192 additions and 23 deletions
  1. +109
    -0
      chuanglan.go
  2. +23
    -0
      chuanglan_test.go
  3. +0
    -20
      common.go
  4. +11
    -2
      tencent.go
  5. +28
    -0
      tencent_test.go
  6. +21
    -1
      ums.go

+ 109
- 0
chuanglan.go View File

@ -0,0 +1,109 @@
package sms
import (
"log"
"git.tetele.net/tgo/helper"
)
/**
* 创蓝253发送短信
*
* @param string $mobile 手机号码
* @param string $msg 短信内容
* @param string $sms_sign 短信签名
* @param string $needstatus 是否需要状态报告
* @return success bool, reply map[string]string, err error
*/
var CL_ACCOUNT string = "N3402351"
var CL_PASSWORD string = "zmkj64U5A"
// const CL_VARIABLE_SMS_URL string = "http://XXX/msg/variable/json"
func SendByCL(account, password, mobile, sms_sign, msg string) (bool, map[string]interface{}, error) {
const CL_SIMPLE_SMS_URL string = "http://smssh1.253.com/msg/send/json"
if account == "" {
account = CL_ACCOUNT
}
if password == "" {
password = CL_PASSWORD
}
//创蓝接口参数
data := map[string]string{
"account": account,
"password": password,
"msg": StringJoin("【", sms_sign, "】", msg),
"phone": mobile,
"report": "true",
}
response, err := PostJsonData(CL_SIMPLE_SMS_URL, data)
if err != nil {
return false, nil, err
}
var reply map[string]interface{}
err = json.Unmarshal(response, &reply)
if err != nil {
log.Println(string(response), err)
return false, nil, err
}
if helper.ToStr(reply["code"]) != "0" {
return false, reply, nil
}
return true, reply, err
}
/**
* 查询额度
*
*/
func QueryRemaining(account, password string) (bool, string, map[string]interface{}, error) {
const URL string = "http://smssh1.253.com/msg/balance/json"
if account == "" {
account = CL_ACCOUNT
}
if password == "" {
password = CL_PASSWORD
}
data := map[string]string{
"account": account,
"password": password,
}
response, err := PostJsonData(URL, data)
/** 返回示例
{"code":0,"balance":"9156","time":"20230217150940","errorMsg":""}
{"code":"130","msgId":"","time":"20230217145243","errorMsg":"请求参数错误"}
{"code":"101","msgId":"","time":"20230217151029","errorMsg":"无此用户"}
*/
if err != nil {
return false, "", nil, err
}
var reply map[string]interface{}
err = json.Unmarshal(response, &reply)
if err != nil {
log.Println(string(response), err)
return false, "", nil, err
}
if helper.ToStr(reply["code"]) != "0" {
return false, "", reply, nil
}
return true, helper.ToStr(reply["balance"]), reply, err
}

+ 23
- 0
chuanglan_test.go View File

@ -0,0 +1,23 @@
package sms
import (
"testing"
)
// func Test_SendByCL(t *testing.T) {
// msg := "罗您好,您的商店新增2单物理灭蚊仪-天眼款,请及时登录商家后台处理!"
// ret, reply, err := SendByCL("", "", "18607565510", "特特乐", msg)
// t.Log(ret)
// t.Log(reply)
// t.Log(err)
// }
func Test_QueryRemaining(t *testing.T) {
ret, total, reply, err := QueryRemaining("", "")
t.Log(ret)
t.Log(total)
t.Log(reply)
t.Log(err)
}

+ 0
- 20
common.go View File

@ -4,15 +4,11 @@ import (
"bytes"
"crypto/md5"
"encoding/hex"
"git.tetele.net/tgo/helper"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/json-iterator/go"
"github.com/tjfoc/gmsm/sm3"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
@ -86,19 +82,3 @@ func PostJsonData(url string, param interface{}, header ...map[string]string) ([
return body, nil
}
// ums验签
func UmsSetSign(data, header map[string]string,secretKey string) string {
str := helper.HttpBuildQuery(data)
// url encode
str = url.QueryEscape(str)
str = header["x-sp-code"] + header["x-app-key"] + secretKey + str + header["x-request-id"]
log.Println("加密前数据: ", str)
// ms3加密
hash := sm3.New()
hash.Write([]byte(str))
sign_byte := hash.Sum(nil)
sign := hex.EncodeToString(sign_byte)
log.Println("加密后数据: ", sign)
return sign
}

+ 11
- 2
tencent.go View File

@ -16,8 +16,12 @@ import (
* @signname 签名
* @templateId 模板id
* @params 参数值
* @region 区域, ap-guangzhou|ap-beijing|ap-shanghai
* @return *requestId,[]*sendStatus,error
*
*/
func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobiles []string, templateId string, params []string) (*string, []*sms.SendStatus, error) {
func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobiles []string, templateId string, params []string, region ...string) (*string, []*sms.SendStatus, error) {
credential := common.NewCredential(
secretId,
@ -25,7 +29,12 @@ func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobile
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
client, _ := sms.NewClient(credential, "ap-guangzhou", cpf)
region_default := "ap-guangzhou"
if len(region) > 0 {
region_default = region[0]
}
client, _ := sms.NewClient(credential, region_default, cpf)
request := sms.NewSendSmsRequest()


+ 28
- 0
tencent_test.go View File

@ -0,0 +1,28 @@
package sms
import (
"testing"
)
func Test_SendByTencent(t *testing.T) {
secretId := "AKIDObaKq9NRFn6A2iBaOxGKBoqeEbjdT5Vg"
secretKey := "0mLiqlU0IMwIOEqGWYMxLVREsMelhT3R"
smsAppId := "1400705248"
signname := "智企易创"
mobiles := []string{"18607565510"}
templateId := "1478144"
params := []string{"666555", "5"}
requestId, status, err := SendByTencent(secretId, secretKey, smsAppId, signname, mobiles, templateId, params, "ap-beijing")
t.Log(*requestId)
if len(status) > 0 {
for _, v := range status {
t.Log(*v.SerialNo, *v.PhoneNumber, *v.Code, *v.Message, *v.Fee, *v.SessionContext, *v.IsoCode)
}
}
t.Log(err)
}

+ 21
- 1
ums.go View File

@ -1,10 +1,14 @@
package sms
import (
"encoding/hex"
"errors"
"git.tetele.net/tgo/helper"
"log"
"net/url"
"time"
"git.tetele.net/tgo/helper"
"github.com/tjfoc/gmsm/sm3"
)
/**
@ -70,3 +74,19 @@ func SendByUms(spCode, appKey, secretKey, mobiles, templateId, content string) (
return true, nil
}
// ums验签
func UmsSetSign(data, header map[string]string, secretKey string) string {
str := helper.HttpBuildQuery(data)
// url encode
str = url.QueryEscape(str)
str = header["x-sp-code"] + header["x-app-key"] + secretKey + str + header["x-request-id"]
log.Println("加密前数据: ", str)
// ms3加密
hash := sm3.New()
hash.Write([]byte(str))
sign_byte := hash.Sum(nil)
sign := hex.EncodeToString(sign_byte)
log.Println("加密后数据: ", sign)
return sign
}

Loading…
Cancel
Save