@tmsfe/tms-core 0.0.88 → 0.0.91
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/report/{proxy/clone.ts → clone.ts} +8 -2
- package/src/report/helper.ts +9 -1
- package/src/report/proxy/app.ts +100 -0
- package/src/report/proxy/component.ts +1 -1
- package/src/report/proxy/helper.ts +18 -8
- package/src/report/proxy/index.ts +5 -6
- package/src/report/proxy/page.ts +1 -54
- package/src/utils/location/index.js +16 -4
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
const maxArrLen = 10;
|
|
6
|
-
const maxStrLen =
|
|
6
|
+
const maxStrLen = 200;
|
|
7
7
|
const maxFieldLen = 20;
|
|
8
8
|
|
|
9
9
|
function isBasicsType(obj: any): { isBasics: boolean, value: any } {
|
|
@@ -13,7 +13,13 @@ function isBasicsType(obj: any): { isBasics: boolean, value: any } {
|
|
|
13
13
|
return { isBasics: true, value: obj };
|
|
14
14
|
}
|
|
15
15
|
if (type === 'string') {
|
|
16
|
-
|
|
16
|
+
// 简单判断是否encodeURIComponent字段,如果时的话则抛弃掉
|
|
17
|
+
// 因为在后端会被decode导致反序列化时语法错误
|
|
18
|
+
if (obj.match(/%/g)?.length >= 2) {
|
|
19
|
+
return { isBasics: true, value: '' };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const value = obj.substring(0, maxStrLen);
|
|
17
23
|
return { isBasics: true, value };
|
|
18
24
|
}
|
|
19
25
|
if (type === 'function' || type === 'symbol') {
|
package/src/report/helper.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// / <reference path='./types.ts'/>
|
|
6
6
|
import syncApi from '../syncfnmanager';
|
|
7
|
+
import clone from './clone';
|
|
7
8
|
|
|
8
9
|
function getTms(): any {
|
|
9
10
|
// 如果是在app.js的onLaunch中调用,则没有getApp().tms为空
|
|
@@ -33,7 +34,8 @@ function init(options: IInitOptions): void {
|
|
|
33
34
|
};
|
|
34
35
|
|
|
35
36
|
wx.onAppShow((options) => {
|
|
36
|
-
|
|
37
|
+
// 克隆函数内会限制对象字段
|
|
38
|
+
launchOptions = clone.deepClone(options);
|
|
37
39
|
});
|
|
38
40
|
}
|
|
39
41
|
|
|
@@ -145,6 +147,10 @@ function getPageInfo(): IPage {
|
|
|
145
147
|
options = page?.options;
|
|
146
148
|
depth = pages.length;
|
|
147
149
|
}
|
|
150
|
+
|
|
151
|
+
// 克隆函数内会限制对象字段
|
|
152
|
+
options = clone.deepClone(options);
|
|
153
|
+
|
|
148
154
|
// wx_navigate_before埋点上报时可能route是一个很大的对象而不是字符串,原因不详
|
|
149
155
|
if (typeof route as any !== 'string') {
|
|
150
156
|
route = '';
|
|
@@ -160,6 +166,8 @@ let launchOptions: any = null;
|
|
|
160
166
|
function getLaunchOptions(): any {
|
|
161
167
|
if (launchOptions === null) {
|
|
162
168
|
launchOptions = syncApi.getLaunchOptionsSync();
|
|
169
|
+
// 克隆函数内会限制对象字段
|
|
170
|
+
launchOptions = clone.deepClone(launchOptions);
|
|
163
171
|
}
|
|
164
172
|
return launchOptions;
|
|
165
173
|
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 负责小程序级的全埋点
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// / <reference path='./types.ts'/>
|
|
6
|
+
|
|
7
|
+
import helper from './helper';
|
|
8
|
+
|
|
9
|
+
// 劫持导航api
|
|
10
|
+
function proxyNavigateApi(api: string): void {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
const originalApi = wx[api];
|
|
13
|
+
Object.defineProperty(wx, api, {
|
|
14
|
+
writable: true,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
value(...args: any) {
|
|
18
|
+
const { url = '' } = args[0] || {};
|
|
19
|
+
const [path, params = ''] = url.split('?');
|
|
20
|
+
const data = {};
|
|
21
|
+
params.split('&').map((str: string) => {
|
|
22
|
+
let [key, value = ''] = str.split('=');
|
|
23
|
+
if (value.indexOf('%') !== -1) {
|
|
24
|
+
value = '';
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
data[key] = value;
|
|
28
|
+
});
|
|
29
|
+
helper.reportData(`wx_${api}_before`, path, data);
|
|
30
|
+
|
|
31
|
+
originalApi.apply(this, args);
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 劫持消息订阅接口
|
|
37
|
+
function proxySubscribeMessage(): void {
|
|
38
|
+
const originalApi = wx.requestSubscribeMessage;
|
|
39
|
+
Object.defineProperty(wx, 'requestSubscribeMessage', {
|
|
40
|
+
writable: true,
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
value(options: any) {
|
|
44
|
+
const { tmplIds } = options;
|
|
45
|
+
const originalSuccess = options.success || helper.emptyFunc;
|
|
46
|
+
const originalFail = options.fail || helper.emptyFunc;
|
|
47
|
+
// eslint-disable-next-line
|
|
48
|
+
options.success = function(res: any) {
|
|
49
|
+
for (const tmplId of tmplIds) {
|
|
50
|
+
const result = res[tmplId];
|
|
51
|
+
if (result) {
|
|
52
|
+
helper.reportData('wx_requestSubscribeMessage_result', tmplId, result);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
originalSuccess.call(this, res);
|
|
56
|
+
};
|
|
57
|
+
// eslint-disable-next-line
|
|
58
|
+
options.fail = function(err: any) {
|
|
59
|
+
helper.reportData('wx_requestSubscribeMessage_fail', tmplIds, err.errMsg);
|
|
60
|
+
originalFail.call(this, err);
|
|
61
|
+
};
|
|
62
|
+
originalApi.call(this, options);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 监听onAppShow跟onAppHide
|
|
68
|
+
function listenerAppVisible(): void {
|
|
69
|
+
let showCount = 0; // 小程序切到前台次数(onAppShow)
|
|
70
|
+
let hideCount = 0; // 小程序切到后台次数(onAppHide)
|
|
71
|
+
let showTime = 0; // 触发onAppShow的时间
|
|
72
|
+
let hideTime = 0; // 触发onAppHide的时间
|
|
73
|
+
wx.onAppShow(() => {
|
|
74
|
+
showCount += 1;
|
|
75
|
+
showTime = Date.now();
|
|
76
|
+
// 距离onAppHide触发的时长
|
|
77
|
+
const lessHideTime = hideCount === 0 ? 0 : showTime - hideTime;
|
|
78
|
+
helper.reportData('App_onAppShow', { showCount, hideCount, lessHideTime });
|
|
79
|
+
});
|
|
80
|
+
wx.onAppHide(() => {
|
|
81
|
+
hideCount += 1;
|
|
82
|
+
hideTime = Date.now();
|
|
83
|
+
// 距离onAppShow触发的时长
|
|
84
|
+
const lessShowTime = hideTime - showTime;
|
|
85
|
+
helper.fastReportData('App_onAppHide', { showCount, hideCount, lessShowTime });
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 劫持App接口
|
|
90
|
+
function init(): void {
|
|
91
|
+
listenerAppVisible();
|
|
92
|
+
proxySubscribeMessage();
|
|
93
|
+
proxyNavigateApi('navigateTo');
|
|
94
|
+
proxyNavigateApi('redirectTo');
|
|
95
|
+
proxyNavigateApi('reLaunch');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
init,
|
|
100
|
+
};
|
|
@@ -4,17 +4,26 @@
|
|
|
4
4
|
|
|
5
5
|
// / <reference path='./types.ts'/>
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
function getReporter(): any {
|
|
8
|
+
// 如果是在app.js的onLaunch中调用,则没有getApp().tms为空
|
|
9
|
+
const tms = getApp()?.tms || wx.tms;
|
|
10
|
+
return tms.getReporter();
|
|
11
|
+
}
|
|
8
12
|
|
|
13
|
+
/**
|
|
14
|
+
* 聚合上报埋点
|
|
15
|
+
*/
|
|
9
16
|
function reportData(...args: any[]): any {
|
|
10
|
-
if (reporter === null) {
|
|
11
|
-
// 如果是在app.js的onLaunch中调用,则没有getApp().tms为空
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
const tms = getApp()?.tms || wx.tms;
|
|
14
|
-
reporter = tms.getReporter();
|
|
15
|
-
}
|
|
16
17
|
console.log('自动埋点:', ...args);
|
|
17
|
-
|
|
18
|
+
getReporter().report2(...args);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 立即上报埋点
|
|
23
|
+
*/
|
|
24
|
+
function fastReportData(...args: any[]): any {
|
|
25
|
+
console.log('快速自动埋点:', ...args);
|
|
26
|
+
getReporter().fastReport2(...args);
|
|
18
27
|
}
|
|
19
28
|
|
|
20
29
|
let systemInfo: any = null;
|
|
@@ -90,6 +99,7 @@ function executeFunc(context: any, func: Function, args: any[], callback: Functi
|
|
|
90
99
|
export default {
|
|
91
100
|
emptyFunc,
|
|
92
101
|
reportData,
|
|
102
|
+
fastReportData,
|
|
93
103
|
executeFunc,
|
|
94
104
|
getSystemInfo,
|
|
95
105
|
getLastBindEvent,
|
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
* 负责页面和组件的全埋点
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import proxyApp from './app';
|
|
5
6
|
import proxyPage from './page';
|
|
6
7
|
import proxyComponent from './component';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
proxyApp.init();
|
|
10
|
+
proxyPage.init();
|
|
11
|
+
proxyComponent.init();
|
|
9
12
|
|
|
13
|
+
// 目前没有需要控制初始化时机的场景
|
|
10
14
|
function init(): void {
|
|
11
|
-
if (!isInit) {
|
|
12
|
-
isInit = true;
|
|
13
|
-
proxyPage.init();
|
|
14
|
-
proxyComponent.init();
|
|
15
|
-
}
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export default {
|
package/src/report/proxy/page.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
// / <reference path='./types.ts'/>
|
|
6
6
|
|
|
7
|
+
import clone from '../clone';
|
|
7
8
|
import helper from './helper';
|
|
8
|
-
import clone from './clone';
|
|
9
9
|
|
|
10
10
|
// 工具为页面根节点插入的触摸事件
|
|
11
11
|
const pageTouchEvent = 'onReportPageTouch';
|
|
@@ -122,62 +122,9 @@ function proxyPage(): void {
|
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
// 劫持导航api
|
|
126
|
-
function proxyNavigateApi(api: string): void {
|
|
127
|
-
// @ts-ignore
|
|
128
|
-
const originalApi = wx[api];
|
|
129
|
-
Object.defineProperty(wx, api, {
|
|
130
|
-
writable: true,
|
|
131
|
-
enumerable: true,
|
|
132
|
-
configurable: true,
|
|
133
|
-
value(...args: any) {
|
|
134
|
-
const { url = '' } = args[0] || {};
|
|
135
|
-
const [path, params = ''] = url.split('?');
|
|
136
|
-
helper.reportData(`wx_${api}_before`, path, params);
|
|
137
|
-
|
|
138
|
-
originalApi.apply(this, args);
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// 劫持消息订阅接口
|
|
144
|
-
function proxySubscribeMessage(): void {
|
|
145
|
-
const originalApi = wx.requestSubscribeMessage;
|
|
146
|
-
Object.defineProperty(wx, 'requestSubscribeMessage', {
|
|
147
|
-
writable: true,
|
|
148
|
-
enumerable: true,
|
|
149
|
-
configurable: true,
|
|
150
|
-
value(options: any) {
|
|
151
|
-
const { tmplIds } = options;
|
|
152
|
-
const originalSuccess = options.success || helper.emptyFunc;
|
|
153
|
-
const originalFail = options.fail || helper.emptyFunc;
|
|
154
|
-
// eslint-disable-next-line
|
|
155
|
-
options.success = function(res: any) {
|
|
156
|
-
for (const tmplId of tmplIds) {
|
|
157
|
-
const result = res[tmplId];
|
|
158
|
-
if (result) {
|
|
159
|
-
helper.reportData('wx_requestSubscribeMessage_result', tmplId, result);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
originalSuccess.call(this, res);
|
|
163
|
-
};
|
|
164
|
-
// eslint-disable-next-line
|
|
165
|
-
options.fail = function(err: any) {
|
|
166
|
-
helper.reportData('wx_requestSubscribeMessage_fail', tmplIds, err.errMsg);
|
|
167
|
-
originalFail.call(this, err);
|
|
168
|
-
};
|
|
169
|
-
originalApi.call(this, options);
|
|
170
|
-
},
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
125
|
// 劫持Page
|
|
175
126
|
function init(): void {
|
|
176
127
|
proxyPage();
|
|
177
|
-
proxySubscribeMessage();
|
|
178
|
-
proxyNavigateApi('navigateTo');
|
|
179
|
-
proxyNavigateApi('redirectTo');
|
|
180
|
-
proxyNavigateApi('reLaunch');
|
|
181
128
|
}
|
|
182
129
|
|
|
183
130
|
export default {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @author: fenggangsun
|
|
4
4
|
* @date: 2022-06-27 11:46:25
|
|
5
5
|
* @lastEditors: fenggangsun
|
|
6
|
-
* @lastEditTime: 2022-
|
|
6
|
+
* @lastEditTime: 2022-07-27 02:35:10
|
|
7
7
|
* @copyright 2022-present, Tencent, Inc. All rights reserved.
|
|
8
8
|
*/
|
|
9
9
|
const defaultLoc = {
|
|
@@ -52,8 +52,7 @@ const getPrioritizedLocation = async (typeOrders, option) => {
|
|
|
52
52
|
.then((data) => {
|
|
53
53
|
const { ad_info: { adcode, cityCode, province, city }, location: { lng, lat } } = data;
|
|
54
54
|
return {
|
|
55
|
-
adCode: adcode, cityCode,
|
|
56
|
-
province, cityName: city, longitude: lng, latitude: lat,
|
|
55
|
+
adCode: adcode, province, cityCode, cityName: city, longitude: lng, latitude: lat,
|
|
57
56
|
...data,
|
|
58
57
|
};
|
|
59
58
|
});
|
|
@@ -70,11 +69,24 @@ const getPrioritizedLocation = async (typeOrders, option) => {
|
|
|
70
69
|
default: break;
|
|
71
70
|
}
|
|
72
71
|
const loc = await Promise.resolve(getLocProm).catch(() => null);
|
|
73
|
-
if (loc) return {
|
|
72
|
+
if (loc) return {
|
|
73
|
+
locType,
|
|
74
|
+
loc: {
|
|
75
|
+
...loc,
|
|
76
|
+
provinceCode: parseProvinceCode(loc),
|
|
77
|
+
},
|
|
78
|
+
};
|
|
74
79
|
}
|
|
75
80
|
return Promise.reject('按照参数指定的类型未能获取到位置');
|
|
76
81
|
};
|
|
77
82
|
|
|
83
|
+
const parseProvinceCode = (loc) => {
|
|
84
|
+
if (!loc) return '';
|
|
85
|
+
const { provinceCode, cityCode } = loc;
|
|
86
|
+
if (provinceCode) return provinceCode;
|
|
87
|
+
return cityCode ? cityCode.slice(0, 2).padEnd(6, 0) : '';
|
|
88
|
+
};
|
|
89
|
+
|
|
78
90
|
/**
|
|
79
91
|
* 按优先级获取用户位置
|
|
80
92
|
* @param {Boolean} visitedIndex 是否打开过首页
|