蛋蛋星球-制度模式
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

137 righe
3.8 KiB

  1. package aliyun
  2. import (
  3. utils "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  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. 发送短信
  15. https://help.aliyun.com/zh/pnvs/developer-reference/api-dypnsapi-2017-05-25-sendsmsverifycode?spm=a2c4g.11186623.0.0.4f51a5d7doAoA4
  16. templateParam= {"code":"##code##"} 可不填
  17. */
  18. func AliyunSendSms(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error {
  19. client, _err := CreateClient(accessKeyId, accessKeySecret)
  20. if _err != nil {
  21. return _err
  22. }
  23. if templateParam == "" {
  24. templateParam = "{\"code\":\"##code##\"}"
  25. }
  26. sendSmsVerifyCodeRequest := &dypnsapi20170525.SendSmsVerifyCodeRequest{
  27. CodeLength: tea.Int64(4),
  28. Interval: tea.Int64(60),
  29. PhoneNumber: tea.String(phone),
  30. SignName: tea.String(signName),
  31. TemplateCode: tea.String(templateCode),
  32. TemplateParam: tea.String(templateParam),
  33. ValidTime: tea.Int64(300),
  34. }
  35. runtime := &util.RuntimeOptions{}
  36. tryErr := func() (_e error) {
  37. defer func() {
  38. if r := tea.Recover(recover()); r != nil {
  39. _e = r
  40. }
  41. }()
  42. // 复制代码运行请自行打印 API 的返回值
  43. body, _err := client.SendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime)
  44. if _err != nil {
  45. return _err
  46. }
  47. if body != nil && body.Body != nil && body.Body.Code != nil && *body.Body.Code != "OK" {
  48. if body.Body.Message != nil {
  49. fmt.Println("阿里短信:" + utils.SerializeStr(body))
  50. }
  51. return errors.New("发送失败")
  52. }
  53. return nil
  54. }()
  55. if tryErr != nil {
  56. var error = &tea.SDKError{}
  57. if _t, ok := tryErr.(*tea.SDKError); ok {
  58. error = _t
  59. } else {
  60. error.Message = tea.String(tryErr.Error())
  61. }
  62. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  63. // 错误 message
  64. fmt.Println(tea.StringValue(error.Message))
  65. // 诊断地址
  66. var data interface{}
  67. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  68. d.Decode(&data)
  69. if m, ok := data.(map[string]interface{}); ok {
  70. recommend, _ := m["Recommend"]
  71. fmt.Println(recommend)
  72. }
  73. _, _err = util.AssertAsString(error.Message)
  74. if _err != nil {
  75. return _err
  76. }
  77. return errors.New("发送失败")
  78. }
  79. return _err
  80. }
  81. // 校验短信
  82. func AliyunCheckSms(accessKeyId, accessKeySecret, phone, verifyCode string) error {
  83. client, _err := CreateClient(accessKeyId, accessKeySecret)
  84. if _err != nil {
  85. return _err
  86. }
  87. checkSmsVerifyCodeRequest := &dypnsapi20170525.CheckSmsVerifyCodeRequest{
  88. PhoneNumber: tea.String(phone),
  89. VerifyCode: tea.String(verifyCode),
  90. }
  91. runtime := &util.RuntimeOptions{}
  92. tryErr := func() (_e error) {
  93. defer func() {
  94. if r := tea.Recover(recover()); r != nil {
  95. _e = r
  96. }
  97. }()
  98. // 复制代码运行请自行打印 API 的返回值
  99. _, _err = client.CheckSmsVerifyCodeWithOptions(checkSmsVerifyCodeRequest, runtime)
  100. if _err != nil {
  101. return _err
  102. }
  103. return nil
  104. }()
  105. if tryErr != nil {
  106. var error = &tea.SDKError{}
  107. if _t, ok := tryErr.(*tea.SDKError); ok {
  108. error = _t
  109. } else {
  110. error.Message = tea.String(tryErr.Error())
  111. }
  112. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  113. // 错误 message
  114. fmt.Println(tea.StringValue(error.Message))
  115. // 诊断地址
  116. var data interface{}
  117. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  118. d.Decode(&data)
  119. if m, ok := data.(map[string]interface{}); ok {
  120. recommend, _ := m["Recommend"]
  121. fmt.Println(recommend)
  122. }
  123. _, _err = util.AssertAsString(error.Message)
  124. if _err != nil {
  125. return _err
  126. }
  127. return errors.New("验证失败")
  128. }
  129. return _err
  130. }