create-interstitial-ad.uvue 1.6 KB
Newer Older
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
<template>
  <page-head title="插屏广告"></page-head>
  <button :type="btnType" style="margin: 10px;" :disabled="btnDisable" @click="showAd()">{{btnText}}</button>
</template>

<script>
  export default {
    data() {
      return {
        btnText: "",
        btnType: "primary",
        btnDisable: false,
        interstitialAd: null as InterstitialAd | null,
        isAdLoadSuccess: false
      }
    },
    onReady() {
      this.loadAd()
    },
    methods: {
      loadAd() {
        if (this.btnDisable)
          return
        this.btnDisable = true
        this.btnText = "正在加载广告"
        this.btnType = "primary"
        if (this.interstitialAd == null) {
          this.interstitialAd = uni.createInterstitialAd({
            adpid: "1111111113" //此处为测试广告位,实际开发中请在uni-ad后台申请自己的广告位后替换
          })
          this.interstitialAd!.onError((_) => {
            this.btnType = "warn"
            this.btnText = "广告加载失败,点击重试"
            this.btnDisable = false
          })
          this.interstitialAd!.onLoad((_) => {
            this.btnType = "primary"
            this.btnText = "广告加载成功,点击观看"
            this.btnDisable = false
            this.isAdLoadSuccess = true
          })
          this.interstitialAd!.onClose((e) => {
            this.isAdLoadSuccess = false
            this.loadAd()
          })
        }
        this.interstitialAd!.load()
      },
      showAd() {
        if (this.isAdLoadSuccess) {
          this.interstitialAd!.show()
        } else {
          this.loadAd()
        }
      }
    }
  }
</script>

<style>

</style>