nanhua_sms.go 4.3 KB

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