@tmsfe/tms-core 0.0.28 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cloudReport.ts +18 -15
- package/src/fastreport.js +17 -56
- package/src/index-proxy.js +5 -0
- package/src/report.js +16 -5
package/package.json
CHANGED
package/src/cloudReport.ts
CHANGED
|
@@ -22,10 +22,9 @@ const openidIndex = 7; // openId的下标,由云函数填充
|
|
|
22
22
|
let initOptions: IInitOptions;
|
|
23
23
|
|
|
24
24
|
function init(options: IInitOptions): void {
|
|
25
|
-
console.log('IInitOptions', options)
|
|
26
25
|
if (!initOptions) {
|
|
27
26
|
initOptions = options;
|
|
28
|
-
wx.cloud.init({env: options.cloudEnvId});
|
|
27
|
+
wx.cloud.init({ env: options.cloudEnvId });
|
|
29
28
|
}
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -38,6 +37,17 @@ function getSystemInfoString(): string {
|
|
|
38
37
|
return encodeURIComponent(JSON.stringify(info));
|
|
39
38
|
}
|
|
40
39
|
|
|
40
|
+
function convert2String(value: any): string {
|
|
41
|
+
const type = typeof value;
|
|
42
|
+
if (type === 'string') {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
if (type === 'object') {
|
|
46
|
+
return JSON.stringify(value);
|
|
47
|
+
}
|
|
48
|
+
return String(value);
|
|
49
|
+
}
|
|
50
|
+
|
|
41
51
|
function getSeqInfo(): { timestamp: number, seqId: string } {
|
|
42
52
|
const timestamp = Date.now();
|
|
43
53
|
const random = Math.random().toString();
|
|
@@ -121,19 +131,10 @@ function formatData(page: IPage, seqId: string, data: any[]): DataType[] {
|
|
|
121
131
|
let index = nextIndex;
|
|
122
132
|
for (const item of data) {
|
|
123
133
|
if (index >= arr.length) {
|
|
124
|
-
console.error(data);
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
const type = typeof item;
|
|
128
|
-
let value: string;
|
|
129
|
-
if (type === 'string') {
|
|
130
|
-
value = item;
|
|
131
|
-
} else if (type === 'object') {
|
|
132
|
-
value = JSON.stringify(item);
|
|
133
|
-
} else {
|
|
134
|
-
value = String(item);
|
|
134
|
+
console.error('埋点参数个数超出上限而被截断', data);
|
|
135
|
+
break;
|
|
135
136
|
}
|
|
136
|
-
arr[index] =
|
|
137
|
+
arr[index] = convert2String(item);
|
|
137
138
|
index += 1;
|
|
138
139
|
}
|
|
139
140
|
return arr;
|
|
@@ -186,11 +187,13 @@ function doCallCloudFunc(arr: DataType[], timestamp: number, seqId: string, open
|
|
|
186
187
|
*/
|
|
187
188
|
function callCloudFunc(arr: string[]): void {
|
|
188
189
|
const { timestamp, seqId } = getSeqInfo();
|
|
189
|
-
doCallCloudFunc(arr, timestamp, seqId,
|
|
190
|
+
doCallCloudFunc(arr, timestamp, seqId, openidIndex);
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
export default {
|
|
193
194
|
init,
|
|
194
195
|
report,
|
|
195
196
|
callCloudFunc,
|
|
197
|
+
convert2String,
|
|
198
|
+
getSystemInfoString,
|
|
196
199
|
};
|
package/src/fastreport.js
CHANGED
|
@@ -3,28 +3,14 @@
|
|
|
3
3
|
* @author Fenggang.Sun <fenggangsun@tencent.com>
|
|
4
4
|
* @file 快速上报数据,不处理过多逻辑,保证快速上报
|
|
5
5
|
*/
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import Sync from './syncfnmanager';
|
|
9
|
-
|
|
10
|
-
let simulatedUserIdCache; // 模拟用户id在内存里的缓存
|
|
11
|
-
|
|
12
|
-
const getSystemInfo = () => {
|
|
13
|
-
const system = Sync.getSystemInfoSync();
|
|
14
|
-
const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host = '' } = system;
|
|
15
|
-
|
|
16
|
-
return { model, wxVersion, platform, SDKVersion, host };
|
|
17
|
-
};
|
|
6
|
+
import { getEnvInfo, getAuthInfo } from './env';
|
|
7
|
+
import CloudReport from './cloudReport';
|
|
18
8
|
|
|
19
9
|
const handleParamOfDifferentType = (param) => {
|
|
20
10
|
const data = new Array(41);
|
|
21
11
|
Object.keys(param).forEach((key) => {
|
|
22
|
-
|
|
23
|
-
if (valType === 'string') data[key] = param[key];
|
|
24
|
-
else if (valType === 'object') data[key] = JSON.stringify(param[key]);
|
|
25
|
-
else data[key] = String(param[key]);
|
|
12
|
+
data[key] = CloudReport.convert2String(param[key]);
|
|
26
13
|
});
|
|
27
|
-
|
|
28
14
|
return data;
|
|
29
15
|
};
|
|
30
16
|
|
|
@@ -39,52 +25,27 @@ export default class FastReport {
|
|
|
39
25
|
* @memberof FastReport
|
|
40
26
|
* @description 快速上报
|
|
41
27
|
* @param {Object} param 埋点数据
|
|
42
|
-
* @param {Boolean} simulatedUserId 是否上报模拟用户Id
|
|
43
|
-
* @param {Number} simulatedUserIdIndex 上报模拟用户ID时放在哪个字段
|
|
44
|
-
* @param {Boolean} reportShowScene 是否上报小程序onShow场景值
|
|
45
|
-
* @param {Boolean} appVer 是否上报小程序版本
|
|
46
|
-
* @returns {Promsie} 返回上报结果
|
|
47
28
|
*/
|
|
48
|
-
static report(param = {}
|
|
49
|
-
if (!param[27])
|
|
29
|
+
static report(param = {}) {
|
|
30
|
+
if (!param[27]) {
|
|
31
|
+
return Promise.reject('invalid report param');
|
|
32
|
+
}
|
|
50
33
|
const env = getEnvInfo();
|
|
51
34
|
const data = handleParamOfDifferentType(param);
|
|
52
35
|
data[9] = '2';
|
|
53
|
-
data[
|
|
54
|
-
if (appVer) {
|
|
55
|
-
data[10] = defAssign(data[10], env.appVersion);
|
|
56
|
-
}
|
|
36
|
+
data[10] = defAssign(data[10], env.appVersion);
|
|
57
37
|
data[26] = defAssign(data[26], data[27]?.[0]);
|
|
58
38
|
data[28] = env.client;
|
|
59
|
-
if (
|
|
39
|
+
if (!data[29]) {
|
|
60
40
|
const appShowScene = wx.getStorageSync('appShowScene');
|
|
61
|
-
if (appShowScene)
|
|
41
|
+
if (appShowScene) {
|
|
42
|
+
data[29] = String(appShowScene);
|
|
43
|
+
}
|
|
62
44
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.catch(() => null);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @memberof FastReport
|
|
73
|
-
* @description 获取模拟的用户身份标识
|
|
74
|
-
* @returns {String} 用户的临时标识
|
|
75
|
-
*/
|
|
76
|
-
static getSimulatedUserId() {
|
|
77
|
-
// 优先使用内存级缓存
|
|
78
|
-
if (simulatedUserIdCache) return simulatedUserIdCache;
|
|
79
|
-
const key = 'SimulatedUserKey';
|
|
80
|
-
// 读取本地缓存记录的值
|
|
81
|
-
simulatedUserIdCache = wx.getStorageSync(key);
|
|
82
|
-
if (simulatedUserIdCache) return simulatedUserIdCache;
|
|
83
|
-
// 生成新的值
|
|
84
|
-
const nonce = Math.random().toString(36)
|
|
85
|
-
.substr(2, 10);
|
|
86
|
-
simulatedUserIdCache = `${Date.now()}_${nonce}`;
|
|
87
|
-
wx.setStorage({ key, data: simulatedUserIdCache });
|
|
88
|
-
return simulatedUserIdCache;
|
|
45
|
+
data[33] = CloudReport.getSystemInfoString();
|
|
46
|
+
getAuthInfo().then((user) => {
|
|
47
|
+
data[5] = user.uid;
|
|
48
|
+
CloudReport.callCloudFunc(data);
|
|
49
|
+
});
|
|
89
50
|
}
|
|
90
51
|
};
|
package/src/index-proxy.js
CHANGED
|
@@ -210,6 +210,11 @@ function getCloudReport() {
|
|
|
210
210
|
return CloudReport;
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
+
wx.onAppShow((options) => {
|
|
214
|
+
// 避免vendor分包加载太慢而错过onAppShow
|
|
215
|
+
wx.tmsAppShowOptions = options;
|
|
216
|
+
});
|
|
217
|
+
|
|
213
218
|
const api = {
|
|
214
219
|
isProxy: true, // 方便定位问题时判断是否proxy
|
|
215
220
|
initProxy,
|
package/src/report.js
CHANGED
|
@@ -299,7 +299,7 @@ const cache = (reportData, reportNow, locPoi = true) => {
|
|
|
299
299
|
}
|
|
300
300
|
return Promise.all(task)
|
|
301
301
|
.then(async (deviceData) => {
|
|
302
|
-
|
|
302
|
+
// 先对上报数据进行预处理
|
|
303
303
|
const result = await formatReportData(reportData, deviceData);
|
|
304
304
|
// 将需要上报的数据缓存在队列中
|
|
305
305
|
ReportDataQueue.push(result);
|
|
@@ -342,6 +342,7 @@ const sendData = async () => {
|
|
|
342
342
|
* @returns {void}
|
|
343
343
|
*/
|
|
344
344
|
const startSendTimer = (clearTimerAfterSend) => {
|
|
345
|
+
clearInterval(ReportTaskId);
|
|
345
346
|
ReportTaskId = setInterval(async () => {
|
|
346
347
|
sendData();
|
|
347
348
|
if (clearTimerAfterSend) {
|
|
@@ -395,15 +396,25 @@ const setAppShowOptions = (options) => {
|
|
|
395
396
|
* init之后再监听
|
|
396
397
|
*/
|
|
397
398
|
const startListenApp = () => {
|
|
399
|
+
const doAppShow = (options) => {
|
|
400
|
+
// 更新onShow参数
|
|
401
|
+
setAppShowOptions(options);
|
|
402
|
+
// 上报数据
|
|
403
|
+
report(true);
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// 避免vendor分包加载太慢而错过onAppShow
|
|
407
|
+
if (wx.tmsAppShowOptions) {
|
|
408
|
+
doAppShow(wx.tmsAppShowOptions);
|
|
409
|
+
wx.tmsAppShowOptions = null;
|
|
410
|
+
}
|
|
411
|
+
|
|
398
412
|
/**
|
|
399
413
|
* 监听小程序onShow事件
|
|
400
414
|
* @private
|
|
401
415
|
*/
|
|
402
416
|
wx.onAppShow((options) => {
|
|
403
|
-
|
|
404
|
-
setAppShowOptions(options);
|
|
405
|
-
// 上报数据
|
|
406
|
-
report(true);
|
|
417
|
+
doAppShow(options);
|
|
407
418
|
});
|
|
408
419
|
|
|
409
420
|
/**
|