@jolibox/implement 1.1.26 → 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.26 clean
3
+ > @jolibox/implement@1.1.28 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.1.26 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.26",
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.26",
10
- "@jolibox/types": "1.1.26",
11
- "@jolibox/native-bridge": "1.1.26",
12
- "@jolibox/ads": "1.1.26",
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
  }>();
@@ -25,6 +141,14 @@ const adsContext: IAdsContext<'GAME'> = {
25
141
  checkNetwork: () => httpClientManager.getNetworkStatus(),
26
142
  track,
27
143
  eventEmitter: adEventEmitter,
144
+ getWindowInfo: () => ({
145
+ width: window.innerWidth * window.devicePixelRatio,
146
+ height: window.innerHeight * window.devicePixelRatio,
147
+ statusBarHeight: 0
148
+ }),
149
+ handleLinkClicked: (link) => {
150
+ window.open(link, '_blank');
151
+ },
28
152
  getContextInfo: () => {
29
153
  return {
30
154
  hostAppId: context.hostInfo?.aid ?? '1',
@@ -40,7 +164,8 @@ const adsContext: IAdsContext<'GAME'> = {
40
164
  deviceModel: context.deviceInfo.model,
41
165
  osType: platform.isAndroid ? 'ANDROID' : platform.isiOS ? 'IOS' : 'PC',
42
166
  runtimeType: 'WEB',
43
- platform: 1000 // WebSDK
167
+ platform: 1000, // WebSDK
168
+ joliSource: context.joliSource ?? ''
44
169
  };
45
170
  }
46
171
  };
@@ -51,14 +176,17 @@ const handleUnlockSuccess = (params: { quantity: number; balance: number }) => {
51
176
  });
52
177
  };
53
178
 
54
- const ads = new JoliboxAdsForGame(adsContext);
55
- 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()));
56
183
  rewardsHelper.registerRewardHandler(
57
184
  'JOLI_COIN',
58
185
  createJolicoinRewardHandler(httpClient, {
59
186
  onUnlockSuccess: handleUnlockSuccess.bind(this)
60
187
  }) as unknown as (params?: unknown) => Promise<boolean>
61
188
  );
189
+
62
190
  rewardsHelper.registerRewardHandler(
63
191
  'JOLI_COIN_ONLY',
64
192
  createJolicoinOnlyRewardHandler(httpClient, {
@@ -70,32 +198,18 @@ adEventEmitter.on('isAdShowing', (isAdShowing) => {
70
198
  notifyCustomEvent('JOLIBOX_ADS_EVENT', { isAdShowing });
71
199
  });
72
200
 
73
- commands.registerCommand('AdsSDK.init', (config) => {
74
- ads.init(config);
201
+ commands.registerCommand('AdsSDK.init', (params?: IAdsInitParams) => {
202
+ adsHandler.init(params);
75
203
  });
76
204
 
77
- commands.registerCommand('AdsSDK.adConfig', (params) => {
78
- ads.adConfig(params);
205
+ commands.registerCommand('AdsSDK.adConfig', (params: IAdConfigParams) => {
206
+ adsHandler.adConfig(params);
79
207
  });
80
208
 
81
- commands.registerCommand('AdsSDK.adBreak', (params) => {
82
- if (params.type === 'reward') {
83
- rewardsHelper.getRewardsTypes(httpClient).then((rewardsTypes) => {
84
- rewardsHelper.handleReward(rewardsTypes, params).catch((e) => {
85
- // error should terminate the reward handler, invoke unlock failed
86
- console.info('handleReward failed', e);
87
- params.adBreakDone?.({
88
- breakType: params.type,
89
- breakFormat: params.type === 'reward' ? 'reward' : 'interstitial',
90
- breakStatus: 'noAdPreloaded'
91
- });
92
- });
93
- });
94
- } else {
95
- ads.adBreak(params);
96
- }
209
+ commands.registerCommand('AdsSDK.adBreak', (params: IAdBreakParams) => {
210
+ adsHandler.adBreak(params);
97
211
  });
98
212
 
99
- commands.registerCommand('AdsSDK.adUnit', (params) => {
100
- ads.adUnit(params);
213
+ commands.registerCommand('AdsSDK.adUnit', (params: IAdUnitParams) => {
214
+ adsHandler.adUnit(params);
101
215
  });
@@ -1,6 +1,7 @@
1
1
  import { IHttpClient } from '@/common/http';
2
2
  import { xUserAgent } from '../../common/http/xua';
3
3
  import { context } from '@/common/context';
4
+ import { platform } from '@jolibox/common';
4
5
 
5
6
  declare global {
6
7
  interface Window {
@@ -131,7 +132,9 @@ class JoliboxHttpClient implements IHttpClient {
131
132
  headers ?? {},
132
133
  { 'Content-Type': 'application/json' },
133
134
  xua ? { 'x-user-agent': xua } : {},
134
- joliSource ? { 'x-joli-source': joliSource } : {}
135
+ joliSource ? { 'x-joli-source': joliSource } : {},
136
+ { 'x-os-type': platform.isAndroid ? 'ANDROID' : platform.isiOS ? 'IOS' : 'PC' },
137
+ { 'x-runtime-type': 'WEB' }
135
138
  );
136
139
  const response = await fetch(url, {
137
140
  method: 'POST',
@@ -54,6 +54,15 @@ const adsContext: IAdsContext<'GAME'> = {
54
54
  checkNetwork: checkNetworkStatus,
55
55
  track,
56
56
  eventEmitter: adEventEmitter,
57
+ // TODO: @Deng Xue
58
+ getWindowInfo: () => ({
59
+ width: window.innerWidth * window.devicePixelRatio,
60
+ height: window.innerHeight * window.devicePixelRatio,
61
+ statusBarHeight: 30
62
+ }),
63
+ handleLinkClicked: (link) => {
64
+ window.open(link, '_blank');
65
+ },
57
66
  getContextInfo: () => {
58
67
  return {
59
68
  hostAppId: context.hostInfo?.aid ?? '1',
@@ -69,7 +78,8 @@ const adsContext: IAdsContext<'GAME'> = {
69
78
  deviceModel: context.deviceInfo.model,
70
79
  osType: context.platform === 'android' ? 'ANDROID' : context.platform === 'ios' ? 'IOS' : 'PC',
71
80
  runtimeType: 'APP',
72
- platform: 1000 // WebSDK
81
+ platform: 1000, // WebSDK
82
+ joliSource: context.joliSource ?? ''
73
83
  };
74
84
  }
75
85
  };