gio-miniprogram-sdk 4.0.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.
- package/.babelrc +4 -0
- package/.eslintignore +11 -0
- package/.eslintrc.js +120 -0
- package/.github/workflows/greetings.yml +13 -0
- package/.github/workflows/publish.yml +43 -0
- package/.prettierignore +19 -0
- package/.prettierrc.js +9 -0
- package/LICENSE +674 -0
- package/README.md +103 -0
- package/babel.config.js +6 -0
- package/bin/build/all.sh +38 -0
- package/bin/build/full.sh +32 -0
- package/bin/build/jd.sh +14 -0
- package/bin/build/ks.sh +14 -0
- package/bin/build/my.sh +14 -0
- package/bin/build/plugins.sh +5 -0
- package/bin/build/qq.sh +14 -0
- package/bin/build/quickapp.sh +14 -0
- package/bin/build/swan.sh +14 -0
- package/bin/build/taro.sh +18 -0
- package/bin/build/tb.sh +18 -0
- package/bin/build/tt.sh +14 -0
- package/bin/build/uniapp.sh +18 -0
- package/bin/build/wx.sh +14 -0
- package/dist/alipay.js +1 -0
- package/dist/baidu.js +1 -0
- package/dist/bytedance.js +1 -0
- package/dist/jingdong.js +1 -0
- package/dist/kuaishou.js +1 -0
- package/dist/plugins/gioABTest.js +1 -0
- package/dist/plugins/gioCompress.js +1 -0
- package/dist/plugins/gioEventAutoTracking.js +1 -0
- package/dist/plugins/gioImpressionTracking.js +1 -0
- package/dist/qq.js +1 -0
- package/dist/quickapp.js +1 -0
- package/dist/taobao.js +1 -0
- package/dist/taro.js +1 -0
- package/dist/uniapp.js +1 -0
- package/dist/wechat.js +1 -0
- package/package.json +144 -0
- package/rollup.config.js +92 -0
- package/rollup.plugins.config.js +76 -0
- package/src/constants/config.ts +136 -0
- package/src/constants/regex.ts +4 -0
- package/src/core/dataStore/eventContextBuilder.ts +70 -0
- package/src/core/dataStore/eventHooks/appEffects.ts +220 -0
- package/src/core/dataStore/eventHooks/index.ts +403 -0
- package/src/core/dataStore/eventHooks/minipPage.ts +132 -0
- package/src/core/dataStore/eventHooks/pageEffects.ts +168 -0
- package/src/core/dataStore/index.ts +543 -0
- package/src/core/growingIO.ts +433 -0
- package/src/core/plugins.ts +152 -0
- package/src/core/uploader.ts +219 -0
- package/src/core/userStore.ts +203 -0
- package/src/index.ts +68 -0
- package/src/minipInstance/base.ts +287 -0
- package/src/minipInstance/framework.ts +22 -0
- package/src/minipInstance/jd.ts +11 -0
- package/src/minipInstance/ks.ts +12 -0
- package/src/minipInstance/my.ts +53 -0
- package/src/minipInstance/qq.ts +12 -0
- package/src/minipInstance/quickapp.ts +230 -0
- package/src/minipInstance/swan.ts +20 -0
- package/src/minipInstance/tb.ts +53 -0
- package/src/minipInstance/tt.ts +12 -0
- package/src/minipInstance/wx.ts +12 -0
- package/src/platformConfig/base.ts +86 -0
- package/src/platformConfig/framework.ts +29 -0
- package/src/platformConfig/jd.ts +11 -0
- package/src/platformConfig/ks.ts +11 -0
- package/src/platformConfig/my.ts +15 -0
- package/src/platformConfig/qq.ts +13 -0
- package/src/platformConfig/quickapp.ts +48 -0
- package/src/platformConfig/swan.ts +11 -0
- package/src/platformConfig/tb.ts +29 -0
- package/src/platformConfig/tt.ts +11 -0
- package/src/platformConfig/wx.ts +20 -0
- package/src/pluginsOut/README.md +50 -0
- package/src/pluginsOut/frameworks/gioTaobaoAdapter.ts +195 -0
- package/src/pluginsOut/frameworks/gioTaroAdapter.ts +358 -0
- package/src/pluginsOut/frameworks/gioUniAppAdapter.ts +220 -0
- package/src/pluginsOut/ordinary/gioABTest.ts +254 -0
- package/src/pluginsOut/ordinary/gioCompress.ts +251 -0
- package/src/pluginsOut/ordinary/gioCustomTracking.ts +71 -0
- package/src/pluginsOut/ordinary/gioEventAutoTracking.ts +164 -0
- package/src/pluginsOut/ordinary/gioImpressionTracking.ts +298 -0
- package/src/types/base.ts +150 -0
- package/src/types/dataStore.ts +43 -0
- package/src/types/eventHooks.ts +141 -0
- package/src/types/gioCustomTracking.ts +9 -0
- package/src/types/gioEventTracking.ts +10 -0
- package/src/types/gioPerformance.ts +8 -0
- package/src/types/growingIO.ts +102 -0
- package/src/types/minipInstance.ts +126 -0
- package/src/types/platforms.ts +65 -0
- package/src/types/plugins.ts +24 -0
- package/src/types/uploader.ts +26 -0
- package/src/types/userStore.ts +22 -0
- package/src/utils/glodash.ts +423 -0
- package/src/utils/tools.ts +384 -0
- package/src/utils/verifiers.ts +76 -0
- package/tsconfig.json +32 -0
- package/typings/global.d.ts +92 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {
|
|
2
|
+
endsWith,
|
|
3
|
+
isArray,
|
|
4
|
+
isEmpty,
|
|
5
|
+
startsWith,
|
|
6
|
+
throttle,
|
|
7
|
+
toString,
|
|
8
|
+
unset
|
|
9
|
+
} from '@@/utils/glodash';
|
|
10
|
+
import { Event } from '@@/types/base';
|
|
11
|
+
import { consoleText } from '@@/utils/tools';
|
|
12
|
+
import { GrowingIOType } from '@@/types/growingIO';
|
|
13
|
+
import { UploaderType } from '@@/types/uploader';
|
|
14
|
+
|
|
15
|
+
class Uploader implements UploaderType {
|
|
16
|
+
// 因forceLogin配置积压的事件请求队列
|
|
17
|
+
public hoardingQueue: any[];
|
|
18
|
+
// 请求队列
|
|
19
|
+
public requestQueue: any[];
|
|
20
|
+
// 请求限制最大数
|
|
21
|
+
public requestLimit: number;
|
|
22
|
+
// 请求超时时间(ms)
|
|
23
|
+
public requestTimeout: number;
|
|
24
|
+
// 请求重试限制最大数
|
|
25
|
+
public retryLimit: number;
|
|
26
|
+
// 请求重试的原请求id
|
|
27
|
+
public retryIds: any;
|
|
28
|
+
// 请求中的请求数
|
|
29
|
+
public requestingNum: number;
|
|
30
|
+
// 请求地址
|
|
31
|
+
public requestURL: string;
|
|
32
|
+
|
|
33
|
+
constructor(public growingIO: GrowingIOType) {
|
|
34
|
+
this.hoardingQueue = [];
|
|
35
|
+
this.requestQueue = [];
|
|
36
|
+
this.requestLimit = 3;
|
|
37
|
+
this.requestTimeout = 5000;
|
|
38
|
+
this.retryLimit = 2;
|
|
39
|
+
this.retryIds = {};
|
|
40
|
+
this.requestingNum = 0;
|
|
41
|
+
this.requestURL = this.generateURL();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 生成上报地址
|
|
45
|
+
generateURL = () => {
|
|
46
|
+
const { serverUrl, projectId } = this.growingIO.vdsConfig;
|
|
47
|
+
if (!startsWith(serverUrl, 'http')) {
|
|
48
|
+
return `https://${serverUrl}/v3/projects/${projectId}/collect`;
|
|
49
|
+
} else {
|
|
50
|
+
return `${serverUrl}/v3/projects/${projectId}/collect`;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// 提交请求(将请求按队列进行管理)
|
|
55
|
+
commitRequest = (reqData: Event) => {
|
|
56
|
+
const data: Event = { ...reqData };
|
|
57
|
+
const { vdsConfig } = this.growingIO;
|
|
58
|
+
// 如果开启forceLogin 则将请求推入积压队列,反之则进入请求队列
|
|
59
|
+
if (vdsConfig.forceLogin) {
|
|
60
|
+
this.hoardingQueue.push(data);
|
|
61
|
+
} else {
|
|
62
|
+
this.requestQueue.push(data);
|
|
63
|
+
this.initiateRequest();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// 发起请求
|
|
68
|
+
initiateRequest = (forceSend?: boolean) => {
|
|
69
|
+
const { plugins, vdsConfig } = this.growingIO;
|
|
70
|
+
// 同时请求数的限制,防止挤占小程序本身的业务请求
|
|
71
|
+
if (
|
|
72
|
+
[...this.hoardingQueue, ...this.requestQueue].length > 0 &&
|
|
73
|
+
this.requestingNum < this.requestLimit
|
|
74
|
+
) {
|
|
75
|
+
// 淘宝云函数单发判断
|
|
76
|
+
if (vdsConfig?.tbConfig?.cloudFuncSend) {
|
|
77
|
+
plugins?.gioTaobaoAdapter?.singleInvoke();
|
|
78
|
+
} else if (forceSend) {
|
|
79
|
+
this.batchInvoke();
|
|
80
|
+
} else {
|
|
81
|
+
this.batchInvokeThrottle();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// 批量发送无延时(强制发送)
|
|
87
|
+
batchInvoke = () => {
|
|
88
|
+
const { vdsConfig, plugins, emitter } = this.growingIO;
|
|
89
|
+
let queues = [...this.hoardingQueue, ...this.requestQueue];
|
|
90
|
+
// 如果事件数大于50,剩下的切断放到下次请求
|
|
91
|
+
if (queues.length > 50) {
|
|
92
|
+
queues = queues.slice(0, 50);
|
|
93
|
+
// 如果积压队列超过50,只截取积压队列,剩下的下一次发
|
|
94
|
+
if (this.hoardingQueue.length > 50) {
|
|
95
|
+
this.hoardingQueue = this.hoardingQueue.slice(50);
|
|
96
|
+
} else {
|
|
97
|
+
// 如果积压队列小于50,正常的请求队列就只截取补满50后剩下的事件
|
|
98
|
+
this.hoardingQueue = [];
|
|
99
|
+
this.requestQueue = this.requestQueue.slice(
|
|
100
|
+
50 - this.hoardingQueue.length
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
this.hoardingQueue = [];
|
|
105
|
+
this.requestQueue = [];
|
|
106
|
+
}
|
|
107
|
+
// 过滤掉重试超过3次的请求(直接丢弃)
|
|
108
|
+
let eventsQueue: Event[] = queues.filter(
|
|
109
|
+
(o) => (this.retryIds[o.requestId] || 0) <= this.retryLimit
|
|
110
|
+
);
|
|
111
|
+
if (isEmpty(eventsQueue)) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
// 生成新的请求事件(过滤事件中不能被上报的字段)
|
|
115
|
+
const requestData = eventsQueue.map((eData) => {
|
|
116
|
+
const rdata = { ...eData };
|
|
117
|
+
unset(rdata, 'requestId');
|
|
118
|
+
return rdata;
|
|
119
|
+
});
|
|
120
|
+
// 开启debug模式时,打印事件日志
|
|
121
|
+
if (vdsConfig.debug) {
|
|
122
|
+
console.log('[GrowingIO Debug]:', JSON.stringify(requestData, null, 2));
|
|
123
|
+
}
|
|
124
|
+
// 广播事件
|
|
125
|
+
emitter.emit('onSendBefore', { requestData });
|
|
126
|
+
if (vdsConfig?.tbConfig?.cloudAppId) {
|
|
127
|
+
plugins?.gioTaobaoAdapter?.tbCloudAppInvoke(requestData);
|
|
128
|
+
} else {
|
|
129
|
+
this.normalBatchInvoke(eventsQueue, requestData);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// 批量发送(1秒钟之内产生的请求只会组团发送一次)
|
|
134
|
+
batchInvokeThrottle = throttle(
|
|
135
|
+
this.batchInvoke,
|
|
136
|
+
this.growingIO.vdsConfig.uploadInterval,
|
|
137
|
+
false,
|
|
138
|
+
false
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// 基础请求逻辑
|
|
142
|
+
normalBatchInvoke = (eventsQueue: any, requestData: any) => {
|
|
143
|
+
const { minipInstance, vdsConfig, emitter, plugins } = this.growingIO;
|
|
144
|
+
this.requestingNum += 1;
|
|
145
|
+
// 根据设置拼装最后请求数据
|
|
146
|
+
const header: any = {
|
|
147
|
+
// !!!京东小程序在7月4号的发版中,ios全匹配了application/json;导致我们的请求没法发出去,这里做一下兼容,等他们修复上线以后改回已注释的部分
|
|
148
|
+
// 'content-type': 'application/json;charset=UTF-8'
|
|
149
|
+
'content-type':
|
|
150
|
+
minipInstance.platform === 'jdp'
|
|
151
|
+
? 'application/json'
|
|
152
|
+
: 'application/json;charset=UTF-8'
|
|
153
|
+
};
|
|
154
|
+
let compressData = [...requestData];
|
|
155
|
+
// 数据加密
|
|
156
|
+
const isCompress =
|
|
157
|
+
vdsConfig.compress && plugins?.gioCompress?.compressToUTF16;
|
|
158
|
+
if (isCompress) {
|
|
159
|
+
compressData = plugins.gioCompress.compressToUTF16(
|
|
160
|
+
JSON.stringify(requestData)
|
|
161
|
+
);
|
|
162
|
+
header['X-Compress-Codec'] = '1';
|
|
163
|
+
}
|
|
164
|
+
minipInstance.request({
|
|
165
|
+
url: `${this.requestURL}?stm=${Date.now()}&compress=${
|
|
166
|
+
isCompress ? '1' : '0'
|
|
167
|
+
}`,
|
|
168
|
+
header,
|
|
169
|
+
method: 'POST',
|
|
170
|
+
data: compressData,
|
|
171
|
+
timeout: this.requestTimeout,
|
|
172
|
+
fail: (result: any) => {
|
|
173
|
+
if (![200, 204].includes(result.code)) {
|
|
174
|
+
if (isArray(eventsQueue)) {
|
|
175
|
+
eventsQueue.forEach((o: any) => {
|
|
176
|
+
this.requestFailFn(o);
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
this.requestFailFn(eventsQueue);
|
|
180
|
+
}
|
|
181
|
+
consoleText(`请求失败!${JSON.stringify(result)}`, 'error');
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
complete: (args: any) => {
|
|
185
|
+
this.requestingNum -= 1;
|
|
186
|
+
emitter.emit('onSendAfter', { result: args });
|
|
187
|
+
this.initiateRequest();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// 请求失败的回调
|
|
193
|
+
requestFailFn = (eventsQueue: any) => {
|
|
194
|
+
// 把重试的请求进行计数,超过重试上限的会被丢弃
|
|
195
|
+
if (!this.retryIds[eventsQueue.requestId]) {
|
|
196
|
+
this.retryIds[eventsQueue.requestId] = 0;
|
|
197
|
+
}
|
|
198
|
+
this.retryIds[eventsQueue.requestId] += 1;
|
|
199
|
+
// 发送失败的事件会重新推入请求队列
|
|
200
|
+
const eventExist = this.requestQueue.some(
|
|
201
|
+
(o) => o.requestId === eventsQueue.requestId
|
|
202
|
+
);
|
|
203
|
+
if (!eventExist) {
|
|
204
|
+
// 延迟半秒后再推入请求队列,给网络一点恢复时间
|
|
205
|
+
let t = setTimeout(() => {
|
|
206
|
+
if (!isEmpty(this.requestQueue)) {
|
|
207
|
+
this.requestQueue.push(eventsQueue);
|
|
208
|
+
} else {
|
|
209
|
+
this.requestQueue.push(eventsQueue);
|
|
210
|
+
this.initiateRequest();
|
|
211
|
+
}
|
|
212
|
+
clearTimeout(t);
|
|
213
|
+
t = null;
|
|
214
|
+
}, 800);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export default Uploader;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { GrowingIOType } from '@@/types/growingIO';
|
|
2
|
+
import { UserStoreType } from '@@/types/userStore';
|
|
3
|
+
import { batchGetStorageSync, batchSetStorageSync, guid } from '@@/utils/tools';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 代码中与3.0测量协议字段对应关系
|
|
7
|
+
* 代码定义 | 3.0测量协议定义 | 字段解释
|
|
8
|
+
* ---------------------------------------------------
|
|
9
|
+
* sessionId | sessionId | 访问会话id
|
|
10
|
+
* uid | deviceId | 访问用户id(持久化)
|
|
11
|
+
* userId | userId | 登录用户id(持久化)
|
|
12
|
+
* gioId | gioId | 上一个非空的登录用户id(持久化)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
class UserStore implements UserStoreType {
|
|
16
|
+
// 访问用户id在存储中的名称
|
|
17
|
+
public uidStorageName: string;
|
|
18
|
+
// 登录用户id在存储中的名称
|
|
19
|
+
public userIdStorageName: string;
|
|
20
|
+
// 登录用户key在存储中的名称
|
|
21
|
+
public userKeyStorageName: string;
|
|
22
|
+
// 最后一次登录用户id在存储中的名称
|
|
23
|
+
public gioIdStorageName: string;
|
|
24
|
+
// 用户访问会话id
|
|
25
|
+
public _sessionId: string;
|
|
26
|
+
// 访问用户id(系统设置持久性存储)
|
|
27
|
+
public _uid: string;
|
|
28
|
+
// 登录用户id(客户设置持久性存储)
|
|
29
|
+
public _userId: string;
|
|
30
|
+
// 登录用户key(客户设置持久性存储)
|
|
31
|
+
public _userKey: string;
|
|
32
|
+
// 最后一次登录用户id(持久性存储)
|
|
33
|
+
public _gioId: string;
|
|
34
|
+
constructor(public growingIO: GrowingIOType) {
|
|
35
|
+
const { inPlugin } = this.growingIO;
|
|
36
|
+
this.uidStorageName = inPlugin ? '_growing_plugin_uid_' : '_growing_uid_';
|
|
37
|
+
this.userIdStorageName = inPlugin
|
|
38
|
+
? '_growing_plugin_userId_'
|
|
39
|
+
: '_growing_userId_';
|
|
40
|
+
this.userKeyStorageName = inPlugin
|
|
41
|
+
? '_growing_plugin_userKey_'
|
|
42
|
+
: '_growing_userKey_';
|
|
43
|
+
this.gioIdStorageName = inPlugin
|
|
44
|
+
? '_growing_plugin_gioId_'
|
|
45
|
+
: '_growing_gioId_';
|
|
46
|
+
this._sessionId = guid();
|
|
47
|
+
this._uid = undefined;
|
|
48
|
+
this._userId = undefined;
|
|
49
|
+
this._userKey = undefined;
|
|
50
|
+
this._gioId = undefined;
|
|
51
|
+
if (this.growingIO?.gioPlatform !== 'quickapp') {
|
|
52
|
+
this.initUserInfo();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
get sessionId() {
|
|
58
|
+
if (!this._sessionId) {
|
|
59
|
+
this._sessionId = guid();
|
|
60
|
+
}
|
|
61
|
+
return this._sessionId;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
set sessionId(id: string) {
|
|
66
|
+
const prevId = this._sessionId;
|
|
67
|
+
if (!id) {
|
|
68
|
+
this._sessionId = guid();
|
|
69
|
+
} else {
|
|
70
|
+
this._sessionId = id;
|
|
71
|
+
}
|
|
72
|
+
// 广播通知登录sessionId变更
|
|
73
|
+
if (prevId !== this._sessionId) {
|
|
74
|
+
this.growingIO.emitter.emit('SESSIONID_UPDATE', {
|
|
75
|
+
newSessionId: this._sessionId,
|
|
76
|
+
oldSessionId: prevId
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
get uid() {
|
|
83
|
+
return this._uid;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// @ts-ignore
|
|
87
|
+
set uid(id: string) {
|
|
88
|
+
const prevId = this._uid;
|
|
89
|
+
this._uid = id;
|
|
90
|
+
if (prevId !== this._uid) {
|
|
91
|
+
this.growingIO.minipInstance.setStorage(this.uidStorageName, this._uid);
|
|
92
|
+
// 广播通知访问用户Id变更
|
|
93
|
+
this.growingIO.emitter.emit('UID_UPDATE', {
|
|
94
|
+
newUId: id,
|
|
95
|
+
oldUId: prevId
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
get userId() {
|
|
102
|
+
return this._userId;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// @ts-ignore
|
|
106
|
+
set userId(id: string) {
|
|
107
|
+
const prevId = this._userId;
|
|
108
|
+
this._userId = id;
|
|
109
|
+
if (prevId !== this._userId) {
|
|
110
|
+
this.growingIO.minipInstance.setStorage(
|
|
111
|
+
this.userIdStorageName,
|
|
112
|
+
this._userId
|
|
113
|
+
);
|
|
114
|
+
// 广播通知登录用户Id变更
|
|
115
|
+
this.growingIO.emitter.emit('USERID_UPDATE', {
|
|
116
|
+
newUserId: id,
|
|
117
|
+
oldUserId: prevId
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
if (id) this.gioId = id;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
get userKey() {
|
|
125
|
+
return this._userKey;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
set userKey(key: string) {
|
|
130
|
+
const prevKey = this._userKey;
|
|
131
|
+
this._userKey = key;
|
|
132
|
+
if (prevKey !== this._userKey) {
|
|
133
|
+
this.growingIO.minipInstance.setStorage(
|
|
134
|
+
this.userKeyStorageName,
|
|
135
|
+
this._userKey
|
|
136
|
+
);
|
|
137
|
+
// 广播通知登录用户key变更
|
|
138
|
+
this.growingIO.emitter.emit('USERKEY_UPDATE', {
|
|
139
|
+
newUserKey: key,
|
|
140
|
+
oldUserKey: prevKey
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
get gioId() {
|
|
147
|
+
return this._gioId;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
set gioId(gioId: string) {
|
|
152
|
+
const prevId = this._gioId;
|
|
153
|
+
this._gioId = gioId;
|
|
154
|
+
if (prevId !== this._gioId) {
|
|
155
|
+
this.growingIO.minipInstance.setStorage(
|
|
156
|
+
this.gioIdStorageName,
|
|
157
|
+
this._gioId
|
|
158
|
+
);
|
|
159
|
+
// 广播通知登录用户Id变更
|
|
160
|
+
this.growingIO.emitter.emit('GIOID_UPDATE', {
|
|
161
|
+
newGioId: gioId,
|
|
162
|
+
oldGioId: prevId
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 初始化用户信息
|
|
168
|
+
initUserInfo = () => {
|
|
169
|
+
const { minipInstance } = this.growingIO;
|
|
170
|
+
const storeKeys = [
|
|
171
|
+
this.uidStorageName,
|
|
172
|
+
this.userIdStorageName,
|
|
173
|
+
this.userKeyStorageName,
|
|
174
|
+
this.gioIdStorageName
|
|
175
|
+
];
|
|
176
|
+
const memoryKeys = ['_uid', '_userId', '_userKey', '_gioId'];
|
|
177
|
+
// 批量获取的存储数据
|
|
178
|
+
const values = batchGetStorageSync(minipInstance, storeKeys) ?? [];
|
|
179
|
+
// 往存储中重设的key
|
|
180
|
+
memoryKeys.forEach((key: string, index: number) => {
|
|
181
|
+
// 往内存中赋值
|
|
182
|
+
this[key] = values[index];
|
|
183
|
+
// 如果uid没有值,则自动生成并赋值(这种情况是用户第一次进或者清了缓存)
|
|
184
|
+
if (key === '_uid' && !this[key]) {
|
|
185
|
+
// this.uid会自动给内存和存储都赋值
|
|
186
|
+
this.uid = guid();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// 退出小程序时在存储中同步用户信息
|
|
192
|
+
saveUserInfo = () => {
|
|
193
|
+
const { minipInstance } = this.growingIO;
|
|
194
|
+
batchSetStorageSync(minipInstance, [
|
|
195
|
+
{ key: this.uidStorageName, value: this.uid },
|
|
196
|
+
{ key: this.userIdStorageName, value: this.userId },
|
|
197
|
+
{ key: this.userKeyStorageName, value: this.userKey },
|
|
198
|
+
{ key: this.gioIdStorageName, value: this.gioId }
|
|
199
|
+
]);
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export default UserStore;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
initPreCheck,
|
|
3
|
+
paramExtraCheck,
|
|
4
|
+
paramsBaseCheck,
|
|
5
|
+
paramsEmptyCheck
|
|
6
|
+
} from '@@/utils/verifiers';
|
|
7
|
+
import { consoleText, getGlobal, niceTry } from '@@/utils/tools';
|
|
8
|
+
import { drop, isString, toString } from '@@/utils/glodash';
|
|
9
|
+
import { HANDLERS, DEPRECATED_HANDLERS } from '@@/constants/config';
|
|
10
|
+
import GrowingIO from './core/growingIO';
|
|
11
|
+
|
|
12
|
+
const gioInstance: any = new GrowingIO();
|
|
13
|
+
|
|
14
|
+
const gdp = function () {
|
|
15
|
+
const actionName = arguments[0];
|
|
16
|
+
// 判断指令为字符串且配置项中允许调用且实例中存在的方法
|
|
17
|
+
if (
|
|
18
|
+
isString(actionName) &&
|
|
19
|
+
HANDLERS.includes(actionName) &&
|
|
20
|
+
gioInstance[actionName]
|
|
21
|
+
) {
|
|
22
|
+
const args = drop(Array.from(arguments));
|
|
23
|
+
// 初始化方法单独处理
|
|
24
|
+
if (actionName === 'init') {
|
|
25
|
+
const preCheckRes = initPreCheck(gioInstance);
|
|
26
|
+
const emptyCheckRes = paramsEmptyCheck(args);
|
|
27
|
+
const baseCheckRes = paramsBaseCheck(args);
|
|
28
|
+
const extraCheckRes = paramExtraCheck(args);
|
|
29
|
+
if (preCheckRes && emptyCheckRes && baseCheckRes && extraCheckRes) {
|
|
30
|
+
const { projectId } = baseCheckRes;
|
|
31
|
+
const { dataSourceId, appId, options } = extraCheckRes;
|
|
32
|
+
return gioInstance.init({
|
|
33
|
+
...options,
|
|
34
|
+
projectId,
|
|
35
|
+
dataSourceId,
|
|
36
|
+
appId
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
} else if (actionName === 'registerPlugins') {
|
|
40
|
+
gioInstance.registerPlugins(args[0]);
|
|
41
|
+
} else if (gioInstance.gioSDKInitialized && gioInstance.vdsConfig) {
|
|
42
|
+
return gioInstance[actionName](...args);
|
|
43
|
+
} else {
|
|
44
|
+
consoleText('SDK未初始化!', 'error');
|
|
45
|
+
}
|
|
46
|
+
} else if (DEPRECATED_HANDLERS.includes(actionName)) {
|
|
47
|
+
consoleText(`方法 ${toString(actionName)} 已被弃用!`, 'warn');
|
|
48
|
+
} else {
|
|
49
|
+
consoleText(`不存在名为 ${toString(actionName)} 的方法调用!`, 'error');
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
getGlobal().gdp = gdp;
|
|
54
|
+
getGlobal().gioSDKVersion = gioInstance.sdkVersion;
|
|
55
|
+
|
|
56
|
+
export const GioApp = niceTry(
|
|
57
|
+
() => gioInstance.dataStore.eventHooks.growingApp ?? App
|
|
58
|
+
);
|
|
59
|
+
export const GioPage = niceTry(
|
|
60
|
+
() => gioInstance.dataStore.eventHooks.growingPage ?? Page
|
|
61
|
+
);
|
|
62
|
+
export const GioComponent = niceTry(
|
|
63
|
+
() => gioInstance.dataStore.eventHooks.growingComponent ?? Component
|
|
64
|
+
);
|
|
65
|
+
export const GioBehavior = niceTry(
|
|
66
|
+
() => gioInstance.dataStore.eventHooks.growingBehavior ?? Behavior
|
|
67
|
+
);
|
|
68
|
+
export default gdp;
|