@tmsfe/tms-core 0.0.27 → 0.0.31

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.27",
3
+ "version": "0.0.31",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,10 @@ const openidIndex = 7; // openId的下标,由云函数填充
22
22
  let initOptions: IInitOptions;
23
23
 
24
24
  function init(options: IInitOptions): void {
25
- initOptions = options;
25
+ if (!initOptions) {
26
+ initOptions = options;
27
+ wx.cloud.init({ env: options.cloudEnvId });
28
+ }
26
29
  }
27
30
 
28
31
  function getSystemInfoString(): string {
@@ -34,6 +37,17 @@ function getSystemInfoString(): string {
34
37
  return encodeURIComponent(JSON.stringify(info));
35
38
  }
36
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
+
37
51
  function getSeqInfo(): { timestamp: number, seqId: string } {
38
52
  const timestamp = Date.now();
39
53
  const random = Math.random().toString();
@@ -117,19 +131,10 @@ function formatData(page: IPage, seqId: string, data: any[]): DataType[] {
117
131
  let index = nextIndex;
118
132
  for (const item of data) {
119
133
  if (index >= arr.length) {
120
- console.error(data);
121
- throw new Error('埋点参数个数超出上限');
122
- }
123
- const type = typeof item;
124
- let value: string;
125
- if (type === 'string') {
126
- value = item;
127
- } else if (type === 'object') {
128
- value = JSON.stringify(item);
129
- } else {
130
- value = String(item);
134
+ console.error('埋点参数个数超出上限而被截断', data);
135
+ break;
131
136
  }
132
- arr[index] = value;
137
+ arr[index] = convert2String(item);
133
138
  index += 1;
134
139
  }
135
140
  return arr;
@@ -182,11 +187,13 @@ function doCallCloudFunc(arr: DataType[], timestamp: number, seqId: string, open
182
187
  */
183
188
  function callCloudFunc(arr: string[]): void {
184
189
  const { timestamp, seqId } = getSeqInfo();
185
- doCallCloudFunc(arr, timestamp, seqId, -1);
190
+ doCallCloudFunc(arr, timestamp, seqId, openidIndex);
186
191
  }
187
192
 
188
193
  export default {
189
194
  init,
190
195
  report,
191
196
  callCloudFunc,
197
+ convert2String,
198
+ getSystemInfoString,
192
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 Request from './request';
7
- import { getEnvInfo } from './env';
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
- const valType = typeof param[key];
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 = {}, simulatedUserId = true, simulatedUserIdIndex = 40, reportShowScene = true, appVer = true) {
49
- if (!param[27]) return Promise.reject('invalid report param');
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[33] = encodeURIComponent(JSON.stringify(getSystemInfo()));
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 (reportShowScene && !data[29]) {
39
+ if (!data[29]) {
60
40
  const appShowScene = wx.getStorageSync('appShowScene');
61
- if (appShowScene) data[29] = String(appShowScene);
41
+ if (appShowScene) {
42
+ data[29] = String(appShowScene);
43
+ }
62
44
  }
63
- if (simulatedUserId && !data[simulatedUserIdIndex]) {
64
- data[simulatedUserIdIndex] = FastReport.getSimulatedUserId();
65
- }
66
-
67
- return new Request().post('basic/event/upload', { batch: [data] })
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
  };