AdMainPage.ets
5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
AdSlotBuilder,
CSJAdCreator,
CSJSplashAd,
CSJAdSdk,
CSJSplashAdInteractionListener,
CSJSplashAdLoadListener,
CSJSplashAdCloseType,
CSJSplashAdLoadParam,
MediationAdInfo
} from '@csj/openadsdk'
import { UIUtil } from '../mediation_adtype/tools/UIUtil'
import { PrintBiddingTokenUtils } from '../mediation_adtype/test/PrintBiddintTokenUtils'
import { window, router } from '@kit.ArkUI'
import { DemoConstants } from '../entryability/DemoConstants'
import('./SplashAdShowPage')
@Builder
function bottomViewBuilder() {
Text('Bottom View')
.backgroundColor(Color.Yellow)
.width(UIUtil.getScreenWidthVp())
.height(UIUtil.getScreenHeightVp() - adHeight)
}
@Builder
function closeBtnBuilder(closeBlock: () => void) {
Text('close')
.textAlign(TextAlign.Center)
.backgroundColor(Color.Yellow)
.width(60)
.height(60)
.borderRadius(30)
.margin({ top: 50, right: 40 })
.onClick(() => {
closeBlock()
})
}
let globalBuilder: WrappedBuilder<[]> = wrapBuilder(bottomViewBuilder)
let globalCloseBtnBuilder: WrappedBuilder<[closeBlock: () => void]> = wrapBuilder(closeBtnBuilder)
let screenWidth: number = UIUtil.getScreenWidthVp()
let screenHeight: number = UIUtil.getScreenHeightVp()
let adWidth: number = Math.round(screenWidth)
let adHeight: number = Math.round(screenHeight)
@Entry
@Component
struct SplashAdDemoPage {
private mAdCreator: CSJAdCreator = CSJAdSdk.getAdCreator()
private splashAd: CSJSplashAd | undefined
context = getContext(this)
@State adLoadSuccess: Boolean = false
@State errorMsg: string | undefined = undefined;
@State startLoad: boolean = false;
private mLoadListener: CSJSplashAdLoadListener = {
// 加载成功,直接展示广告
onAdLoaded: (splashAd: CSJSplashAd) => {
console.log("开屏回调 - onAdLoaded")
this.splashAd = splashAd;
this.adLoadSuccess = true;
this.startLoad = false;
// 方式一:直接showSplashAd
this.splashAd.setInteractionListener(this.mSplashAdInteractionListener)
if (adHeight < screenHeight) {
this.splashAd?.showSplashAd(DemoConstants.windowStage, globalBuilder,
this.useCustomCloseBtn ? globalCloseBtnBuilder : undefined)
} else {
this.splashAd?.showSplashAd(DemoConstants.windowStage, undefined,
this.useCustomCloseBtn ? globalCloseBtnBuilder : undefined)
}
},
// 加载失败,关闭广告并跳转到登录页
onError: (code: number, message: string) => {
this.errorMsg = "onError code: " + code + "message: " + message;
console.log(`开屏回调 - onError code: ${code} message: ${message}`)
this.closeWin()
router.replaceUrl({url: "pages/Login"})
},
// 广告渲染成功,展示广告
onRenderSuccess: (splashAd: CSJSplashAd) => {
this.splashAd = splashAd;
this.adLoadSuccess = true;
this.startLoad = false;
console.log("开屏回调 - onRenderSuccess")
},
// 广告渲染失败,关闭广告并跳转到登录页
onRenderFail: () => {
console.log("开屏回调 - onRenderFail")
this.closeWin()
router.replaceUrl({url: "pages/Login"})
}
}
@State widthLabelText: string = "宽:" + adWidth
@State heightLabelText: string = "高:" + adHeight
private useCustomCloseBtn: boolean = false
private loadTimeout: number = 3000
private mWindow: window.Window | undefined = undefined
closeWin() {
const win = window.findWindow('startAd') // 找到子窗口
win.destroyWindow() // 销毁窗口
}
aboutToAppear(){
let adSlot = new AdSlotBuilder()
.setCodeId("103438700")
.setAcceptSize(adWidth,
adHeight)
.build()
let param = new CSJSplashAdLoadParam(adSlot, this.mLoadListener, this.getUIContext(), this.loadTimeout)
PrintBiddingTokenUtils.printBiddingToken(adSlot, this.mAdCreator);
this.mAdCreator.loadSplashAd(param)
}
build() {
Column() {
Stack(){
Progress({ value: 0, total: 10, type: ProgressType.Ring })
.width(120).color('#409EFF').backgroundColor('#fff')
.style({ strokeWidth: 10, status: ProgressStatus.LOADING })
Image($r('app.media.logo')).width(60).height(60)
}
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
// CSJSplashAdInteractionListener
private mSplashAdInteractionListener: CSJSplashAdInteractionListener = {
// 广告展示
onDidShow: () => {
console.log("开屏回调 - onDidShow")
// 聚合show信息获取
let info = this.splashAd?.getMediaExtraInfo()
if (info instanceof MediationAdInfo) {
let showCpm = info.getShowEcpm()
if (showCpm) {
console.log("GMMediation_showCpm", "adnName:"+showCpm.getAdnName())
console.log("GMMediation_showCpm", "ritType:"+showCpm.getRitType())
console.log("GMMediation_showCpm", "adnRitId:"+showCpm.getAdnRitId())
console.log("GMMediation_showCpm", "eCpm:"+showCpm.getEcpm())
}
}
},
// 关闭广告 closeType.value对应以下关闭原因1: 点击跳过,2: 展示倒计时时间到达,3: 点击广告,0: 其他
onDidClose: (closeType: CSJSplashAdCloseType) => {
this.splashAd = undefined
// 方式二:需自行移除广告
this.mWindow?.destroyWindow()
if(closeType.value == 1 || closeType.value == 2) {
this.closeWin()
router.pushUrl({ url: "pages/Login"})
}
},
// 广告被点击了
onDidClick: () => {
console.log("开屏回调 - onDidClick")
},
// 视频广告播放完成
onVideoDidPlayFinish: () => {
console.log("开屏回调 - onVideoDidPlayFinish")
},
// 视频广告播放错误
onVideoDidPlayFail: () => {
console.log("开屏回调 - onVideoDidPlayFail")
this.closeWin()
router.replaceUrl({url: "pages/Login"})
}
}
}