wangshan пре 2 недеља
родитељ
комит
ffd465c7f4
1 измењених фајлова са 89 додато и 86 уклоњено
  1. 89 86
      filter/xweb/visitFilter.go

+ 89 - 86
filter/xweb/visitFilter.go

@@ -103,106 +103,113 @@ func SetSevenSourceByRedis(sess *httpsession.Session, source string) bool {
 // 继承过滤器方法 优先级 短地址 < seo < sem < 渠道合作商
 func (l *VisitFilter) Do() bool {
 	var (
-		rawQuery    = l.R.URL.RawQuery
-		refer       = l.R.Referer()
-		sevenSource = GetSevenSourceByRedis(l.Session)
-		userId      = common.InterfaceToStr(l.SessVal["userId"])
+		rawQuery      = l.R.URL.RawQuery
+		refer         = l.R.Referer()
+		source        string
+		sevenSource   = GetSevenSourceByRedis(l.Session)
+		userId        = common.InterfaceToStr(l.SessVal["userId"])
+		loginCacheKey = fmt.Sprintf("login_log_%s_%s", userId, date.NowFormat(date.Date_yyyyMMdd))
 	)
+	if b, _ := redis.Exists(RedisCode, loginCacheKey); b {
+		return true
+	}
+	//1、未登录 且 sevenSource 为空
+	//2、已登录 且 未存登录日志
 	if l.SessVal["RReferer"] == nil && refer != "" {
 		l.Session.Set("RReferer", refer)
 	}
-	if userId == "" {
-		if sevenSource == "" {
-			//sem  示例:https://www.jianyu360.cn?jySource=360SEM  老参数:?id=360SEM
-			for _, sv := range SourceStrs {
-				if strings.Contains(rawQuery, sv) || strings.Contains(refer, sv) {
-					if strings.Contains(rawQuery, sv) {
-						rawQuery = strings.Split(rawQuery, sv)[1]
-					}
-					jySources := strings.Split(rawQuery, "&") //例:?jySource=x&a=x&b=x 只获取jySource的值
-					if len(jySources) == 0 {
-						jySources = strings.Split(refer, sv)
-						jySources = strings.Split(jySources[1], "&")
-					}
-					sevenSource = jySources[0]
-					//首先查看是否从百度等搜索引擎过来的referer
-					if sevenSource != "" {
-						for k, v := range semPromotion {
-							if k == sevenSource {
-								sevenSource = v
-								break
-							}
-						}
-						if l.SessVal["RSource"] == nil {
-							l.Session.Set("RSource", sevenSource)
-						}
-						l.Session.Set("RModule", sevenSource+"活动页")
-						l.Session.Set("RActiveCode", sevenSource)
-						fmt.Printf("sem: %s, 参数: %s\n", rawQuery, sevenSource)
-						break
-					}
-				}
+	//sem  示例:https://www.jianyu360.cn?jySource=360SEM  老参数:?id=360SEM
+	for _, sv := range SourceStrs {
+		if strings.Contains(rawQuery, sv) || strings.Contains(refer, sv) {
+			if strings.Contains(rawQuery, sv) {
+				rawQuery = strings.Split(rawQuery, sv)[1]
 			}
-
-			if sevenSource == "" && refer != "" {
-				//seo 示例:https://www.baidu.com/
-				for k, v := range refers {
-					if strings.Contains(refer, k) {
-						sevenSource = v
-						fmt.Printf("refer: %s, 参数: %s\n", refer, sevenSource)
+			jySources := strings.Split(rawQuery, "&") //例:?jySource=x&a=x&b=x 只获取jySource的值
+			if len(jySources) == 0 {
+				jySources = strings.Split(refer, sv)
+				jySources = strings.Split(jySources[1], "&")
+			}
+			source = jySources[0]
+			//首先查看是否从百度等搜索引擎过来的referer
+			if source != "" {
+				for k, v := range semPromotion {
+					if k == source {
+						source = v
 						break
 					}
 				}
-				if sevenSource == "" {
-					//渠道合作商
-					for _, sv := range channels {
-						re := regexp.MustCompile(sv)
-						matches := re.FindStringSubmatch(refer)
-						if len(matches) >= 2 {
-							sevenSource = matches[1]
-							if l.SessVal["RSource"] == nil {
-								l.Session.Set("RSource", sevenSource)
-							}
-							break
-						}
-					}
-					fmt.Printf("channel: %s, 参数: %s\n", refer, sevenSource)
+				if l.SessVal["RSource"] == nil {
+					l.Session.Set("RSource", source)
 				}
 				if sevenSource == "" {
-					//非本网站域名
-					if !strings.Contains(refer, webDomain) {
-						sevenSource = refer
-					}
+					l.Session.Set("RModule", source+"活动页")
+					l.Session.Set("RActiveCode", source)
 				}
-				if sevenSource != "" {
+				fmt.Printf("sem: %s, 参数: %s\n", rawQuery, source)
+				break
+			}
+		}
+	}
+
+	if source == "" && refer != "" {
+		//seo 示例:https://www.baidu.com/
+		for k, v := range refers {
+			if strings.Contains(refer, k) {
+				source = v
+				fmt.Printf("refer: %s, 参数: %s\n", refer, source)
+				break
+			}
+		}
+		if source == "" {
+			//渠道合作商
+			for _, sv := range channels {
+				re := regexp.MustCompile(sv)
+				matches := re.FindStringSubmatch(refer)
+				if len(matches) >= 2 {
+					source = matches[1]
 					if l.SessVal["RSource"] == nil {
-						l.Session.Set("RSource", refer)
+						l.Session.Set("RSource", source)
 					}
+					break
 				}
-				fmt.Printf("seo: %s, 参数: %s\n", rawQuery, sevenSource)
 			}
-			if sevenSource == "" {
-				//短地址
-				for _, sv := range paths {
-					re := regexp.MustCompile(sv)
-					matches := re.FindStringSubmatch(l.R.URL.Path)
-					if len(matches) >= 2 {
-						sevenSource = matches[1]
-						if l.SessVal["RSource"] == nil {
-							l.Session.Set("RSource", sevenSource)
-						}
-						break
-					}
-				}
-				fmt.Printf("path: %s, 参数: %s\n", l.R.URL.Path, sevenSource)
+			fmt.Printf("channel: %s, 参数: %s\n", refer, source)
+		}
+		if source == "" {
+			//非本网站域名
+			if !strings.Contains(refer, webDomain) {
+				source = refer
 			}
-			if sevenSource != "" {
-				SetSevenSourceByRedis(l.Session, sevenSource)
+		}
+		if source != "" {
+			if l.SessVal["RSource"] == nil {
+				l.Session.Set("RSource", refer)
 			}
 		}
-	} else {
+		fmt.Printf("seo: %s, 参数: %s\n", rawQuery, source)
+	}
+	if source == "" {
+		//短地址
+		for _, sv := range paths {
+			re := regexp.MustCompile(sv)
+			matches := re.FindStringSubmatch(l.R.URL.Path)
+			if len(matches) >= 2 {
+				source = matches[1]
+				if l.SessVal["RSource"] == nil {
+					l.Session.Set("RSource", source)
+				}
+				break
+			}
+		}
+		fmt.Printf("path: %s, 参数: %s\n", l.R.URL.Path, source)
+	}
+	//更新redis cache
+	if source != "" && sevenSource == "" {
+		SetSevenSourceByRedis(l.Session, source)
+	}
+	if userId != "" {
 		//登录日志
-		go l.addLoginLog(sevenSource)
+		go l.addLoginLog(source)
 	}
 	return true
 }
@@ -225,11 +232,7 @@ func (l *VisitFilter) addLoginLog(source string) {
 	todayTimeout := GetLastTime()
 	todayStr := date.NowFormat(date.Date_yyyyMMdd)
 	key := fmt.Sprintf("login_log_%s_%s", mgoUserid, todayStr)
-	if b, _ := redis.Exists(RedisCode, key); b {
-		return
-	} else {
-		redis.Put(RedisCode, key, 1, int(todayTimeout))
-	}
+	redis.Put(RedisCode, key, 1, int(todayTimeout))
 	timeNow := time.Now()
 	agent := l.R.Header.Get("user-agent")
 	sUrl := l.R.RequestURI