@chiyou/minigame-framework 1.3.8 → 1.4.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/package.json
CHANGED
|
@@ -10,6 +10,18 @@ import { PlatformID } from "./SystemDefinition";
|
|
|
10
10
|
/** 事件参数类型 */
|
|
11
11
|
export type EventParams = Record<string, string | number | boolean> | string;
|
|
12
12
|
|
|
13
|
+
/** 通用属性(trackEvent 自动合并) */
|
|
14
|
+
export interface CommonParams {
|
|
15
|
+
/** 游戏编号:6 */
|
|
16
|
+
app_index: number;
|
|
17
|
+
/** 游戏名称:点点方块三连消 */
|
|
18
|
+
app_name: string;
|
|
19
|
+
/** 平台标识:2(PlatformID数值) */
|
|
20
|
+
platform_id: number;
|
|
21
|
+
/** 游戏版本:3.5.1 */
|
|
22
|
+
app_version: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
13
25
|
// ==================== 配置接口 ====================
|
|
14
26
|
|
|
15
27
|
/** 友盟统计配置 */
|
|
@@ -83,7 +95,7 @@ export interface StageRunningItemParams {
|
|
|
83
95
|
}
|
|
84
96
|
|
|
85
97
|
/** 友盟关卡 SDK 接口 */
|
|
86
|
-
export interface
|
|
98
|
+
export interface UmaSDKStage {
|
|
87
99
|
/** 关卡开始 */
|
|
88
100
|
onStart(params: StageStartParams): void;
|
|
89
101
|
/** 关卡结束 */
|
|
@@ -127,7 +139,7 @@ export interface UmaSDK {
|
|
|
127
139
|
/** 调用分享 */
|
|
128
140
|
shareAppMessage(options: object): void;
|
|
129
141
|
/** 关卡行为上报(stage 对象) */
|
|
130
|
-
stage:
|
|
142
|
+
stage: UmaSDKStage;
|
|
131
143
|
}
|
|
132
144
|
|
|
133
145
|
// ==================== 预定义事件 ID ====================
|
|
@@ -3,9 +3,10 @@ import { LogUtils } from "../Utils/LogUtils";
|
|
|
3
3
|
import { ServiceLocator } from "../Utils/ServiceLocator";
|
|
4
4
|
import { FwkErrorCode } from "../Definition/FwkErrorDefinition";
|
|
5
5
|
import { PlatformID } from "../Definition/SystemDefinition";
|
|
6
|
-
import { UmengConfig, EventParams, AnalyticsEventId, AnalyticsParamKey, UmaSDK,
|
|
6
|
+
import { UmengConfig, EventParams, AnalyticsEventId, AnalyticsParamKey, UmaSDK, StageEventType, StageRunningEventType, StageEndParams, StageRunningParams, CommonParams, UmaSDKStage } from "../Definition/AnalyticsDefinition";
|
|
7
7
|
import { EventMgr } from "./EventMgr";
|
|
8
8
|
import { FrameworkBase } from "../Definition/FrameworkBase";
|
|
9
|
+
import { SystemMgr } from "./SystemMgr";
|
|
9
10
|
|
|
10
11
|
export class AnalyticsMgr extends BaseMgr {
|
|
11
12
|
/** 单例实例 */
|
|
@@ -20,7 +21,9 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
20
21
|
/** 当前平台对应的 SDK 实例(通过 wx.uma 全局访问) */
|
|
21
22
|
private _sdk: UmaSDK = null;
|
|
22
23
|
/** 关卡 SDK 实例 */
|
|
23
|
-
private
|
|
24
|
+
private _sdkStage: UmaSDKStage = null;
|
|
25
|
+
/** 通用属性(trackEvent 自动合并) */
|
|
26
|
+
private _commonParams: CommonParams = null;
|
|
24
27
|
|
|
25
28
|
onLoad(): void {
|
|
26
29
|
super.onLoad();
|
|
@@ -76,7 +79,7 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
76
79
|
if (enabled) {
|
|
77
80
|
// 从全局获取 SDK 实例(在 game.js 中初始化后挂载到 wx.uma)
|
|
78
81
|
this._sdk = this._getGlobalSDK();
|
|
79
|
-
this.
|
|
82
|
+
this._sdkStage = this._sdk?.stage || null;
|
|
80
83
|
this._isEnabled = !!this._sdk;
|
|
81
84
|
} else {
|
|
82
85
|
this._isEnabled = false;
|
|
@@ -85,7 +88,11 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
85
88
|
});
|
|
86
89
|
}
|
|
87
90
|
|
|
91
|
+
// 自动初始化通用属性(从 SystemMgr 获取 appIndex/appName/platformID/appVersion)
|
|
92
|
+
this._initCommonParams();
|
|
93
|
+
|
|
88
94
|
this._isInited = true;
|
|
95
|
+
|
|
89
96
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "初始化完成", {
|
|
90
97
|
enabled: this._isEnabled,
|
|
91
98
|
platformID: PlatformID[platformID],
|
|
@@ -123,17 +130,58 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
123
130
|
// ==================== 生命周期 ====================
|
|
124
131
|
// 注意:SDK 在 game.js 中已自动注册 wx.onShow/wx.onHide 的 resume/pause 调用
|
|
125
132
|
|
|
133
|
+
// ==================== 通用属性 ====================
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 初始化通用属性(由 init() 内部调用)
|
|
137
|
+
*
|
|
138
|
+
* 自动从 SystemMgr 获取 appIndex/appName/platformID/appVersion,
|
|
139
|
+
* 后续所有 trackEvent 调用自动合并这些属性。
|
|
140
|
+
* 合并规则:通用属性与自定义属性同级平铺,自定义属性可覆盖同名通用属性。
|
|
141
|
+
*
|
|
142
|
+
* 注意:仅适用于 trackEvent,不适用于 stage 系列接口。
|
|
143
|
+
* 友盟 SDK 的 setSuperProperty 在小游戏环境不可用,需在框架封装层实现。
|
|
144
|
+
*/
|
|
145
|
+
private _initCommonParams(): void {
|
|
146
|
+
const systemMgr = ServiceLocator.Instance.get("SystemMgr") as SystemMgr;
|
|
147
|
+
if (!systemMgr) {
|
|
148
|
+
LogUtils.Instance.warn(AnalyticsMgr.TAG, "SystemMgr未注册,跳过通用属性初始化");
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const appItem = systemMgr.getAppItem();
|
|
153
|
+
if (!appItem) {
|
|
154
|
+
LogUtils.Instance.warn(AnalyticsMgr.TAG, "AppItem未设置,跳过通用属性初始化");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const platformID: PlatformID = BaseMgr.Instance.getCurrentPlatformID();
|
|
159
|
+
this._commonParams = {
|
|
160
|
+
app_index: appItem.appIndex,
|
|
161
|
+
app_name: appItem.appName,
|
|
162
|
+
platform_id: platformID,
|
|
163
|
+
app_version: systemMgr.getAppVersion(),
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
LogUtils.Instance.info(AnalyticsMgr.TAG, "通用属性初始化", {
|
|
167
|
+
operation: "_initCommonParams",
|
|
168
|
+
...this._commonParams,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
126
172
|
// ==================== 事件上报 ====================
|
|
127
173
|
|
|
128
174
|
/**
|
|
129
175
|
* 上报自定义事件
|
|
176
|
+
* 自动合并通用属性(通用属性在前,自定义属性在后,自定义属性可覆盖同名通用属性)
|
|
130
177
|
* @param eventId 事件 ID
|
|
131
178
|
* @param params 事件参数
|
|
132
179
|
*/
|
|
133
180
|
public trackEvent(eventId: string | AnalyticsEventId, params?: EventParams): void {
|
|
134
181
|
if (!this._isEnabled) return;
|
|
135
182
|
try {
|
|
136
|
-
this.
|
|
183
|
+
const mergedParams = this._mergeCommonParams(params);
|
|
184
|
+
this._sdk.trackEvent(eventId, mergedParams);
|
|
137
185
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "事件上报", {
|
|
138
186
|
eventId: eventId,
|
|
139
187
|
});
|
|
@@ -270,9 +318,9 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
270
318
|
* @param stageName 关卡名称
|
|
271
319
|
*/
|
|
272
320
|
public stageOnStart(stageId: string, stageName: string): void {
|
|
273
|
-
if (!this._isEnabled || !this.
|
|
321
|
+
if (!this._isEnabled || !this._sdkStage) return;
|
|
274
322
|
try {
|
|
275
|
-
this.
|
|
323
|
+
this._sdkStage.onStart({ stageId, stageName });
|
|
276
324
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡开始", {
|
|
277
325
|
operation: "stageOnStart",
|
|
278
326
|
stageId,
|
|
@@ -296,13 +344,13 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
296
344
|
* @param duration 关卡耗时(毫秒,可选)
|
|
297
345
|
*/
|
|
298
346
|
public stageOnEnd(stageId: string, stageName: string, event: StageEventType, duration?: number): void {
|
|
299
|
-
if (!this._isEnabled || !this.
|
|
347
|
+
if (!this._isEnabled || !this._sdkStage) return;
|
|
300
348
|
try {
|
|
301
349
|
const params: StageEndParams = { stageId, stageName, event };
|
|
302
350
|
if (duration !== undefined) {
|
|
303
351
|
params._um_sdu = duration;
|
|
304
352
|
}
|
|
305
|
-
this.
|
|
353
|
+
this._sdkStage.onEnd(params);
|
|
306
354
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡结束", {
|
|
307
355
|
operation: "stageOnEnd",
|
|
308
356
|
stageId,
|
|
@@ -338,7 +386,7 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
338
386
|
itemCount?: number,
|
|
339
387
|
itemMoney?: number
|
|
340
388
|
): void {
|
|
341
|
-
if (!this._isEnabled || !this.
|
|
389
|
+
if (!this._isEnabled || !this._sdkStage) return;
|
|
342
390
|
try {
|
|
343
391
|
const params: StageRunningParams = {
|
|
344
392
|
stageId,
|
|
@@ -346,7 +394,7 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
346
394
|
event: StageRunningEventType.Tools,
|
|
347
395
|
params: { itemName, itemId, itemCount, itemMoney },
|
|
348
396
|
};
|
|
349
|
-
this.
|
|
397
|
+
this._sdkStage.onRunning(params);
|
|
350
398
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡使用道具", {
|
|
351
399
|
operation: "stageOnRunningTools",
|
|
352
400
|
stageId,
|
|
@@ -384,7 +432,7 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
384
432
|
itemCount?: number,
|
|
385
433
|
itemMoney?: number
|
|
386
434
|
): void {
|
|
387
|
-
if (!this._isEnabled || !this.
|
|
435
|
+
if (!this._isEnabled || !this._sdkStage) return;
|
|
388
436
|
try {
|
|
389
437
|
const params: StageRunningParams = {
|
|
390
438
|
stageId,
|
|
@@ -392,7 +440,7 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
392
440
|
event: StageRunningEventType.Award,
|
|
393
441
|
params: { itemName, itemId, itemCount, itemMoney },
|
|
394
442
|
};
|
|
395
|
-
this.
|
|
443
|
+
this._sdkStage.onRunning(params);
|
|
396
444
|
LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡获得奖励", {
|
|
397
445
|
operation: "stageOnRunningAward",
|
|
398
446
|
stageId,
|
|
@@ -433,4 +481,20 @@ export class AnalyticsMgr extends BaseMgr {
|
|
|
433
481
|
if (typeof params === "string") return { value: params };
|
|
434
482
|
return params as Record<string, any>;
|
|
435
483
|
}
|
|
484
|
+
|
|
485
|
+
/** 合并通用属性与自定义属性(通用属性在前,自定义属性在后,自定义属性可覆盖) */
|
|
486
|
+
private _mergeCommonParams(customParams?: EventParams): Record<string, any> | undefined {
|
|
487
|
+
const customObj = this._flattenParams(customParams);
|
|
488
|
+
|
|
489
|
+
// 无通用属性时直接返回自定义属性
|
|
490
|
+
if (!this._commonParams) {
|
|
491
|
+
return customObj;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// 合并:通用属性在前,自定义属性在后(自定义属性可覆盖同名通用属性)
|
|
495
|
+
return {
|
|
496
|
+
...this._commonParams,
|
|
497
|
+
...customObj,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
436
500
|
}
|
|
@@ -25,11 +25,13 @@ export class SystemMgr extends BaseMgr {
|
|
|
25
25
|
static TAG: string = "SystemMgr";
|
|
26
26
|
|
|
27
27
|
/** 应用配置 */
|
|
28
|
-
private
|
|
28
|
+
private _appItem: AppItem = null;
|
|
29
29
|
/** 版权信息 */
|
|
30
|
-
private
|
|
30
|
+
private _copyrightInfo: CopyrightInfo = null;
|
|
31
31
|
/** 是否本地游戏 */
|
|
32
|
-
private
|
|
32
|
+
private _localGame: boolean = true;
|
|
33
|
+
/** 游戏版本号 */
|
|
34
|
+
private _appVersion: string = "";
|
|
33
35
|
|
|
34
36
|
onLoad(): void {
|
|
35
37
|
super.onLoad();
|
|
@@ -48,8 +50,9 @@ export class SystemMgr extends BaseMgr {
|
|
|
48
50
|
* 初始化系统管理器
|
|
49
51
|
* @param appItem 应用配置
|
|
50
52
|
* @param copyrightInfo 版权信息
|
|
53
|
+
* @param appVersion 游戏版本号(如 '3.5.1')
|
|
51
54
|
*/
|
|
52
|
-
public init(appItem: AppItem, copyrightInfo: CopyrightInfo): void {
|
|
55
|
+
public init(appItem: AppItem, copyrightInfo: CopyrightInfo, appVersion: string): void {
|
|
53
56
|
if (!appItem) {
|
|
54
57
|
LogUtils.Instance.error(SystemMgr.TAG, FwkErrorCode.System.ConfigError, {
|
|
55
58
|
operation: "init",
|
|
@@ -66,19 +69,20 @@ export class SystemMgr extends BaseMgr {
|
|
|
66
69
|
});
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
this.
|
|
70
|
-
this.
|
|
72
|
+
this._appItem = appItem;
|
|
73
|
+
this._copyrightInfo = copyrightInfo;
|
|
74
|
+
this._appVersion = appVersion;
|
|
71
75
|
|
|
72
76
|
let currPlatformID: PlatformID = BaseMgr.Instance.getCurrentPlatformID();
|
|
73
77
|
|
|
74
|
-
if (this.
|
|
75
|
-
this.
|
|
76
|
-
let icpItem: ICPItem = this.
|
|
78
|
+
if (this._copyrightInfo && this._copyrightInfo.icpMap &&
|
|
79
|
+
this._copyrightInfo.icpMap.has(currPlatformID)) {
|
|
80
|
+
let icpItem: ICPItem = this._copyrightInfo.icpMap.get(currPlatformID);
|
|
77
81
|
if (icpItem !== null) {
|
|
78
|
-
this.
|
|
82
|
+
this._localGame = icpItem.isLocalGame;
|
|
79
83
|
}
|
|
80
84
|
} else if (currPlatformID === PlatformID.ID_H5_DesktopBrowser) {
|
|
81
|
-
this.
|
|
85
|
+
this._localGame = false;
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
let platformAdapterImpl: AbsPlatformAdapter = null;
|
|
@@ -131,13 +135,14 @@ export class SystemMgr extends BaseMgr {
|
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
LogUtils.Instance.info(SystemMgr.TAG, "初始化完成", {
|
|
134
|
-
appName: this.
|
|
135
|
-
appIndex: this.
|
|
136
|
-
platformCount: this.
|
|
138
|
+
appName: this._appItem?.appName || "未设置",
|
|
139
|
+
appIndex: this._appItem?.appIndex || "未设置",
|
|
140
|
+
platformCount: this._appItem?.appIDMap?.size || 0,
|
|
137
141
|
platformID: currPlatformID,
|
|
138
142
|
platformName: PlatformID[currPlatformID],
|
|
139
|
-
appID: this.
|
|
140
|
-
isLocalGame: this.
|
|
143
|
+
appID: this._appItem?.appIDMap?.get(currPlatformID) || "未配置",
|
|
144
|
+
isLocalGame: this._localGame,
|
|
145
|
+
appVersion: this._appVersion,
|
|
141
146
|
});
|
|
142
147
|
}
|
|
143
148
|
|
|
@@ -146,7 +151,7 @@ export class SystemMgr extends BaseMgr {
|
|
|
146
151
|
* @return 应用配置
|
|
147
152
|
*/
|
|
148
153
|
public getAppItem(): AppItem {
|
|
149
|
-
return this.
|
|
154
|
+
return this._appItem;
|
|
150
155
|
}
|
|
151
156
|
|
|
152
157
|
/**
|
|
@@ -154,7 +159,15 @@ export class SystemMgr extends BaseMgr {
|
|
|
154
159
|
* @return 是否本地游戏
|
|
155
160
|
*/
|
|
156
161
|
public isLocalGame(): boolean {
|
|
157
|
-
return this.
|
|
162
|
+
return this._localGame;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 获取游戏版本号
|
|
167
|
+
* @return 游戏版本号
|
|
168
|
+
*/
|
|
169
|
+
public getAppVersion(): string {
|
|
170
|
+
return this._appVersion;
|
|
158
171
|
}
|
|
159
172
|
|
|
160
173
|
/**
|
|
@@ -474,9 +474,11 @@ export class UIMgr extends BaseMgr {
|
|
|
474
474
|
labelComponent.string = content;
|
|
475
475
|
labelComponent.updateRenderData(true);
|
|
476
476
|
let textWidth: number = labelComponent.node.getComponent(UITransform).contentSize.width;
|
|
477
|
+
let textHeight: number = labelComponent.node.getComponent(UITransform).contentSize.height;
|
|
477
478
|
|
|
478
479
|
this.node_toastRoot.active = true;
|
|
479
480
|
this.node_toastRoot.getComponent(UITransform).width = textWidth + 80;
|
|
481
|
+
this.node_toastRoot.getComponent(UITransform).height = textHeight + 40;
|
|
480
482
|
|
|
481
483
|
// 设置 Toast 位置
|
|
482
484
|
if (position !== undefined) {
|