UserLog.go
4.97 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package Api
import (
"encoding/json"
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"letu/DB"
"math"
"strconv"
"time"
)
// @Title 增加访问日志
// @Description 增加用户行为日志
// @Accept json
// @Produce json
// @Param Type 访问页面 string true "安装;卸载;访问页面;使用功能;缩放地图;进入景区"
// @Param SubType 景区详情 string true "推荐;景区详情;登陆;商城;投诉建议;问券调查....(app中能点的都加上)"
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Param UserId 5dfb03070a9ac17ac7a82054 string true "用户ID"
// @Param UserName Aaron string true "用户名称"
// @Param Location {"Latitude": 119, "Longitude": 39} string true "位置"
// @Param Remarks 备注 string true "备注"
// @Param Source 用户分享 string true "来源"
// @Param DeviceId abc123 string true "手机唯一识别码,不重复(存放于http.header中)"
// @Param Mac abc123 string true "网卡Mac地址(存放于http.header中)"
// @Param SystemType ios string true "ios,android(存放于http.header中)"
// @Param SystemVersion 13.01 string true "手机版本(存放于http.header中)"
// @Param SystemModel iphone8 string true "手机型号(存放于http.header中)"
// @Param AppVersion 1.0 string true "app版本号(存放于http.header中)"
// @Param DeviceToken abc string true "推送token(存放于http.header中)"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UserLog? [post]
func UserLog(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.Request.Header.Get("DeviceId") == "" {
c.JSON(200, tools.ResponseError{
1,
"DeviceId不正确",
})
return
}
var Location DB.SLocation
json.Unmarshal([]byte(c.PostForm("Location")), &Location)
DB.CUserLog.InsertOne(tools.GetContext(),DB.SUserLog{
c.PostForm("Type"),
c.PostForm("SubType"),
c.PostForm("ScenicId"),
c.PostForm("UserId"),
c.PostForm("UserName"),
time.Now().Unix(),
Location,
c.PostForm("Remarks"),
c.Request.Host,
c.PostForm("Source"),
DB.SDevice{
c.Request.Header.Get("DeviceId"),
c.Request.Header.Get("Mac"),
c.Request.Header.Get("UDID"),
c.Request.Header.Get("SystemVersion"),
c.Request.Header.Get("SystemModel"),
c.Request.Header.Get("AppVersion"),
c.Request.Header.Get("AppVersion"),
c.Request.Header.Get("DeviceToken"),
},
})
upsert := true
DB.CDevice.FindOneAndUpdate(tools.GetContext(),
bson.M{"DeviceId": c.Request.Header.Get("DeviceId")},
bson.M{"$set": bson.M{
"Mac": c.Request.Header.Get("Mac"),
"UDID": c.Request.Header.Get("UDID"),
"SystemType": c.Request.Header.Get("SystemType"),
"SystemVersion": c.Request.Header.Get("SystemVersion"),
"SystemModel": c.Request.Header.Get("SystemModel"),
"AppVersion": c.Request.Header.Get("AppVersion"),
"DeviceToken": c.Request.Header.Get("DeviceToken"),
}}, &options.FindOneAndUpdateOptions{
Upsert: &upsert,
},
)
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}
// @Title 查询所有用户行为
// @Description 查询所有用户行为
// @Accept json
// @Produce json
// @Param ScenicId 5dfb03070a9ac17ac7a82054 string true "景区id"
// @Param Page 1 int true "当前第几页"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"total":1,"currpage":1,"totalpages":1,"prepage":20,"result":}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllUserLog? [get]
func AllUserLog(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
total,_ := DB.CUserLog.CountDocuments(tools.GetContext(), bson.M{"ScenicId":c.Query("ScenicId")})
limit, _ := strconv.ParseInt(c.Query("Limit"),10,64)
if limit == 0 {
limit = 50
}
currPage, _ := strconv.ParseInt(c.Query("Page"),10,64)
if currPage == 0 {
currPage = 1
}
skip := (currPage - 1) * limit
var aUserLog []DB.SUserLog
cur, err := DB.CUserLog.Find(tools.GetContext(), bson.M{"ScenicId":c.Query("ScenicId")}, &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}})
defer cur.Close(tools.GetContext())
if err == nil {
for cur.Next(tools.GetContext()) {
var e DB.SUserLog
cur.Decode(&e)
aUserLog = append(aUserLog,e)
}
}
c.JSON(200, tools.Page{
0,
total,
currPage,
int64(math.Ceil(float64(total) / float64(limit))),
limit,
aUserLog,
})
}