135 lines
3.5 KiB
Go
135 lines
3.5 KiB
Go
package visitlog
|
|
|
|
import (
|
|
"91porn-server/app/service/adser"
|
|
"91porn-server/common/constant"
|
|
"context"
|
|
"time"
|
|
|
|
"91porn-server/app/appg"
|
|
"91porn-server/common"
|
|
"91porn-server/common/constant/redisconst"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/timeutil"
|
|
"91porn-server/middleware/ua"
|
|
"91porn-server/models/l/visitlogmod"
|
|
"91porn-server/models/v/usermod"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// LogMiddleware LogMiddleware
|
|
func Log(c *gin.Context) {
|
|
uid, err := common.GetUID(c)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if uid == 0 {
|
|
// log.Info("uid is empty ") //
|
|
return
|
|
}
|
|
uas, err := common.GetUA(c)
|
|
if err != nil {
|
|
log.Warn("get ua error", log.E(err))
|
|
return
|
|
}
|
|
now := time.Now()
|
|
//统计访问频率
|
|
userVisitLogKey := redisconst.UserVisitLogsKey(now)
|
|
//2021-01-11去掉用户行为记录
|
|
//registerBehaKey := redisconst.RegistBehaviorKey(uid)
|
|
//每日访问日志
|
|
//common.Go(func() {
|
|
// redisVisit(uid, uas.SysType, now)
|
|
//})
|
|
var exists bool
|
|
if val, err := appg.Redis.GetBit(userVisitLogKey, int64(uid)); err == nil {
|
|
exists = val == 1
|
|
}
|
|
if !exists { //有值了就返回
|
|
_ = appg.Redis.SetBit(userVisitLogKey, int64(uid), 1)
|
|
_, _ = appg.Redis.ExpireKey(userVisitLogKey, redisconst.UserVisitLogExpire(now))
|
|
common.Go(func() {
|
|
ip := common.GetIP(c)
|
|
recordVisitLogFull(uid, ip, uas, now)
|
|
})
|
|
common.Go(func() {
|
|
user, err := usermod.FindUserByUID(uid)
|
|
if err != nil {
|
|
log.Warn("find user by uid error", log.E(err))
|
|
return
|
|
}
|
|
if user == nil {
|
|
return
|
|
}
|
|
_ = adser.UpsertAdStat(context.Background(), user, time.Now(), 0, 0, 0)
|
|
})
|
|
}
|
|
}
|
|
|
|
//func redisVisit(uid uint64, sysType string, now time.Time) {
|
|
// recentMinute := timerange.RecentMinute(now, statrecordmod.FiveMinuteScale)
|
|
// redisKey := redisconst.VisitKey(recentMinute)
|
|
// _, _ = appg.Redis.SAdd(redisKey, usermod.VisitValue{UID: uid, SysType: sysType}.JsonString())
|
|
// _, _ = appg.Redis.ExpireKey(redisKey, redisconst.VisitExpireMax)
|
|
//}
|
|
|
|
func recordVisitLog(uid uint64, ip string, ua ua.UA, now time.Time) {
|
|
res, err := visitlogmod.UpsertUserVisit(&visitlogmod.VisitLog{
|
|
SumDate: timeutil.BeginningOfDay(now),
|
|
UID: uid,
|
|
IP: ip,
|
|
SysType: common.HandleSysType(ua.SysType),
|
|
Ver: ua.Ver,
|
|
DevType: ua.DevType,
|
|
DevID: ua.DevID,
|
|
BuildID: ua.BuildID,
|
|
IsDeduction: true,
|
|
})
|
|
if err != nil {
|
|
log.Warn("recordVisitLog UpsertUserVisit", log.E(err))
|
|
return
|
|
}
|
|
if res != nil && res.UpsertedCount > 0 {
|
|
if err := usermod.ChangeVisit(uid, ua.Ver, ua.SysType, now); err != nil {
|
|
log.Warn("recordVisitLog UserSelector", log.E(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
func recordVisitLogFull(uid uint64, ip string, ua ua.UA, now time.Time) {
|
|
u, _ := usermod.FindUserByUID(uid)
|
|
if u == nil {
|
|
recordVisitLog(uid, ip, ua, now)
|
|
return
|
|
}
|
|
res, err := visitlogmod.UpsertUserVisit(&visitlogmod.VisitLog{
|
|
SumDate: timeutil.BeginningOfDay(now),
|
|
UID: uid,
|
|
IP: ip,
|
|
SysType: common.HandleSysType(ua.SysType),
|
|
Ver: ua.Ver,
|
|
DevType: ua.DevType,
|
|
DevID: ua.DevID,
|
|
BuildID: ua.BuildID,
|
|
IsDirect: u.IsDirect,
|
|
DistrictCode: u.DistrictCode,
|
|
RegisterTime: u.CreatedAt,
|
|
IsDeduction: false,
|
|
})
|
|
if err != nil {
|
|
log.Warn("recordVisitLog UpsertUserVisit", log.E(err))
|
|
return
|
|
}
|
|
if res != nil && res.UpsertedCount > 0 {
|
|
if err := usermod.ChangeVisit(uid, ua.Ver, ua.SysType, now); err != nil {
|
|
log.Warn("recordVisitLog UserSelector", log.E(err))
|
|
}
|
|
}
|
|
// 游客信息不记录
|
|
if ua.Terminal == constant.TerminalWeb {
|
|
return
|
|
}
|
|
|
|
}
|