sms.go 1.4 KB

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