|
@@ -0,0 +1,136 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "net/http"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "eta/eta_mobile/models"
|
|
|
+ "eta/eta_mobile/utils"
|
|
|
+)
|
|
|
+
|
|
|
+type DongWuSms struct{}
|
|
|
+
|
|
|
+func (cli *DongWuSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSmsCodeResult, err error) {
|
|
|
+ if req.Mobile == "" || req.VerifyCode == "" {
|
|
|
+ err = fmt.Errorf("参数有误, Mobile: %s, VerifyCode: %s", req.Mobile, req.VerifyCode)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // Get configuration
|
|
|
+ confMap, e := models.GetBusinessConf()
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tpl := confMap[models.BusinessConfLoginSmsTplContent]
|
|
|
+ if tpl == "" {
|
|
|
+ err = fmt.Errorf("请先配置短信模板")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ appKey := confMap[models.BusinessConfDongwuSmsAppKey]
|
|
|
+ if appKey == "" {
|
|
|
+ err = fmt.Errorf("请先配置东吴短信AppKey")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Call DongWu SMS API
|
|
|
+ apiURL := confMap[models.BusinessConfDongwuSmsApiUrl]
|
|
|
+ if apiURL == "" {
|
|
|
+ err = fmt.Errorf("请先配置东吴短信API地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 短信内容
|
|
|
+ smsContent := strings.Replace(tpl, "{{VERIFY_CODE}}", req.VerifyCode, 1)
|
|
|
+ smsContent = strings.Replace(smsContent, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
|
|
|
+ apiResp, err := cli.sendUserLoginCode(req.Mobile, apiURL, appKey, smsContent)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if apiResp.Status == "success" {
|
|
|
+ if apiResp.Result.Result == 0 {
|
|
|
+ result.Success = true
|
|
|
+ result.Message = apiResp.Result.Desc
|
|
|
+ }else{
|
|
|
+ result.Success = false
|
|
|
+ result.Message = fmt.Sprintf("发送失败, 错误码: %d, 错误信息: %s", apiResp.Result.Result, apiResp.Result.Desc)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }else{
|
|
|
+ result.Success = false
|
|
|
+ result.Message = apiResp.Message
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+type DongWuSmsApiParams struct {
|
|
|
+ GeneralAccount string `json:"generalAccount"`
|
|
|
+ Receivers string `json:"receivers"`
|
|
|
+ Text string `json:"text"`
|
|
|
+
|
|
|
+ SubCode string `json:"subCode"`
|
|
|
+ SmsId string `json:"smsId"` //短信编号 (32 位 UUID),需保证唯一,选填
|
|
|
+ SendTime string `json:"sendTime"` // 选填,定时发送时间,格式 yyyyMMddHHmm,为空或早于当前时间则 立即发送;
|
|
|
+ Sign string `json:"sign"` // 选填,短信签名,为空时使用默认签名,默认为【东吴期货】
|
|
|
+}
|
|
|
+
|
|
|
+type DongWuSmsApiResult struct {
|
|
|
+ Status string `json:"status"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ SmsType string `json:"smsType"` //短信平台类型,仅当status=”success”时不为空,“ssdj”代表三三得九平台,“dh3t”代表大汉三通平台
|
|
|
+ Result struct {
|
|
|
+ Msgid string `json:"msgid"`
|
|
|
+ Result int `json:"result"`
|
|
|
+ Desc string `json:"desc"`
|
|
|
+ FailPhones string `json:"failPhones"`
|
|
|
+ } `json:"result"`
|
|
|
+}
|
|
|
+
|
|
|
+func (cli *DongWuSms) sendUserLoginCode(mobile, apiURL, appKey, content string) (apiResp *DongWuSmsApiResult, err error) {
|
|
|
+ // Prepare request parameters
|
|
|
+ params := DongWuSmsApiParams{
|
|
|
+ GeneralAccount: appKey,
|
|
|
+ Receivers: mobile,
|
|
|
+ Text: content,
|
|
|
+ }
|
|
|
+
|
|
|
+ // Make HTTP request
|
|
|
+ jsonData, e := json.Marshal(params)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("json marshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ httpReq, e := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ httpReq.Header.Set("Content-Type", "application/json")
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, e := client.Do(httpReq)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("send request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, e := ioutil.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("read response err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ apiResp = &DongWuSmsApiResult{}
|
|
|
+ if e = json.Unmarshal(body, &apiResp); e != nil {
|
|
|
+ err = fmt.Errorf("parse response err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|