Browse Source

Merge branch 'feature/dongwu_sms' into debug

# Conflicts:
#	models/business_conf.go
xyxie 2 days ago
parent
commit
070afb17c0
3 changed files with 143 additions and 0 deletions
  1. 5 0
      models/business_conf.go
  2. 136 0
      services/dongwu_sms.go
  3. 2 0
      services/sms.go

+ 5 - 0
models/business_conf.go

@@ -60,6 +60,10 @@ const (
 	BusinessConfReport2ImgUrl      = "Report2ImgUrl"      // 报告转长图地址(用于兼容内外网环境的)
 	BusinessConfReportViewUrl      = "ReportViewUrl"      // 报告详情地址     // 报告详情地址
 
+	// DongWu SMS configuration
+	BusinessConfDongwuSmsAppKey    = "DongWuSmsAppKey"    // 东吴短信AppKey
+	BusinessConfDongwuSmsApiUrl    = "DongWuSmsApiUrl"    // 东吴短信API地址
+
 	BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
 )
 
@@ -67,6 +71,7 @@ const (
 	BusinessConfReportApproveTypeEta   = "eta"
 	BusinessConfReportApproveTypeOther = "other"
 	BusinessConfClientFlagNanHua       = "nhqh" // 南华标记
+	BusinessConfClientFlagDongwu       = "dwqh" // 东吴标记
 	BusinessConfEmailClientSmtp        = "smtp" // 普通邮箱标记
 )
 

+ 136 - 0
services/dongwu_sms.go

@@ -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
+}

+ 2 - 0
services/sms.go

@@ -209,6 +209,8 @@ func NewSmsClient() (cli SmsClient, err error) {
 	}
 	if confMap[models.BusinessConfSmsClient] == models.BusinessConfClientFlagNanHua {
 		return new(NanHuaSms), nil
+	} else if confMap[models.BusinessConfSmsClient] == models.BusinessConfClientFlagDongwu {
+		return new(DongWuSms), nil
 	}
 	return new(HzSms), nil
 }