@chiyou/minigame-framework 1.3.2 → 1.3.3

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.2",
3
+ "version": "1.3.3",
4
4
  "description": "基于 Cocos Creator 3.x 的小游戏开发框架,支持多平台发布",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -24,14 +24,6 @@ export interface UmengConfig {
24
24
  debug?: boolean;
25
25
  }
26
26
 
27
- /** 统计分析总配置 */
28
- export interface AnalyticsConfig {
29
- /** 是否启用统计功能 */
30
- enable: boolean;
31
- /** 友盟配置 */
32
- umeng?: UmengConfig;
33
- }
34
-
35
27
  // ==================== 用户相关 ====================
36
28
 
37
29
  /** 用户属性 */
@@ -47,29 +39,6 @@ export interface UserProperties {
47
39
  [key: string]: any;
48
40
  }
49
41
 
50
- // ==================== 友盟 SDK 接口 ====================
51
-
52
- /** 友盟 SDK 实例方法 */
53
- export interface UmengSDK {
54
- init(config: UmengConfig): void;
55
- resume(options?: any): void;
56
- pause(): void;
57
- trackEvent(eventName: string, params?: object | string): void;
58
- trackShare(shareOptions: object): object;
59
- trackPageStart(pageName: string): void;
60
- trackPageEnd(pageName: string): void;
61
- setOpenid(openid: string): void;
62
- setUnionid(unionid: string): void;
63
- setUserid(userid: string, provider?: string): void;
64
- removeUserid(): void;
65
- setUserInfo(userInfo: object): void;
66
- setAnonymousid(anonymousid: string): void;
67
- setAppVersion(version: string): void;
68
- setSuperProperty(property: string): void;
69
- onShareAppMessage(callback: () => object): void;
70
- shareAppMessage(options: object): void;
71
- }
72
-
73
42
  // ==================== 预定义事件 ID ====================
74
43
 
75
44
  /** 预定义事件 ID */
@@ -102,6 +71,7 @@ export enum AnalyticsParamKey {
102
71
  Price = 'price',
103
72
  Currency = 'currency',
104
73
  ShareType = 'share_type',
74
+ ShareContent = 'share_content',
105
75
  ErrorCode = 'error_code',
106
76
  ErrorMsg = 'error_msg',
107
77
  }
@@ -21,5 +21,7 @@ export class FrameworkBase {
21
21
  static readonly Message = class {
22
22
  static readonly LifeCycle_onGameHide: string = "LifeCycle_onGameHide";
23
23
  static readonly LifeCycle_onGameShow: string = "LifeCycle_onGameShow";
24
+
25
+ static readonly UserMgr_PlatformOpenId_Ready: string = "UserMgr_PlatformOpenId_Ready";
24
26
  }
25
27
  }
@@ -76,16 +76,4 @@ export enum UpdateUserDataResult {
76
76
  Result_NoNeed = -1,
77
77
  Result_Success = 0,
78
78
  Result_Fail = 1,
79
- }
80
-
81
- export enum LoginResultType {
82
- Type_SessionAvailable = 0,
83
- Type_LoginSuccess_Auth_Code = 1,
84
- Type_LoginSuccess_Auth_AccountID = 2,
85
- Type_LoginFail_ContinueGame = 3,
86
- Type_LoginFail_ExitGame = 4,
87
- }
88
-
89
- export type LoginResult = {
90
- type: LoginResultType;
91
79
  }
@@ -5,6 +5,8 @@ import { FwkErrorCode } from "../Definition/FwkErrorDefinition";
5
5
  import { PlatformID } from "../Definition/SystemDefinition";
6
6
  import { UmengConfig, EventParams, UserProperties, AnalyticsEventId, AnalyticsParamKey } from "../Definition/AnalyticsDefinition";
7
7
  import { UmaInitConfig, UmaSDK, uma as wxUma } from "../SDK/Umeng/umtrack-wx-game";
8
+ import { EventMgr } from "./EventMgr";
9
+ import { FrameworkBase } from "../Definition/FrameworkBase";
8
10
  // 后续新增平台 SDK 在此导入,例如:
9
11
  // import { uma as ttUma } from "../SDK/Umeng/umtrack-tt-game";
10
12
 
@@ -53,9 +55,13 @@ export class AnalyticsMgr extends BaseMgr {
53
55
 
54
56
  // 注意:SDK 内部已注册 wx.onShow/onHide 自动调用 resume/pause
55
57
  // 无需在 AnalyticsMgr 监听生命周期事件
58
+
59
+ EventMgr.Instance.on(FrameworkBase.Message.UserMgr_PlatformOpenId_Ready, this._onPlatformOpenIdReady, this);
56
60
  }
57
61
 
58
62
  onDestroy(): void {
63
+ EventMgr.Instance.off(FrameworkBase.Message.UserMgr_PlatformOpenId_Ready, this._onPlatformOpenIdReady, this);
64
+
59
65
  super.onDestroy();
60
66
  }
61
67
 
@@ -92,8 +98,7 @@ export class AnalyticsMgr extends BaseMgr {
92
98
  if (appKey && sdk) {
93
99
  this._appKey = appKey;
94
100
  this._sdk = sdk;
95
- this._isEnabled = true;
96
- this._initUmeng();
101
+ this._isEnabled = this._initUmeng();
97
102
  } else {
98
103
  this._isEnabled = false;
99
104
  if (!sdk) {
@@ -114,6 +119,14 @@ export class AnalyticsMgr extends BaseMgr {
114
119
  });
115
120
  }
116
121
 
122
+ private _onPlatformOpenIdReady(platformOpenId: string): void {
123
+ if (!this._isEnabled) return;
124
+
125
+ if (this._umengConfig && this._umengConfig.useOpenid && !this._umengConfig.autoGetOpenid) {
126
+ this.setOpenId(platformOpenId);
127
+ }
128
+ }
129
+
117
130
  // ==================== 生命周期 ====================
118
131
  // 注意:SDK 内部已自动处理 wx.onShow/onHide 的 resume/pause 调用
119
132
 
@@ -295,7 +308,7 @@ export class AnalyticsMgr extends BaseMgr {
295
308
  public trackShare(shareType: string, shareContent?: string): void {
296
309
  const params: Record<string, any> = {
297
310
  [AnalyticsParamKey.ShareType]: shareType,
298
- ...(shareContent ? { [AnalyticsParamKey.ShareType]: shareContent } : {}),
311
+ ...(shareContent ? { [AnalyticsParamKey.ShareContent]: shareContent } : {}),
299
312
  };
300
313
  this.trackEvent(AnalyticsEventId.Share, params);
301
314
  }
@@ -317,7 +330,7 @@ export class AnalyticsMgr extends BaseMgr {
317
330
  // ==================== 私有方法 ====================
318
331
 
319
332
  /** 初始化友盟 SDK */
320
- private _initUmeng(): void {
333
+ private _initUmeng(): boolean {
321
334
  try {
322
335
  const sdkConfig: UmaInitConfig = {
323
336
  appKey: this._appKey,
@@ -331,12 +344,16 @@ export class AnalyticsMgr extends BaseMgr {
331
344
  useOpenid: sdkConfig.useOpenid,
332
345
  debug: sdkConfig.debug,
333
346
  });
347
+
348
+ return true;
334
349
  } catch (e) {
335
350
  LogUtils.Instance.error(AnalyticsMgr.TAG, FwkErrorCode.Analytics.SDKInitFailed, {
336
351
  operation: "_initUmeng",
337
352
  reason: String(e),
338
353
  appKey: this._appKey,
339
354
  });
355
+
356
+ return false;
340
357
  }
341
358
  }
342
359
 
@@ -1,5 +1,5 @@
1
1
  import { FrameworkBase } from "../Definition/FrameworkBase";
2
- import { EnhanceLoginResult, LocalLoginResult, LoginResult, LoginResultType, ServerGetUserDataResult, ServerLoginResult, ServerUpdateUserDataResult, UpdateUserDataResult, UserBehaviorPlatformInfo, UserDataCategory, UserDataInfo, UserDataKey, UserDataType } from "../Definition/UserDefinition";
2
+ import { EnhanceLoginResult, LocalLoginResult, ServerGetUserDataResult, ServerLoginResult, ServerUpdateUserDataResult, UpdateUserDataResult, UserBehaviorPlatformInfo, UserDataCategory, UserDataInfo, UserDataKey, UserDataType } from "../Definition/UserDefinition";
3
3
  import { FwkErrorCode } from "../Definition/FwkErrorDefinition";
4
4
  import { LogUtils } from "../Utils/LogUtils";
5
5
  import { BaseMgr } from "./BaseMgr";
@@ -240,150 +240,6 @@ export class UserMgr extends BaseMgr {
240
240
  }
241
241
  }
242
242
 
243
- /**
244
- * 登录
245
- * @param needForceLogin 是否强制登录
246
- * @param callback 登录回调
247
- */
248
- public login(needForceLogin: boolean, callback: Function): void {
249
- if (callback === null) {
250
- LogUtils.Instance.error(UserMgr.TAG, FwkErrorCode.User.InvalidData, {
251
- operation: "login",
252
- reason: "回调函数为空"
253
- });
254
- return;
255
- }
256
-
257
- if (this.currentPlatformAdapter === null) {
258
- LogUtils.Instance.error(UserMgr.TAG, FwkErrorCode.User.NotInit, {
259
- operation: "login",
260
- reason: "平台适配器未初始化"
261
- });
262
- callback({
263
- type: LoginResultType.Type_LoginFail_ContinueGame,
264
- });
265
- return;
266
- }
267
-
268
- if (this.currentPlatformId === PlatformID.ID_H5_DesktopBrowser ||
269
- this.currentPlatformId === PlatformID.ID_MiniGame_ZhiFuBao ||
270
- this.currentPlatformId === PlatformID.ID_QuickGame_HuaWei ||
271
- this.currentPlatformId === PlatformID.ID_QuickGame_Honor ||
272
- this.currentPlatformId === PlatformID.ID_QuickGame_Vivo ||
273
- this.currentPlatformId === PlatformID.ID_QuickGame_XiaoMi ||
274
- this.currentPlatformId === PlatformID.ID_QuickGame_Oppo
275
- ) {
276
- needForceLogin = true;
277
- }
278
-
279
- LogUtils.Instance.info(UserMgr.TAG, "开始登录", {
280
- needForceLogin: needForceLogin,
281
- });
282
-
283
- if (!needForceLogin) {
284
- this._checkSession().then((loginResult: LoginResult) => {
285
- callback(loginResult);
286
- }).catch(() => {
287
- this._login().then((loginResult: LoginResult) => {
288
- callback(loginResult);
289
- }).catch((loginResult: LoginResult) => {
290
- LogUtils.Instance.info(UserMgr.TAG, "用户登录失败(Session检查失败后重试登录)", {
291
- resultType: loginResult.type,
292
- canContinue: loginResult.type === LoginResultType.Type_LoginFail_ContinueGame,
293
- });
294
- callback(loginResult);
295
- });
296
- });
297
- } else {
298
- this._login().then((loginResult: LoginResult) => {
299
- callback(loginResult);
300
- }).catch((loginResult: LoginResult) => {
301
- LogUtils.Instance.info(UserMgr.TAG, "用户登录失败(强制登录模式)", {
302
- resultType: loginResult.type,
303
- canContinue: loginResult.type === LoginResultType.Type_LoginFail_ContinueGame
304
- });
305
- callback(loginResult);
306
- });
307
- }
308
- }
309
-
310
- /** 检查 Session(内部方法) */
311
- private _checkSession(): Promise<LoginResult> {
312
- return new Promise((resolve, reject) => {
313
- if (this.currentPlatformAdapter === null) {
314
- reject();
315
- return;
316
- }
317
-
318
- this.currentPlatformAdapter.checkSession(
319
- (isValid: boolean) => {
320
- LogUtils.Instance.info(UserMgr.TAG, "_checkSession()结果", {
321
- isValid: isValid,
322
- });
323
-
324
- if (isValid) {
325
- resolve({
326
- type: LoginResultType.Type_SessionAvailable,
327
- });
328
- } else {
329
- reject();
330
- }
331
- });
332
- });
333
- }
334
-
335
- /** 登录(内部方法) */
336
- private _login(): Promise<LoginResult> {
337
- return new Promise((resolve, reject) => {
338
- if (this.currentPlatformAdapter === null) {
339
- reject("");
340
- return;
341
- }
342
-
343
- this.currentPlatformAdapter.login(
344
- (res) => {
345
- // 华为:playerId
346
- // 小米:appAccountId
347
- // 其他平台:code
348
- LogUtils.Instance.info(UserMgr.TAG, "_login()成功", {
349
- res: res,
350
- });
351
-
352
- if (res) {
353
- if (res.code) {
354
- resolve({
355
- type: LoginResultType.Type_LoginSuccess_Auth_Code,
356
- });
357
- } else if (res.playerId || res.appAccountId) {
358
- resolve({
359
- type: LoginResultType.Type_LoginSuccess_Auth_AccountID,
360
- });
361
- } else {
362
- reject({
363
- type: LoginResultType.Type_LoginFail_ContinueGame,
364
- });
365
- }
366
- } else {
367
- reject({
368
- type: LoginResultType.Type_LoginFail_ContinueGame,
369
- });
370
- }
371
- }, () => {
372
- LogUtils.Instance.info(UserMgr.TAG, "_login()失败", {});
373
-
374
- if (this.currentPlatformId === PlatformID.ID_QuickGame_HuaWei) {
375
- reject({
376
- type: LoginResultType.Type_LoginFail_ExitGame,
377
- });
378
- } else {
379
- reject({
380
- type: LoginResultType.Type_LoginFail_ContinueGame,
381
- });
382
- }
383
- });
384
- });
385
- }
386
-
387
243
  public async enhanceLogin(
388
244
  loginCallbackFunc: (result: EnhanceLoginResult) => void,
389
245
  ): Promise<void> {
@@ -468,6 +324,10 @@ export class UserMgr extends BaseMgr {
468
324
  }
469
325
  }
470
326
 
327
+ if (this.platformOpenId !== "") {
328
+ EventMgr.Instance.emit(FrameworkBase.Message.UserMgr_PlatformOpenId_Ready, this.platformOpenId);
329
+ }
330
+
471
331
  let needServerUserData: boolean = false;
472
332
  if (this.uid !== "" && this.userBehaviorPlatformInfo.supportServerUserData) {
473
333
  needServerUserData = true;