From 88657cb9ad18bb2ec531b611447e6a0eac7297dd Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 17 Feb 2023 15:22:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E8=93=9D=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E5=8F=91=E9=80=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chuanglan.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++ chuanglan_test.go | 23 ++++++++++ common.go | 20 --------- tencent.go | 13 +++++- tencent_test.go | 28 ++++++++++++ ums.go | 22 +++++++++- 6 files changed, 192 insertions(+), 23 deletions(-) create mode 100644 chuanglan.go create mode 100644 chuanglan_test.go create mode 100644 tencent_test.go diff --git a/chuanglan.go b/chuanglan.go new file mode 100644 index 0000000..f7044a6 --- /dev/null +++ b/chuanglan.go @@ -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 +} diff --git a/chuanglan_test.go b/chuanglan_test.go new file mode 100644 index 0000000..ba869ad --- /dev/null +++ b/chuanglan_test.go @@ -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) +} diff --git a/common.go b/common.go index 825cdd5..cc7677d 100644 --- a/common.go +++ b/common.go @@ -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 -} diff --git a/tencent.go b/tencent.go index 5fd9561..523a11c 100644 --- a/tencent.go +++ b/tencent.go @@ -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() diff --git a/tencent_test.go b/tencent_test.go new file mode 100644 index 0000000..1fce410 --- /dev/null +++ b/tencent_test.go @@ -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) + +} diff --git a/ums.go b/ums.go index a5364e7..f1abaf3 100644 --- a/ums.go +++ b/ums.go @@ -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 +}