sms.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package sms
  2. import (
  3. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  4. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
  5. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
  6. sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711"
  7. "zhiyuan/pkg/config"
  8. )
  9. var client *sms.Client
  10. func Setup() {
  11. credential := common.NewCredential(
  12. config.Cfg.Sms.AccessKey,
  13. config.Cfg.Sms.AccessSecret,
  14. )
  15. client, _ = sms.NewClient(credential, "ap-guangzhou", profile.NewClientProfile())
  16. }
  17. func SendSms(phone []string, signName string, templateCode string, templeteParam []string) error {
  18. request := sms.NewSendSmsRequest()
  19. request.SmsSdkAppid = common.StringPtr(config.Cfg.Sms.AppID)
  20. request.Sign = common.StringPtr(signName)
  21. if templateCode != "" {
  22. request.TemplateID = common.StringPtr(templateCode)
  23. }
  24. if templeteParam != nil {
  25. request.TemplateParamSet = common.StringPtrs(templeteParam)
  26. }
  27. request.PhoneNumberSet = common.StringPtrs(phone)
  28. _, err := client.SendSms(request)
  29. if _, ok := err.(*errors.TencentCloudSDKError); ok {
  30. return err
  31. }
  32. if err != nil {
  33. return err
  34. }
  35. //fmt.Println(response.Response)
  36. return nil
  37. }