/** * rewards event handlers */ import { context } from '@/common/context'; import { rewardsEmitter, UseModalEventName, IUseModalEvent, PaymentResultEventName, UseModalResultEventName, InvokePaymentEventName, IInvokePaymentEvent, IUseModalResultEvent, UseUnloginModalResultEventName, InvokeUnloginModalEventName, InvokeSubscriptionEventName, UseSubscriptionResultEventName } from '@/common/rewards/reward-emitter'; import { notifyCustomEvent, onCustomEvent } from '@/common/utils'; import { uuidv4 as v4 } from '@jolibox/common'; /** * confirm jolicoin modal */ const UseCoinEventMap: Record = { USE_COIN: 'CONFIRM', ADS: 'FAILED', CANCEL: 'CANCEL' }; onCustomEvent('ON_JOLIBOX_JOLI_COIN_USE_RESULT', (data) => { console.log('onCustomEvent ON_JOLIBOX_JOLI_COIN_USE_RESULT', UseCoinEventMap[data.result], data.result); rewardsEmitter.emit(UseModalResultEventName, { useModalResult: UseCoinEventMap[data.result] }); }); rewardsEmitter.on( UseModalEventName, (type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', params: IUseModalEvent) => { // not support gem in h5 if (type == 'ADS-JOLI_GEM' || type == 'JOLI_GEM') { return; } notifyCustomEvent('JOLIBOX_JOLI_COIN_USE_EVENT', { JOLI_COIN: { quantity: params.quantity, balance: params.userCurrency.balance, enableAutoDeduct: params.enableAutoDeduct }, type: type as 'JOLI_COIN' | 'ADS-JOLI_COIN', sequenceId: params.sequenceId ?? v4() }); } ); /** * payment jolicoin modal */ onCustomEvent('ON_JOLIBOX_TOPUP_JOLI_COIN_RESULT', (data) => { rewardsEmitter.emit(PaymentResultEventName, { paymentResult: data.result == 'ADS' ? 'FAILED' : data.result, userJoliCoin: data.joliCoin, currency: 'JOLI_COIN' // h5 only support jolicoin, should get currency from data later }); }); rewardsEmitter.on( InvokePaymentEventName, async (type: 'JOLI_COIN' | 'ADS-JOLI_COIN' | 'JOLI_GEM' | 'ADS-JOLI_GEM', params: IInvokePaymentEvent) => { // 充值面板 if (type == 'ADS-JOLI_GEM' || type == 'JOLI_GEM') { return; } notifyCustomEvent('JOLIBOX_TOPUP_EVENT', { JOLI_COIN: { quantity: params.quantity, balance: params.userCurrency.balance, enableAutoDeduct: params.enableAutoDeduct }, type: type as 'JOLI_COIN' | 'ADS-JOLI_COIN', sequenceId: params.sequenceId ?? v4() }); } ); // unlogin modal onCustomEvent('ON_JOLIBOX_JOLI_UNLOGIN_MODAL_RESULT_EVENT', (data) => { rewardsEmitter.emit(UseUnloginModalResultEventName, { result: data.result == 'ADS' ? 'FAILED' : data.result, rewards: { claimedRewardValue: data.extra?.firstLogin?.reward?.rewardValue ?? 0, isFirstLogin: data.isFirstLogin ?? false } }); if (data.result === 'SUCCESS') { context.onEnvConfigChanged({ hostUserInfo: { isLogin: true } }); } }); rewardsEmitter.on(InvokeUnloginModalEventName, async (type) => { // 未登录面板 if (type == 'ADS-JOLI_GEM' || type == 'JOLI_GEM') { return; } notifyCustomEvent('JOLIBOX_JOLI_UNLOGIN_MODAL_EVENT', { sequenceId: v4(), type: type }); }); // subscription onCustomEvent('ON_JOLIBOX_SUB_RESULT_EVENT', (params) => { console.log('----on custom event ON_JOLIBOX_SUB_RESULT_EVENT-----', params); rewardsEmitter.emit(UseSubscriptionResultEventName, { result: params.result }); }); rewardsEmitter.on(InvokeSubscriptionEventName, async (type) => { console.log('----notify custom event-----', type); notifyCustomEvent('JOLIBOX_SUB_EVENT', { sequenceId: v4() }); });