sms.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. openapi "github.com/alibabacloud-go/darabonba-openapi/client"
  7. dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
  8. "github.com/alibabacloud-go/tea/tea"
  9. )
  10. func createSmsClient() (_result *dysmsapi20170525.Client, _err error) {
  11. accessKeyId := tea.String("LTAI4FmvA9HNunGVq6biNASf")
  12. accessKeySecret := tea.String("QNHsxlW0iESK15TpouJwoLQzRMSxgu")
  13. config := &openapi.Config{
  14. AccessKeyId: accessKeyId,
  15. AccessKeySecret: accessKeySecret,
  16. }
  17. config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
  18. _result, _err = dysmsapi20170525.NewClient(config)
  19. return _result, _err
  20. }
  21. type SupplierSmsReq struct {
  22. Porduct string
  23. SerialNumber string
  24. }
  25. func SendSmsNotify(phone string, info *SupplierSmsReq) error {
  26. // TODO dev
  27. phone = "13408547823"
  28. return errors.New("待配置短信模板")
  29. client, _err := createSmsClient()
  30. if _err != nil {
  31. return _err
  32. }
  33. infobytes, _ := json.Marshal(info)
  34. sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
  35. PhoneNumbers: tea.String(phone),
  36. SignName: tea.String("中鱼互动"),
  37. TemplateCode: tea.String("SMS_460545008"),
  38. TemplateParam: tea.String(string(infobytes)),
  39. }
  40. resp, err := client.SendSms(sendSmsRequest)
  41. if err != nil {
  42. return err
  43. }
  44. if *resp.Body.Code == "OK" {
  45. return nil
  46. }
  47. return fmt.Errorf("code err %s", *resp.Body.Code)
  48. }