reportTrackFunMixin.js
3.71 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
145
146
147
import {
Util
} from "@/common/util"
import {
reportTrack
} from "@api/resident"
export default {
data() {
return {
prePagePath: "",
track_timerFlag: false,
track_batchId: null,
track_page_view_flag: false,
track_timer_out: null
}
},
beforeRouteEnter(to, from, next) {
next((vm) => {
vm.prePagePath = from.path
})
},
computed: {
loginAccount() {
return this.$store.state.d2admin.user.info.loginAccount
}
},
methods: {
setReportTrackData(
eventType,
eventCode = "",
businessParam = "",
batchId = ""
) {
let recordList = localStorage.getItem("recordList")
const route = this.$route
const {
loginAccout,
uuid,
userType
} = this.loginAccount
let {
prePagePath
} = this
const item = {
loginAccount: loginAccout,
uuid,
userType,
systemSource: 1,
eventTime: Util.dateFormate(Date.now(), "YYYY-MM-DD HH:mm:ss"),
eventType,
pagePath: route.path
}
if (!prePagePath) prePagePath = this.getPrePagePath(this)
item.prePagePath = prePagePath || "/index"
if (batchId) item.batchId = batchId
if (eventCode) item.eventCode = eventCode
if (businessParam) item.businessParam = JSON.stringify(businessParam)
if (!recordList) recordList = [item]
else {
recordList = JSON.parse(recordList)
recordList.push(item)
}
localStorage.setItem("recordList", JSON.stringify(recordList))
if (recordList.length > 9) {
reportTrack({
recordList
}).then((res) => {
if (res) localStorage.removeItem("recordList")
})
}
},
getRandomString(length = 30) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
let result = ""
for (let i = 0; i < length; i++) {
result += characters.charAt(
Math.floor(Math.random() * characters.length)
)
}
return result
},
onPageShow() {
if (this.track_page_view_flag) return
if (!this.track_batchId) {
this.track_batchId = `DJ_${this.getRandomString()}_${Date.now()}`
}
this.track_timerFlag = true
this.track_page_view_flag = true
if (this.track_timer_out) {
clearTimeout(this.track_timer_out)
this.track_timer_out = null
}
setTimeout(() => {
this.setBrowseTrackData()
}, 300)
},
onPageHide() {
this.track_timerFlag = false
this.track_batchId = null
this.track_page_view_flag = false
clearTimeout(this.track_timer_out)
this.track_timer_out = null
},
setBrowseTrackData() {
try {
if (!this.track_timerFlag || (this.$route.path != "/qualityControlList" && this.$route.path != "/shopConfig")) {
return
} else {
this.track_timer_out = setTimeout(() => {
this.setBrowseTrackData()
}, 10e3)
}
this.setReportTrackData(
2,
this.track_event_code,
this[this.track_businessParam_key],
this.track_batchId
)
} catch (error) {
console.log(error)
}
},
getPrePagePath(_this) {
let current = _this
const visited = new Set()
while (current) {
if (visited.has(current)) {
// 防止无限循环
console.error("Detected circular reference in parent chain")
return ""
}
visited.add(current)
if (current.$parent && current.$parent.prePagePath) {
return current.$parent.prePagePath
}
current = current.$parent
}
return ""
}
}
}