report.ts
4.34 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
148
149
150
151
152
import storage from './loggerAndStorage.js'
import http from './request.js'
import Util from '../utils/helper.js'
import type { ReportOption } from '../types/report.js'
// 异步发送策略
class sendStrategy {
dataHasSend = true
dataHasChange = false
syncStorage = false
failTime = 0
is_first_batch_write = false
DEFAULT_SETTING!: ReportOption
init(options: ReportOption) {
this.DEFAULT_SETTING = options
if (this.DEFAULT_SETTING.switch_start) {
this.dataHasChange = true
this.is_first_batch_write = true
this.batchInterval()
}
}
batchInterval() {
const _this = this
function loopWrite() {
setTimeout(() => {
_this.batchWrite()
loopWrite()
}, 500)
}
function loopSend() {
setTimeout(() => {
_this.batchSend()
loopSend()
}, 1000 * _this.DEFAULT_SETTING.send_time * Math.pow(2, _this.failTime))
}
loopWrite()
loopSend()
}
batchWrite() {
const _this = this
if (this.dataHasChange) {
if (this.is_first_batch_write) {
this.is_first_batch_write = false
setTimeout(() => {
_this.batchSend()
}, 1000)
}
this.dataHasChange = false
}
}
batchSend(singeleID?: number) {
if (this.dataHasSend) {
let data = []
if (singeleID) {
data = storage.getTrack(0, true)
data = data.filter(item => item.id == singeleID)
} else {
data = storage.getTrack(this.DEFAULT_SETTING.send_count, true)
console.log(data, 'data')
}
if (data.length > 0) {
this.dataHasSend = false
this.webrequest({
data,
success: this.batchRemove.bind(this),
fail: this.sendFail.bind(this)
})
}
}
}
batchRemove(data: any) {
// 删除数据
storage.removeTrack(data)
this.dataHasSend = true
this.dataHasChange = true
this.batchWrite()
this.failTime = 0
}
sendFail(data: any) {
this.dataHasSend = true
this.failTime++
storage.removeUploadData(data)
}
webrequest(option: { data: any; success: any; fail: any }) {
if (option.data.length > 0) {
console.log(option.data, new Date())
if (this.DEFAULT_SETTING.post_way == 'image') {
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const base_url = this.DEFAULT_SETTING.server_img_url
const param = JSON.stringify(upload_data)
const date = new Date().getTime()
try {
http.imagehttp.server_url = `${base_url}?d=${date}&data=${param}`
http.imagehttp.successCallBack = function () {
option.success(option.data)
console.log('发送成功', new Date())
}
let _this = this
http.imagehttp.errorCallback = function () {
console.log('发送失败', new Date())
option.fail(option.data)
_this.useAjax(option)
}
http.imagehttp.start()
} catch (error) {
console.log(error)
}
} else if (this.DEFAULT_SETTING.post_way == 'ajax') {
this.useAjax(option)
}
}
}
send(data: any) {
if (data && this.DEFAULT_SETTING.switch_start) {
this.dataHasChange = true
const id = storage.setTrack(data)
this.batchSend(id as unknown as number)
}
}
useAjax(option: { data: any; success: any; fail: any }) {
const url = this.DEFAULT_SETTING.server_url
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const param = JSON.stringify(upload_data)
let _this = this
http.ajax({
url,
type: 'POST',
data: { data: param },
success: function (res) {
option.success(option.data)
},
error: function (err) {
option.fail(option.data)
_this.useBeacon(option)
}
})
}
useBeacon(option: { data: any; success: any; fail?: any }) {
const url = this.DEFAULT_SETTING.server_url
const upload_data = Util.handlerTrackData(option.data)
storage.uploadTrack(option.data)
const param = JSON.stringify(upload_data)
const blob = new Blob([param], { type: 'application/json' })
http.beason.server_url = url
http.beason.start(blob)
http.beason.callback = function () {
option.success(option.data)
}
}
}
export default new sendStrategy()