|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package sms
-
- import (
- "encoding/json"
- "errors"
- "fmt"
- openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
- dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client"
- util "github.com/alibabacloud-go/tea-utils/v2/service"
- "github.com/alibabacloud-go/tea/tea"
- "strings"
- )
-
- /**
- 发送短信
- https://help.aliyun.com/zh/pnvs/developer-reference/api-dypnsapi-2017-05-25-sendsmsverifycode?spm=a2c4g.11186623.0.0.4f51a5d7doAoA4
- templateParam= {"code":"##code##"} 可不填
- */
-
- func AliyunSendSms(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error {
- client, _err := CreateClient(accessKeyId, accessKeySecret)
- if _err != nil {
- return _err
- }
- if templateParam == "" {
- templateParam = "{\"code\":\"##code##\"}"
- }
- sendSmsVerifyCodeRequest := &dypnsapi20170525.SendSmsVerifyCodeRequest{
- CodeLength: tea.Int64(4),
- Interval: tea.Int64(60),
- PhoneNumber: tea.String(phone),
- SignName: tea.String(signName),
- TemplateCode: tea.String(templateCode),
- TemplateParam: tea.String(templateParam),
- ValidTime: tea.Int64(300),
- }
- runtime := &util.RuntimeOptions{}
- tryErr := func() (_e error) {
- defer func() {
- if r := tea.Recover(recover()); r != nil {
- _e = r
- }
- }()
- // 复制代码运行请自行打印 API 的返回值
- _, _err = client.SendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime)
- if _err != nil {
- return _err
- }
-
- 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 errors.New("发送失败")
- }
- return _err
- }
-
- // 校验短信
- func AliyunCheckSms(accessKeyId, accessKeySecret, phone, verifyCode string) error {
- client, _err := CreateClient(accessKeyId, accessKeySecret)
- if _err != nil {
- return _err
- }
- checkSmsVerifyCodeRequest := &dypnsapi20170525.CheckSmsVerifyCodeRequest{
- PhoneNumber: tea.String(phone),
- VerifyCode: tea.String(verifyCode),
- }
- runtime := &util.RuntimeOptions{}
- tryErr := func() (_e error) {
- defer func() {
- if r := tea.Recover(recover()); r != nil {
- _e = r
- }
- }()
- // 复制代码运行请自行打印 API 的返回值
- _, _err = client.CheckSmsVerifyCodeWithOptions(checkSmsVerifyCodeRequest, runtime)
- if _err != nil {
- return _err
- }
-
- 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 errors.New("验证失败")
- }
- return _err
- }
-
- // Description:
- // 使用AK&SK初始化账号Client
- // @return Client
- // @throws Exception
- func CreateClient(accessKeyId, accessKeySecret string) (_result *dypnsapi20170525.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/Dypnsapi
- config.Endpoint = tea.String("dypnsapi.aliyuncs.com")
- _result = &dypnsapi20170525.Client{}
- _result, _err = dypnsapi20170525.NewClient(config)
- return _result, _err
- }
|