aegis-mp-rum-wrapper 0.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.
@@ -0,0 +1,54 @@
1
+ export declare const GPPRumID = "zYpo1fz400Vk86JR5Z";
2
+ export interface AegisUserInfo {
3
+ openid?: string;
4
+ roleID?: string;
5
+ area?: number | string;
6
+ zone?: number | string;
7
+ channelID?: number | string;
8
+ plat?: number | string;
9
+ serviceType?: string;
10
+ accountType?: string;
11
+ channelUID?: string;
12
+ columnUID?: string;
13
+ platName?: string;
14
+ eventID?: string;
15
+ contentID?: string;
16
+ country?: string;
17
+ language?: string;
18
+ locale?: string;
19
+ in_game?: number | string;
20
+ env?: string;
21
+ ext1?: string;
22
+ ext2?: string;
23
+ ext3?: string;
24
+ }
25
+ export interface AegisMpWrapperConfig {
26
+ id?: string;
27
+ uin?: string;
28
+ reportApiSpeed?: boolean;
29
+ reportAssetSpeed?: boolean;
30
+ pagePerformance?: boolean;
31
+ webVitals?: boolean;
32
+ delay?: number;
33
+ }
34
+ export interface ReportParams {
35
+ name: string;
36
+ ext1?: string;
37
+ ext2?: string;
38
+ ext3?: string;
39
+ [key: string]: any;
40
+ }
41
+ export declare function configureWrapper(config: AegisMpWrapperConfig): void;
42
+ export declare function setUser(user: Partial<AegisUserInfo>): void;
43
+ export declare function setRumID(rumID: string): void;
44
+ export declare function reportEvent(params: string | ReportParams): void;
45
+ export declare function getInstances(): {
46
+ defaultAegis: any;
47
+ serviceAegis: any;
48
+ };
49
+ export interface AegisWrapperFacade {
50
+ user: AegisUserInfo;
51
+ rumID: string;
52
+ reportEvent: (params: string | ReportParams) => void;
53
+ }
54
+ export declare const AegisW: AegisWrapperFacade;
package/dist/index.js ADDED
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.AegisW = exports.GPPRumID = void 0;
18
+ exports.configureWrapper = configureWrapper;
19
+ exports.setUser = setUser;
20
+ exports.setRumID = setRumID;
21
+ exports.reportEvent = reportEvent;
22
+ exports.getInstances = getInstances;
23
+ var aegis_mp_sdk_1 = __importDefault(require("aegis-mp-sdk"));
24
+ // rum id
25
+ exports.GPPRumID = 'zYpo1fz400Vk86JR5Z'; // cn services
26
+ var logItemTemplate = {
27
+ report_type: 11,
28
+ event_id: '',
29
+ service_type: '',
30
+ openid: '',
31
+ role_id: '',
32
+ area_id: 0,
33
+ zone_id: 0,
34
+ plat_id: 0,
35
+ channel_id: 0,
36
+ mod_name: 'h5_frontend',
37
+ country: '',
38
+ language: '',
39
+ seq: '',
40
+ client_ip: '',
41
+ env: 'prd',
42
+ log_time: '',
43
+ log_ts: 0,
44
+ extend1: '',
45
+ extend2: '',
46
+ extend3: '',
47
+ extend4: '',
48
+ in_game: 2,
49
+ open_panel: '2',
50
+ player_attr: 1,
51
+ inviter_open_id: '',
52
+ inviter_role_id: '',
53
+ inviter_zone_area_id: 0,
54
+ inviter_zone_id: 0,
55
+ inviter_plat_id: 0,
56
+ invitation_token: '',
57
+ price: 0,
58
+ coin_type: '',
59
+ login_phase: 0,
60
+ share_channel: 0,
61
+ task_id: '',
62
+ account_type: '',
63
+ column_uid: '',
64
+ channel_uid: '',
65
+ plat: '',
66
+ content_id: '',
67
+ source: 'fe_rum',
68
+ };
69
+ // 內部狀態
70
+ var defaultAegis;
71
+ var serviceAegis;
72
+ var internalState = {
73
+ id: exports.GPPRumID,
74
+ uin: '',
75
+ reportApiSpeed: false,
76
+ reportAssetSpeed: false,
77
+ pagePerformance: false,
78
+ webVitals: false,
79
+ delay: 100,
80
+ user: {},
81
+ rumID: '',
82
+ ready: false,
83
+ };
84
+ function buildReportStr(params, isPV) {
85
+ var _a, _b, _c, _d, _e;
86
+ var logItem = __assign({}, logItemTemplate);
87
+ if (isPV) {
88
+ logItem.report_type = 1;
89
+ }
90
+ var user = internalState.user;
91
+ logItem.openid = ((_a = user.openid) !== null && _a !== void 0 ? _a : '') + '';
92
+ logItem.role_id = user.roleID ? user.roleID + '' : '';
93
+ logItem.area_id = user.area ? parseInt(String(user.area), 10) : 0;
94
+ logItem.zone_id = user.zone ? parseInt(String(user.zone), 10) : 0;
95
+ logItem.plat_id = user.plat ? parseInt(String(user.plat), 10) : 0;
96
+ logItem.channel_id = user.channelID ? parseInt(String(user.channelID), 10) : 0;
97
+ logItem.service_type = ((_b = user.serviceType) !== null && _b !== void 0 ? _b : '') + '';
98
+ logItem.event_id = ((_c = user.eventID) !== null && _c !== void 0 ? _c : '') + '';
99
+ logItem.country = user.country ? user.country + '' : '';
100
+ logItem.content_id = user.contentID ? user.contentID + '' : '';
101
+ var lang = (_e = (_d = user.language) !== null && _d !== void 0 ? _d : user.locale) !== null && _e !== void 0 ? _e : '';
102
+ logItem.language = lang ? lang + '' : '';
103
+ logItem.in_game = user.in_game ? parseInt(String(user.in_game), 10) : logItem.in_game;
104
+ logItem.env = user.env ? user.env + '' : logItem.env;
105
+ logItem.account_type = user.accountType ? user.accountType + '' : '';
106
+ logItem.column_uid = user.columnUID ? user.columnUID + '' : '';
107
+ logItem.channel_uid = user.channelUID ? user.channelUID + '' : '';
108
+ logItem.plat = user.platName ? user.platName + '' : '';
109
+ if (typeof params === 'string') {
110
+ logItem.extend1 = params;
111
+ }
112
+ else if (typeof params === 'object' && params) {
113
+ logItem.extend1 = params.name;
114
+ if (typeof params.ext1 === 'string') {
115
+ logItem.extend2 = params.ext1;
116
+ }
117
+ if (typeof params.ext2 === 'string') {
118
+ logItem.extend3 = params.ext2;
119
+ }
120
+ if (typeof params.ext3 === 'string') {
121
+ logItem.extend4 = params.ext3;
122
+ }
123
+ }
124
+ logItem.log_ts = Math.floor(Date.now() / 1000);
125
+ return JSON.stringify(logItem);
126
+ }
127
+ function initAegis(isDefault) {
128
+ var id = internalState.id;
129
+ var apiSpeed = internalState.reportApiSpeed;
130
+ var assetSpeed = internalState.reportAssetSpeed;
131
+ var pagePerformance = internalState.pagePerformance;
132
+ var webVitals = internalState.webVitals;
133
+ var delay = internalState.delay;
134
+ var ext1;
135
+ if (!isDefault) {
136
+ id = internalState.rumID || internalState.id;
137
+ apiSpeed = true;
138
+ assetSpeed = true;
139
+ pagePerformance = true;
140
+ webVitals = true;
141
+ delay = 1000;
142
+ }
143
+ if (internalState.ready) {
144
+ ext1 = buildReportStr({
145
+ name: '@@init',
146
+ ext1: internalState.user.ext1,
147
+ ext2: internalState.user.ext2,
148
+ ext3: internalState.user.ext3,
149
+ }, true);
150
+ }
151
+ // 小程序環境直接使用構造函數
152
+ if (typeof aegis_mp_sdk_1.default === 'function') {
153
+ return new aegis_mp_sdk_1.default({
154
+ id: id,
155
+ uin: internalState.uin,
156
+ reportApiSpeed: apiSpeed,
157
+ reportAssetSpeed: assetSpeed,
158
+ pagePerformance: pagePerformance,
159
+ webVitals: webVitals,
160
+ delay: delay,
161
+ spa: true,
162
+ ext1: ext1,
163
+ beforeRequest: function (data) {
164
+ if (!isDefault) {
165
+ return data;
166
+ }
167
+ if (data.logType === 'pv' || data.logType === 'event') {
168
+ return data;
169
+ }
170
+ return false;
171
+ },
172
+ });
173
+ }
174
+ console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 構造函數,請確認依賴是否正確安裝和引用');
175
+ return undefined;
176
+ }
177
+ // 對外暴露的 API
178
+ function configureWrapper(config) {
179
+ if (config.id)
180
+ internalState.id = config.id;
181
+ if (typeof config.uin === 'string')
182
+ internalState.uin = config.uin;
183
+ if (typeof config.reportApiSpeed === 'boolean')
184
+ internalState.reportApiSpeed = config.reportApiSpeed;
185
+ if (typeof config.reportAssetSpeed === 'boolean')
186
+ internalState.reportAssetSpeed = config.reportAssetSpeed;
187
+ if (typeof config.pagePerformance === 'boolean')
188
+ internalState.pagePerformance = config.pagePerformance;
189
+ if (typeof config.webVitals === 'boolean')
190
+ internalState.webVitals = config.webVitals;
191
+ if (typeof config.delay === 'number')
192
+ internalState.delay = config.delay;
193
+ }
194
+ function setUser(user) {
195
+ var incoming = user;
196
+ var nextUser = __assign(__assign({}, internalState.user), user);
197
+ // 兼容 channeldID(文檔示例裡的字段名)到 channelID
198
+ if (incoming.channeldID != null && nextUser.channelID == null) {
199
+ nextUser.channelID = incoming.channeldID;
200
+ }
201
+ // 兼容 locale -> language
202
+ if (incoming.locale != null && nextUser.language == null) {
203
+ nextUser.language = incoming.locale;
204
+ }
205
+ internalState.user = nextUser;
206
+ var u = internalState.user;
207
+ if (u.openid && u.serviceType && u.eventID) {
208
+ if (!internalState.ready) {
209
+ internalState.ready = true;
210
+ defaultAegis = initAegis(true);
211
+ console.log('[Aegis Wrapper] default aegis ready');
212
+ }
213
+ }
214
+ else {
215
+ console.log('[Aegis Wrapper] 初始化條件不足: 需要 openid / serviceType / eventID', u);
216
+ }
217
+ }
218
+ function setRumID(rumID) {
219
+ internalState.rumID = rumID;
220
+ serviceAegis = initAegis(false);
221
+ console.log('[Aegis Wrapper] service aegis rumID 已設置');
222
+ }
223
+ function reportEvent(params) {
224
+ // 先調用服務實例(如果存在)
225
+ if (serviceAegis && typeof serviceAegis.reportEvent === 'function') {
226
+ serviceAegis.reportEvent(params);
227
+ }
228
+ if (!defaultAegis || typeof defaultAegis.reportEvent !== 'function') {
229
+ console.error('[Aegis Wrapper] default aegis 尚未準備就緒,請先調用 setUser 並補全必要字段');
230
+ return;
231
+ }
232
+ if (typeof params !== 'string' && typeof params !== 'object') {
233
+ console.error('[Aegis Wrapper] reportEvent 參數類型錯誤,應為 string 或 object');
234
+ return;
235
+ }
236
+ if (typeof params === 'object' && params.name === undefined) {
237
+ console.error('[Aegis Wrapper] reportEvent 缺少 name 字段');
238
+ return;
239
+ }
240
+ var payload = typeof params === 'string' ? { name: params } : __assign({}, params);
241
+ payload.ext1 = buildReportStr(payload, false);
242
+ defaultAegis.reportEvent(payload);
243
+ }
244
+ // 方便調試或高級用戶直接訪問實例
245
+ function getInstances() {
246
+ return {
247
+ defaultAegis: defaultAegis,
248
+ serviceAegis: serviceAegis,
249
+ };
250
+ }
251
+ exports.AegisW = {};
252
+ Object.defineProperty(exports.AegisW, 'user', {
253
+ get: function () {
254
+ return internalState.user;
255
+ },
256
+ set: function (v) {
257
+ setUser(v);
258
+ },
259
+ enumerable: true,
260
+ });
261
+ Object.defineProperty(exports.AegisW, 'rumID', {
262
+ get: function () {
263
+ return internalState.rumID;
264
+ },
265
+ set: function (v) {
266
+ setRumID(v);
267
+ },
268
+ enumerable: true,
269
+ });
270
+ exports.AegisW.reportEvent = reportEvent;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "aegis-mp-rum-wrapper",
3
+ "version": "0.1.0",
4
+ "description": "基於 aegis-mp-sdk 的小程序上報封裝 SDK,支持雙實例與自定義 RUM 日誌構造。",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc -p tsconfig.json",
9
+ "clean": "rm -rf dist"
10
+ },
11
+ "keywords": [
12
+ "aegis",
13
+ "rum",
14
+ "mp",
15
+ "miniprogram",
16
+ "sdk"
17
+ ],
18
+ "author": "",
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "aegis-mp-sdk": "^1.0.0"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.6.0"
25
+ },
26
+ "files": ["dist", "src", "README.md", "package.json"]
27
+ }
28
+
29
+
package/src/index.ts ADDED
@@ -0,0 +1,376 @@
1
+ import AegisCtor from 'aegis-mp-sdk';
2
+
3
+ // rum id
4
+ export const GPPRumID = 'zYpo1fz400Vk86JR5Z'; // cn services
5
+
6
+ // 封裝的用戶信息結構
7
+ export interface AegisUserInfo {
8
+ openid?: string;
9
+ roleID?: string;
10
+ area?: number | string;
11
+ zone?: number | string;
12
+ channelID?: number | string;
13
+ plat?: number | string;
14
+ serviceType?: string;
15
+ accountType?: string;
16
+ channelUID?: string;
17
+ columnUID?: string;
18
+ platName?: string;
19
+ eventID?: string;
20
+ contentID?: string;
21
+ country?: string;
22
+ language?: string;
23
+ locale?: string;
24
+ in_game?: number | string;
25
+ env?: string;
26
+ ext1?: string;
27
+ ext2?: string;
28
+ ext3?: string;
29
+ }
30
+
31
+ export interface AegisMpWrapperConfig {
32
+ id?: string; // 默認使用 GPPRumID
33
+ uin?: string;
34
+ reportApiSpeed?: boolean;
35
+ reportAssetSpeed?: boolean;
36
+ pagePerformance?: boolean;
37
+ webVitals?: boolean;
38
+ delay?: number;
39
+ }
40
+
41
+ // log item template
42
+ interface LogItem {
43
+ report_type: number;
44
+ event_id: string;
45
+ service_type: string;
46
+ openid: string;
47
+ role_id: string;
48
+ area_id: number;
49
+ zone_id: number;
50
+ plat_id: number;
51
+ channel_id: number;
52
+ mod_name: string;
53
+ country: string;
54
+ language: string;
55
+ seq: string;
56
+ client_ip: string;
57
+ env: string;
58
+ log_time: string;
59
+ log_ts: number;
60
+ extend1: string;
61
+ extend2: string;
62
+ extend3: string;
63
+ extend4: string;
64
+ in_game: number;
65
+ open_panel: string;
66
+ player_attr: number;
67
+ inviter_open_id: string;
68
+ inviter_role_id: string;
69
+ inviter_zone_area_id: number;
70
+ inviter_zone_id: number;
71
+ inviter_plat_id: number;
72
+ invitation_token: string;
73
+ price: number;
74
+ coin_type: string;
75
+ login_phase: number;
76
+ share_channel: number;
77
+ task_id: string;
78
+ account_type: string;
79
+ column_uid: string;
80
+ channel_uid: string;
81
+ plat: string;
82
+ content_id: string;
83
+ source: string;
84
+ }
85
+
86
+ const logItemTemplate: LogItem = {
87
+ report_type: 11,
88
+ event_id: '',
89
+ service_type: '',
90
+ openid: '',
91
+ role_id: '',
92
+ area_id: 0,
93
+ zone_id: 0,
94
+ plat_id: 0,
95
+ channel_id: 0,
96
+ mod_name: 'h5_frontend',
97
+ country: '',
98
+ language: '',
99
+ seq: '',
100
+ client_ip: '',
101
+ env: 'prd',
102
+ log_time: '',
103
+ log_ts: 0,
104
+ extend1: '',
105
+ extend2: '',
106
+ extend3: '',
107
+ extend4: '',
108
+ in_game: 2,
109
+ open_panel: '2',
110
+ player_attr: 1,
111
+ inviter_open_id: '',
112
+ inviter_role_id: '',
113
+ inviter_zone_area_id: 0,
114
+ inviter_zone_id: 0,
115
+ inviter_plat_id: 0,
116
+ invitation_token: '',
117
+ price: 0,
118
+ coin_type: '',
119
+ login_phase: 0,
120
+ share_channel: 0,
121
+ task_id: '',
122
+ account_type: '',
123
+ column_uid: '',
124
+ channel_uid: '',
125
+ plat: '',
126
+ content_id: '',
127
+ source: 'fe_rum',
128
+ };
129
+
130
+ // 內部狀態
131
+ let defaultAegis: any;
132
+ let serviceAegis: any;
133
+
134
+ const internalState = {
135
+ id: GPPRumID,
136
+ uin: '',
137
+ reportApiSpeed: false,
138
+ reportAssetSpeed: false,
139
+ pagePerformance: false,
140
+ webVitals: false,
141
+ delay: 100,
142
+ user: {} as AegisUserInfo,
143
+ rumID: '',
144
+ ready: false,
145
+ };
146
+
147
+ export interface ReportParams {
148
+ name: string;
149
+ ext1?: string;
150
+ ext2?: string;
151
+ ext3?: string;
152
+ [key: string]: any;
153
+ }
154
+
155
+ function buildReportStr(params: string | ReportParams, isPV: boolean): string {
156
+ const logItem: LogItem = { ...logItemTemplate };
157
+
158
+ if (isPV) {
159
+ logItem.report_type = 1;
160
+ }
161
+
162
+ const user = internalState.user;
163
+
164
+ logItem.openid = (user.openid ?? '') + '';
165
+ logItem.role_id = user.roleID ? user.roleID + '' : '';
166
+ logItem.area_id = user.area ? parseInt(String(user.area), 10) : 0;
167
+ logItem.zone_id = user.zone ? parseInt(String(user.zone), 10) : 0;
168
+ logItem.plat_id = user.plat ? parseInt(String(user.plat), 10) : 0;
169
+ logItem.channel_id = user.channelID ? parseInt(String(user.channelID), 10) : 0;
170
+ logItem.service_type = (user.serviceType ?? '') + '';
171
+ logItem.event_id = (user.eventID ?? '') + '';
172
+ logItem.country = user.country ? user.country + '' : '';
173
+ logItem.content_id = user.contentID ? user.contentID + '' : '';
174
+ const lang = user.language ?? user.locale ?? '';
175
+ logItem.language = lang ? lang + '' : '';
176
+ logItem.in_game = user.in_game ? parseInt(String(user.in_game), 10) : logItem.in_game;
177
+ logItem.env = user.env ? user.env + '' : logItem.env;
178
+ logItem.account_type = user.accountType ? user.accountType + '' : '';
179
+ logItem.column_uid = user.columnUID ? user.columnUID + '' : '';
180
+ logItem.channel_uid = user.channelUID ? user.channelUID + '' : '';
181
+ logItem.plat = user.platName ? user.platName + '' : '';
182
+
183
+ if (typeof params === 'string') {
184
+ logItem.extend1 = params;
185
+ } else if (typeof params === 'object' && params) {
186
+ logItem.extend1 = params.name;
187
+ if (typeof params.ext1 === 'string') {
188
+ logItem.extend2 = params.ext1;
189
+ }
190
+ if (typeof params.ext2 === 'string') {
191
+ logItem.extend3 = params.ext2;
192
+ }
193
+ if (typeof params.ext3 === 'string') {
194
+ logItem.extend4 = params.ext3;
195
+ }
196
+ }
197
+
198
+ logItem.log_ts = Math.floor(Date.now() / 1000);
199
+ return JSON.stringify(logItem);
200
+ }
201
+
202
+ function initAegis(isDefault: boolean): any | undefined {
203
+ let id = internalState.id;
204
+ let apiSpeed = internalState.reportApiSpeed;
205
+ let assetSpeed = internalState.reportAssetSpeed;
206
+ let pagePerformance = internalState.pagePerformance;
207
+ let webVitals = internalState.webVitals;
208
+ let delay = internalState.delay;
209
+ let ext1: string | undefined;
210
+
211
+ if (!isDefault) {
212
+ id = internalState.rumID || internalState.id;
213
+ apiSpeed = true;
214
+ assetSpeed = true;
215
+ pagePerformance = true;
216
+ webVitals = true;
217
+ delay = 1000;
218
+ }
219
+
220
+ if (internalState.ready) {
221
+ ext1 = buildReportStr(
222
+ {
223
+ name: '@@init',
224
+ ext1: internalState.user.ext1,
225
+ ext2: internalState.user.ext2,
226
+ ext3: internalState.user.ext3,
227
+ },
228
+ true,
229
+ );
230
+ }
231
+
232
+ // 小程序環境直接使用構造函數
233
+ if (typeof AegisCtor === 'function') {
234
+ return new (AegisCtor as any)({
235
+ id,
236
+ uin: internalState.uin,
237
+ reportApiSpeed: apiSpeed,
238
+ reportAssetSpeed: assetSpeed,
239
+ pagePerformance,
240
+ webVitals,
241
+ delay,
242
+ spa: true,
243
+ ext1,
244
+ beforeRequest(data: any) {
245
+ if (!isDefault) {
246
+ return data;
247
+ }
248
+ if (data.logType === 'pv' || data.logType === 'event') {
249
+ return data;
250
+ }
251
+ return false;
252
+ },
253
+ });
254
+ }
255
+
256
+ console.error('[Aegis Wrapper] 找不到 aegis-mp-sdk 構造函數,請確認依賴是否正確安裝和引用');
257
+ return undefined;
258
+ }
259
+
260
+ // 對外暴露的 API
261
+ export function configureWrapper(config: AegisMpWrapperConfig): void {
262
+ if (config.id) internalState.id = config.id;
263
+ if (typeof config.uin === 'string') internalState.uin = config.uin;
264
+ if (typeof config.reportApiSpeed === 'boolean') internalState.reportApiSpeed = config.reportApiSpeed;
265
+ if (typeof config.reportAssetSpeed === 'boolean') internalState.reportAssetSpeed = config.reportAssetSpeed;
266
+ if (typeof config.pagePerformance === 'boolean') internalState.pagePerformance = config.pagePerformance;
267
+ if (typeof config.webVitals === 'boolean') internalState.webVitals = config.webVitals;
268
+ if (typeof config.delay === 'number') internalState.delay = config.delay;
269
+ }
270
+
271
+ export function setUser(user: Partial<AegisUserInfo>): void {
272
+ const incoming: any = user as any;
273
+ const nextUser: AegisUserInfo = {
274
+ ...internalState.user,
275
+ ...user,
276
+ };
277
+
278
+ // 兼容 channeldID(文檔示例裡的字段名)到 channelID
279
+ if (incoming.channeldID != null && nextUser.channelID == null) {
280
+ (nextUser as any).channelID = incoming.channeldID;
281
+ }
282
+
283
+ // 兼容 locale -> language
284
+ if (incoming.locale != null && nextUser.language == null) {
285
+ nextUser.language = incoming.locale;
286
+ }
287
+
288
+ internalState.user = nextUser;
289
+
290
+ const u = internalState.user;
291
+ if (u.openid && u.serviceType && u.eventID) {
292
+ if (!internalState.ready) {
293
+ internalState.ready = true;
294
+ defaultAegis = initAegis(true);
295
+ console.log('[Aegis Wrapper] default aegis ready');
296
+ }
297
+ } else {
298
+ console.log('[Aegis Wrapper] 初始化條件不足: 需要 openid / serviceType / eventID', u);
299
+ }
300
+ }
301
+
302
+ export function setRumID(rumID: string): void {
303
+ internalState.rumID = rumID;
304
+ serviceAegis = initAegis(false);
305
+ console.log('[Aegis Wrapper] service aegis rumID 已設置');
306
+ }
307
+
308
+ export function reportEvent(params: string | ReportParams): void {
309
+ // 先調用服務實例(如果存在)
310
+ if (serviceAegis && typeof (serviceAegis as any).reportEvent === 'function') {
311
+ (serviceAegis as any).reportEvent(params as any);
312
+ }
313
+
314
+ if (!defaultAegis || typeof (defaultAegis as any).reportEvent !== 'function') {
315
+ console.error('[Aegis Wrapper] default aegis 尚未準備就緒,請先調用 setUser 並補全必要字段');
316
+ return;
317
+ }
318
+
319
+ if (typeof params !== 'string' && typeof params !== 'object') {
320
+ console.error('[Aegis Wrapper] reportEvent 參數類型錯誤,應為 string 或 object');
321
+ return;
322
+ }
323
+
324
+ if (typeof params === 'object' && (params as any).name === undefined) {
325
+ console.error('[Aegis Wrapper] reportEvent 缺少 name 字段');
326
+ return;
327
+ }
328
+
329
+ const payload: any = typeof params === 'string' ? { name: params } : { ...params };
330
+ payload.ext1 = buildReportStr(payload, false);
331
+ (defaultAegis as any).reportEvent(payload);
332
+ }
333
+
334
+ // 方便調試或高級用戶直接訪問實例
335
+ export function getInstances() {
336
+ return {
337
+ defaultAegis,
338
+ serviceAegis,
339
+ };
340
+ }
341
+
342
+ // 物件式封裝,支持示例中的兩種用法:
343
+ // 1)AegisW.user = {...}
344
+ // 2)AegisW.reportEvent({...})
345
+ export interface AegisWrapperFacade {
346
+ user: AegisUserInfo;
347
+ rumID: string;
348
+ reportEvent: (params: string | ReportParams) => void;
349
+ }
350
+
351
+ export const AegisW: AegisWrapperFacade = {} as any;
352
+
353
+ Object.defineProperty(AegisW, 'user', {
354
+ get() {
355
+ return internalState.user;
356
+ },
357
+ set(v: AegisUserInfo) {
358
+ setUser(v);
359
+ },
360
+ enumerable: true,
361
+ });
362
+
363
+ Object.defineProperty(AegisW, 'rumID', {
364
+ get() {
365
+ return internalState.rumID;
366
+ },
367
+ set(v: string) {
368
+ setRumID(v);
369
+ },
370
+ enumerable: true,
371
+ });
372
+
373
+ (AegisW as any).reportEvent = reportEvent;
374
+
375
+
376
+