dongwu_sms.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "eta/eta_api/models"
  11. "eta/eta_api/utils"
  12. )
  13. type DongWuSms struct{}
  14. func (cli *DongWuSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSmsCodeResult, err error) {
  15. if req.Mobile == "" || req.VerifyCode == "" {
  16. err = fmt.Errorf("参数有误, Mobile: %s, VerifyCode: %s", req.Mobile, req.VerifyCode)
  17. return
  18. }
  19. // Get configuration
  20. confMap, e := models.GetBusinessConf()
  21. if e != nil {
  22. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  23. return
  24. }
  25. tpl := confMap[models.BusinessConfLoginSmsTplContent]
  26. if tpl == "" {
  27. err = fmt.Errorf("请先配置短信模板")
  28. return
  29. }
  30. appKey := confMap[models.BusinessConfDongwuSmsAppKey]
  31. if appKey == "" {
  32. err = fmt.Errorf("请先配置东吴短信AppKey")
  33. return
  34. }
  35. // Call DongWu SMS API
  36. apiURL := confMap[models.BusinessConfDongwuSmsApiUrl]
  37. if apiURL == "" {
  38. err = fmt.Errorf("请先配置东吴短信API地址")
  39. return
  40. }
  41. // 短信内容
  42. smsContent := strings.Replace(tpl, "{{VERIFY_CODE}}", req.VerifyCode, 1)
  43. smsContent = strings.Replace(smsContent, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  44. apiResp, err := cli.sendUserLoginCode(req.Mobile, apiURL, appKey, smsContent)
  45. if err != nil {
  46. return
  47. }
  48. if apiResp.Status == "success" {
  49. if apiResp.Result != "" {
  50. var resultDetail DongWuSmsResultDetail
  51. if e := json.Unmarshal([]byte(apiResp.Result), &resultDetail); e != nil {
  52. err = fmt.Errorf("parse result detail err: %s", e.Error())
  53. return
  54. }
  55. if resultDetail.Result == "0" {
  56. result.Success = true
  57. result.Message = resultDetail.Desc
  58. } else {
  59. result.Success = false
  60. result.Message = fmt.Sprintf("发送失败, 错误码: %s, 错误信息: %s", resultDetail.Result, resultDetail.Desc)
  61. }
  62. }
  63. return
  64. } else {
  65. result.Success = false
  66. result.Message = apiResp.Message
  67. }
  68. return
  69. }
  70. type DongWuSmsApiParams struct {
  71. GeneralAccount string `json:"generalAccount"`
  72. Receivers string `json:"receivers"`
  73. Text string `json:"text"`
  74. SubCode string `json:"subCode"`
  75. SmsId string `json:"smsId"` //短信编号 (32 位 UUID),需保证唯一,选填
  76. SendTime string `json:"sendTime"` // 选填,定时发送时间,格式 yyyyMMddHHmm,为空或早于当前时间则 立即发送;
  77. Sign string `json:"sign"` // 选填,短信签名,为空时使用默认签名,默认为【东吴期货】
  78. }
  79. type DongWuSmsApiResult struct {
  80. Status string `json:"status"`
  81. Message string `json:"message"`
  82. SmsType string `json:"smsType"` //短信平台类型,仅当status="success"时不为空,"ssdj"代表三三得九平台,"dh3t"代表大汉三通平台
  83. Result string `json:"result"`
  84. }
  85. type DongWuSmsResultDetail struct {
  86. Msgid string `json:"msgid"`
  87. Result string `json:"result"`
  88. Desc string `json:"desc"`
  89. FailPhones string `json:"failPhones"`
  90. }
  91. func (cli *DongWuSms) sendUserLoginCode(mobile, apiURL, appKey, content string) (apiResp *DongWuSmsApiResult, err error) {
  92. // Prepare request parameters
  93. params := DongWuSmsApiParams{
  94. GeneralAccount: appKey,
  95. Receivers: mobile,
  96. Text: content,
  97. }
  98. // Make HTTP request
  99. jsonData, e := json.Marshal(params)
  100. if e != nil {
  101. err = fmt.Errorf("json marshal err: %s", e.Error())
  102. return
  103. }
  104. httpReq, e := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  105. if e != nil {
  106. err = fmt.Errorf("create request err: %s", e.Error())
  107. return
  108. }
  109. httpReq.Header.Set("Content-Type", "application/json")
  110. client := &http.Client{}
  111. resp, e := client.Do(httpReq)
  112. if e != nil {
  113. err = fmt.Errorf("send request err: %s", e.Error())
  114. return
  115. }
  116. defer resp.Body.Close()
  117. body, e := ioutil.ReadAll(resp.Body)
  118. if e != nil {
  119. err = fmt.Errorf("read response err: %s", e.Error())
  120. return
  121. }
  122. apiResp = &DongWuSmsApiResult{}
  123. if e = json.Unmarshal(body, &apiResp); e != nil {
  124. err = fmt.Errorf("parse response err: %s", e.Error())
  125. return
  126. }
  127. // Parse the result string if status is success
  128. if apiResp.Status == "success" && apiResp.Result != "" {
  129. var resultDetail DongWuSmsResultDetail
  130. if e := json.Unmarshal([]byte(apiResp.Result), &resultDetail); e != nil {
  131. err = fmt.Errorf("parse result detail err: %s", e.Error())
  132. return
  133. }
  134. // Update the response handling in SendUserLoginCode
  135. if resultDetail.Result == "0" {
  136. return
  137. }
  138. }
  139. return
  140. }