蛋蛋星球-制度模式
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 6 Tagen
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package sms
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  7. dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client"
  8. util "github.com/alibabacloud-go/tea-utils/v2/service"
  9. "github.com/alibabacloud-go/tea/tea"
  10. "strings"
  11. )
  12. /**
  13. 发送短信
  14. https://help.aliyun.com/zh/pnvs/developer-reference/api-dypnsapi-2017-05-25-sendsmsverifycode?spm=a2c4g.11186623.0.0.4f51a5d7doAoA4
  15. templateParam= {"code":"##code##"} 可不填
  16. */
  17. func AliyunSendSms(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error {
  18. client, _err := CreateClient(accessKeyId, accessKeySecret)
  19. if _err != nil {
  20. return _err
  21. }
  22. if templateParam == "" {
  23. templateParam = "{\"code\":\"##code##\"}"
  24. }
  25. sendSmsVerifyCodeRequest := &dypnsapi20170525.SendSmsVerifyCodeRequest{
  26. CodeLength: tea.Int64(4),
  27. Interval: tea.Int64(60),
  28. PhoneNumber: tea.String(phone),
  29. SignName: tea.String(signName),
  30. TemplateCode: tea.String(templateCode),
  31. TemplateParam: tea.String(templateParam),
  32. ValidTime: tea.Int64(300),
  33. }
  34. runtime := &util.RuntimeOptions{}
  35. tryErr := func() (_e error) {
  36. defer func() {
  37. if r := tea.Recover(recover()); r != nil {
  38. _e = r
  39. }
  40. }()
  41. // 复制代码运行请自行打印 API 的返回值
  42. _, _err = client.SendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime)
  43. if _err != nil {
  44. return _err
  45. }
  46. return nil
  47. }()
  48. if tryErr != nil {
  49. var error = &tea.SDKError{}
  50. if _t, ok := tryErr.(*tea.SDKError); ok {
  51. error = _t
  52. } else {
  53. error.Message = tea.String(tryErr.Error())
  54. }
  55. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  56. // 错误 message
  57. fmt.Println(tea.StringValue(error.Message))
  58. // 诊断地址
  59. var data interface{}
  60. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  61. d.Decode(&data)
  62. if m, ok := data.(map[string]interface{}); ok {
  63. recommend, _ := m["Recommend"]
  64. fmt.Println(recommend)
  65. }
  66. _, _err = util.AssertAsString(error.Message)
  67. if _err != nil {
  68. return _err
  69. }
  70. return errors.New("发送失败")
  71. }
  72. return _err
  73. }
  74. // 校验短信
  75. func AliyunCheckSms(accessKeyId, accessKeySecret, phone, verifyCode string) error {
  76. client, _err := CreateClient(accessKeyId, accessKeySecret)
  77. if _err != nil {
  78. return _err
  79. }
  80. checkSmsVerifyCodeRequest := &dypnsapi20170525.CheckSmsVerifyCodeRequest{
  81. PhoneNumber: tea.String(phone),
  82. VerifyCode: tea.String(verifyCode),
  83. }
  84. runtime := &util.RuntimeOptions{}
  85. tryErr := func() (_e error) {
  86. defer func() {
  87. if r := tea.Recover(recover()); r != nil {
  88. _e = r
  89. }
  90. }()
  91. // 复制代码运行请自行打印 API 的返回值
  92. _, _err = client.CheckSmsVerifyCodeWithOptions(checkSmsVerifyCodeRequest, runtime)
  93. if _err != nil {
  94. return _err
  95. }
  96. return nil
  97. }()
  98. if tryErr != nil {
  99. var error = &tea.SDKError{}
  100. if _t, ok := tryErr.(*tea.SDKError); ok {
  101. error = _t
  102. } else {
  103. error.Message = tea.String(tryErr.Error())
  104. }
  105. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  106. // 错误 message
  107. fmt.Println(tea.StringValue(error.Message))
  108. // 诊断地址
  109. var data interface{}
  110. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  111. d.Decode(&data)
  112. if m, ok := data.(map[string]interface{}); ok {
  113. recommend, _ := m["Recommend"]
  114. fmt.Println(recommend)
  115. }
  116. _, _err = util.AssertAsString(error.Message)
  117. if _err != nil {
  118. return _err
  119. }
  120. return errors.New("验证失败")
  121. }
  122. return _err
  123. }
  124. // Description:
  125. // 使用AK&SK初始化账号Client
  126. // @return Client
  127. // @throws Exception
  128. func CreateClient(accessKeyId, accessKeySecret string) (_result *dypnsapi20170525.Client, _err error) {
  129. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  130. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
  131. config := &openapi.Config{
  132. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  133. AccessKeyId: tea.String(accessKeyId),
  134. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  135. AccessKeySecret: tea.String(accessKeySecret),
  136. }
  137. // Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi
  138. config.Endpoint = tea.String("dypnsapi.aliyuncs.com")
  139. _result = &dypnsapi20170525.Client{}
  140. _result, _err = dypnsapi20170525.NewClient(config)
  141. return _result, _err
  142. }