AdMainPage.ets
5.0 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
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, promptAction, 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((event) => {
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
export 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}`)
},
onRenderSuccess: (splashAd: CSJSplashAd) => {
this.splashAd = splashAd;
this.adLoadSuccess = true;
this.startLoad = false;
console.log("开屏回调 - onRenderSuccess")
},
onRenderFail: (code: number, message: string) => {
console.log("开屏回调 - onRenderFail")
}
}
@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("103398151")
.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() {
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())
}
}
},
onDidClose: (closeType: CSJSplashAdCloseType) => {
this.splashAd = undefined
// 方式二:需自行移除广告
this.mWindow?.destroyWindow()
router.pushUrl({ url: "pages/Login"})
this.closeWin()
},
onDidClick: () => {
console.log("开屏回调 - onDidClick")
router.pushUrl({ url: "pages/Login"})
},
onVideoDidPlayFinish: () => {
console.log("开屏回调 - onVideoDidPlayFinish")
},
onVideoDidPlayFail: () => {
console.log("开屏回调 - onVideoDidPlayFail")
}
}
}