@tmsfe/tms-core 0.0.89 → 0.0.92

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.89",
3
+ "version": "0.0.92",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,17 +3,28 @@
3
3
  */
4
4
 
5
5
  const maxArrLen = 10;
6
- const maxStrLen = 30;
6
+ const maxStrLen = 200;
7
7
  const maxFieldLen = 20;
8
8
 
9
- function isBasicsType(obj: any): { isBasics: boolean, value: any } {
9
+ // 字符串字段白名单,白名单内的字段不截断
10
+ const fieldWhiteList = [
11
+ 'tmapExtra', // 手图跳过来时附加的加密数据,用于分析用户操作路径,奉刚要求字段不能截断
12
+ ];
13
+
14
+ function isBasicsType(obj: any, isConstraintLen = true): { isBasics: boolean, value: any } {
10
15
  const type = typeof obj;
11
16
  if (obj === null || obj === undefined
12
17
  || type === 'number' || type === 'boolean' || type === 'bigint') {
13
18
  return { isBasics: true, value: obj };
14
19
  }
15
20
  if (type === 'string') {
16
- const value = obj.substr(0, maxStrLen);
21
+ // 简单判断是否encodeURIComponent字段,如果时的话则抛弃掉
22
+ // 因为在后端会被decode导致反序列化时语法错误
23
+ if (obj.match(/%/g)?.length >= 2) {
24
+ return { isBasics: true, value: '' };
25
+ }
26
+
27
+ const value = isConstraintLen ? obj.substring(0, maxStrLen) : obj;
17
28
  return { isBasics: true, value };
18
29
  }
19
30
  if (type === 'function' || type === 'symbol') {
@@ -55,7 +66,11 @@ function deepClone(obj: any, depth = 0, maxDepth = 5): any {
55
66
  continue;
56
67
  }
57
68
  const value = obj[name];
58
- const res1 = isBasicsType(value);
69
+ const isConstraintLen = !fieldWhiteList.includes(name);
70
+ if (!isConstraintLen) {
71
+ debugger
72
+ }
73
+ const res1 = isBasicsType(value, isConstraintLen);
59
74
  if (res1.isBasics) {
60
75
  // @ts-ignore
61
76
  newObj[name] = res1.value;
@@ -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
- launchOptions = options;
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,103 @@
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('&').forEach((str: string) => {
22
+ const [key, val] = str.split('=');
23
+ let value = val || '';
24
+ // 简单判断是否encodeURIComponent字段,如果时的话则抛弃掉
25
+ // 因为在后端会被decode导致反序列化时语法错误
26
+ if (value.indexOf('%') !== -1) {
27
+ value = '';
28
+ }
29
+ // @ts-ignore
30
+ data[key] = value;
31
+ });
32
+ helper.reportData(`wx_${api}_before`, path, data);
33
+
34
+ originalApi.apply(this, args);
35
+ },
36
+ });
37
+ }
38
+
39
+ // 劫持消息订阅接口
40
+ function proxySubscribeMessage(): void {
41
+ const originalApi = wx.requestSubscribeMessage;
42
+ Object.defineProperty(wx, 'requestSubscribeMessage', {
43
+ writable: true,
44
+ enumerable: true,
45
+ configurable: true,
46
+ value(options: any) {
47
+ const { tmplIds } = options;
48
+ const originalSuccess = options.success || helper.emptyFunc;
49
+ const originalFail = options.fail || helper.emptyFunc;
50
+ // eslint-disable-next-line
51
+ options.success = function(res: any) {
52
+ for (const tmplId of tmplIds) {
53
+ const result = res[tmplId];
54
+ if (result) {
55
+ helper.reportData('wx_requestSubscribeMessage_result', tmplId, result);
56
+ }
57
+ }
58
+ originalSuccess.call(this, res);
59
+ };
60
+ // eslint-disable-next-line
61
+ options.fail = function(err: any) {
62
+ helper.reportData('wx_requestSubscribeMessage_fail', tmplIds, err.errMsg);
63
+ originalFail.call(this, err);
64
+ };
65
+ originalApi.call(this, options);
66
+ },
67
+ });
68
+ }
69
+
70
+ // 监听onAppShow跟onAppHide
71
+ function listenerAppVisible(): void {
72
+ let showCount = 0; // 小程序切到前台次数(onAppShow)
73
+ let hideCount = 0; // 小程序切到后台次数(onAppHide)
74
+ let showTime = 0; // 触发onAppShow的时间
75
+ let hideTime = 0; // 触发onAppHide的时间
76
+ wx.onAppShow(() => {
77
+ showCount += 1;
78
+ showTime = Date.now();
79
+ // 距离onAppHide触发的时长
80
+ const lessHideTime = hideCount === 0 ? 0 : showTime - hideTime;
81
+ helper.reportData('App_onAppShow', { showCount, hideCount, lessHideTime });
82
+ });
83
+ wx.onAppHide(() => {
84
+ hideCount += 1;
85
+ hideTime = Date.now();
86
+ // 距离onAppShow触发的时长
87
+ const lessShowTime = hideTime - showTime;
88
+ helper.fastReportData('App_onAppHide', { showCount, hideCount, lessShowTime });
89
+ });
90
+ }
91
+
92
+ // 劫持App接口
93
+ function init(): void {
94
+ listenerAppVisible();
95
+ proxySubscribeMessage();
96
+ proxyNavigateApi('navigateTo');
97
+ proxyNavigateApi('redirectTo');
98
+ proxyNavigateApi('reLaunch');
99
+ }
100
+
101
+ export default {
102
+ init,
103
+ };
@@ -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
  let originalComponent: any = null;
11
11
 
@@ -4,17 +4,26 @@
4
4
 
5
5
  // / <reference path='./types.ts'/>
6
6
 
7
- let reporter: any = null;
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
- reporter.report2(...args);
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
- let isInit = false;
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 {
@@ -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 {