gio-miniprogram-sdk 4.0.1 → 4.1.0

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.
Files changed (76) hide show
  1. package/dist/alipay.js +1 -1
  2. package/dist/baidu.js +1 -1
  3. package/dist/bytedance.js +1 -1
  4. package/dist/jingdong.js +1 -1
  5. package/dist/kuaishou.js +1 -1
  6. package/dist/plugins/gioABTest.js +1 -1
  7. package/dist/plugins/gioCompress.js +1 -1
  8. package/dist/plugins/gioEventAutoTracking.js +1 -1
  9. package/dist/plugins/gioImpressionTracking.js +1 -1
  10. package/dist/plugins/gioMultipleInstances.js +1 -0
  11. package/dist/qq.js +1 -1
  12. package/dist/quickapp.js +1 -1
  13. package/dist/taobao.js +1 -1
  14. package/dist/taro.js +1 -1
  15. package/dist/uniapp.js +1 -1
  16. package/dist/wechat.js +1 -1
  17. package/package.json +39 -42
  18. package/rollup.config.js +11 -6
  19. package/rollup.plugins.config.js +13 -10
  20. package/src/{pluginsOut/frameworks → adapters}/gioTaobaoAdapter.ts +47 -43
  21. package/src/{pluginsOut/frameworks → adapters}/gioTaroAdapter.ts +4 -4
  22. package/src/{pluginsOut/frameworks → adapters}/gioUniAppAdapter.ts +35 -6
  23. package/src/constants/config.ts +47 -18
  24. package/src/constants/emitMsg.ts +23 -0
  25. package/src/constants/regex.ts +0 -1
  26. package/src/core/dataStore/eventContextBuilder.ts +28 -17
  27. package/src/core/dataStore/eventHooks/appEffects.ts +50 -31
  28. package/src/core/dataStore/eventHooks/index.ts +7 -5
  29. package/src/core/dataStore/eventHooks/minipPage.ts +2 -11
  30. package/src/core/dataStore/eventHooks/pageEffects.ts +61 -27
  31. package/src/core/dataStore/index.ts +263 -185
  32. package/src/core/growingIO.ts +312 -178
  33. package/src/core/pluginsInstance/base.ts +157 -0
  34. package/src/core/pluginsInstance/taro.ts +15 -0
  35. package/src/core/pluginsInstance/tb.ts +15 -0
  36. package/src/core/pluginsInstance/uniapp.ts +15 -0
  37. package/src/core/uploader.ts +131 -82
  38. package/src/core/userStore.ts +160 -121
  39. package/src/index.ts +37 -28
  40. package/src/minipInstance/base.ts +2 -1
  41. package/src/minipInstance/quickapp.ts +5 -19
  42. package/src/platformConfig/base.ts +0 -4
  43. package/src/{pluginsOut/ordinary → plugins}/gioABTest.ts +74 -37
  44. package/src/{pluginsOut/ordinary → plugins}/gioEventAutoTracking.ts +25 -18
  45. package/src/{pluginsOut/ordinary → plugins}/gioImpressionTracking.ts +55 -22
  46. package/src/plugins/gioMultipleInstances.ts +176 -0
  47. package/src/types/base.ts +10 -3
  48. package/src/types/dataStore.ts +31 -16
  49. package/src/types/eventHooks.ts +5 -6
  50. package/src/types/growingIO.ts +30 -14
  51. package/src/types/platforms.ts +0 -4
  52. package/src/types/plugins.ts +1 -1
  53. package/src/types/uploader.ts +16 -10
  54. package/src/types/userStore.ts +21 -17
  55. package/src/utils/glodash.ts +10 -3
  56. package/src/utils/tools.ts +27 -6
  57. package/src/utils/verifiers.ts +25 -50
  58. package/typings/global.d.ts +5 -0
  59. package/.babelrc +0 -4
  60. package/bin/build/full.sh +0 -32
  61. package/bin/build/jd.sh +0 -14
  62. package/bin/build/ks.sh +0 -14
  63. package/bin/build/my.sh +0 -14
  64. package/bin/build/plugins.sh +0 -5
  65. package/bin/build/qq.sh +0 -14
  66. package/bin/build/quickapp.sh +0 -14
  67. package/bin/build/swan.sh +0 -14
  68. package/bin/build/taro.sh +0 -18
  69. package/bin/build/tb.sh +0 -18
  70. package/bin/build/tt.sh +0 -14
  71. package/bin/build/uniapp.sh +0 -18
  72. package/bin/build/wx.sh +0 -14
  73. package/src/core/plugins.ts +0 -152
  74. package/src/pluginsOut/ordinary/gioCustomTracking.ts +0 -71
  75. package/bin/{build/all.sh → build.sh} +3 -3
  76. /package/src/{pluginsOut/ordinary → plugins}/gioCompress.ts +0 -0
@@ -0,0 +1,157 @@
1
+ // 原生环境下的plugin实例,无任何内置插件
2
+ import { GrowingIOType } from '@@/types/growingIO';
3
+ import { PluginItem, PluginsType } from '@@/types/plugins';
4
+ import {
5
+ find,
6
+ isEmpty,
7
+ isFunction,
8
+ keys,
9
+ lowerFirst,
10
+ unset
11
+ } from '@@/utils/glodash';
12
+ import { consoleText } from '@@/utils/tools';
13
+ import EMIT_MSG from '@@/constants/emitMsg';
14
+
15
+ // 会被强制剔除出内置的插件(即必须通过手动注册的方式加载的内置插件)
16
+ const EXTRA_PLUGINS = ['gioPerformance'];
17
+ // @ts-nocheck
18
+ class BasePlugins implements PluginsType {
19
+ public pluginItems: PluginItem[];
20
+ public pluginsContext: any;
21
+ constructor(public growingIO: GrowingIOType) {
22
+ this.pluginsContext = { plugins: {} };
23
+ this.pluginItems = [];
24
+ this.growingIO.emitter?.on(EMIT_MSG.ON_INSTALL, (args) =>
25
+ this.callLifeCycle('onInstall', args)
26
+ );
27
+ this.growingIO.emitter?.on(EMIT_MSG.ON_ERROR, (args) =>
28
+ this.callLifeCycle('onError', args)
29
+ );
30
+ this.growingIO.emitter?.on(EMIT_MSG.ON_COMPOSE_BEFORE, (args) =>
31
+ this.callLifeCycle('onComposeBefore', args)
32
+ );
33
+ this.growingIO.emitter?.on(EMIT_MSG.ON_COMPOSE_AFTER, (args) =>
34
+ this.callLifeCycle('onComposeAfter', args)
35
+ );
36
+ this.growingIO.emitter?.on(EMIT_MSG.ON_SEND_BEFORE, (args) =>
37
+ this.callLifeCycle('onSendBefore', args)
38
+ );
39
+ this.growingIO.emitter?.on(EMIT_MSG.ON_SEND_AFTER, (args) =>
40
+ this.callLifeCycle('onSendAfter', args)
41
+ );
42
+ }
43
+
44
+ // 初始化内置插件,不允许客户自己热插拔的插件,内置打包完成
45
+ innerPluginInit = () => {
46
+ EXTRA_PLUGINS.forEach((o) => unset(this.pluginsContext?.plugins, o));
47
+ keys(this.pluginsContext?.plugins).forEach((key) => {
48
+ const { name, method }: PluginItem =
49
+ this.pluginsContext?.plugins[key] ?? {};
50
+ // 若列表中出现同名插件,以第一个扫描出来的插件为准
51
+ const isExist = find(this.pluginItems, (o) => o.name === name);
52
+ if (!isExist) {
53
+ this.pluginItems.push({
54
+ // 优先使用指定插件名
55
+ name: lowerFirst(name || key),
56
+ method: method ? method : (growingIO) => {} // eslint-disable-line
57
+ });
58
+ }
59
+ });
60
+ if (!isEmpty(this.pluginItems)) {
61
+ this.installAll();
62
+ }
63
+ };
64
+
65
+ // 挂载插件
66
+ install = (pluginName: string, pluginItem?: any, options?: any) => {
67
+ const { emitter } = this.growingIO;
68
+ const pluginTarget: PluginItem =
69
+ pluginItem ||
70
+ find(this.pluginItems, (o: PluginItem) => o.name === pluginName);
71
+ // 插件已加载或与已加载的插件重名
72
+ if ((this.growingIO?.plugins as any)[pluginName]) {
73
+ consoleText(`重复加载插件 ${pluginName} 或插件重名,已跳过加载!`, 'warn');
74
+ return false;
75
+ }
76
+ // 插件列表中不存在要加载的插件
77
+ if (!pluginTarget) {
78
+ consoleText(`插件加载失败!不存在名为 ${pluginName} 的插件!`, 'error');
79
+ return false;
80
+ }
81
+ // 将插件实例挂载到growingIO实例上使得其他模块可以调用
82
+ try {
83
+ (this.growingIO?.plugins as any)[pluginName] = new (
84
+ pluginTarget as any
85
+ ).method(this.growingIO, options);
86
+ if (pluginItem) {
87
+ emitter.emit(EMIT_MSG.ON_INSTALL, pluginName);
88
+ consoleText(`加载插件 ${pluginName}`, 'info');
89
+ }
90
+ return true;
91
+ } catch (error) {
92
+ this.lifeError(pluginItem, error);
93
+ consoleText(`插件【${pluginName}】加载异常 ${error}`, 'error');
94
+ return false;
95
+ }
96
+ };
97
+
98
+ // 挂载所有插件
99
+ installAll = (plugins?: PluginItem[]) => {
100
+ (plugins || this.pluginItems).forEach((o: PluginItem) => {
101
+ const suc = this.install(
102
+ o.name,
103
+ plugins ? o : undefined,
104
+ plugins ? o?.options : undefined
105
+ );
106
+ if (suc && !find(this.pluginItems, (p) => p.name === o.name)) {
107
+ this.pluginItems.push({
108
+ // 优先使用指定插件名
109
+ name: lowerFirst(o.name),
110
+ // @ts-ignore
111
+ method: o.method ? o.method : () => {}
112
+ });
113
+ }
114
+ });
115
+ };
116
+
117
+ // 移除插件
118
+ uninstall = (pluginName: string) => {
119
+ unset(this.pluginItems, pluginName);
120
+ const u = unset(this.growingIO?.plugins, pluginName);
121
+ if (!u) {
122
+ consoleText(`卸载插件 ${pluginName} 失败!`, 'error');
123
+ }
124
+ return u as boolean;
125
+ };
126
+
127
+ // 移除所有插件
128
+ uninstallAll = () => {
129
+ this.pluginItems.forEach((o: PluginItem) => this.uninstall(o.name));
130
+ };
131
+
132
+ lifeError = (p: PluginItem, e: any) => {
133
+ this.growingIO.emitter.emit(EMIT_MSG.ON_ERROR, {
134
+ plugin: p,
135
+ error: e
136
+ });
137
+ consoleText(`插件执行错误 ${p.name} ${e}`, 'error');
138
+ };
139
+
140
+ // 触发插件的生命周期事件
141
+ callLifeCycle = (lifetime: string, args: any) => {
142
+ this.pluginItems.forEach((o: PluginItem) => {
143
+ const method = (this.growingIO.plugins as any)[o.name]
144
+ ? (this.growingIO.plugins as any)[o.name][lifetime]
145
+ : undefined;
146
+ if (method && isFunction(method)) {
147
+ try {
148
+ method(args);
149
+ } catch (e) {
150
+ this.lifeError(o, e);
151
+ }
152
+ }
153
+ });
154
+ };
155
+ }
156
+
157
+ export default BasePlugins;
@@ -0,0 +1,15 @@
1
+ // taro环境下的plugins实例,添加taro适配插件
2
+ import { GrowingIOType } from '@@/types/growingIO';
3
+ import BasePlugins from './base';
4
+ import gioTaroAdapter from '@@/adapters/gioTaroAdapter';
5
+
6
+ class Plugins extends BasePlugins {
7
+ constructor(public growingIO: GrowingIOType) {
8
+ super(growingIO);
9
+ this.pluginsContext = {
10
+ plugins: { gioTaroAdapter }
11
+ };
12
+ }
13
+ }
14
+
15
+ export default Plugins;
@@ -0,0 +1,15 @@
1
+ // taobao环境下的plugins实例,添加taobao适配插件
2
+ import { GrowingIOType } from '@@/types/growingIO';
3
+ import BasePlugins from './base';
4
+ import gioTaobaoAdapter from '@@/adapters/gioTaobaoAdapter';
5
+
6
+ class Plugins extends BasePlugins {
7
+ constructor(public growingIO: GrowingIOType) {
8
+ super(growingIO);
9
+ this.pluginsContext = {
10
+ plugins: { gioTaobaoAdapter }
11
+ };
12
+ }
13
+ }
14
+
15
+ export default Plugins;
@@ -0,0 +1,15 @@
1
+ // uniapp环境下的plugins实例,添加uniapp适配插件
2
+ import { GrowingIOType } from '@@/types/growingIO';
3
+ import BasePlugins from './base';
4
+ import gioUniAppAdapter from '@@/adapters/gioUniAppAdapter';
5
+
6
+ class Plugins extends BasePlugins {
7
+ constructor(public growingIO: GrowingIOType) {
8
+ super(growingIO);
9
+ this.pluginsContext = {
10
+ plugins: { gioUniAppAdapter }
11
+ };
12
+ }
13
+ }
14
+
15
+ export default Plugins;
@@ -1,20 +1,22 @@
1
1
  import {
2
- isArray,
2
+ head,
3
3
  isEmpty,
4
+ isNil,
4
5
  startsWith,
5
6
  throttle,
6
7
  unset
7
8
  } from '@@/utils/glodash';
8
- import { Event } from '@@/types/base';
9
+ import { EVENT, EXTEND_EVENT } from '@@/types/base';
9
10
  import { consoleText } from '@@/utils/tools';
10
11
  import { GrowingIOType } from '@@/types/growingIO';
11
12
  import { UploaderType } from '@@/types/uploader';
13
+ import EMIT_MSG from '@@/constants/emitMsg';
12
14
 
13
15
  class Uploader implements UploaderType {
14
16
  // 因forceLogin配置积压的事件请求队列
15
- public hoardingQueue: any[];
17
+ public hoardingQueue: any;
16
18
  // 请求队列
17
- public requestQueue: any[];
19
+ public requestQueue: any;
18
20
  // 请求限制最大数
19
21
  public requestLimit: number;
20
22
  // 请求重试限制最大数
@@ -23,22 +25,38 @@ class Uploader implements UploaderType {
23
25
  public retryIds: any;
24
26
  // 请求中的请求数
25
27
  public requestingNum: number;
26
- // 请求地址
27
- public requestURL: string;
28
28
 
29
29
  constructor(public growingIO: GrowingIOType) {
30
- this.hoardingQueue = [];
31
- this.requestQueue = [];
30
+ this.hoardingQueue = {};
31
+ this.requestQueue = {};
32
32
  this.requestLimit = 3;
33
33
  this.retryLimit = 2;
34
34
  this.retryIds = {};
35
35
  this.requestingNum = 0;
36
- this.requestURL = this.generateURL();
36
+ const { emitter, vdsConfig, plugins } = this.growingIO;
37
+ emitter.on(
38
+ EMIT_MSG.ON_SEND_BEFORE,
39
+ ({ eventsQueue, requestData, trackingId }) => {
40
+ // 核心逻辑中只发送主实例的事件
41
+ if (trackingId === this.growingIO.trackingId) {
42
+ if (vdsConfig?.tbConfig?.cloudAppId) {
43
+ // 通过淘宝云应用转发
44
+ plugins?.gioTaobaoAdapter?.tbCloudAppInvoke(
45
+ eventsQueue,
46
+ requestData
47
+ );
48
+ } else {
49
+ this.sendEvent(eventsQueue, requestData);
50
+ }
51
+ }
52
+ }
53
+ );
37
54
  }
38
55
 
39
56
  // 生成上报地址
40
- generateURL = () => {
41
- const { serverUrl, projectId } = this.growingIO.vdsConfig;
57
+ generateURL = (trackingId: string) => {
58
+ const { serverUrl, projectId } =
59
+ this.growingIO.dataStore.getTrackerVds(trackingId);
42
60
  if (!startsWith(serverUrl, 'http')) {
43
61
  return `https://${serverUrl}/v3/projects/${projectId}/collect`;
44
62
  } else {
@@ -46,70 +64,114 @@ class Uploader implements UploaderType {
46
64
  }
47
65
  };
48
66
 
67
+ // 获取积压队列
68
+ getHoardingQueue = (trackingId: string) =>
69
+ this.hoardingQueue[trackingId] ?? [];
70
+
71
+ // 获取请求队列
72
+ getRequestQueue = (trackingId: string) => this.requestQueue[trackingId] ?? [];
73
+
49
74
  // 提交请求(将请求按队列进行管理)
50
- commitRequest = (reqData: Event) => {
51
- const data: Event = { ...reqData };
52
- const { vdsConfig } = this.growingIO;
75
+ commitRequest = (commitData: EXTEND_EVENT) => {
76
+ const data: EXTEND_EVENT = { ...commitData };
77
+ const { vdsConfig, emitter } = this.growingIO;
78
+ emitter.emit(EMIT_MSG.ON_COMMIT_REQUEST, {
79
+ eventData: commitData,
80
+ trackingId: commitData.trackingId
81
+ });
53
82
  // 如果开启forceLogin 则将请求推入积压队列,反之则进入请求队列
54
83
  if (vdsConfig.forceLogin) {
55
- this.hoardingQueue.push(data);
84
+ if (isNil(this.hoardingQueue[commitData.trackingId])) {
85
+ this.hoardingQueue[commitData.trackingId] = [];
86
+ }
87
+ this.hoardingQueue[commitData.trackingId]?.push(data);
56
88
  } else {
57
- this.requestQueue.push(data);
58
- this.initiateRequest();
89
+ if (isNil(this.requestQueue[commitData.trackingId])) {
90
+ this.requestQueue[commitData.trackingId] = [];
91
+ }
92
+ this.requestQueue[commitData.trackingId]?.push(data);
93
+ this.initiateRequest(commitData.trackingId);
59
94
  }
60
95
  };
61
96
 
62
97
  // 发起请求
63
- initiateRequest = (forceSend?: boolean) => {
64
- const { plugins, vdsConfig } = this.growingIO;
98
+ initiateRequest = (trackingId: string, forceSend?: boolean) => {
99
+ const { plugins, vdsConfig, minipInstance, dataStore } = this.growingIO;
100
+ const hoardingQueue = this.getHoardingQueue(trackingId);
101
+ const requestQueue = this.getRequestQueue(trackingId);
65
102
  // 同时请求数的限制,防止挤占小程序本身的业务请求
66
- if (
67
- [...this.hoardingQueue, ...this.requestQueue].length > 0 &&
68
- this.requestingNum < this.requestLimit
69
- ) {
70
- // 淘宝云函数单发判断
103
+ const queues = [...hoardingQueue, ...requestQueue];
104
+ const requestProcess = (tid: string) => {
105
+ // 淘宝云函数直接发送要单发
71
106
  if (vdsConfig?.tbConfig?.cloudFuncSend) {
72
- plugins?.gioTaobaoAdapter?.singleInvoke();
107
+ plugins?.gioTaobaoAdapter?.singleInvoke(tid);
73
108
  } else if (forceSend) {
74
- this.batchInvoke();
109
+ // 强制即时发送
110
+ this.batchInvoke(tid);
111
+ } else {
112
+ // 有节流的批量发送(1秒钟之内产生的请求只会组团发送一次)
113
+ const { uploadInterval } = this.growingIO.dataStore.getTrackerVds(tid);
114
+ throttle(this.batchInvoke, uploadInterval, false, false)(tid);
115
+ }
116
+ // 发起visit请求时把存储里的originalSource删掉
117
+ const hasVisit = queues.some((e) => e.eventType === 'VISIT');
118
+ if (hasVisit) {
119
+ minipInstance.removeStorageSync(
120
+ dataStore.getStorageKey(tid, 'originalSource')
121
+ );
122
+ }
123
+ };
124
+ if (queues.length > 0) {
125
+ // 同时请求数在3个以下直接发起请求
126
+ if (this.requestingNum < this.requestLimit) {
127
+ requestProcess(trackingId);
75
128
  } else {
76
- this.batchInvokeThrottle();
129
+ // 同时有3个请求在发送时,设轮询延时直到请求数在3个以下
130
+ let t = setInterval(() => {
131
+ if (this.requestingNum < this.requestLimit) {
132
+ requestProcess(trackingId);
133
+ clearInterval(t);
134
+ t = undefined;
135
+ }
136
+ }, 500);
77
137
  }
78
138
  }
79
139
  };
80
140
 
81
141
  // 批量发送无延时(强制发送)
82
- batchInvoke = () => {
83
- const { vdsConfig, plugins, emitter } = this.growingIO;
84
- let queues = [...this.hoardingQueue, ...this.requestQueue];
142
+ batchInvoke = (trackingId: string) => {
143
+ const { vdsConfig, emitter } = this.growingIO;
144
+ const hoardingQueue = this.getHoardingQueue(trackingId);
145
+ const requestQueue = this.getRequestQueue(trackingId);
146
+ let queues = [...hoardingQueue, ...requestQueue];
85
147
  // 如果事件数大于50,剩下的切断放到下次请求
86
148
  if (queues.length > 50) {
87
149
  queues = queues.slice(0, 50);
88
150
  // 如果积压队列超过50,只截取积压队列,剩下的下一次发
89
- if (this.hoardingQueue.length > 50) {
90
- this.hoardingQueue = this.hoardingQueue.slice(50);
151
+ if (hoardingQueue.length > 50) {
152
+ this.hoardingQueue[trackingId] = hoardingQueue.slice(50);
91
153
  } else {
92
154
  // 如果积压队列小于50,正常的请求队列就只截取补满50后剩下的事件
93
- this.requestQueue = this.requestQueue.slice(
94
- 50 - this.hoardingQueue.length
155
+ this.requestQueue[trackingId] = requestQueue.slice(
156
+ 50 - hoardingQueue.length
95
157
  );
96
- this.hoardingQueue = [];
158
+ this.hoardingQueue[trackingId] = [];
97
159
  }
98
160
  } else {
99
- this.hoardingQueue = [];
100
- this.requestQueue = [];
161
+ this.hoardingQueue[trackingId] = [];
162
+ this.requestQueue[trackingId] = [];
101
163
  }
102
164
  // 过滤掉重试超过3次的请求(直接丢弃)
103
- let eventsQueue: Event[] = queues.filter(
165
+ let eventsQueue: EXTEND_EVENT[] = queues.filter(
104
166
  (o) => (this.retryIds[o.requestId] || 0) <= this.retryLimit
105
167
  );
106
168
  if (isEmpty(eventsQueue)) {
107
169
  return;
108
170
  }
109
171
  // 生成新的请求事件(过滤事件中不能被上报的字段)
110
- const requestData = eventsQueue.map((eData) => {
172
+ const requestData: EVENT[] = eventsQueue.map((eData) => {
111
173
  const rdata = { ...eData };
112
- unset(rdata, 'requestId');
174
+ unset(rdata, ['requestId', 'trackingId']);
113
175
  return rdata;
114
176
  });
115
177
  // 开启debug模式时,打印事件日志
@@ -117,25 +179,18 @@ class Uploader implements UploaderType {
117
179
  console.log('[GrowingIO Debug]:', JSON.stringify(requestData, null, 2));
118
180
  }
119
181
  // 广播事件
120
- emitter.emit('onSendBefore', { requestData });
121
- if (vdsConfig?.tbConfig?.cloudAppId) {
122
- plugins?.gioTaobaoAdapter?.tbCloudAppInvoke(requestData);
123
- } else {
124
- this.normalBatchInvoke(eventsQueue, requestData);
125
- }
182
+ emitter.emit(EMIT_MSG.ON_SEND_BEFORE, {
183
+ eventsQueue,
184
+ requestData,
185
+ trackingId
186
+ });
126
187
  };
127
188
 
128
- // 批量发送(1秒钟之内产生的请求只会组团发送一次)
129
- batchInvokeThrottle = throttle(
130
- this.batchInvoke,
131
- this.growingIO.vdsConfig.uploadInterval,
132
- false,
133
- false
134
- );
135
-
136
- // 基础请求逻辑
137
- normalBatchInvoke = (eventsQueue: any, requestData: any) => {
138
- const { minipInstance, vdsConfig, emitter, plugins } = this.growingIO;
189
+ // 发送事件
190
+ sendEvent = (eventsQueue: EXTEND_EVENT[], requestData: EVENT[]) => {
191
+ const trackingId = head(eventsQueue).trackingId;
192
+ const { minipInstance, dataStore, emitter, plugins } = this.growingIO;
193
+ const { compress, requestTimeout } = dataStore.getTrackerVds(trackingId);
139
194
  this.requestingNum += 1;
140
195
  // 根据设置拼装最后请求数据
141
196
  const header: any = {
@@ -148,61 +203,55 @@ class Uploader implements UploaderType {
148
203
  };
149
204
  let compressData = [...requestData];
150
205
  // 数据加密
151
- const isCompress =
152
- vdsConfig.compress && plugins?.gioCompress?.compressToUTF16;
206
+ const isCompress = !!compress && plugins?.gioCompress?.compressToUTF16;
153
207
  if (isCompress) {
154
208
  compressData = plugins.gioCompress.compressToUTF16(
155
209
  JSON.stringify(requestData)
156
210
  );
157
211
  header['X-Compress-Codec'] = '1';
158
212
  }
213
+ const sendURL = this.generateURL(trackingId);
159
214
  minipInstance.request({
160
- url: `${this.requestURL}?stm=${Date.now()}&compress=${
161
- isCompress ? '1' : '0'
162
- }`,
215
+ url: `${sendURL}?stm=${Date.now()}&compress=${isCompress ? '1' : '0'}`,
163
216
  header,
164
217
  method: 'POST',
165
218
  data: compressData,
166
- timeout: vdsConfig.requestTimeout,
219
+ timeout: requestTimeout,
167
220
  fail: (result: any) => {
168
221
  if (![200, 204].includes(result.code)) {
169
- if (isArray(eventsQueue)) {
170
- eventsQueue.forEach((o: any) => {
171
- this.requestFailFn(o);
172
- });
173
- } else {
174
- this.requestFailFn(eventsQueue);
175
- }
222
+ eventsQueue.forEach((o: any) => {
223
+ this.requestFailFn(o);
224
+ });
176
225
  consoleText(`请求失败!${JSON.stringify(result)}`, 'error');
177
226
  }
178
227
  },
179
228
  complete: (args: any) => {
180
229
  this.requestingNum -= 1;
181
- emitter.emit('onSendAfter', { result: args });
182
- this.initiateRequest();
230
+ emitter.emit(EMIT_MSG.ON_SEND_AFTER, { result: args, requestData });
231
+ this.initiateRequest(trackingId);
183
232
  }
184
233
  });
185
234
  };
186
235
 
187
236
  // 请求失败的回调
188
- requestFailFn = (eventsQueue: any) => {
237
+ requestFailFn = (event: EXTEND_EVENT) => {
189
238
  // 把重试的请求进行计数,超过重试上限的会被丢弃
190
- if (!this.retryIds[eventsQueue.requestId]) {
191
- this.retryIds[eventsQueue.requestId] = 0;
239
+ if (!this.retryIds[event.requestId]) {
240
+ this.retryIds[event.requestId] = 0;
192
241
  }
193
- this.retryIds[eventsQueue.requestId] += 1;
242
+ this.retryIds[event.requestId] += 1;
194
243
  // 发送失败的事件会重新推入请求队列
195
- const eventExist = this.requestQueue.some(
196
- (o) => o.requestId === eventsQueue.requestId
244
+ const eventExist = this.requestQueue[event.trackingId].some(
245
+ (o) => o.requestId === event.requestId
197
246
  );
198
247
  if (!eventExist) {
199
248
  // 延迟半秒后再推入请求队列,给网络一点恢复时间
200
249
  let t = setTimeout(() => {
201
- if (!isEmpty(this.requestQueue)) {
202
- this.requestQueue.push(eventsQueue);
250
+ if (!isEmpty(this.requestQueue[event.trackingId])) {
251
+ this.requestQueue[event.trackingId].push(event);
203
252
  } else {
204
- this.requestQueue.push(eventsQueue);
205
- this.initiateRequest();
253
+ this.requestQueue[event.trackingId].push(event);
254
+ this.initiateRequest(event.trackingId);
206
255
  }
207
256
  clearTimeout(t);
208
257
  t = null;