@jolibox/implement 1.1.38 → 1.1.40

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.
Files changed (48) hide show
  1. package/.rush/temp/package-deps_build.json +26 -15
  2. package/dist/common/rewards/registers/utils/coins/commands/index.d.ts +3 -0
  3. package/dist/common/rewards/registers/utils/coins/commands/use-jolicoin.d.ts +16 -0
  4. package/dist/common/rewards/registers/utils/coins/commands/use-payment.d.ts +16 -0
  5. package/dist/common/rewards/registers/utils/coins/commands/use-unlogin.d.ts +18 -0
  6. package/dist/common/rewards/registers/utils/coins/fetch-balance.d.ts +4 -0
  7. package/dist/common/rewards/registers/utils/coins/index.d.ts +3 -17
  8. package/dist/common/rewards/registers/utils/coins/rewards-command.d.ts +1 -0
  9. package/dist/common/rewards/registers/utils/common.d.ts +8 -0
  10. package/dist/common/rewards/registers/utils/event-listener.d.ts +1 -0
  11. package/dist/common/rewards/reward-emitter.d.ts +30 -4
  12. package/dist/common/utils/index.d.ts +19 -0
  13. package/dist/index.js +25 -25
  14. package/dist/index.native.js +132 -46
  15. package/dist/native/api/login.d.ts +20 -0
  16. package/dist/native/rewards/check-frequency.d.ts +13 -16
  17. package/dist/native/rewards/index.d.ts +3 -1
  18. package/dist/native/rewards/ui/payment-modal.d.ts +1 -0
  19. package/dist/native/rewards/ui/unlogin-modal.d.ts +1 -0
  20. package/dist/native/rewards/ui/use-modal.d.ts +1 -0
  21. package/dist/native/rewards/ui/utils.d.ts +6 -0
  22. package/implement.build.log +2 -2
  23. package/package.json +5 -5
  24. package/src/common/rewards/fetch-reward.ts +6 -3
  25. package/src/common/rewards/registers/use-jolicoin-only.ts +13 -13
  26. package/src/common/rewards/registers/use-jolicoin.ts +15 -11
  27. package/src/common/rewards/registers/utils/coins/commands/index.ts +3 -0
  28. package/src/common/rewards/registers/utils/coins/commands/use-jolicoin.ts +67 -0
  29. package/src/common/rewards/registers/utils/coins/commands/use-payment.ts +85 -0
  30. package/src/common/rewards/registers/utils/coins/commands/use-unlogin.ts +96 -0
  31. package/src/common/rewards/registers/utils/coins/fetch-balance.ts +15 -0
  32. package/src/common/rewards/registers/utils/coins/index.ts +45 -99
  33. package/src/common/rewards/registers/utils/coins/rewards-command.ts +3 -0
  34. package/src/common/rewards/registers/utils/common.ts +8 -0
  35. package/src/common/rewards/registers/utils/event-listener.ts +13 -0
  36. package/src/common/rewards/reward-emitter.ts +33 -4
  37. package/src/common/rewards/reward-helper.ts +3 -3
  38. package/src/common/utils/index.ts +20 -0
  39. package/src/h5/bootstrap/index.ts +20 -2
  40. package/src/h5/rewards/index.ts +29 -1
  41. package/src/native/api/ads.ts +17 -7
  42. package/src/native/api/login.ts +62 -32
  43. package/src/native/rewards/check-frequency.ts +46 -84
  44. package/src/native/rewards/index.ts +3 -370
  45. package/src/native/rewards/ui/payment-modal.ts +254 -0
  46. package/src/native/rewards/ui/unlogin-modal.ts +121 -0
  47. package/src/native/rewards/ui/use-modal.ts +104 -0
  48. package/src/native/rewards/ui/utils.ts +25 -0
@@ -1,24 +1,20 @@
1
1
  import { IHttpClient } from '@/common/http';
2
2
  import { context } from '@/common/context';
3
3
  import { uuidv4 } from '@jolibox/common';
4
- import {
5
- rewardsEmitter,
6
- UnlockOptionsEventName,
7
- IUnlockOptionsEvent,
8
- InvokePaymentEventName,
9
- PaymentResultEventName,
10
- IPaymentEvent,
11
- IUseModalResultEvent,
12
- UseModalEventName,
13
- UseModalResultEventName
14
- } from '@/common/rewards/reward-emitter';
4
+ import { UnlockOptionsEventName, IUnlockOptionsEvent } from '@/common/rewards/reward-emitter';
15
5
  import { IAdBreakParams } from '@/common/ads';
16
- import { joliCoinIsEnough } from '../index';
17
- import { createEventPromiseHandler, EventPromiseHandler } from '../event-listener';
18
- import { IJoliCoin } from '@/common/rewards/type';
6
+ import { EventPromiseHandler } from '../event-listener';
19
7
 
20
- type IPaymentResult = 'SUCCESS' | 'FAILED' | 'CANCEL';
21
- type IUseModalResult = 'CONFIRM' | 'CANCEL' | 'FAILED';
8
+ import {
9
+ createInitiateAndAwaitPayment,
10
+ registerUseJolicoinCommand,
11
+ registerUsePaymentCommand,
12
+ registerUseUnloginCommand,
13
+ createShowUnlockWithJolicoinModal,
14
+ createShowUnloginModal
15
+ } from './commands';
16
+ import { rewardsCommands } from './rewards-command';
17
+ import { RewardsCommandType } from '@jolibox/types';
22
18
 
23
19
  interface IJolicoinUnlockRes {
24
20
  code: 'SUCCESS' | 'BALANCE_NOT_ENOUGH' | 'EPISODE_LOCK_JUMP' | 'EPISODE_UNLOCK_ALREADY';
@@ -30,61 +26,8 @@ interface IJolicoinUnlockRes {
30
26
  };
31
27
  }
32
28
 
33
- export const createInitiateAndAwaitPayment = (
34
- type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
35
- buttons: {
36
- confirmButtonText: string;
37
- cancelButtonText: string;
38
- }
39
- ) => {
40
- return async (params: { userJoliCoin: IJoliCoin; joliCoinQuantity: number }) => {
41
- const paymentHandler = createEventPromiseHandler<IPaymentEvent, typeof PaymentResultEventName>(
42
- rewardsEmitter,
43
- PaymentResultEventName
44
- );
45
- rewardsEmitter.emit(InvokePaymentEventName, type, {
46
- userJoliCoin: params.userJoliCoin,
47
- joliCoinQuantity: params.joliCoinQuantity,
48
- enableAutoDeduct: params.userJoliCoin.enableAutoDeduct,
49
- confirmButtonText: buttons.confirmButtonText,
50
- cancelButtonText: buttons.cancelButtonText
51
- });
52
-
53
- // Wait for payment result
54
- const paymentResult = await paymentHandler.getFreshData();
55
- return paymentResult.paymentResult;
56
- };
57
- };
58
-
59
- export const createShowUnlockWithJolicoinModal = (
60
- type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
61
- buttons: {
62
- confirmButtonText: string;
63
- cancelButtonText: string;
64
- }
65
- ) => {
66
- return async (params: { enableAutoDeduct: boolean; userJoliCoin: IJoliCoin; joliCoinQuantity: number }) => {
67
- const modalHandler = createEventPromiseHandler<IUseModalResultEvent, typeof UseModalResultEventName>(
68
- rewardsEmitter,
69
- UseModalResultEventName
70
- );
71
- rewardsEmitter.emit(UseModalEventName, type, {
72
- enableAutoDeduct: params.enableAutoDeduct,
73
- userJoliCoin: params.userJoliCoin,
74
- joliCoinQuantity: params.joliCoinQuantity,
75
- confirmButtonText: buttons.confirmButtonText,
76
- cancelButtonText: buttons.cancelButtonText
77
- });
78
- const modalResult = await modalHandler.getFreshData();
79
- return modalResult.useModalResult;
80
- };
81
- };
82
-
83
- const checkIfCancel = (result: IPaymentResult | IUseModalResult) => {
84
- return result == 'CANCEL';
85
- };
86
-
87
29
  export const createCommonJolicoinRewardHandler = (
30
+ type: 'JOLI_COIN' | 'ADS-JOLI_COIN',
88
31
  httpClient: IHttpClient,
89
32
  {
90
33
  handlers: {
@@ -92,7 +35,8 @@ export const createCommonJolicoinRewardHandler = (
92
35
  handleUnlockFailed,
93
36
  unlockOptionsHandler,
94
37
  initiateAndAwaitPayment,
95
- showUnlockWithJolicoinModal
38
+ showUnlockWithJolicoinModal,
39
+ showUnloginModal
96
40
  }
97
41
  }: {
98
42
  handlers: {
@@ -101,43 +45,43 @@ export const createCommonJolicoinRewardHandler = (
101
45
  unlockOptionsHandler: EventPromiseHandler<IUnlockOptionsEvent, typeof UnlockOptionsEventName>;
102
46
  initiateAndAwaitPayment: ReturnType<typeof createInitiateAndAwaitPayment>;
103
47
  showUnlockWithJolicoinModal: ReturnType<typeof createShowUnlockWithJolicoinModal>;
48
+ showUnloginModal: ReturnType<typeof createShowUnloginModal>;
104
49
  };
105
50
  }
106
51
  ) => {
52
+ // register commands
53
+ registerUseUnloginCommand(type, {
54
+ httpClient: httpClient,
55
+ showUnloginModal: showUnloginModal
56
+ });
57
+ registerUsePaymentCommand(type, {
58
+ httpClient: httpClient,
59
+ initiateAndAwaitPayment: initiateAndAwaitPayment
60
+ });
61
+ registerUseJolicoinCommand(type, {
62
+ showUnlockWithJolicoinModal: showUnlockWithJolicoinModal
63
+ });
107
64
  return async (params: IAdBreakParams) => {
108
65
  try {
109
- let shouldSkipConfirm = false;
110
- const unlockOptions = await unlockOptionsHandler.getData();
111
- const joliCoinQuantity =
112
- unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0]
113
- .joliCoinQuantity ?? 0;
114
- if (!joliCoinIsEnough(unlockOptions?.options || [], unlockOptions?.userJoliCoin)) {
115
- const paymentResult = await initiateAndAwaitPayment({
116
- userJoliCoin: unlockOptions?.userJoliCoin,
117
- joliCoinQuantity
118
- });
66
+ let result = true;
119
67
 
120
- if (paymentResult !== 'SUCCESS') {
121
- handleUnlockFailed?.(params);
122
- return false;
123
- }
68
+ const commands = [
69
+ `Rewards.${type}.useUnloginModal`,
70
+ `Rewards.${type}.usePayment`,
71
+ `Rewards.${type}.useJolicoin`
72
+ ];
124
73
 
125
- shouldSkipConfirm = true;
74
+ for (const command of commands) {
75
+ const commandResult = await rewardsCommands.executeCommand(command as RewardsCommandType);
76
+ result = result && commandResult.result !== 'FAILED';
77
+ if (commandResult.result !== 'CONTINUE') {
78
+ break;
79
+ }
126
80
  }
127
81
 
128
- const enableAutoDeduct = !!unlockOptions?.userJoliCoin?.enableAutoDeduct;
129
-
130
- if (!shouldSkipConfirm) {
131
- const shouldUnlock = await showUnlockWithJolicoinModal({
132
- enableAutoDeduct,
133
- userJoliCoin: unlockOptions?.userJoliCoin,
134
- joliCoinQuantity
135
- });
136
-
137
- if (shouldUnlock !== 'CONFIRM') {
138
- handleUnlockFailed?.(params);
139
- return false;
140
- }
82
+ if (!result) {
83
+ handleUnlockFailed?.(params);
84
+ return false;
141
85
  }
142
86
 
143
87
  const unlockWithJolicoin = await httpClient.post<IJolicoinUnlockRes>('/api/joli-coin/unlock', {
@@ -150,6 +94,8 @@ export const createCommonJolicoinRewardHandler = (
150
94
  }
151
95
  }
152
96
  });
97
+
98
+ console.log('-----unlockWithJolicoin request result-----', unlockWithJolicoin);
153
99
  if (unlockWithJolicoin.code == 'SUCCESS') {
154
100
  params.adBreakDone?.({
155
101
  breakType: params.type,
@@ -0,0 +1,3 @@
1
+ import { createCommands } from '@jolibox/common';
2
+
3
+ export const rewardsCommands = createCommands();
@@ -0,0 +1,8 @@
1
+ import { IUnlockOptionsEvent, UnlockOptionsEventName } from '../../reward-emitter';
2
+ import { createEventPromiseHandler } from './event-listener';
3
+ import { rewardsEmitter } from '../../reward-emitter';
4
+
5
+ export const unlockOptionsHandler = createEventPromiseHandler<
6
+ IUnlockOptionsEvent,
7
+ typeof UnlockOptionsEventName
8
+ >(rewardsEmitter, UnlockOptionsEventName);
@@ -57,6 +57,19 @@ export function createEventPromiseHandler<T, E extends string = string>(
57
57
  dataPromise = null;
58
58
  createDataPromise();
59
59
  return dataPromise!;
60
+ },
61
+
62
+ updateData: (newData: Partial<T>) => {
63
+ if (!cachedData) {
64
+ return;
65
+ }
66
+ cachedData = { ...cachedData, ...newData };
67
+ dataPromise = Promise.resolve(cachedData);
68
+ if (resolveData) {
69
+ resolveData(cachedData);
70
+ resolveData = null;
71
+ }
72
+ return cachedData;
60
73
  }
61
74
  };
62
75
  }
@@ -15,6 +15,8 @@ export const PaymentResultEventName = 'ON_PAYMENT_RESULT' as const;
15
15
  export const UseModalEventName = 'ON_USE_MODAL_EVENT' as const;
16
16
  export const UseModalResultEventName = 'ON_USE_MODAL_RESULT' as const;
17
17
  export const UseModalFrequencyEventName = 'ON_USE_MODAL_FREQUENCY' as const;
18
+ export const UseUnloginModalResultEventName = 'ON_USE_UNLOGIN_MODAL_EVENT' as const;
19
+ export const InvokeUnloginModalEventName = 'INVOKE_UNLOGIN_MODAL_EVENT' as const;
18
20
 
19
21
  type IPaymentResult = 'SUCCESS' | 'FAILED' | 'CANCEL';
20
22
  export interface IPaymentEvent {
@@ -47,21 +49,41 @@ export interface IUseModalEvent {
47
49
  sequenceId?: string;
48
50
  }
49
51
 
52
+ export interface IUseUnloginModalEvent {
53
+ confirmButtonText: string;
54
+ cancelButtonText: string;
55
+ }
56
+
50
57
  export interface IUseModalResultEvent {
51
58
  useModalResult: 'CONFIRM' | 'CANCEL' | 'FAILED';
52
59
  }
53
60
 
61
+ export interface IUseUnloginModalResultEvent {
62
+ result: 'SUCCESS' | 'CANCEL' | 'ADS' | 'FAILED' | 'NOT_SUPPORT';
63
+ rewards?: {
64
+ jolicoin?: {
65
+ claimedRewardValue: number;
66
+ isFirstLogin: boolean;
67
+ };
68
+ };
69
+ }
70
+
54
71
  export interface IUseModalFrequencyConfig {
55
72
  joliCoinUseAndCharge: {
56
73
  useJolicoin: {
57
- dailyMaxPopUps: number;
58
- minInterval: number;
74
+ dailyMaxPopUps?: number;
75
+ minInterval?: number;
59
76
  };
60
77
  charge: {
61
- dailyMaxPopUps: number;
62
- minInterval: number;
78
+ dailyMaxPopUps?: number;
79
+ minInterval?: number;
63
80
  };
64
81
  };
82
+ loginGuide: {
83
+ show: boolean;
84
+ dailyMaxPopUps?: number;
85
+ minInterval?: number;
86
+ };
65
87
  }
66
88
 
67
89
  export interface RewardsEventMap extends Record<string, unknown[]> {
@@ -71,6 +93,8 @@ export interface RewardsEventMap extends Record<string, unknown[]> {
71
93
  [UseModalResultEventName]: [IUseModalResultEvent];
72
94
  [UseModalEventName]: ['JOLI_COIN' | 'ADS-JOLI_COIN', IUseModalEvent];
73
95
  [UseModalFrequencyEventName]: [IUseModalFrequencyConfig];
96
+ [InvokeUnloginModalEventName]: ['JOLI_COIN' | 'ADS-JOLI_COIN', IUseUnloginModalEvent];
97
+ [UseUnloginModalResultEventName]: [IUseUnloginModalResultEvent];
74
98
  }
75
99
 
76
100
  export const originalRewardsEmitter = new EventEmitter<RewardsEventMap>();
@@ -106,3 +130,8 @@ export const DefaltJoliCoinUseAndCharge = {
106
130
  minInterval: 8 * 60 * 60 * 1000
107
131
  }
108
132
  };
133
+
134
+ export const DefaltLoginGuide = {
135
+ show: true,
136
+ minInterval: 8 * 60 * 60 * 1000
137
+ };
@@ -2,7 +2,7 @@ export type RewardType = 'ADS' | 'JOLI_COIN' | 'JOLI_COIN_ONLY';
2
2
 
3
3
  import { context } from '../context';
4
4
  import type { AdsRewardsHandler } from './registers/use-ads';
5
- import { IUseModalFrequencyConfig, DefaltJoliCoinUseAndCharge } from './reward-emitter';
5
+ import { IUseModalFrequencyConfig, DefaltJoliCoinUseAndCharge, DefaltLoginGuide } from './reward-emitter';
6
6
 
7
7
  export interface RewardHandlerMap {
8
8
  ADS: AdsRewardsHandler;
@@ -57,7 +57,7 @@ export function createRewardsHelper() {
57
57
  return config;
58
58
  } catch (e) {
59
59
  console.error(`getRewardOptions error:`, e);
60
- return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge };
60
+ return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge, loginGuide: DefaltLoginGuide };
61
61
  }
62
62
  };
63
63
  },
@@ -70,7 +70,7 @@ export function createRewardsHelper() {
70
70
  },
71
71
  async getRewardFrequencyConfig(...args: unknown[]): Promise<IUseModalFrequencyConfig> {
72
72
  if (!rewardFrequencyConfigFetcher) {
73
- return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge };
73
+ return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge, loginGuide: DefaltLoginGuide };
74
74
  }
75
75
  return await rewardFrequencyConfigFetcher(...args);
76
76
  }
@@ -5,6 +5,8 @@ const JOLIBOX_CUSTOM_ADS_EVENT_TYPE = 'JOLIBOX_ADS_EVENT';
5
5
  const JOLIBOX_CUSTOM_REWARDS_EVENT_TYPE = 'JOLIBOX_CUSTOM_REWARDS_EVENT';
6
6
  const JOLIBOX_TOPUP_EVENT = 'JOLIBOX_TOPUP_EVENT';
7
7
  const JOLIBOX_JOLI_COIN_USE_EVENT = 'JOLIBOX_JOLI_COIN_USE_EVENT';
8
+ const JOLIBOX_JOLI_UNLOGIN_MODAL_EVENT = 'JOLIBOX_JOLI_UNLOGIN_MODAL_EVENT'; // unlogin modal event, triggered when unlogin
9
+ // unlogin modal result event success.
8
10
 
9
11
  interface JoliboxCustomEvent {
10
12
  [JOLIBOX_CUSTOM_ADS_EVENT_TYPE]: {
@@ -35,6 +37,10 @@ interface JoliboxCustomEvent {
35
37
  type: 'ADS-JOLI_COIN' | 'JOLI_COIN';
36
38
  sequenceId: string;
37
39
  };
40
+ [JOLIBOX_JOLI_UNLOGIN_MODAL_EVENT]: {
41
+ sequenceId: string;
42
+ type: 'ADS-JOLI_COIN' | 'JOLI_COIN';
43
+ };
38
44
  }
39
45
 
40
46
  const notifyCustomEvent = <T extends keyof JoliboxCustomEvent>(eventName: T, data: JoliboxCustomEvent[T]) => {
@@ -58,6 +64,7 @@ const notifyCustomEvent = <T extends keyof JoliboxCustomEvent>(eventName: T, dat
58
64
 
59
65
  const ON_JOLIBOX_TOPUP_JOLI_COIN_RESULT = 'ON_JOLIBOX_TOPUP_JOLI_COIN_RESULT';
60
66
  const ON_JOLIBOX_JOLI_COIN_USE_RESULT = 'ON_JOLIBOX_JOLI_COIN_USE_RESULT';
67
+ const ON_JOLIBOX_JOLI_UNLOGIN_MODAL_RESULT_EVENT = 'ON_JOLIBOX_JOLI_UNLOGIN_MODAL_RESULT_EVENT';
61
68
 
62
69
  interface ReceivedJoliboxCustomEvent {
63
70
  [ON_JOLIBOX_TOPUP_JOLI_COIN_RESULT]: {
@@ -73,6 +80,19 @@ interface ReceivedJoliboxCustomEvent {
73
80
  sequenceId: string;
74
81
  result: 'USE_COIN' | 'ADS' | 'CANCEL';
75
82
  };
83
+ [ON_JOLIBOX_JOLI_UNLOGIN_MODAL_RESULT_EVENT]: {
84
+ sequenceId: string;
85
+ result: 'SUCCESS' | 'CANCEL' | 'ADS' | 'FAILED' | 'NOT_SUPPORT';
86
+ extra?: {
87
+ firstLogin?: {
88
+ reward: {
89
+ rewardValue: number;
90
+ rewardType: 'JOLI_COIN';
91
+ };
92
+ };
93
+ };
94
+ isFirstLogin?: boolean;
95
+ };
76
96
  }
77
97
 
78
98
  type ReceivedJoliboxCustomEventResult = keyof ReceivedJoliboxCustomEvent;
@@ -3,7 +3,7 @@ import { taskTracker, track } from '../report';
3
3
  import { onFCP, onLCP, onTTFB } from 'web-vitals';
4
4
  import { context } from '@/common/context';
5
5
  import { httpClientManager } from '../http';
6
- import { IResponse } from '@jolibox/ads';
6
+ import { StandardResponse } from '@jolibox/types';
7
7
  import { reportError } from '@/common/report/errors/report';
8
8
 
9
9
  function trackPerformance() {
@@ -32,9 +32,27 @@ function trackPerformance() {
32
32
  });
33
33
  }
34
34
 
35
+ async function checkSession(): Promise<boolean> {
36
+ const httpClient = httpClientManager.create();
37
+ try {
38
+ const response = await httpClient.get<StandardResponse<void>>('/api/users/info');
39
+ if (response.code !== 'SUCCESS' || !response.data) {
40
+ return false;
41
+ }
42
+ return true;
43
+ } catch (error) {
44
+ reportError(new InternalGlobalJSError(error as Error));
45
+ return false;
46
+ }
47
+ }
48
+
35
49
  function addDomContentLoaded() {
36
50
  hostEmitter.on('onDocumentReady', async (startTime: number) => {
37
- hostEmitter.emit('LifecycleEvent.onReady', { isLogin: false });
51
+ const isLogin = await checkSession();
52
+ context.onEnvConfigChanged({ hostUserInfo: { isLogin } });
53
+ hostEmitter.emit('LifecycleEvent.onReady', {
54
+ ...(context.hostUserInfo ? context.hostUserInfo : { isLogin: false })
55
+ });
38
56
 
39
57
  if (context.mpType === 'game') {
40
58
  taskTracker.start(Date.now() - startTime);
@@ -2,6 +2,7 @@
2
2
  * rewards event handlers
3
3
  */
4
4
 
5
+ import { context } from '@/common/context';
5
6
  import {
6
7
  rewardsEmitter,
7
8
  UseModalEventName,
@@ -10,7 +11,9 @@ import {
10
11
  UseModalResultEventName,
11
12
  InvokePaymentEventName,
12
13
  IInvokePaymentEvent,
13
- IUseModalResultEvent
14
+ IUseModalResultEvent,
15
+ UseUnloginModalResultEventName,
16
+ InvokeUnloginModalEventName
14
17
  } from '@/common/rewards/reward-emitter';
15
18
  import { notifyCustomEvent, onCustomEvent } from '@/common/utils';
16
19
  import { uuidv4 as v4 } from '@jolibox/common';
@@ -67,3 +70,28 @@ rewardsEmitter.on(
67
70
  });
68
71
  }
69
72
  );
73
+
74
+ // unlogin modal
75
+ onCustomEvent('ON_JOLIBOX_JOLI_UNLOGIN_MODAL_RESULT_EVENT', (data) => {
76
+ rewardsEmitter.emit(UseUnloginModalResultEventName, {
77
+ result: data.result == 'ADS' ? 'FAILED' : data.result,
78
+ rewards: {
79
+ jolicoin: {
80
+ claimedRewardValue: data.extra?.firstLogin?.reward?.rewardValue ?? 0,
81
+ isFirstLogin: data.isFirstLogin ?? false
82
+ }
83
+ }
84
+ });
85
+
86
+ if (data.result === 'SUCCESS') {
87
+ context.onEnvConfigChanged({ hostUserInfo: { isLogin: true } });
88
+ }
89
+ });
90
+
91
+ rewardsEmitter.on(InvokeUnloginModalEventName, async (type) => {
92
+ // 未登录面板
93
+ notifyCustomEvent('JOLIBOX_JOLI_UNLOGIN_MODAL_EVENT', {
94
+ sequenceId: v4(),
95
+ type: type
96
+ });
97
+ });
@@ -142,18 +142,28 @@ const adConfig = createSyncAPI('adConfig', {
142
142
  }
143
143
  });
144
144
 
145
+ let rewardLocked = false;
145
146
  const adBreak = createSyncAPI('adBreak', {
146
147
  implement: (params: IAdBreakParams) => {
147
148
  if (params.type === 'reward') {
149
+ if (rewardLocked) {
150
+ return;
151
+ }
152
+ rewardLocked = true;
148
153
  rewardsHelper.getRewardsTypes(httpClient).then((rewardsTypes) => {
149
- rewardsHelper.handleReward(rewardsTypes, params).catch((e) => {
150
- console.info('handleReward failed in native', e);
151
- params.adBreakDone?.({
152
- breakType: params.type,
153
- breakFormat: params.type === 'reward' ? 'reward' : 'interstitial',
154
- breakStatus: 'noAdPreloaded'
154
+ rewardsHelper
155
+ .handleReward(rewardsTypes, params)
156
+ .catch((e) => {
157
+ console.info('handleReward failed in native', e);
158
+ params.adBreakDone?.({
159
+ breakType: params.type,
160
+ breakFormat: params.type === 'reward' ? 'reward' : 'interstitial',
161
+ breakStatus: 'noAdPreloaded'
162
+ });
163
+ })
164
+ .finally(() => {
165
+ rewardLocked = false;
155
166
  });
156
- });
157
167
  });
158
168
  } else {
159
169
  ads.adBreak(params);
@@ -23,16 +23,74 @@ registerCanIUse('checkSession', {
23
23
  }
24
24
  });
25
25
 
26
- const loginDeferredMap = new Map<string, Deferred<{ isLogin: boolean; token?: string }>>();
26
+ const loginDeferredMap = new Map<
27
+ string,
28
+ Deferred<{ isLogin: boolean; token?: string; isFirstLogin?: boolean; extra?: LoginExtra }>
29
+ >();
27
30
 
28
- onNative('onLoginStateChange', ({ isLogin, token, uuid }) => {
31
+ onNative('onLoginStateChange', ({ isLogin, token, uuid, isFirstLogin, extra }) => {
29
32
  const deferred = loginDeferredMap.get(uuid);
30
33
  if (deferred) {
31
- deferred.resolve({ isLogin, token });
34
+ deferred.resolve({ isLogin, token, isFirstLogin, extra });
32
35
  loginDeferredMap.delete(uuid);
33
36
  }
34
37
  });
35
38
 
39
+ type LoginExtra = {
40
+ firstLogin?: {
41
+ reward: {
42
+ rewardType: 'JOLI_COIN';
43
+ rewardValue: number;
44
+ };
45
+ };
46
+ };
47
+
48
+ export const loginImplement = async (
49
+ params: { skipLogin?: boolean; triggerFrom?: string },
50
+ needExtra = false
51
+ ): Promise<{
52
+ isLogin: boolean;
53
+ token?: string;
54
+ isFirstLogin?: boolean;
55
+ extra?: LoginExtra;
56
+ }> => {
57
+ const {
58
+ data: { isLogin }
59
+ } = await applyNative('checkLoginAsync');
60
+ if (isLogin) {
61
+ return { isLogin: true, token: context.hostUserInfo?.token };
62
+ }
63
+
64
+ const { skipLogin, triggerFrom } = params ?? {};
65
+ const res = invokeNative('loginSync', {
66
+ skipLogin,
67
+ triggerFrom
68
+ });
69
+
70
+ const {
71
+ data: { uuid: loginUUID }
72
+ } = res;
73
+ if (!loginUUID) {
74
+ throw createAPIError({
75
+ code: -1,
76
+ msg: 'login failed'
77
+ });
78
+ }
79
+
80
+ const deferred = new Deferred<{
81
+ isLogin: boolean;
82
+ token?: string;
83
+ isFirstLogin?: boolean;
84
+ extra?: LoginExtra;
85
+ }>();
86
+ loginDeferredMap.set(loginUUID, deferred);
87
+ const loginRes = await deferred.promise;
88
+ context.onEnvConfigChanged({ hostUserInfo: loginRes });
89
+ const { extra, ...rest } = loginRes;
90
+ hostEmitter.emit('onLoginComplete', rest);
91
+ return needExtra ? loginRes : rest;
92
+ };
93
+
36
94
  export const login = createAPI('login', {
37
95
  paramsSchema: t.tuple(
38
96
  t
@@ -43,35 +101,7 @@ export const login = createAPI('login', {
43
101
  .optional()
44
102
  ),
45
103
  async implement(params) {
46
- const {
47
- data: { isLogin }
48
- } = await applyNative('checkLoginAsync');
49
- if (isLogin) {
50
- return { isLogin: true, token: context.hostUserInfo?.token };
51
- }
52
-
53
- const { skipLogin, triggerFrom } = params ?? {};
54
- const res = invokeNative('loginSync', {
55
- skipLogin,
56
- triggerFrom
57
- });
58
-
59
- const {
60
- data: { uuid: loginUUID }
61
- } = res;
62
- if (!loginUUID) {
63
- throw createAPIError({
64
- code: -1,
65
- msg: 'login failed'
66
- });
67
- }
68
-
69
- const deferred = new Deferred<{ isLogin: boolean; token?: string }>();
70
- loginDeferredMap.set(loginUUID, deferred);
71
- const loginRes = await deferred.promise;
72
- context.onEnvConfigChanged({ hostUserInfo: loginRes });
73
- hostEmitter.emit('onLoginComplete', loginRes);
74
- return loginRes;
104
+ return await loginImplement(params ?? {});
75
105
  }
76
106
  });
77
107