@chiyou/minigame-framework 1.3.9 → 1.4.1

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": "@chiyou/minigame-framework",
3
- "version": "1.3.9",
3
+ "version": "1.4.1",
4
4
  "description": "基于 Cocos Creator 3.x 的小游戏开发框架,支持多平台发布",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -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
  /** 友盟统计配置 */
@@ -20,88 +32,14 @@ export interface UmengConfig {
20
32
 
21
33
  // ==================== SDK 类型声明 ====================
22
34
 
23
- /** 关卡结束事件类型 */
24
- export enum StageEventType {
25
- /** 关卡完成 */
26
- Complete = "complete",
27
- /** 关卡失败 */
28
- Fail = "fail",
29
- }
30
-
31
- /** 关卡中行为事件类型 */
32
- export enum StageRunningEventType {
33
- /** 使用道具 */
34
- Tools = "tools",
35
- /** 获得奖励 */
36
- Award = "award",
37
- }
38
-
39
- /** 关卡开始参数 */
40
- export interface StageStartParams {
41
- /** 关卡ID(必传,string类型) */
42
- stageId: string;
43
- /** 关卡名称(必传) */
44
- stageName: string;
45
- }
46
-
47
- /** 关卡结束参数 */
48
- export interface StageEndParams {
49
- /** 关卡ID(必传,string类型) */
50
- stageId: string;
51
- /** 关卡名称(必传) */
52
- stageName: string;
53
- /** 关卡结束结果(必传,complete/fail) */
54
- event: StageEventType;
55
- /** 关卡耗时(毫秒,可选) */
56
- _um_sdu?: number;
57
- }
58
-
59
- /** 关卡中行为事件参数 */
60
- export interface StageRunningParams {
61
- /** 关卡ID(必传,string类型) */
62
- stageId: string;
63
- /** 关卡名称(必传) */
64
- stageName: string;
65
- /** 事件类型(必传,tools/award) */
66
- event: StageRunningEventType;
67
- /** 事件参数 */
68
- params?: StageRunningItemParams;
69
- }
70
-
71
- /** 关卡中行为物品参数 */
72
- export interface StageRunningItemParams {
73
- /** 商品/道具名称(必传) */
74
- itemName: string;
75
- /** 商品/道具ID(可选) */
76
- itemId?: string;
77
- /** 商品/道具数量(可选) */
78
- itemCount?: number;
79
- /** 商品/道具单价(可选) */
80
- itemMoney?: number;
81
- /** 描述(可选) */
82
- desc?: string;
83
- }
84
-
85
- /** 友盟关卡 SDK 接口 */
86
- export interface UmaStageSDK {
87
- /** 关卡开始 */
88
- onStart(params: StageStartParams): void;
89
- /** 关卡结束 */
90
- onEnd(params: StageEndParams): void;
91
- /** 关卡中行为 */
92
- onRunning(params: StageRunningParams): void;
93
- }
94
-
95
35
  /**
96
36
  * 友盟 SDK 接口定义
97
37
  *
98
- * 仅包含实际可用的方法(共12个)
38
+ * 仅包含实际可用的方法(共11个)
99
39
  * 已移除:removeUserid, setUserInfo, setAnonymousid, setAppVersion, setSuperProperty(不可用)
100
40
  * 已移除:revenue, stage, level, rc(插件方法)
101
41
  *
102
42
  * 使用方式:SDK 在 game.js 中初始化后,通过 wx.uma 全局实例访问
103
- *
104
- * 关卡行为上报通过 wx.uma.stage 对象访问
105
43
  */
106
44
  export interface UmaSDK {
107
45
  /** 恢复会话(onShow 时调用) */
@@ -126,8 +64,6 @@ export interface UmaSDK {
126
64
  onShareAppMessage(callback: () => object): void;
127
65
  /** 调用分享 */
128
66
  shareAppMessage(options: object): void;
129
- /** 关卡行为上报(stage 对象) */
130
- stage: UmaStageSDK;
131
67
  }
132
68
 
133
69
  // ==================== 预定义事件 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, UmaStageSDK, StageEventType, StageRunningEventType, StageEndParams, StageRunningParams } from "../Definition/AnalyticsDefinition";
6
+ import { UmengConfig, EventParams, AnalyticsEventId, AnalyticsParamKey, UmaSDK, CommonParams } from "../Definition/AnalyticsDefinition";
7
7
  import { EventMgr } from "./EventMgr";
8
8
  import { FrameworkBase } from "../Definition/FrameworkBase";
9
+ import type { SystemMgr } from "./SystemMgr";
9
10
 
10
11
  export class AnalyticsMgr extends BaseMgr {
11
12
  /** 单例实例 */
@@ -19,8 +20,8 @@ export class AnalyticsMgr extends BaseMgr {
19
20
  private _isEnabled: boolean = false;
20
21
  /** 当前平台对应的 SDK 实例(通过 wx.uma 全局访问) */
21
22
  private _sdk: UmaSDK = null;
22
- /** 关卡 SDK 实例 */
23
- private _stageSdk: UmaStageSDK = null;
23
+ /** 通用属性(trackEvent 自动合并) */
24
+ private _commonParams: CommonParams = null;
24
25
 
25
26
  onLoad(): void {
26
27
  super.onLoad();
@@ -71,12 +72,20 @@ export class AnalyticsMgr extends BaseMgr {
71
72
 
72
73
  // 根据当前平台是否开启统计来决定是否启用
73
74
  const platformID: PlatformID = BaseMgr.Instance.getCurrentPlatformID();
74
- const enabled = umengConfig.platformEnableMap.get(platformID);
75
+ let enabled = umengConfig.platformEnableMap.get(platformID);
76
+
77
+ if (enabled) {
78
+ // 额外检查:本地游戏无网络请求能力,不上报统计
79
+ const systemMgr = ServiceLocator.Instance.get<SystemMgr>("SystemMgr");
80
+ if (systemMgr && !systemMgr.supportRequest()) {
81
+ enabled = false;
82
+ LogUtils.Instance.info(AnalyticsMgr.TAG, "本地游戏不支持网络请求,统计禁用");
83
+ }
84
+ }
75
85
 
76
86
  if (enabled) {
77
87
  // 从全局获取 SDK 实例(在 game.js 中初始化后挂载到 wx.uma)
78
88
  this._sdk = this._getGlobalSDK();
79
- this._stageSdk = this._sdk?.stage || null;
80
89
  this._isEnabled = !!this._sdk;
81
90
  } else {
82
91
  this._isEnabled = false;
@@ -85,7 +94,11 @@ export class AnalyticsMgr extends BaseMgr {
85
94
  });
86
95
  }
87
96
 
97
+ // 自动初始化通用属性(从 SystemMgr 获取 appIndex/appName/platformID/appVersion)
98
+ this._initCommonParams();
99
+
88
100
  this._isInited = true;
101
+
89
102
  LogUtils.Instance.info(AnalyticsMgr.TAG, "初始化完成", {
90
103
  enabled: this._isEnabled,
91
104
  platformID: PlatformID[platformID],
@@ -123,17 +136,57 @@ export class AnalyticsMgr extends BaseMgr {
123
136
  // ==================== 生命周期 ====================
124
137
  // 注意:SDK 在 game.js 中已自动注册 wx.onShow/wx.onHide 的 resume/pause 调用
125
138
 
139
+ // ==================== 通用属性 ====================
140
+
141
+ /**
142
+ * 初始化通用属性(由 init() 内部调用)
143
+ *
144
+ * 自动从 SystemMgr 获取 appIndex/appName/platformID/appVersion,
145
+ * 后续所有 trackEvent 调用自动合并这些属性。
146
+ * 合并规则:通用属性与自定义属性同级平铺,自定义属性可覆盖同名通用属性。
147
+ *
148
+ * 友盟 SDK 的 setSuperProperty 在小游戏环境不可用,需在框架封装层实现。
149
+ */
150
+ private _initCommonParams(): void {
151
+ const systemMgr = ServiceLocator.Instance.get("SystemMgr") as SystemMgr;
152
+ if (!systemMgr) {
153
+ LogUtils.Instance.warn(AnalyticsMgr.TAG, "SystemMgr未注册,跳过通用属性初始化");
154
+ return;
155
+ }
156
+
157
+ const appItem = systemMgr.getAppItem();
158
+ if (!appItem) {
159
+ LogUtils.Instance.warn(AnalyticsMgr.TAG, "AppItem未设置,跳过通用属性初始化");
160
+ return;
161
+ }
162
+
163
+ const platformID: PlatformID = BaseMgr.Instance.getCurrentPlatformID();
164
+ this._commonParams = {
165
+ app_index: appItem.appIndex,
166
+ app_name: appItem.appName,
167
+ platform_id: platformID,
168
+ app_version: systemMgr.getAppVersion(),
169
+ };
170
+
171
+ LogUtils.Instance.info(AnalyticsMgr.TAG, "通用属性初始化", {
172
+ operation: "_initCommonParams",
173
+ ...this._commonParams,
174
+ });
175
+ }
176
+
126
177
  // ==================== 事件上报 ====================
127
178
 
128
179
  /**
129
180
  * 上报自定义事件
181
+ * 自动合并通用属性(通用属性在前,自定义属性在后,自定义属性可覆盖同名通用属性)
130
182
  * @param eventId 事件 ID
131
183
  * @param params 事件参数
132
184
  */
133
185
  public trackEvent(eventId: string | AnalyticsEventId, params?: EventParams): void {
134
186
  if (!this._isEnabled) return;
135
187
  try {
136
- this._sdk.trackEvent(eventId, params);
188
+ const mergedParams = this._mergeCommonParams(params);
189
+ this._sdk.trackEvent(eventId, mergedParams);
137
190
  LogUtils.Instance.info(AnalyticsMgr.TAG, "事件上报", {
138
191
  eventId: eventId,
139
192
  });
@@ -262,157 +315,6 @@ export class AnalyticsMgr extends BaseMgr {
262
315
  this.trackEvent(AnalyticsEventId.AdClick, params);
263
316
  }
264
317
 
265
- // ==================== 关卡行为上报(友盟 stage 接口) ====================
266
-
267
- /**
268
- * 关卡开始
269
- * @param stageId 关卡ID(string类型)
270
- * @param stageName 关卡名称
271
- */
272
- public stageOnStart(stageId: string, stageName: string): void {
273
- if (!this._isEnabled || !this._stageSdk) return;
274
- try {
275
- this._stageSdk.onStart({ stageId, stageName });
276
- LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡开始", {
277
- operation: "stageOnStart",
278
- stageId,
279
- stageName,
280
- });
281
- } catch (e) {
282
- LogUtils.Instance.error(AnalyticsMgr.TAG, FwkErrorCode.Analytics.SDKCallFailed, {
283
- operation: "stageOnStart",
284
- reason: String(e),
285
- stageId,
286
- stageName,
287
- });
288
- }
289
- }
290
-
291
- /**
292
- * 关卡结束
293
- * @param stageId 关卡ID(string类型)
294
- * @param stageName 关卡名称
295
- * @param event 结束结果("complete" | "fail")
296
- * @param duration 关卡耗时(毫秒,可选)
297
- */
298
- public stageOnEnd(stageId: string, stageName: string, event: StageEventType, duration?: number): void {
299
- if (!this._isEnabled || !this._stageSdk) return;
300
- try {
301
- const params: StageEndParams = { stageId, stageName, event };
302
- if (duration !== undefined) {
303
- params._um_sdu = duration;
304
- }
305
- this._stageSdk.onEnd(params);
306
- LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡结束", {
307
- operation: "stageOnEnd",
308
- stageId,
309
- stageName,
310
- event,
311
- duration,
312
- });
313
- } catch (e) {
314
- LogUtils.Instance.error(AnalyticsMgr.TAG, FwkErrorCode.Analytics.SDKCallFailed, {
315
- operation: "stageOnEnd",
316
- reason: String(e),
317
- stageId,
318
- stageName,
319
- event,
320
- });
321
- }
322
- }
323
-
324
- /**
325
- * 关卡中行为 - 使用道具
326
- * @param stageId 关卡ID(string类型)
327
- * @param stageName 关卡名称
328
- * @param itemName 道具名称
329
- * @param itemId 道具ID(可选)
330
- * @param itemCount 道具数量(可选)
331
- * @param itemMoney 道具单价(可选)
332
- */
333
- public stageOnRunningTools(
334
- stageId: string,
335
- stageName: string,
336
- itemName: string,
337
- itemId?: string,
338
- itemCount?: number,
339
- itemMoney?: number
340
- ): void {
341
- if (!this._isEnabled || !this._stageSdk) return;
342
- try {
343
- const params: StageRunningParams = {
344
- stageId,
345
- stageName,
346
- event: StageRunningEventType.Tools,
347
- params: { itemName, itemId, itemCount, itemMoney },
348
- };
349
- this._stageSdk.onRunning(params);
350
- LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡使用道具", {
351
- operation: "stageOnRunningTools",
352
- stageId,
353
- stageName,
354
- itemName,
355
- itemId,
356
- itemCount,
357
- itemMoney,
358
- });
359
- } catch (e) {
360
- LogUtils.Instance.error(AnalyticsMgr.TAG, FwkErrorCode.Analytics.SDKCallFailed, {
361
- operation: "stageOnRunningTools",
362
- reason: String(e),
363
- stageId,
364
- stageName,
365
- itemName,
366
- });
367
- }
368
- }
369
-
370
- /**
371
- * 关卡中行为 - 获得奖励
372
- * @param stageId 关卡ID(string类型)
373
- * @param stageName 关卡名称
374
- * @param itemName 奖励名称
375
- * @param itemId 奖励ID(可选)
376
- * @param itemCount 奖励数量(可选)
377
- * @param itemMoney 奖励单价(可选)
378
- */
379
- public stageOnRunningAward(
380
- stageId: string,
381
- stageName: string,
382
- itemName: string,
383
- itemId?: string,
384
- itemCount?: number,
385
- itemMoney?: number
386
- ): void {
387
- if (!this._isEnabled || !this._stageSdk) return;
388
- try {
389
- const params: StageRunningParams = {
390
- stageId,
391
- stageName,
392
- event: StageRunningEventType.Award,
393
- params: { itemName, itemId, itemCount, itemMoney },
394
- };
395
- this._stageSdk.onRunning(params);
396
- LogUtils.Instance.info(AnalyticsMgr.TAG, "关卡获得奖励", {
397
- operation: "stageOnRunningAward",
398
- stageId,
399
- stageName,
400
- itemName,
401
- itemId,
402
- itemCount,
403
- itemMoney,
404
- });
405
- } catch (e) {
406
- LogUtils.Instance.error(AnalyticsMgr.TAG, FwkErrorCode.Analytics.SDKCallFailed, {
407
- operation: "stageOnRunningAward",
408
- reason: String(e),
409
- stageId,
410
- stageName,
411
- itemName,
412
- });
413
- }
414
- }
415
-
416
318
  // ==================== 状态查询 ====================
417
319
 
418
320
  /** 是否已初始化 */
@@ -433,4 +335,20 @@ export class AnalyticsMgr extends BaseMgr {
433
335
  if (typeof params === "string") return { value: params };
434
336
  return params as Record<string, any>;
435
337
  }
338
+
339
+ /** 合并通用属性与自定义属性(通用属性在前,自定义属性在后,自定义属性可覆盖) */
340
+ private _mergeCommonParams(customParams?: EventParams): Record<string, any> | undefined {
341
+ const customObj = this._flattenParams(customParams);
342
+
343
+ // 无通用属性时直接返回自定义属性
344
+ if (!this._commonParams) {
345
+ return customObj;
346
+ }
347
+
348
+ // 合并:通用属性在前,自定义属性在后(自定义属性可覆盖同名通用属性)
349
+ return {
350
+ ...this._commonParams,
351
+ ...customObj,
352
+ };
353
+ }
436
354
  }
@@ -25,11 +25,13 @@ export class SystemMgr extends BaseMgr {
25
25
  static TAG: string = "SystemMgr";
26
26
 
27
27
  /** 应用配置 */
28
- private appItem: AppItem = null;
28
+ private _appItem: AppItem = null;
29
29
  /** 版权信息 */
30
- private copyrightInfo: CopyrightInfo = null;
30
+ private _copyrightInfo: CopyrightInfo = null;
31
31
  /** 是否本地游戏 */
32
- private localGame: boolean = true;
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.appItem = appItem;
70
- this.copyrightInfo = copyrightInfo;
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.copyrightInfo && this.copyrightInfo.icpMap &&
75
- this.copyrightInfo.icpMap.has(currPlatformID)) {
76
- let icpItem: ICPItem = this.copyrightInfo.icpMap.get(currPlatformID);
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.localGame = icpItem.isLocalGame;
82
+ this._localGame = icpItem.isLocalGame;
79
83
  }
80
84
  } else if (currPlatformID === PlatformID.ID_H5_DesktopBrowser) {
81
- this.localGame = false;
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.appItem?.appName || "未设置",
135
- appIndex: this.appItem?.appIndex || "未设置",
136
- platformCount: this.appItem?.appIDMap?.size || 0,
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.appItem?.appIDMap?.get(currPlatformID) || "未配置",
140
- isLocalGame: this.localGame
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.appItem;
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.localGame;
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
  /**