wangshan 5 сар өмнө
parent
commit
2614274278

+ 18 - 1
src/config.json

@@ -55,7 +55,24 @@
 		}
 		}
 	},
 	},
 	"redis":  "other=172.20.45.129:1712",
 	"redis":  "other=172.20.45.129:1712",
-	"strategy": [
+	"strategyPhone": [
+		{
+			"day": "3",
+			"num": 1,
+			"date": 180
+		},
+		{
+			"day": "7",
+			"num": 2,
+			"date": 420
+		},
+		{
+			"day": "2",
+			"num": 3,
+			"date": 7200
+		}
+	],
+	"strategyIp": [
 		{
 		{
 			"day": "3",
 			"day": "3",
 			"num": 1,
 			"num": 1,

+ 28 - 7
src/config/config.go

@@ -8,9 +8,26 @@ import (
 )
 )
 
 
 var (
 var (
-	Config       *config
-	BaseMysql    *mysql.Mysql
-	SmsStrategyS = []strategy{
+	Config           *config
+	BaseMysql        *mysql.Mysql
+	SmsStrategyPhone = []Strategy{
+		{
+			"1",
+			6,
+			24 * 60 * 60,
+		},
+		{
+			"7",
+			20,
+			7 * 24 * 60 * 60,
+		},
+		{
+			"30",
+			40,
+			30 * 24 * 60 * 60,
+		},
+	}
+	SmsStrategyIp = []Strategy{
 		{
 		{
 			"1",
 			"1",
 			6,
 			6,
@@ -72,12 +89,13 @@ type config struct {
 		} `json:"base"`
 		} `json:"base"`
 	} `json:"mysql"`
 	} `json:"mysql"`
 	Redis          string
 	Redis          string
-	Strategy       []strategy `json:"strategy"`
+	StrategyPhone  []Strategy `json:"strategyPhone"`
+	StrategyIp     []Strategy `json:"strategyIp"`
 	SmsIpMap       []string   `json:"smsIpMap"`
 	SmsIpMap       []string   `json:"smsIpMap"`
 	StrategySwitch bool       `json:"strategySwitch"`
 	StrategySwitch bool       `json:"strategySwitch"`
 }
 }
 
 
-type strategy struct {
+type Strategy struct {
 	Day  string `json:"day"`
 	Day  string `json:"day"`
 	Num  int    `json:"num"`
 	Num  int    `json:"num"`
 	Date int64  `json:"date"`
 	Date int64  `json:"date"`
@@ -100,8 +118,11 @@ func init() {
 		}
 		}
 		BaseMysql.Init()
 		BaseMysql.Init()
 	}
 	}
-	if len(Config.Strategy) > 0 {
-		SmsStrategyS = Config.Strategy
+	if len(Config.StrategyPhone) > 0 {
+		SmsStrategyPhone = Config.StrategyPhone
+	}
+	if len(Config.StrategyIp) > 0 {
+		SmsStrategyIp = Config.StrategyIp
 	}
 	}
 	if len(Config.SmsIpMap) > 0 {
 	if len(Config.SmsIpMap) > 0 {
 		SmsIpMap = map[string]bool{}
 		SmsIpMap = map[string]bool{}

+ 4 - 3
src/main_test.go

@@ -3,6 +3,7 @@ package main
 import (
 import (
 	"app.yhyue.com/moapp/jybase/date"
 	"app.yhyue.com/moapp/jybase/date"
 	"encoding/json"
 	"encoding/json"
+	"github.com/zeromicro/go-zero/core/logx"
 	"net/rpc"
 	"net/rpc"
 	"testing"
 	"testing"
 )
 )
@@ -11,7 +12,7 @@ func Test_Main(t *testing.T) {
 	var repl string
 	var repl string
 	client, err := rpc.DialHTTP("tcp", "127.0.0.1:932")
 	client, err := rpc.DialHTTP("tcp", "127.0.0.1:932")
 	if err != nil {
 	if err != nil {
-		logx.Println(err.Error())
+		logx.Info(err.Error())
 		return
 		return
 	}
 	}
 	defer client.Close()
 	defer client.Close()
@@ -22,8 +23,8 @@ func Test_Main(t *testing.T) {
 	})
 	})
 	err = client.Call("Sms.Execute", b, &repl)
 	err = client.Call("Sms.Execute", b, &repl)
 	if err != nil {
 	if err != nil {
-		logx.Println(err.Error())
+		logx.Info(err.Error())
 		return
 		return
 	}
 	}
-	logx.Println(repl)
+	logx.Info(repl)
 }
 }

+ 71 - 60
src/service/verify.go

@@ -12,7 +12,6 @@ import (
 )
 )
 
 
 var (
 var (
-	smsExistKey  = "sms_exist_%s"
 	smsIpKey     = "sms_ip_%s_%s"
 	smsIpKey     = "sms_ip_%s_%s"
 	smsPhoneKey  = "sms_phone_%s_%s"
 	smsPhoneKey  = "sms_phone_%s_%s"
 	smsCacheCode = "other"
 	smsCacheCode = "other"
@@ -21,9 +20,8 @@ var (
 )
 )
 
 
 func SmsStrategy(keys []string) (abnormalKey []string) {
 func SmsStrategy(keys []string) (abnormalKey []string) {
-	jylog.Info("config.SmsStrategyS:", config.SmsStrategyS)
-	for _, key := range keys {
-		for _, s := range config.SmsStrategyS {
+	var strategyFun = func(key string, ss []config.Strategy) {
+		for _, s := range ss {
 			cacheKey := fmt.Sprintf(key, s.Day)
 			cacheKey := fmt.Sprintf(key, s.Day)
 			num := redis.GetInt(smsCacheCode, cacheKey)
 			num := redis.GetInt(smsCacheCode, cacheKey)
 			ttl := redis.GetTTL(smsCacheCode, cacheKey)
 			ttl := redis.GetTTL(smsCacheCode, cacheKey)
@@ -38,6 +36,14 @@ func SmsStrategy(keys []string) (abnormalKey []string) {
 			redis.Put(smsCacheCode, cacheKey, num, int(ttl))
 			redis.Put(smsCacheCode, cacheKey, num, int(ttl))
 		}
 		}
 	}
 	}
+	for _, key := range keys {
+		switch {
+		case strings.Contains(key, "sms_ip"):
+			strategyFun(key, config.SmsStrategyIp)
+		case strings.Contains(key, "sms_phone"):
+			strategyFun(key, config.SmsStrategyPhone)
+		}
+	}
 	return
 	return
 }
 }
 
 
@@ -58,68 +64,73 @@ func Verify(reqData *ReqData) bool {
 	}
 	}
 	aks := SmsStrategy(keys)
 	aks := SmsStrategy(keys)
 	if len(aks) > 0 {
 	if len(aks) > 0 {
-		go func(aks []string, ip, mobile string) {
-			smsChan <- true
-			defer func() {
-				<-smsChan
-			}()
-			var (
-				ipState, phoneState int
-				now                 = time.Now()
-			)
-			for _, v := range aks {
-				if strings.Contains(v, "sms_ip") {
-					ipState = ipState + 1
-				}
-				if strings.Contains(v, "sms_phone") {
-					phoneState = phoneState + 1
-				}
+		go SaveRecord(aks, reqData.Ip, reqData.Phones)
+		jylog.Info(reqData.Phones, "---发送短信次数过多,规则:"+strings.Join(aks, "###"))
+		return false
+	}
+	return true
+}
+
+// 数据库数据处理
+func SaveRecord(aks []string, ip, mobile string) {
+	select {
+	case smsChan <- true:
+		defer func() {
+			<-smsChan
+		}()
+		var (
+			ipState, phoneState int
+			now                 = time.Now()
+		)
+		for _, v := range aks {
+			if strings.Contains(v, "sms_ip") {
+				ipState = ipState + 1
 			}
 			}
-			if ipState > 0 {
-				ipQuery := map[string]interface{}{
-					"ip": ip,
-				}
-				ipData := map[string]interface{}{
-					"ip":       ip,
-					"ip_state": ipState,
-					"date":     date.FormatDate(&now, date.Date_Full_Layout),
+			if strings.Contains(v, "sms_phone") {
+				phoneState = phoneState + 1
+			}
+		}
+		if ipState > 0 {
+			ipQuery := map[string]interface{}{
+				"ip": ip,
+			}
+			ipData := map[string]interface{}{
+				"ip":       ip,
+				"ip_state": ipState,
+				"date":     date.FormatDate(&now, date.Date_Full_Layout),
+			}
+			tableName := "useranaly.sms_black_ip"
+			if ipC := config.BaseMysql.Count(tableName, ipQuery); ipC == 0 {
+				if id := config.BaseMysql.Insert(tableName, ipData); id <= 0 {
+					jylog.Info("sms_black_ip 信息保存异常:", mobile, ip)
 				}
 				}
-				tableName := "useranaly.sms_black_ip"
-				if ipD := config.BaseMysql.FindOne(tableName, ipQuery, "ip,ip_state", "date desc"); ipD == nil {
-					if id := config.BaseMysql.Insert(tableName, ipData); id <= 0 {
-						jylog.Info("sms_black_ip 信息保存异常:", mobile, ip)
-					}
-				} else {
-					delete(ipData, "ip")
-					if ok := config.BaseMysql.Update(tableName, ipQuery, ipData); !ok {
-						jylog.Info("sms_black_ip 信息更新异常:", mobile, ip)
-					}
+			} else {
+				if id := config.BaseMysql.UpdateOrDeleteBySql("UPDATE "+tableName+" SET ip_state=ip_state+?  WHERE ip = ?", ipState, ip); id <= 0 {
+					jylog.Info("sms_black_ip 信息更新异常:", mobile, ip)
 				}
 				}
 			}
 			}
-			if phoneState > 0 {
-				phoneQuery := map[string]interface{}{
-					"phone": mobile,
-				}
-				phoneData := map[string]interface{}{
-					"phone":       mobile,
-					"phone_state": phoneState,
-					"date":        date.FormatDate(&now, date.Date_Full_Layout),
+		}
+		if phoneState > 0 {
+			phoneQuery := map[string]interface{}{
+				"phone": mobile,
+			}
+			phoneData := map[string]interface{}{
+				"phone":       mobile,
+				"phone_state": phoneState,
+				"date":        date.FormatDate(&now, date.Date_Full_Layout),
+			}
+			tableName := "useranaly.sms_black_phone"
+			if phoneC := config.BaseMysql.Count(tableName, phoneQuery); phoneC == 0 {
+				if id := config.BaseMysql.Insert(tableName, phoneData); id <= 0 {
+					jylog.Info("sms_black_phone 信息保存异常:", mobile, ip)
 				}
 				}
-				tableName := "useranaly.sms_black_phone"
-				if phoneD := config.BaseMysql.FindOne(tableName, phoneQuery, "phone,phone_state", "date desc"); phoneD == nil {
-					if id := config.BaseMysql.Insert(tableName, phoneData); id <= 0 {
-						jylog.Info("sms_black_phone 信息保存异常:", mobile, ip)
-					}
-				} else {
-					delete(phoneData, "phone")
-					if ok := config.BaseMysql.Update(tableName, phoneQuery, phoneData); !ok {
-						jylog.Info("sms_black_phone 信息更新异常:", mobile, ip)
-					}
+			} else {
+				if id := config.BaseMysql.UpdateOrDeleteBySql("UPDATE "+tableName+" SET phone_state=phone_state+?  WHERE phone = ?", phoneState, mobile); id <= 0 {
+					jylog.Info("sms_black_phone 信息更新异常:", mobile, ip)
 				}
 				}
 			}
 			}
-		}(aks, reqData.Ip, reqData.Phones)
-		jylog.Info(reqData.Phones, "---发送短信次数过多,规则:"+strings.Join(aks, "###"))
-		return false
+		}
+	case <-time.After(5 * time.Second):
+		jylog.Infof("数据库更新数据超时,手机号:%s, ip:%s, 触发规则:%s", mobile, ip, aks)
 	}
 	}
-	return true
 }
 }