@jolibox/implement 1.1.27 → 1.1.28

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.
@@ -1,9 +1,9 @@
1
1
  Invoking: npm run clean && npm run build:esm && tsc
2
2
 
3
- > @jolibox/implement@1.1.27 clean
3
+ > @jolibox/implement@1.1.28 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.1.27 build:esm
7
+ > @jolibox/implement@1.1.28 build:esm
8
8
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=esm
9
9
 
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@jolibox/implement",
3
3
  "description": "This project is Jolibox JS-SDk implement for Native && H5",
4
- "version": "1.1.27",
4
+ "version": "1.1.28",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@jolibox/common": "1.1.27",
10
- "@jolibox/types": "1.1.27",
11
- "@jolibox/native-bridge": "1.1.27",
12
- "@jolibox/ads": "1.1.27",
9
+ "@jolibox/common": "1.1.28",
10
+ "@jolibox/types": "1.1.28",
11
+ "@jolibox/native-bridge": "1.1.28",
12
+ "@jolibox/ads": "1.1.28",
13
13
  "localforage": "1.10.0",
14
14
  "@jolibox/ui": "1.0.0",
15
15
  "web-vitals": "4.2.4"
@@ -36,6 +36,17 @@ const env = Object.assign({}, nativeEnv?.() ?? defaultEnv);
36
36
 
37
37
  type MPType = 'game' | 'miniApp';
38
38
 
39
+ export enum SDKEnvironment {
40
+ jolibox = 'jolibox', // 默认
41
+ dlightek = 'dlightek', // 传音
42
+ funmax = 'funmax' // 小米
43
+ }
44
+
45
+ function hasMetaTag(name: string, content: string): boolean {
46
+ const metaTag = document?.head.querySelector(`meta[name="${name}"][content="${content}"]`);
47
+ return metaTag !== null;
48
+ }
49
+
39
50
  const wrapContext = () => {
40
51
  const { payloadJson, headerJson, signature } = env.schema.length ? parseUrlQuery(env.schema) : {};
41
52
  const defaultSessionId = `${env.deviceInfo.did}-${new Date().getTime()}`;
@@ -115,6 +126,14 @@ const wrapContext = () => {
115
126
  get language(): string {
116
127
  return env.deviceInfo.lang;
117
128
  },
129
+ get sdkEnvironment(): SDKEnvironment {
130
+ const hasDlightekMeta = hasMetaTag('dlightek', 'jolibox');
131
+ if (hasDlightekMeta) {
132
+ return SDKEnvironment.dlightek;
133
+ }
134
+ return SDKEnvironment.jolibox;
135
+ },
136
+
118
137
  onEnvConfigChanged: (newConfig: Partial<Env>) => {
119
138
  mergeWith(env, newConfig, mergeArray);
120
139
  },
@@ -57,9 +57,7 @@ export const createRewardFrequencyConfigFetcher = (rewardsHelper: RewardsHelper)
57
57
  '/api/fe-configs/web-common/global-config',
58
58
  {}
59
59
  );
60
- if (res.code !== 'SUCCESS') {
61
- return { joliCoinUseAndCharge: DefaltJoliCoinUseAndCharge };
62
- }
60
+
63
61
  rewardsEmitter.emit(UseModalFrequencyEventName, {
64
62
  joliCoinUseAndCharge: res.data?.joliCoinUseAndCharge || DefaltJoliCoinUseAndCharge
65
63
  });
package/src/h5/api/ads.ts CHANGED
@@ -8,12 +8,128 @@ import {
8
8
  createJolicoinRewardHandler,
9
9
  createJolicoinOnlyRewardHandler
10
10
  } from '@/common/rewards';
11
- import { JoliboxAdsForGame, type IAdsContext } from '@jolibox/ads';
12
- import { context } from '@/common/context';
11
+ import {
12
+ JoliboxAdsForGame,
13
+ type IAdsContext,
14
+ type IAdsInitParams,
15
+ type IAdBreakParams,
16
+ type IAdConfigParams,
17
+ type IAdUnitParams
18
+ } from '@jolibox/ads';
19
+ import { context, SDKEnvironment } from '@/common/context';
13
20
  import '../rewards';
14
21
 
15
- const commands = createCommands();
22
+ declare global {
23
+ interface Window {
24
+ h5sdk: any;
25
+ }
26
+ }
27
+
28
+ // 统一的广告处理器接口
29
+ interface IAdsHandler {
30
+ init(config?: IAdsInitParams): void;
31
+ adConfig(params: IAdConfigParams): void;
32
+ adBreak(params: IAdBreakParams): Promise<void> | void;
33
+ adUnit(params: IAdUnitParams): void;
34
+ getAdsInstance(): JoliboxAdsForGame | any;
35
+ }
36
+
37
+ // 具体处理器实现
38
+ class DlightekAdsHandler implements IAdsHandler {
39
+ constructor(private ads: any) {}
40
+
41
+ getAdsInstance(): any {
42
+ return this.ads;
43
+ }
44
+
45
+ init(_config?: IAdsInitParams): void {
46
+ // Dlightek 不需要初始化
47
+ }
48
+
49
+ adConfig(params: IAdConfigParams): void {
50
+ this.ads.adConfig(params);
51
+ }
52
+
53
+ adBreak(params: IAdBreakParams): void {
54
+ this.ads.adBreak(params);
55
+ }
56
+
57
+ adUnit(_params: IAdUnitParams): void {
58
+ // Dlightek 不需要 adUnit
59
+ }
60
+ }
61
+
62
+ class JoliboxAdsHandler implements IAdsHandler {
63
+ constructor(private ads: JoliboxAdsForGame) {}
64
+
65
+ getAdsInstance(): JoliboxAdsForGame {
66
+ return this.ads;
67
+ }
68
+
69
+ init(config?: IAdsInitParams): void {
70
+ this.ads.init(config);
71
+ }
72
+
73
+ adConfig(params: IAdConfigParams): void {
74
+ this.ads.adConfig(params);
75
+ }
76
+
77
+ async adBreak(params: IAdBreakParams): Promise<void> {
78
+ if (params.type === 'reward') {
79
+ try {
80
+ const rewardsTypes = await rewardsHelper.getRewardsTypes(httpClient);
81
+ await rewardsHelper.handleReward(rewardsTypes, params);
82
+ } catch (e) {
83
+ console.info('handleReward failed', e);
84
+ params.adBreakDone?.({
85
+ breakType: params.type,
86
+ breakFormat: params.type === 'reward' ? 'reward' : 'interstitial',
87
+ breakStatus: 'noAdPreloaded'
88
+ });
89
+ }
90
+ } else {
91
+ this.ads.adBreak(params);
92
+ }
93
+ }
94
+
95
+ adUnit(params: IAdUnitParams): void {
96
+ this.ads.adUnit(params);
97
+ }
98
+ }
99
+
100
+ class H5AdsManager {
101
+ private context: IAdsContext<'GAME'>;
102
+ private cacheHandler: IAdsHandler | null = null;
103
+
104
+ constructor(context: IAdsContext<'GAME'>) {
105
+ this.context = context;
106
+ }
107
+
108
+ private createAdsHandler(sdkEnvironment: SDKEnvironment): IAdsHandler {
109
+ if (this.cacheHandler) {
110
+ return this.cacheHandler;
111
+ }
112
+
113
+ switch (sdkEnvironment) {
114
+ case SDKEnvironment.dlightek: {
115
+ this.cacheHandler = new DlightekAdsHandler(window.h5sdk);
116
+ break;
117
+ }
118
+ default: {
119
+ const joliboxAds = new JoliboxAdsForGame(this.context);
120
+ this.cacheHandler = new JoliboxAdsHandler(joliboxAds);
121
+ break;
122
+ }
123
+ }
124
+ return this.cacheHandler;
125
+ }
16
126
 
127
+ getAdsHandler(sdkEnvironment: SDKEnvironment): IAdsHandler {
128
+ return this.createAdsHandler(sdkEnvironment);
129
+ }
130
+ }
131
+
132
+ const commands = createCommands();
17
133
  const adEventEmitter = new EventEmitter<{
18
134
  isAdShowing: [boolean];
19
135
  }>();
@@ -60,14 +176,17 @@ const handleUnlockSuccess = (params: { quantity: number; balance: number }) => {
60
176
  });
61
177
  };
62
178
 
63
- const ads = new JoliboxAdsForGame(adsContext);
64
- rewardsHelper.registerRewardHandler('ADS', createAdsRewardHandler(ads));
179
+ const adsManager = new H5AdsManager(adsContext);
180
+ const adsHandler = adsManager.getAdsHandler(context.sdkEnvironment);
181
+
182
+ rewardsHelper.registerRewardHandler('ADS', createAdsRewardHandler(adsHandler.getAdsInstance()));
65
183
  rewardsHelper.registerRewardHandler(
66
184
  'JOLI_COIN',
67
185
  createJolicoinRewardHandler(httpClient, {
68
186
  onUnlockSuccess: handleUnlockSuccess.bind(this)
69
187
  }) as unknown as (params?: unknown) => Promise<boolean>
70
188
  );
189
+
71
190
  rewardsHelper.registerRewardHandler(
72
191
  'JOLI_COIN_ONLY',
73
192
  createJolicoinOnlyRewardHandler(httpClient, {
@@ -79,32 +198,18 @@ adEventEmitter.on('isAdShowing', (isAdShowing) => {
79
198
  notifyCustomEvent('JOLIBOX_ADS_EVENT', { isAdShowing });
80
199
  });
81
200
 
82
- commands.registerCommand('AdsSDK.init', (config) => {
83
- ads.init(config);
201
+ commands.registerCommand('AdsSDK.init', (params?: IAdsInitParams) => {
202
+ adsHandler.init(params);
84
203
  });
85
204
 
86
- commands.registerCommand('AdsSDK.adConfig', (params) => {
87
- ads.adConfig(params);
205
+ commands.registerCommand('AdsSDK.adConfig', (params: IAdConfigParams) => {
206
+ adsHandler.adConfig(params);
88
207
  });
89
208
 
90
- commands.registerCommand('AdsSDK.adBreak', (params) => {
91
- if (params.type === 'reward') {
92
- rewardsHelper.getRewardsTypes(httpClient).then((rewardsTypes) => {
93
- rewardsHelper.handleReward(rewardsTypes, params).catch((e) => {
94
- // error should terminate the reward handler, invoke unlock failed
95
- console.info('handleReward failed', e);
96
- params.adBreakDone?.({
97
- breakType: params.type,
98
- breakFormat: params.type === 'reward' ? 'reward' : 'interstitial',
99
- breakStatus: 'noAdPreloaded'
100
- });
101
- });
102
- });
103
- } else {
104
- ads.adBreak(params);
105
- }
209
+ commands.registerCommand('AdsSDK.adBreak', (params: IAdBreakParams) => {
210
+ adsHandler.adBreak(params);
106
211
  });
107
212
 
108
- commands.registerCommand('AdsSDK.adUnit', (params) => {
109
- ads.adUnit(params);
213
+ commands.registerCommand('AdsSDK.adUnit', (params: IAdUnitParams) => {
214
+ adsHandler.adUnit(params);
110
215
  });