|
|
@@ -4,6 +4,7 @@
|
|
|
*/
|
|
|
|
|
|
import ad from '@service.ad'
|
|
|
+import prompt from '@system.prompt'
|
|
|
|
|
|
class AdManager {
|
|
|
constructor() {
|
|
|
@@ -15,22 +16,31 @@ class AdManager {
|
|
|
// 广告位ID配置 - 请替换为您的实际广告位ID
|
|
|
this.adConfig = {
|
|
|
banner: {
|
|
|
- posId: 'your_banner_pos_id', // Banner广告位ID
|
|
|
+ posId: '9bcd5671a506459c9e6ef9c642468dc9', // Banner广告位ID
|
|
|
style: {
|
|
|
width: 750,
|
|
|
height: 100
|
|
|
}
|
|
|
},
|
|
|
interstitial: {
|
|
|
- posId: 'your_interstitial_pos_id' // 插屏广告位ID
|
|
|
+ posId: '6bd7f72fb64249bda8770adc0619d0cc' // 插屏广告位ID
|
|
|
},
|
|
|
native: {
|
|
|
- posId: 'your_native_pos_id' // 原生广告位ID
|
|
|
+ // posId: '805a483bc3b14205a1d160cb97d0eca0'
|
|
|
+ posId: '6f2e50489ff74bae800ce65c992bfec0' // 原生广告位ID
|
|
|
},
|
|
|
rewarded: {
|
|
|
- posId: 'your_rewarded_pos_id' // 激励视频广告位ID
|
|
|
+ posId: 'd3d0539a806145ebabf502d29d0d1b43' // 激励视频广告位ID
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 广告状态
|
|
|
+ this.adStatus = {
|
|
|
+ banner: { loaded: false, showing: false },
|
|
|
+ interstitial: { loaded: false },
|
|
|
+ native: { loaded: false, data: null },
|
|
|
+ rewarded: { loaded: false }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -39,28 +49,28 @@ class AdManager {
|
|
|
initBannerAd() {
|
|
|
try {
|
|
|
this.bannerAd = ad.createBannerAd({
|
|
|
- posId: this.adConfig.banner.posId,
|
|
|
+ adUnitId: this.adConfig.banner.posId,
|
|
|
style: this.adConfig.banner.style
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载完成
|
|
|
+
|
|
|
this.bannerAd.onLoad(() => {
|
|
|
- console.log('Banner广告加载完成')
|
|
|
+ console.log('Banner广告加载成功')
|
|
|
+ this.adStatus.banner.loaded = true
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载失败
|
|
|
+
|
|
|
this.bannerAd.onError((err) => {
|
|
|
- console.error('Banner广告加载失败:', err)
|
|
|
+ console.log('Banner广告加载失败:', err)
|
|
|
+ this.adStatus.banner.loaded = false
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告点击
|
|
|
- this.bannerAd.onClose(() => {
|
|
|
- console.log('Banner广告被关闭')
|
|
|
+
|
|
|
+ this.bannerAd.onClose((res) => {
|
|
|
+ console.log('Banner广告关闭:', res)
|
|
|
+ this.adStatus.banner.showing = false
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
return this.bannerAd
|
|
|
- } catch (error) {
|
|
|
- console.error('初始化Banner广告失败:', error)
|
|
|
+ } catch (e) {
|
|
|
+ console.log('初始化Banner广告失败:', e)
|
|
|
return null
|
|
|
}
|
|
|
}
|
|
|
@@ -69,10 +79,13 @@ class AdManager {
|
|
|
* 显示Banner广告
|
|
|
*/
|
|
|
showBannerAd() {
|
|
|
- if (this.bannerAd) {
|
|
|
+ if (this.bannerAd && this.adStatus.banner.loaded) {
|
|
|
this.bannerAd.show()
|
|
|
+ this.adStatus.banner.showing = true
|
|
|
+ return true
|
|
|
} else {
|
|
|
- console.warn('Banner广告未初始化')
|
|
|
+ console.log('Banner广告未加载或不可用')
|
|
|
+ return false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -80,8 +93,9 @@ class AdManager {
|
|
|
* 隐藏Banner广告
|
|
|
*/
|
|
|
hideBannerAd() {
|
|
|
- if (this.bannerAd) {
|
|
|
+ if (this.bannerAd && this.adStatus.banner.showing) {
|
|
|
this.bannerAd.hide()
|
|
|
+ this.adStatus.banner.showing = false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -91,27 +105,27 @@ class AdManager {
|
|
|
initInterstitialAd() {
|
|
|
try {
|
|
|
this.interstitialAd = ad.createInterstitialAd({
|
|
|
- posId: this.adConfig.interstitial.posId
|
|
|
+ adUnitId: this.adConfig.interstitial.posId
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载完成
|
|
|
+
|
|
|
this.interstitialAd.onLoad(() => {
|
|
|
- console.log('插屏广告加载完成')
|
|
|
+ console.log('插屏广告加载成功')
|
|
|
+ this.adStatus.interstitial.loaded = true
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载失败
|
|
|
+
|
|
|
this.interstitialAd.onError((err) => {
|
|
|
- console.error('插屏广告加载失败:', err)
|
|
|
+ console.log('插屏广告加载失败:', err)
|
|
|
+ this.adStatus.interstitial.loaded = false
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告关闭
|
|
|
- this.interstitialAd.onClose(() => {
|
|
|
- console.log('插屏广告被关闭')
|
|
|
+
|
|
|
+ this.interstitialAd.onClose((res) => {
|
|
|
+ console.log('插屏广告关闭:', res)
|
|
|
+ this.adStatus.interstitial.loaded = false
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
return this.interstitialAd
|
|
|
- } catch (error) {
|
|
|
- console.error('初始化插屏广告失败:', error)
|
|
|
+ } catch (e) {
|
|
|
+ console.log('初始化插屏广告失败:', e)
|
|
|
return null
|
|
|
}
|
|
|
}
|
|
|
@@ -120,53 +134,60 @@ class AdManager {
|
|
|
* 显示插屏广告
|
|
|
*/
|
|
|
showInterstitialAd() {
|
|
|
- if (this.interstitialAd) {
|
|
|
+ if (this.interstitialAd && this.adStatus.interstitial.loaded) {
|
|
|
this.interstitialAd.show()
|
|
|
+ return true
|
|
|
} else {
|
|
|
- console.warn('插屏广告未初始化')
|
|
|
+ console.log('插屏广告未加载或不可用')
|
|
|
+ return false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化原生广告
|
|
|
+ * 预加载原生广告2.0
|
|
|
*/
|
|
|
- initNativeAd() {
|
|
|
- try {
|
|
|
- this.nativeAd = ad.createNativeAd({
|
|
|
- posId: this.adConfig.native.posId
|
|
|
- })
|
|
|
-
|
|
|
- // 监听广告加载完成
|
|
|
- this.nativeAd.onLoad((res) => {
|
|
|
- console.log('原生广告加载完成:', res)
|
|
|
- })
|
|
|
-
|
|
|
- // 监听广告加载失败
|
|
|
- this.nativeAd.onError((err) => {
|
|
|
- console.error('原生广告加载失败:', err)
|
|
|
- })
|
|
|
-
|
|
|
- // 监听广告点击
|
|
|
- this.nativeAd.onClick(() => {
|
|
|
- console.log('原生广告被点击')
|
|
|
- })
|
|
|
-
|
|
|
- return this.nativeAd
|
|
|
- } catch (error) {
|
|
|
- console.error('初始化原生广告失败:', error)
|
|
|
- return null
|
|
|
+ preloadNativeAd(callback) {
|
|
|
+ const adParams = {
|
|
|
+ adUnitId: this.adConfig.native.posId,
|
|
|
+ type: 'native',
|
|
|
+ adCount: 1
|
|
|
}
|
|
|
+
|
|
|
+ ad.preloadAd({
|
|
|
+ ...adParams,
|
|
|
+ success: (data) => {
|
|
|
+ console.log('原生广告预加载成功:', data)
|
|
|
+ if (data && data.adList && data.adList.length > 0) {
|
|
|
+ this.adStatus.native.loaded = true
|
|
|
+ this.adStatus.native.data = data.adList[0]
|
|
|
+ if (callback) callback(true, data.adList[0])
|
|
|
+ } else {
|
|
|
+ this.adStatus.native.loaded = false
|
|
|
+ this.adStatus.native.data = null
|
|
|
+ if (callback) callback(false, null)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (data, code) => {
|
|
|
+ console.log('原生广告预加载失败:', data, code)
|
|
|
+ this.adStatus.native.loaded = false
|
|
|
+ this.adStatus.native.data = null
|
|
|
+ if (callback) callback(false, null)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 显示原生广告
|
|
|
+ * 获取原生广告数据
|
|
|
*/
|
|
|
- showNativeAd() {
|
|
|
- if (this.nativeAd) {
|
|
|
- this.nativeAd.show()
|
|
|
- } else {
|
|
|
- console.warn('原生广告未初始化')
|
|
|
- }
|
|
|
+ getNativeAdData() {
|
|
|
+ return this.adStatus.native.data
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查原生广告是否已加载
|
|
|
+ */
|
|
|
+ isNativeAdLoaded() {
|
|
|
+ return this.adStatus.native.loaded
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -175,35 +196,35 @@ class AdManager {
|
|
|
initRewardedVideoAd() {
|
|
|
try {
|
|
|
this.rewardedVideoAd = ad.createRewardedVideoAd({
|
|
|
- posId: this.adConfig.rewarded.posId
|
|
|
+ adUnitId: this.adConfig.rewarded.posId
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载完成
|
|
|
+
|
|
|
this.rewardedVideoAd.onLoad(() => {
|
|
|
- console.log('激励视频广告加载完成')
|
|
|
+ console.log('激励视频广告加载成功')
|
|
|
+ this.adStatus.rewarded.loaded = true
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告加载失败
|
|
|
+
|
|
|
this.rewardedVideoAd.onError((err) => {
|
|
|
- console.error('激励视频广告加载失败:', err)
|
|
|
+ console.log('激励视频广告加载失败:', err)
|
|
|
+ this.adStatus.rewarded.loaded = false
|
|
|
})
|
|
|
-
|
|
|
- // 监听广告关闭
|
|
|
+
|
|
|
this.rewardedVideoAd.onClose((res) => {
|
|
|
- console.log('激励视频广告被关闭:', res)
|
|
|
+ console.log('激励视频广告关闭:', res)
|
|
|
+ this.adStatus.rewarded.loaded = false
|
|
|
if (res && res.isEnded) {
|
|
|
- // 正常播放结束,可以下发游戏奖励
|
|
|
- console.log('激励视频播放完成,可以发放奖励')
|
|
|
- this.onRewardedVideoComplete && this.onRewardedVideoComplete()
|
|
|
+ console.log('激励视频正常播放结束,可以发放奖励')
|
|
|
+ if (this.onRewardedVideoComplete) {
|
|
|
+ this.onRewardedVideoComplete()
|
|
|
+ }
|
|
|
} else {
|
|
|
- // 播放中途退出,不下发游戏奖励
|
|
|
- console.log('激励视频播放中途退出')
|
|
|
+ console.log('激励视频播放中途退出,不发放奖励')
|
|
|
}
|
|
|
})
|
|
|
-
|
|
|
+
|
|
|
return this.rewardedVideoAd
|
|
|
- } catch (error) {
|
|
|
- console.error('初始化激励视频广告失败:', error)
|
|
|
+ } catch (e) {
|
|
|
+ console.log('初始化激励视频广告失败:', e)
|
|
|
return null
|
|
|
}
|
|
|
}
|
|
|
@@ -212,10 +233,12 @@ class AdManager {
|
|
|
* 显示激励视频广告
|
|
|
*/
|
|
|
showRewardedVideoAd() {
|
|
|
- if (this.rewardedVideoAd) {
|
|
|
+ if (this.rewardedVideoAd && this.adStatus.rewarded.loaded) {
|
|
|
this.rewardedVideoAd.show()
|
|
|
+ return true
|
|
|
} else {
|
|
|
- console.warn('激励视频广告未初始化')
|
|
|
+ console.log('激励视频广告未加载或不可用')
|
|
|
+ return false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -223,10 +246,23 @@ class AdManager {
|
|
|
* 预加载所有广告
|
|
|
*/
|
|
|
preloadAllAds() {
|
|
|
- this.initBannerAd()
|
|
|
- this.initInterstitialAd()
|
|
|
- this.initNativeAd()
|
|
|
- this.initRewardedVideoAd()
|
|
|
+ // 预加载Banner广告
|
|
|
+ if (this.bannerAd) {
|
|
|
+ this.bannerAd.load()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 预加载插屏广告
|
|
|
+ if (this.interstitialAd) {
|
|
|
+ this.interstitialAd.load()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 预加载原生广告
|
|
|
+ this.preloadNativeAd()
|
|
|
+
|
|
|
+ // 预加载激励视频广告
|
|
|
+ if (this.rewardedVideoAd) {
|
|
|
+ this.rewardedVideoAd.load()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -237,18 +273,38 @@ class AdManager {
|
|
|
this.bannerAd.destroy()
|
|
|
this.bannerAd = null
|
|
|
}
|
|
|
+
|
|
|
if (this.interstitialAd) {
|
|
|
this.interstitialAd.destroy()
|
|
|
this.interstitialAd = null
|
|
|
}
|
|
|
- if (this.nativeAd) {
|
|
|
- this.nativeAd.destroy()
|
|
|
- this.nativeAd = null
|
|
|
- }
|
|
|
+
|
|
|
if (this.rewardedVideoAd) {
|
|
|
this.rewardedVideoAd.destroy()
|
|
|
this.rewardedVideoAd = null
|
|
|
}
|
|
|
+
|
|
|
+ // 重置状态
|
|
|
+ this.adStatus = {
|
|
|
+ banner: { loaded: false, showing: false },
|
|
|
+ interstitial: { loaded: false },
|
|
|
+ native: { loaded: false, data: null },
|
|
|
+ rewarded: { loaded: false }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取广告状态
|
|
|
+ */
|
|
|
+ getAdStatus() {
|
|
|
+ return this.adStatus
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查广告是否可用
|
|
|
+ */
|
|
|
+ isAdAvailable(adType) {
|
|
|
+ return this.adStatus[adType] && this.adStatus[adType].loaded
|
|
|
}
|
|
|
}
|
|
|
|