AdMainPage.ets 5.2 KB
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("103397742")
      .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())
        }
      }
    },

    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")
    }
  }
}