@tmsfe/tms-core 0.0.52 → 0.0.55

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.52",
3
+ "version": "0.0.55",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,7 @@ function getBaseData(deviceData: IDeviceData): DataItem[] {
12
12
  const { networkType, location } = deviceData;
13
13
  const { appVersion, client } = helper.getInitOptions();
14
14
  const { host } = helper.getSystemInfo();
15
- const arr = new Array<string>(helper.dataArrLen);
15
+ const arr = new Array<DataItem>(helper.dataArrLen);
16
16
  // todo: 如何区分新旧埋点?新:f20不为空,旧:f20为空
17
17
  // ++++++++++++++++++++++++++字段列表++++++++++++++++++++++++++
18
18
  // 0: log_time,日志入库时间
@@ -44,15 +44,15 @@ function getBaseData(deviceData: IDeviceData): DataItem[] {
44
44
  // 18: f18,city - 城市
45
45
  arr[18] = location.cityName;
46
46
  // 19: f19,当前小程序运行的宿主环境
47
- arr[19] = helper.convert2String(host);
47
+ arr[19] = host;
48
48
  // 28: f28,sinan、mycar等
49
49
  arr[28] = client;
50
50
  // 29: f29,小程序场景值
51
51
  arr[29] = helper.getAppScene();
52
52
  // 33: f33,系统信息
53
- arr[33] = helper.getSystemInfoString();
53
+ arr[33] = helper.getSystemInfo();
54
54
  // 36: f36,小程序启动时的url和参数
55
- arr[36] = helper.getLaunchOptionsString();
55
+ arr[36] = helper.getLaunchOptions();
56
56
  // --------------------------字段列表--------------------------
57
57
  return arr;
58
58
  }
@@ -64,7 +64,7 @@ function jointData(data: IOldParams, deviceData: IDeviceData): DataItem[] {
64
64
  const arr = getBaseData(deviceData);
65
65
  const keys = Object.keys(data) as any as number[];
66
66
  for (const key of keys) {
67
- arr[key] = helper.convert2String(data[key]);
67
+ arr[key] = data[key];
68
68
  }
69
69
  return arr;
70
70
  }
@@ -8,11 +8,10 @@ import helper from './helper';
8
8
  /**
9
9
  * 获取埋点的基础字段
10
10
  */
11
- function getBaseData(deviceData: IDeviceData): { arr: DataItem[], nextIndex: number } {
12
- const { page, pageDepth } = helper.getPageInfo();
11
+ function getBaseData(page: IPage, deviceData: IDeviceData): { arr: DataItem[], nextIndex: number } {
13
12
  const { networkType, location } = deviceData;
14
13
  const { appVersion, client } = helper.getInitOptions();
15
- const arr = new Array<string>(helper.dataArrLen);
14
+ const arr = new Array<DataItem>(helper.dataArrLen);
16
15
  // todo: 如何区分新旧埋点?新:f20不为空,旧:f20为空
17
16
  // ++++++++++++++++++++++++++字段列表++++++++++++++++++++++++++
18
17
  // 0: log_time,日志入库时间
@@ -43,20 +42,22 @@ function getBaseData(deviceData: IDeviceData): { arr: DataItem[], nextIndex: num
43
42
  // 18: f18,city - 城市
44
43
  arr[18] = location.cityName;
45
44
  // 19: f19,系统信息
46
- arr[19] = helper.getSystemInfoString();
45
+ arr[19] = helper.getSystemInfo();
47
46
  // 20: f20,sinan、mycar等
48
47
  arr[20] = client;
49
48
  // 21: f21,小程序场景值
50
49
  arr[21] = helper.getAppScene();
51
50
  // 22: f22,小程序启动时的url和参数
52
- arr[22] = helper.getLaunchOptionsString();
51
+ arr[22] = helper.getLaunchOptions();
53
52
  // 23: f23,当前页面的url
54
- arr[23] = page?.route;
53
+ arr[23] = page.route;
55
54
  // 24: f24,当前页面的query
56
- arr[24] = JSON.stringify(page?.options);
55
+ arr[24] = page.options;
57
56
  // 25: f25,当前页面深度
58
- arr[25] = pageDepth.toString();
59
- // 26 ~ 30: 预留字段给后续扩展使用
57
+ arr[25] = page.depth;
58
+ // 26: f26,一次小程序生命周期中的埋点统一标记
59
+ arr[26] = helper.getLifeReportKey();
60
+ // 27 ~ 30: 预留字段给后续扩展使用
60
61
  // 31 ~ 40: 提供给开发自定义
61
62
  // --------------------------字段列表--------------------------
62
63
  return { arr, nextIndex: 31 };
@@ -65,15 +66,15 @@ function getBaseData(deviceData: IDeviceData): { arr: DataItem[], nextIndex: num
65
66
  /**
66
67
  * 拼接埋点数组
67
68
  */
68
- function jointData(data: any[], deviceData: IDeviceData): DataItem[] {
69
- const { arr, nextIndex } = getBaseData(deviceData);
69
+ function jointData(page: IPage, data: any[], deviceData: IDeviceData): DataItem[] {
70
+ const { arr, nextIndex } = getBaseData(page, deviceData);
70
71
  let index = nextIndex;
71
72
  for (const item of data) {
72
73
  if (index >= arr.length) {
73
74
  console.error('埋点参数个数超出上限而被截断', data);
74
75
  break;
75
76
  }
76
- arr[index] = helper.convert2String(item);
77
+ arr[index] = item;
77
78
  index += 1;
78
79
  }
79
80
  return arr;
@@ -83,9 +84,11 @@ function jointData(data: any[], deviceData: IDeviceData): DataItem[] {
83
84
  * 格式化上报埋点数据
84
85
  */
85
86
  function formatData(data: any[]): Promise<DataItem[]> {
87
+ // getDeviceData可能会太慢导致页面已跳转完,所以先获取page比较准确
88
+ const page = helper.getPageInfo();
86
89
  return new Promise<DataItem[]>((resolve) => {
87
90
  helper.getDeviceData().then((deviceData: IDeviceData) => {
88
- const arr = jointData(data, deviceData);
91
+ const arr = jointData(page, data, deviceData);
89
92
  resolve(arr);
90
93
  });
91
94
  });
@@ -95,7 +98,9 @@ function formatData(data: any[]): Promise<DataItem[]> {
95
98
  * 格式化快速上报埋点数据,不依赖用户位置
96
99
  */
97
100
  function formatFastData(data: any[]): DataItem[] {
98
- return jointData(data, helper.getCacheDeviceData());
101
+ const page = helper.getPageInfo();
102
+ const deviceData = helper.getCacheDeviceData();
103
+ return jointData(page, data, deviceData);
99
104
  }
100
105
 
101
106
  export default {
@@ -53,25 +53,13 @@ function getSystemInfo(): ISystemInfo {
53
53
  return systemInfo;
54
54
  }
55
55
 
56
- let systemString: string | null = null;
57
-
58
56
  /**
59
- * 获取系统信息并格式化成字符串
60
- */
61
- function getSystemInfoString(): string {
62
- if (systemString === null) {
63
- systemString = JSON.stringify(getSystemInfo());
64
- }
65
- return systemString;
66
- }
67
-
68
- /**
69
- * 把值/对象转成字符串
57
+ * 把值或对象转成字符串
70
58
  * @param value
71
59
  */
72
- function convert2String(value: any): string {
60
+ function convert2String(value: any): string | null {
73
61
  if (value === null || value === undefined) {
74
- return '';
62
+ return null;
75
63
  }
76
64
  const type = typeof value;
77
65
  if (type === 'string') {
@@ -83,6 +71,17 @@ function convert2String(value: any): string {
83
71
  return String(value);
84
72
  }
85
73
 
74
+ /**
75
+ * 转成字符串数据
76
+ * @param arr
77
+ */
78
+ function convert2StringArray(arr: DataItem[]) {
79
+ for (let i = 0; i < arr.length; i++) {
80
+ // eslint-disable-next-line no-param-reassign
81
+ arr[i] = convert2String(arr[i]);
82
+ }
83
+ }
84
+
86
85
  function num2Str(num: number, maxLength = 2): string {
87
86
  return num.toString().padStart(maxLength, '0');
88
87
  }
@@ -92,7 +91,7 @@ function num2Str(num: number, maxLength = 2): string {
92
91
  */
93
92
  function getNowString(): string {
94
93
  const date = new Date();
95
- const year = date.getFullYear().toString();
94
+ const year = date.getFullYear();
96
95
  const month = num2Str(date.getMonth() + 1);
97
96
  const day = num2Str(date.getDate());
98
97
  const hours = num2Str(date.getHours());
@@ -102,34 +101,50 @@ function getNowString(): string {
102
101
  return `${year}${month}${day}${hours}${minutes}${seconds}${ms}`;
103
102
  }
104
103
 
104
+ let lifeReportKey = '';
105
+
106
+ /**
107
+ * 一次小程序生命周期中的埋点统一标记
108
+ */
109
+ function getLifeReportKey(): string {
110
+ if (lifeReportKey === '') {
111
+ lifeReportKey = getNowString();
112
+ }
113
+ return lifeReportKey;
114
+ }
115
+
105
116
  /**
106
117
  * 获取当前页面信息
107
118
  */
108
- function getPageInfo(): { page: IPage, pageDepth: number } {
119
+ function getPageInfo(): IPage {
109
120
  const pages = getCurrentPages();
110
- let page: IPage;
111
- let pageDepth = 1; // 页面深度
112
- // 首页未渲染
121
+ let route: string;
122
+ let options: object;
123
+ let depth: number; // 页面深度
124
+ // 刚启动时首页未渲染
113
125
  if (pages.length === 0) {
114
126
  const launch = syncApi.getLaunchOptionsSync() as any;
115
- page = { route: launch.path, options: launch.query };
127
+ route = launch.path;
128
+ options = launch.qurey;
129
+ depth = 1;
116
130
  } else {
117
- pageDepth = pages.length;
118
131
  // 如果最后一个为空,则当前是在插件页,继续找上一个页面
119
- page = pages.reverse().find((t: IPage) => t);
132
+ const page = pages.reverse().find((t: any) => t);
133
+ route = page?.route;
134
+ options = page?.options;
135
+ depth = pages.length;
120
136
  }
121
- return { page, pageDepth };
137
+ return { route, options, depth };
122
138
  }
123
139
 
124
- let launchOptions: string | null = null;
140
+ let launchOptions: object | null = null;
125
141
 
126
142
  /**
127
143
  * 获取小程序启动参数
128
144
  */
129
- function getLaunchOptionsString(): string {
145
+ function getLaunchOptions(): object {
130
146
  if (launchOptions === null) {
131
- const obj = syncApi.getLaunchOptionsSync();
132
- launchOptions = JSON.stringify(obj);
147
+ launchOptions = syncApi.getLaunchOptionsSync();
133
148
  }
134
149
  return launchOptions;
135
150
  }
@@ -145,9 +160,9 @@ function getLaunchFrom(): string {
145
160
  /**
146
161
  * 获取小程序启动场景值
147
162
  */
148
- function getAppScene(): string {
163
+ function getAppScene(): number {
149
164
  const { scene = -1 } = syncApi.getLaunchOptionsSync() as any;
150
- return scene.toString();
165
+ return scene;
151
166
  }
152
167
 
153
168
  // 是否爬虫
@@ -159,7 +174,7 @@ let isCrawler: boolean | null = null;
159
174
  function canReport(): boolean {
160
175
  if (isCrawler === null) {
161
176
  const scene = getAppScene();
162
- isCrawler = scene === '1129' || scene === '1030';
177
+ isCrawler = scene === 1129 || scene === 1030;
163
178
  }
164
179
  // 小程序爬虫,不上报
165
180
  return !isCrawler;
@@ -225,11 +240,11 @@ export default {
225
240
  getTms,
226
241
  getInitOptions,
227
242
  getSystemInfo,
228
- getSystemInfoString,
229
- convert2String,
243
+ convert2StringArray,
230
244
  getNowString,
245
+ getLifeReportKey,
231
246
  getPageInfo,
232
- getLaunchOptionsString,
247
+ getLaunchOptions,
233
248
  getLaunchFrom,
234
249
  getAppScene,
235
250
  canReport,
@@ -6,7 +6,7 @@
6
6
  import helper from './helper';
7
7
 
8
8
  // 缓存队列
9
- const cacheArr = new Array<DataItem[]>();
9
+ const cacheArr = new Array<string[]>();
10
10
  const max = 50; // 超过最大限制就马上发送
11
11
  const delay = 3000; // 延迟N毫秒再聚合发送
12
12
  let timer = 0; // 计时器
@@ -73,6 +73,7 @@ function requestFail(batch: DataItem[][]): void {
73
73
  */
74
74
  function send(arr: DataItem[]): void {
75
75
  stopTimer();
76
+ helper.convert2StringArray(arr);
76
77
  cacheArr.unshift(arr); // 如果队列中很多,排前面比较稳妥
77
78
  batchSendData();
78
79
  }
@@ -82,6 +83,7 @@ function send(arr: DataItem[]): void {
82
83
  * @param arr
83
84
  */
84
85
  function queue(arr: DataItem[]): void {
86
+ helper.convert2StringArray(arr);
85
87
  cacheArr.push(arr);
86
88
  checkQueue(false);
87
89
  }
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable */
2
2
 
3
- type DataItem = string | null | undefined;
3
+ type DataItem = any;
4
4
 
5
5
  /**
6
6
  * 初始化参数对象
@@ -16,6 +16,7 @@ interface IInitOptions {
16
16
  interface IPage {
17
17
  route: string,
18
18
  options: object,
19
+ depth: number,
19
20
  }
20
21
 
21
22
  /**