@tmsfe/tms-core 0.0.29 → 0.0.33
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 +2 -2
- package/src/cloudReport.ts +17 -13
- package/src/fastreport.js +17 -56
- package/src/index-proxy.js +5 -0
- package/src/report.js +15 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tmsfe/tms-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.33",
|
|
4
4
|
"description": "tms运行时框架",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"dev": "rimraf dist && rollup -wc --environment TARGET:tms-core,INTER_ENV:public",
|
|
11
11
|
"build": "rimraf dist && rollup -c --environment TARGET:tms-core,INTER_ENV:public"
|
|
12
12
|
},
|
|
13
|
-
"main": "
|
|
13
|
+
"main": "src/index",
|
|
14
14
|
"miniprogram": "src",
|
|
15
15
|
"buildOptions": {
|
|
16
16
|
"formats": [
|
package/src/cloudReport.ts
CHANGED
|
@@ -37,6 +37,17 @@ function getSystemInfoString(): string {
|
|
|
37
37
|
return encodeURIComponent(JSON.stringify(info));
|
|
38
38
|
}
|
|
39
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
|
+
|
|
40
51
|
function getSeqInfo(): { timestamp: number, seqId: string } {
|
|
41
52
|
const timestamp = Date.now();
|
|
42
53
|
const random = Math.random().toString();
|
|
@@ -120,19 +131,10 @@ function formatData(page: IPage, seqId: string, data: any[]): DataType[] {
|
|
|
120
131
|
let index = nextIndex;
|
|
121
132
|
for (const item of data) {
|
|
122
133
|
if (index >= arr.length) {
|
|
123
|
-
console.error(data);
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
const type = typeof item;
|
|
127
|
-
let value: string;
|
|
128
|
-
if (type === 'string') {
|
|
129
|
-
value = item;
|
|
130
|
-
} else if (type === 'object') {
|
|
131
|
-
value = JSON.stringify(item);
|
|
132
|
-
} else {
|
|
133
|
-
value = String(item);
|
|
134
|
+
console.error('埋点参数个数超出上限而被截断', data);
|
|
135
|
+
break;
|
|
134
136
|
}
|
|
135
|
-
arr[index] =
|
|
137
|
+
arr[index] = convert2String(item);
|
|
136
138
|
index += 1;
|
|
137
139
|
}
|
|
138
140
|
return arr;
|
|
@@ -185,11 +187,13 @@ function doCallCloudFunc(arr: DataType[], timestamp: number, seqId: string, open
|
|
|
185
187
|
*/
|
|
186
188
|
function callCloudFunc(arr: string[]): void {
|
|
187
189
|
const { timestamp, seqId } = getSeqInfo();
|
|
188
|
-
doCallCloudFunc(arr, timestamp, seqId,
|
|
190
|
+
doCallCloudFunc(arr, timestamp, seqId, openidIndex);
|
|
189
191
|
}
|
|
190
192
|
|
|
191
193
|
export default {
|
|
192
194
|
init,
|
|
193
195
|
report,
|
|
194
196
|
callCloudFunc,
|
|
197
|
+
convert2String,
|
|
198
|
+
getSystemInfoString,
|
|
195
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
|
@@ -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
|
/**
|