Browse Source

修改国际短信发送逻辑,聚合信息从数据库读取

xyxie 1 year ago
parent
commit
e36c031a8a
2 changed files with 79 additions and 25 deletions
  1. 61 19
      services/sms.go
  2. 18 6
      services/user_login.go

+ 61 - 19
services/sms.go

@@ -2,6 +2,7 @@ package services
 
 import (
 	"encoding/json"
+	"eta/eta_api/models"
 	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/utils"
 	"fmt"
@@ -27,8 +28,25 @@ func SendSmsCode(mobile, vCode, tplId string) (flag bool) {
 			go alarm_msg.SendAlarmMsg(tips, 2)
 		}
 	}()
-
-	result, e := sendSms(mobile, tplId, vCode)
+	// 获取配置好的短信模版
+	smsCond := ` AND conf_key = ? `
+	smsPars := make([]interface{}, 0)
+	smsPars = append(smsPars, "SmsJhgnAppKey")
+	conf := new(models.BusinessConf)
+	conf, e := conf.GetItemByCondition(smsCond, smsPars)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			err = fmt.Errorf("请先配置聚合短信Appkey")
+			return
+		}
+		err = fmt.Errorf("获取聚合短信配置信息失败, Err: %s", e.Error())
+		return
+	}
+	if conf.ConfVal == "" {
+		err = fmt.Errorf("请先配置聚合短信Appkey")
+		return
+	}
+	result, e := sendSms(conf.ConfVal, mobile, tplId, vCode)
 	if e != nil {
 		err = fmt.Errorf("send sms err: %s", e.Error())
 		return
@@ -58,7 +76,7 @@ func SendSmsCode(mobile, vCode, tplId string) (flag bool) {
 }
 
 // sendSms 发送国内短信
-func sendSms(mobile, tplId, code string) (rs []byte, err error) {
+func sendSms(jhGnAppKey, mobile, tplId, code string) (rs []byte, err error) {
 	var Url *url.URL
 	apiURL := "http://v.juhe.cn/sms/send"
 	//初始化参数
@@ -67,8 +85,8 @@ func sendSms(mobile, tplId, code string) (rs []byte, err error) {
 	param.Set("mobile", mobile) //接受短信的用户手机号码
 	param.Set("tpl_id", tplId)  //您申请的短信模板ID,根据实际情况修改
 	tplVal := fmt.Sprintf(`#code#=%s&#m#=%d`, code, utils.VerifyCodeExpireMinute)
-	param.Set("tpl_value", tplVal)     //您设置的模板变量,根据实际情况
-	param.Set("key", utils.JhGnAppKey) //应用APPKEY(应用详细页查询)
+	param.Set("tpl_value", tplVal) //您设置的模板变量,根据实际情况
+	param.Set("key", jhGnAppKey)   //应用APPKEY(应用详细页查询)
 
 	Url, err = url.Parse(apiURL)
 	if err != nil {
@@ -87,9 +105,34 @@ func sendSms(mobile, tplId, code string) (rs []byte, err error) {
 }
 
 // SendSmsCodeGj 发送国际短信
-func SendSmsCodeGj(mobile, vCode, areaNum string) bool {
-	flag := false
-	result, err := sendSmsGj(mobile, vCode, areaNum)
+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
@@ -98,34 +141,33 @@ func SendSmsCodeGj(mobile, vCode, areaNum string) bool {
 	var netReturn map[string]interface{}
 	err = json.Unmarshal(result, &netReturn)
 	if err != nil {
-		//go SendEmail("短信验证码发送失败", "err:"+err.Error()+" result"+string(result), utils.EmailSendToUsers)
-		go alarm_msg.SendAlarmMsg("短信验证码发送失败, Err:"+err.Error()+";Result:"+string(result), 2)
-		flag = false
+		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"])
-		flag = true
+		return true
 	} else {
 		// 忽略错误的手机号码这种错误
 		if netReturn["error_code"].(float64) != 205401 {
-			go alarm_msg.SendAlarmMsg("短信验证码发送失败, Result:"+string(result), 2)
+			err = fmt.Errorf("短信验证码发送失败, Result:" + string(result))
 		}
-		flag = false
+		return false
 	}
-	return flag
 }
 
 // sendSmsGj 发送国际短信
-func sendSmsGj(mobile, code, areaNum string) (rs []byte, err error) {
+func sendSmsGj(jhGjAppKey, mobile, code, areaNum, tplId string) (rs []byte, err error) {
 	var Url *url.URL
 	apiURL := "http://v.juhe.cn/smsInternational/send.php"
 	//初始化参数
 	param := url.Values{}
 	//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
-	param.Set("mobile", mobile)           //接受短信的用户手机号码
-	param.Set("tplId", "10054")           //您申请的短信模板ID,根据实际情况修改
+	param.Set("mobile", mobile) //接受短信的用户手机号码
+	//param.Set("tplId", "10054")           //您申请的短信模板ID,根据实际情况修改
+	param.Set("tplId", tplId)             //您申请的短信模板ID,根据实际情况修改
 	param.Set("tplValue", "#code#="+code) //您设置的模板变量,根据实际情况
-	param.Set("key", utils.JhGjAppKey)    //应用APPKEY(应用详细页查询)
+	param.Set("key", jhGjAppKey)          //应用APPKEY(应用详细页查询)
 	param.Set("areaNum", areaNum)         //应用APPKEY(应用详细页查询)
 
 	Url, err = url.Parse(apiURL)

+ 18 - 6
services/user_login.go

@@ -28,11 +28,11 @@ func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, er
 		return
 	}
 	// 获取配置好的短信模版
-	smsCond := ` AND conf_key = ? `
+	smsCond := ` AND conf_key in (?,?) `
 	smsPars := make([]interface{}, 0)
-	smsPars = append(smsPars, "LoginSmsTpId")
+	smsPars = append(smsPars, "LoginSmsTpId", "LoginSmsGjTpId")
 	conf := new(models.BusinessConf)
-	conf, e := conf.GetItemByCondition(smsCond, smsPars)
+	confList, e := conf.GetItemsByCondition(smsCond, smsPars, []string{"conf_key", "conf_val"}, "")
 	if e != nil {
 		if e.Error() == utils.ErrNoRow() {
 			err = fmt.Errorf("请先配置短信模版")
@@ -42,8 +42,16 @@ func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, er
 		return
 	}
 
-	//tplId := utils.SmsNewLoginTplId
-	tplId := conf.ConfVal
+	tplId := ""
+	gjTplId := ""
+	for _, v := range confList {
+		if v.ConfKey == "LoginSmsTpId" {
+			tplId = v.ConfVal
+		} else if v.ConfKey == "LoginSmsGjTpId" {
+			gjTplId = v.ConfVal
+		}
+	}
+
 	if tplId == "" {
 		err = fmt.Errorf("请先配置短信模版")
 		return
@@ -51,7 +59,11 @@ func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, er
 	if areaCode == "86" {
 		ok = SendSmsCode(mobile, verifyCode, tplId)
 	} else {
-		ok = SendSmsCodeGj(mobile, verifyCode, areaCode)
+		if gjTplId == "" {
+			err = fmt.Errorf("请先配置国际短信模版")
+			return
+		}
+		ok = SendSmsCodeGj(mobile, verifyCode, areaCode, gjTplId)
 	}
 	record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
 	if !ok {