nanhua_sms.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. )
  12. type NanHuaSms struct{}
  13. func (cli *NanHuaSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSmsCodeResult, err error) {
  14. confMap, e := models.GetBusinessConf()
  15. if e != nil {
  16. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  17. return
  18. }
  19. tpl := confMap[models.BusinessConfLoginSmsTplContent]
  20. if tpl == "" {
  21. err = fmt.Errorf("短信模板有误")
  22. return
  23. }
  24. smsContent := strings.Replace(tpl, "{{VERIFY_CODE}}", req.VerifyCode, 1)
  25. smsContent = strings.Replace(smsContent, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  26. var params NanHuaSmsApiParams
  27. var mobileRequest NanHuaSmsApiRequest
  28. mobileRequest.Mobile = req.Mobile
  29. params.ApiHost = confMap[models.BusinessConfNanHuaSmsApiHost]
  30. params.AppKey = confMap[models.BusinessConfNanHuaSmsAppKey]
  31. params.AppSecret = confMap[models.BusinessConfNanHuaSmsAppSecret]
  32. params.Content = smsContent
  33. params.TypeCode = 1
  34. params.Requests = append(params.Requests, mobileRequest)
  35. res, e := CurlNanHuaSmsApi(params)
  36. if e != nil {
  37. err = fmt.Errorf("CurlNanHuaSmsApi err: %s", e.Error())
  38. return
  39. }
  40. result.Success = res.Success
  41. result.Message = res.Message
  42. if len(res.Data.SmsResponses) > 0 {
  43. result.RequestId = res.Data.SmsResponses[0].RecId
  44. }
  45. return
  46. }
  47. type NanHuaSmsApiParams struct {
  48. ApiHost string `json:"-" description:"api-host"`
  49. AppKey string `json:"-" description:"app-key"`
  50. AppSecret string `json:"-" description:"app-secret"`
  51. Content string `json:"content" description:"短信内容,必填"`
  52. Username string `json:"username" description:"使用人南华域账号,非必填"`
  53. TypeCode int `json:"typeCode" description:"1-普通短信;2-营销短信"`
  54. Requests []NanHuaSmsApiRequest `json:"requests" description:"手机号、内容信息"`
  55. }
  56. type NanHuaSmsApiRequest struct {
  57. Content string `json:"content" description:"短信内容,非必填,填写则该手机号使用此内容单独发送"`
  58. Mobile string `json:"mobile" description:"手机号,必填"`
  59. OrgId string `json:"orgId" description:"客户所属ctp机构ID,非必填,用来计费(建议填写)"`
  60. OaDepartmentId string `json:"oaDepartmentId" description:"客户所属oa部门ID,非必填,用来计费"`
  61. }
  62. type NanHuaSmsApiResult struct {
  63. ErrorCode int `json:"errorcode" description:"状态码:0-成功;400-失败"`
  64. Success bool `json:"success" description:"true-成功;false-失败"`
  65. Message string `json:"message" description:"请求失败提示"`
  66. Data struct {
  67. SmsResponses []struct {
  68. Mobile string `json:"mobile" description:"手机号"`
  69. RecId string `json:"recId" description:"短信id,用来回查发送状态"`
  70. } `json:"smsResponses"`
  71. } `json:"data"`
  72. }
  73. func CurlNanHuaSmsApi(params NanHuaSmsApiParams) (result NanHuaSmsApiResult, err error) {
  74. url := fmt.Sprint(params.ApiHost)
  75. byteParams, e := json.Marshal(params)
  76. if e != nil {
  77. err = fmt.Errorf("data json marshal err: %s", e.Error())
  78. return
  79. }
  80. body := ioutil.NopCloser(strings.NewReader(string(byteParams)))
  81. client := &http.Client{}
  82. req, e := http.NewRequest("POST", url, body)
  83. if e != nil {
  84. err = fmt.Errorf("http create request err: %s", e.Error())
  85. return
  86. }
  87. contentType := "application/json;charset=utf-8"
  88. req.Header.Set("Content-Type", contentType)
  89. req.Header.Set("App-Key", params.AppKey)
  90. req.Header.Set("App-Secret", params.AppSecret)
  91. resp, e := client.Do(req)
  92. if e != nil {
  93. err = fmt.Errorf("http client do err: %s", e.Error())
  94. return
  95. }
  96. defer func() {
  97. _ = resp.Body.Close()
  98. }()
  99. b, e := ioutil.ReadAll(resp.Body)
  100. if e != nil {
  101. err = fmt.Errorf("resp body read err: %s", e.Error())
  102. return
  103. }
  104. if len(b) == 0 {
  105. err = fmt.Errorf("resp body is empty")
  106. return
  107. }
  108. if e = json.Unmarshal(b, &result); e != nil {
  109. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  110. return
  111. }
  112. return
  113. }