蛋蛋星球-制度模式
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sms.go 3.5 KiB

1 månad sedan
1 månad sedan
1 månad sedan
1 månad sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package aliyun
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client"
  7. util "github.com/alibabacloud-go/tea-utils/v2/service"
  8. "github.com/alibabacloud-go/tea/tea"
  9. "strings"
  10. )
  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. }