sms.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package api
  2. import (
  3. "box-cost/log"
  4. "encoding/json"
  5. "fmt"
  6. "sync"
  7. openapi "github.com/alibabacloud-go/darabonba-openapi/client"
  8. dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
  9. "github.com/alibabacloud-go/tea/tea"
  10. )
  11. func createSmsClient() (_result *dysmsapi20170525.Client, _err error) {
  12. accessKeyId := tea.String("LTAI4FmvA9HNunGVq6biNASf")
  13. accessKeySecret := tea.String("QNHsxlW0iESK15TpouJwoLQzRMSxgu")
  14. config := &openapi.Config{
  15. AccessKeyId: accessKeyId,
  16. AccessKeySecret: accessKeySecret,
  17. }
  18. config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
  19. _result, _err = dysmsapi20170525.NewClient(config)
  20. return _result, _err
  21. }
  22. type SupplierSmsReq struct {
  23. Product string `json:"product"`
  24. SerialNumber string `json:"serialNumber"`
  25. }
  26. func SendSmsNotify(phone string, info *SupplierSmsReq, wg *sync.WaitGroup) error {
  27. defer wg.Done()
  28. client, _err := createSmsClient()
  29. if _err != nil {
  30. log.Error(_err)
  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. log.Error(err)
  43. return err
  44. }
  45. if *resp.Body.Code == "OK" {
  46. return nil
  47. }
  48. reserr := fmt.Errorf("code err %s", *resp.Body.Code)
  49. log.Error(reserr)
  50. return reserr
  51. }