diff --git a/aliyun/sms_own.go b/aliyun/sms_own.go new file mode 100644 index 0000000..7b2c3c4 --- /dev/null +++ b/aliyun/sms_own.go @@ -0,0 +1,94 @@ +package aliyun + +import ( + utils "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils" + "encoding/json" + "errors" + "fmt" + openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v4/client" + util "github.com/alibabacloud-go/tea-utils/v2/service" + "github.com/alibabacloud-go/tea/tea" + "strings" +) + +// Description: +// +// 使用AK&SK初始化账号Client +// +// @return Client +// +// @throws Exception +func CreateClientOwn(accessKeyId, accessKeySecret string) (_result *dysmsapi20170525.Client, _err error) { + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 + // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。 + config := &openapi.Config{ + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 + AccessKeyId: tea.String(accessKeyId), + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + AccessKeySecret: tea.String(accessKeySecret), + } + // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi + config.Endpoint = tea.String("dysmsapi.aliyuncs.com") + _result = &dysmsapi20170525.Client{} + _result, _err = dysmsapi20170525.NewClient(config) + return _result, _err +} + +func AliyunSendSmsOwn(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error { + client, _err := CreateClientOwn(accessKeyId, accessKeySecret) + if _err != nil { + return _err + } + sendSmsRequest := &dysmsapi20170525.SendSmsRequest{ + PhoneNumbers: tea.String(phone), + SignName: tea.String(signName), + TemplateCode: tea.String(templateCode), + TemplateParam: tea.String(templateParam), + } + runtime := &util.RuntimeOptions{} + tryErr := func() (_e error) { + defer func() { + if r := tea.Recover(recover()); r != nil { + _e = r + } + }() + // 复制代码运行请自行打印 API 的返回值 + body, _err := client.SendSmsWithOptions(sendSmsRequest, runtime) + if _err != nil { + return _err + } + if body != nil && body.Body != nil && body.Body.Code != nil && *body.Body.Code != "OK" { + if body.Body.Message != nil { + fmt.Println("阿里短信:" + utils.SerializeStr(body)) + } + return errors.New("发送失败") + } + return nil + }() + + if tryErr != nil { + var error = &tea.SDKError{} + if _t, ok := tryErr.(*tea.SDKError); ok { + error = _t + } else { + error.Message = tea.String(tryErr.Error()) + } + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + fmt.Println(tea.StringValue(error.Message)) + // 诊断地址 + var data interface{} + d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data))) + d.Decode(&data) + if m, ok := data.(map[string]interface{}); ok { + recommend, _ := m["Recommend"] + fmt.Println(recommend) + } + _, _err = util.AssertAsString(error.Message) + if _err != nil { + return _err + } + } + return _err +} diff --git a/go.mod b/go.mod index d40a6a1..13a4eb4 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 github.com/alibabacloud-go/darabonba-openapi/v2 v2.0.10 github.com/alibabacloud-go/dypnsapi-20170525/v2 v2.2.3 + github.com/alibabacloud-go/dysmsapi-20170525/v4 v4.1.0 github.com/alibabacloud-go/tea v1.2.2 github.com/alibabacloud-go/tea-utils/v2 v2.0.6 github.com/go-redis/redis v6.15.9+incompatible diff --git a/go.sum b/go.sum index 0610063..7019d37 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/alibabacloud-go/debug v1.0.1 h1:MsW9SmUtbb1Fnt3ieC6NNZi6aEwrXfDksD4QA github.com/alibabacloud-go/debug v1.0.1/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc= github.com/alibabacloud-go/dypnsapi-20170525/v2 v2.2.3 h1:g+y1k8ZtcCf2a/wFYGxWXzOgwv5a8wGnZ4o0sBFgcbo= github.com/alibabacloud-go/dypnsapi-20170525/v2 v2.2.3/go.mod h1:QvHPFXM6vrPxrbf0IdNGJA1KbT7aCOWzICZjqHxEnMk= +github.com/alibabacloud-go/dysmsapi-20170525/v4 v4.1.0 h1:DRd/m0TAVzmVoacUv4+C32HrpoehAPxe7Yi8jBwW5HY= +github.com/alibabacloud-go/dysmsapi-20170525/v4 v4.1.0/go.mod h1:XA0GIZV/FIFTuasWP6WlGolkSfH/t8J5eqxT2dHAFbE= github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q= github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE= github.com/alibabacloud-go/openapi-util v0.1.0 h1:0z75cIULkDrdEhkLWgi9tnLe+KhAFE/r5Pb3312/eAY=