瀏覽代碼

国际短信模板兼容

hsun 1 年之前
父節點
當前提交
b48d88a1e5
共有 2 個文件被更改,包括 70 次插入56 次删除
  1. 1 0
      models/business_conf.go
  2. 69 56
      services/sms.go

+ 1 - 0
models/business_conf.go

@@ -42,6 +42,7 @@ const (
 	BusinessConfLoginEmailTemplateContent = "LoginEmailTemplateContent"
 	BusinessConfLdapBindUserSuffix        = "LdapBindUserSuffix"
 	BusinessConfLdapUserFilter            = "LdapUserFilter"
+	BusinessConfSmsJhgjVariable           = "SmsJhgjVariable" // 聚合国际短信变量
 )
 
 const (

+ 69 - 56
services/sms.go

@@ -9,6 +9,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"net/url"
+	"strings"
 )
 
 // SendSmsCode 发送国内短信
@@ -105,59 +106,59 @@ func sendSms(jhGnAppKey, mobile, tplId, code string) (rs []byte, err error) {
 }
 
 // SendSmsCodeGj 发送国际短信
-func SendSmsCodeGj(mobile, vCode, areaNum, tplId string) bool {
-	var err error
-	defer func() {
-		if err != nil {
-			tips := fmt.Sprintf("短信验证码发送失败, Err: %s", err.Error())
-			utils.FileLog.Info("%s", tips)
-			go alarm_msg.SendAlarmMsg(tips, 2)
-		}
-	}()
-	// 获取配置好的短信模版
-	smsCond := ` AND conf_key = ? `
-	smsPars := make([]interface{}, 0)
-	smsPars = append(smsPars, "SmsJhgjAppKey")
-	conf := new(models.BusinessConf)
-	conf, e := conf.GetItemByCondition(smsCond, smsPars)
-	if e != nil {
-		if e.Error() == utils.ErrNoRow() {
-			err = fmt.Errorf("请先配置聚合短信Appkey")
-			return false
-		}
-		err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
-		return false
-	}
-	if conf.ConfVal == "" {
-		err = fmt.Errorf("请先配置聚合短信Appkey")
-		return false
-	}
-	result, err := sendSmsGj(conf.ConfVal, mobile, vCode, areaNum, tplId)
-	if err != nil {
-		fmt.Println("发送短信失败")
-		return false
-	}
-	fmt.Println("result", string(result))
-	var netReturn map[string]interface{}
-	err = json.Unmarshal(result, &netReturn)
-	if err != nil {
-		err = fmt.Errorf("短信验证码发送失败, Err:" + err.Error() + ";Result:" + string(result))
-		return false
-	}
-	if netReturn["error_code"].(float64) == 0 {
-		fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
-		return true
-	} else {
-		// 忽略错误的手机号码这种错误
-		if netReturn["error_code"].(float64) != 205401 {
-			err = fmt.Errorf("短信验证码发送失败, Result:" + string(result))
-		}
-		return false
-	}
-}
+//func SendSmsCodeGj(mobile, vCode, areaNum, tplId string) bool {
+//	var err error
+//	defer func() {
+//		if err != nil {
+//			tips := fmt.Sprintf("短信验证码发送失败, Err: %s", err.Error())
+//			utils.FileLog.Info("%s", tips)
+//			go alarm_msg.SendAlarmMsg(tips, 2)
+//		}
+//	}()
+//	// 获取配置好的短信模版
+//	smsCond := ` AND conf_key = ? `
+//	smsPars := make([]interface{}, 0)
+//	smsPars = append(smsPars, "SmsJhgjAppKey")
+//	conf := new(models.BusinessConf)
+//	conf, e := conf.GetItemByCondition(smsCond, smsPars)
+//	if e != nil {
+//		if e.Error() == utils.ErrNoRow() {
+//			err = fmt.Errorf("请先配置聚合短信Appkey")
+//			return false
+//		}
+//		err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
+//		return false
+//	}
+//	if conf.ConfVal == "" {
+//		err = fmt.Errorf("请先配置聚合短信Appkey")
+//		return false
+//	}
+//	result, err := sendSmsGj(conf.ConfVal, mobile, vCode, areaNum, tplId)
+//	if err != nil {
+//		fmt.Println("发送短信失败")
+//		return false
+//	}
+//	fmt.Println("result", string(result))
+//	var netReturn map[string]interface{}
+//	err = json.Unmarshal(result, &netReturn)
+//	if err != nil {
+//		err = fmt.Errorf("短信验证码发送失败, Err:" + err.Error() + ";Result:" + string(result))
+//		return false
+//	}
+//	if netReturn["error_code"].(float64) == 0 {
+//		fmt.Printf("接口返回result字段是:\r\n%v", netReturn["result"])
+//		return true
+//	} else {
+//		// 忽略错误的手机号码这种错误
+//		if netReturn["error_code"].(float64) != 205401 {
+//			err = fmt.Errorf("短信验证码发送失败, Result:" + string(result))
+//		}
+//		return false
+//	}
+//}
 
 // sendSmsGj 发送国际短信
-func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId string) (rs []byte, err error) {
+func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId, tplValue string) (rs []byte, err error) {
 	var Url *url.URL
 	apiURL := "http://v.juhe.cn/smsInternational/send.php"
 	//初始化参数
@@ -165,10 +166,17 @@ func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId string) (rs []byte, err
 	//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
 	param.Set("mobile", mobile) //接受短信的用户手机号码
 	//param.Set("tplId", "10054")           //您申请的短信模板ID,根据实际情况修改
-	param.Set("tplId", tplId)             //您申请的短信模板ID,根据实际情况修改
-	param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
-	param.Set("key", jhGjAppKey)          //应用APPKEY(应用详细页查询)
-	param.Set("areaNum", areaNum)         //应用APPKEY(应用详细页查询)
+	param.Set("tplId", tplId) //您申请的短信模板ID,根据实际情况修改
+	if strings.Contains(tplValue, "#code#=%s") {
+		tplValue = strings.Replace(tplValue, "#code#=%s", "#code#="+code, 1)
+	}
+	if strings.Contains(tplValue, "#m#=%d") {
+		tplValue = strings.Replace(tplValue, "#m#=%d", fmt.Sprintf("#m#=%d", utils.VerifyCodeExpireMinute), 1)
+	}
+	//param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
+	param.Set("tplValue", tplValue) //您设置的模板变量,根据实际情况
+	param.Set("key", jhGjAppKey)    //应用APPKEY(应用详细页查询)
+	param.Set("areaNum", areaNum)   //应用APPKEY(应用详细页查询)
 
 	Url, err = url.Parse(apiURL)
 	if err != nil {
@@ -268,8 +276,13 @@ func (cli *HzSms) SendUserLoginCode(req UserLoginSmsCodeReq) (result UserLoginSm
 			err = fmt.Errorf("请先配置聚合短信AppKey")
 			return
 		}
+		tplValue := confMap[models.BusinessConfSmsJhgjVariable]
+		if tplValue == "" {
+			// 默认初版变量
+			tplValue = "#code#=%s"
+		}
 
-		smsRes, e := sendSmsGj(appKey, req.Mobile, req.VerifyCode, req.TelAreaCode, tplId)
+		smsRes, e := sendSmsGj(appKey, req.Mobile, req.VerifyCode, req.TelAreaCode, tplId, tplValue)
 		if e != nil {
 			err = fmt.Errorf("send gj sms err: %s", e.Error())
 			return