-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
100 lines (84 loc) 路 2.64 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package helper
import (
"github.com/duxweb/go-fast/cache"
"github.com/duxweb/go-fast/database"
"github.com/duxweb/go-fast/models"
"github.com/golang-module/carbon/v2"
"github.com/labstack/echo/v4"
"github.com/samber/lo"
"github.com/spf13/cast"
"gorm.io/gorm"
"strings"
)
func VisitIncrement(ctx echo.Context, hasType string, hasID uint, driver string, path string) error {
date := carbon.Now().ToDateStruct()
path = lo.Ternary[string](path == "", path, ctx.Path())
ua := ctx.Request().UserAgent()
uaParse, err := UaParser(ua)
if err != nil {
return err
}
browser := uaParse.UserAgent.ToString()
ip := ctx.RealIP()
visit := models.LogVisit{}
database.Gorm().Model(models.LogVisit{}).FirstOrCreate(&visit, map[string]any{
"has_type": hasType,
"has_id": hasID,
})
database.Gorm().Model(models.LogVisit{}).Where("id = ?", visit.ID).UpdateColumn("pv", gorm.Expr("pv + ?", 1))
database.Gorm().Model(models.LogVisitData{}).Debug().Create(&models.LogVisitData{
HasType: hasType,
HasId: hasID,
Date: date,
Ip: ip,
Browser: browser,
Driver: driver,
})
visitData := models.LogVisitData{}
database.Gorm().Model(models.LogVisitData{}).FirstOrCreate(&visitData, models.LogVisitData{
HasType: hasType,
HasId: hasID,
Date: date,
Ip: ip,
Browser: browser,
Driver: driver,
})
database.Gorm().Model(models.LogVisitData{}).Where("id = ?", visitData.ID).UpdateColumn("num", gorm.Expr("num + ?", 1))
keys := []string{
hasType,
cast.ToString(hasID),
driver,
ip,
browser,
}
key := strings.Join(keys, ".")
_, err = cache.Injector().Get([]byte(key))
if err != nil {
seconds := carbon.NewCarbon().EndOfDay().DiffAbsInSeconds(carbon.NewCarbon())
cache.Injector().Set([]byte(key), []byte("lock"), int(seconds))
database.Gorm().Model(models.LogVisit{}).Where("id = ?", visit.ID).UpdateColumn("uv", gorm.Expr("uv + ?", 1))
IpParse, _ := IpParser(ip)
if visitData.Country == "" && IpParse != "" {
address := strings.Split(IpParse, "|")
if len(address) >= 4 {
database.Gorm().Model(models.LogVisitData{}).Where("id = ?", visitData.ID).Updates(&models.LogVisitData{
Country: address[0],
Province: address[2],
City: address[3],
})
}
}
}
if uaParse.Device.Brand == "Spider" {
visitSpider := models.LogVisitSpider{}
database.Gorm().Model(&models.LogVisitSpider{}).FirstOrCreate(&visitSpider, models.LogVisitSpider{
HasType: hasType,
HasId: hasID,
Date: date,
Name: uaParse.Device.Family,
Path: path,
})
database.Gorm().Model(models.LogVisitSpider{}).Where("id = ?", visitSpider.ID).UpdateColumn("num", gorm.Expr("num + ?", 1))
}
return nil
}