蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 rivejä
3.0 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. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  8. dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
  9. util "github.com/alibabacloud-go/tea-utils/v2/service"
  10. "github.com/alibabacloud-go/tea/tea"
  11. "strings"
  12. )
  13. // Description:
  14. //
  15. // 使用AK&SK初始化账号Client
  16. //
  17. // @return Client
  18. //
  19. // @throws Exception
  20. func CreateClientOwn(accessKeyId, accessKeySecret string) (_result *dysmsapi20170525.Client, _err error) {
  21. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  22. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
  23. config := &openapi.Config{
  24. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  25. AccessKeyId: tea.String(accessKeyId),
  26. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  27. AccessKeySecret: tea.String(accessKeySecret),
  28. }
  29. // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
  30. config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
  31. _result = &dysmsapi20170525.Client{}
  32. _result, _err = dysmsapi20170525.NewClient(config)
  33. return _result, _err
  34. }
  35. func AliyunSendSmsOwn(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error {
  36. client, _err := CreateClientOwn(accessKeyId, accessKeySecret)
  37. if _err != nil {
  38. return _err
  39. }
  40. sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
  41. PhoneNumbers: tea.String(phone),
  42. SignName: tea.String(signName),
  43. TemplateCode: tea.String(templateCode),
  44. TemplateParam: tea.String(templateParam),
  45. }
  46. runtime := &util.RuntimeOptions{}
  47. tryErr := func() (_e error) {
  48. defer func() {
  49. if r := tea.Recover(recover()); r != nil {
  50. _e = r
  51. }
  52. }()
  53. // 复制代码运行请自行打印 API 的返回值
  54. body, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
  55. if _err != nil {
  56. return _err
  57. }
  58. if body != nil && body.Body != nil && body.Body.Code != nil && *body.Body.Code != "OK" {
  59. if body.Body.Message != nil {
  60. fmt.Println("阿里短信:" + utils.SerializeStr(body))
  61. }
  62. return errors.New("发送失败")
  63. }
  64. return nil
  65. }()
  66. if tryErr != nil {
  67. var error = &tea.SDKError{}
  68. if _t, ok := tryErr.(*tea.SDKError); ok {
  69. error = _t
  70. } else {
  71. error.Message = tea.String(tryErr.Error())
  72. }
  73. // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
  74. // 错误 message
  75. fmt.Println(tea.StringValue(error.Message))
  76. // 诊断地址
  77. var data interface{}
  78. d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
  79. d.Decode(&data)
  80. if m, ok := data.(map[string]interface{}); ok {
  81. recommend, _ := m["Recommend"]
  82. fmt.Println(recommend)
  83. }
  84. _, _err = util.AssertAsString(error.Message)
  85. if _err != nil {
  86. return _err
  87. }
  88. }
  89. return _err
  90. }