From b83fa2a0d005439d41102fe98049449e65cbcfc0 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 15 Mar 2022 17:53:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=94=E9=80=9A=E5=92=8C=E8=85=BE=E8=AE=AF?= =?UTF-8?q?=E4=BA=91=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++- common.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ common_test.go | 29 +++++++++++++++++ go.mod | 9 ++++++ go.sum | 19 ++++++++++++ tencent.go | 50 ++++++++++++++++++++++++++++++ wo.go | 49 +++++++++++++++++++++++++++++ wo_test.go | 18 +++++++++++ 8 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 common.go create mode 100644 common_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 tencent.go create mode 100644 wo.go create mode 100644 wo_test.go diff --git a/README.md b/README.md index 7cb0c33..0358f82 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ # sms -短信发送方法 \ No newline at end of file +短信发送方法 + +## SendByWo() +发送联通融合通信短信 + +## SendByTencent() +使用腾讯云发送短信 \ No newline at end of file diff --git a/common.go b/common.go new file mode 100644 index 0000000..d7e1003 --- /dev/null +++ b/common.go @@ -0,0 +1,84 @@ +package sms + +import ( + "bytes" + "crypto/md5" + "encoding/hex" + "io/ioutil" + "net/http" + "strings" + + "github.com/json-iterator/go" +) + +var json = jsoniter.ConfigCompatibleWithStandardLibrary + +//密码加密 +func Md5Str(str ...string) string { + + var build strings.Builder + if len(str) > 0 { + for _, v := range str { + build.WriteString(v) + } + } else { + return "" + } + + h := md5.New() + h.Write([]byte(build.String())) // 需要加密的字符串 + cipher2Str := h.Sum(nil) + sMd5 := hex.EncodeToString(cipher2Str) // 输出加密结果 + return sMd5 +} + +/* + * 连接多个字符串 + */ +func StringJoin(s ...string) string { + var build strings.Builder + if len(s) > 0 { + for _, v := range s { + build.WriteString(v) + } + } + + return build.String() +} + +/** + * post 请求 + */ +func PostJsonData(url string, param interface{}, header ...map[string]string) ([]byte, error) { + httpClient := &http.Client{} + params, err := json.Marshal(param) + if err != nil { + return []byte(""), err + } + req, err := http.NewRequest("POST", url, bytes.NewBuffer(params)) + if err != nil { + return []byte(""), err + } + + req.Header.Set("Content-Type", "application/json") + + if len(header) > 0 { + for _, item := range header { + for k, v := range item { + req.Header[k] = []string{v} + } + } + } + resp, err := httpClient.Do(req) + if err != nil { + return []byte(""), err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return []byte(""), err + } + + return body, nil +} diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..e6afce6 --- /dev/null +++ b/common_test.go @@ -0,0 +1,29 @@ +package sms + +import ( + // jsonorg "encoding/json" + "testing" + "time" + // "github.com/json-iterator/go" +) + +func Test_PostJsonData(t *testing.T) { + + t.Log(time.Now().UnixNano()) + // data := map[string]string{"11": "22", "33": "44", "55": "66"} + + var d []byte + var err error + + org := `{"11":"22","33":"44","55":"66"} ` + var org_old map[string]string + + for i := 0; i < 10000000; i++ { + err = json.Unmarshal([]byte(org), &org_old) + } + + t.Log(org_old) + t.Log(string(d), err) + t.Log(time.Now().UnixNano()) + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e708ed3 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module git.tetele.net/tgo/sms + +go 1.16 + +require ( + github.com/json-iterator/go v1.1.12 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365 + github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.365 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4406bcd --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365 h1:WBjY/1zAWHINPh8UB90HY/cMkbfUTtnb/H4TDqGwI/Q= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.365 h1:akX9xDCzYK75pQLrbZIxb/MTPoBF0QVJDARNtWaLzs8= +github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.365/go.mod h1:R8lU5rztxefamuYaDWz/SfIF8p0givTsfV9FcSm58YU= diff --git a/tencent.go b/tencent.go new file mode 100644 index 0000000..5fd9561 --- /dev/null +++ b/tencent.go @@ -0,0 +1,50 @@ +package sms + +import ( + "log" + + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" + sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" +) + +/** + * 使用腾讯云发送短信 + * @secretId 腾讯云密钥id + * @secretKey 腾讯云密钥key + * @signname 签名 + * @templateId 模板id + * @params 参数值 + */ +func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobiles []string, templateId string, params []string) (*string, []*sms.SendStatus, error) { + + credential := common.NewCredential( + secretId, + secretKey, + ) + cpf := profile.NewClientProfile() + cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com" + client, _ := sms.NewClient(credential, "ap-guangzhou", cpf) + + request := sms.NewSendSmsRequest() + + request.PhoneNumberSet = common.StringPtrs(mobiles) //[]string{"18607552321"} + request.SmsSdkAppId = common.StringPtr(smsAppId) + request.SignName = common.StringPtr(signname) + request.TemplateId = common.StringPtr(templateId) + request.TemplateParamSet = common.StringPtrs(params) //[]string{"param1", "param2"} + + response, err := client.SendSms(request) + if _, ok := err.(*errors.TencentCloudSDKError); ok { + log.Println("An API error has returned: ", err) + return nil, nil, err + } + if err != nil { + log.Println(err) + return nil, nil, err + } + + return response.Response.RequestId, response.Response.SendStatusSet, nil + +} diff --git a/wo.go b/wo.go new file mode 100644 index 0000000..2359c6f --- /dev/null +++ b/wo.go @@ -0,0 +1,49 @@ +package sms + +import ( + "log" + "strings" + // "github.com/json-iterator/go" +) + +/** + * 发送联通融合通信短信 + * @cpcode 商户编号 + * @key accessKey + * @excode 扩展码 + * @mobiles 手机号,多个用逗号隔开 + * @templetId 模板id + * @msg 模板中变量的值,如果包含多个参数,以半角英文逗号分隔 + */ + +func SendByWo(cpcode, key, excode, mobiles, templetid, msg string) (map[string]interface{}, error) { + sign_str := StringJoin(cpcode, msg, mobiles, excode, templetid, key) + sign := strings.ToLower(Md5Str(sign_str)) + + url := "http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg" + data := map[string]string{ + "cpcode": cpcode, + "msg": msg, + "mobiles": mobiles, + "excode": excode, + "templetid": templetid, + "sign": sign, + } + + reply, err := PostJsonData(url, data) + if err != nil { + return nil, err + } + + var reply_data map[string]interface{} + + err = json.Unmarshal(reply, &reply_data) + + if err != nil { + log.Println(string(reply), err) + return nil, err + } + + return reply_data, err + +} diff --git a/wo_test.go b/wo_test.go new file mode 100644 index 0000000..8b3af09 --- /dev/null +++ b/wo_test.go @@ -0,0 +1,18 @@ +package sms + +import ( + "testing" +) + +func Test_SendByWo(t *testing.T) { + cpcode := "AACDUZ" + key := "c1a4245d51ad7315e7c1bb5d80eb98b0" + excode := "750058" + msg := "345678" + mobiles := "18607565510" + templetId := "290901" + + ret, err := SendByWo(cpcode, key, excode, mobiles, templetId, msg) + t.Log(ret) + t.Log(err) +}