create-rewarded-video-ad.uvue 1.9 KB
Newer Older
1
<template>
W
wanganxp 已提交
2 3
  <page-head title="激励视频广告"></page-head>
  <button :type="btnType" style="margin: 10px;" :disabled="btnDisable" @click="showAd()">{{btnText}}</button>
4 5 6 7 8 9 10 11 12 13
</template>

<script>
  export default {
    data() {
      return {
        btnText: "",
        btnType: "primary",
        btnDisable: false,
        rewardAd: null as RewardedVideoAd | null,
W
wanganxp 已提交
14
        isAdLoadSuccess: false
15 16 17 18 19 20 21 22 23 24 25 26 27 28
      }
    },
    onReady() {
      this.loadAd()
    },
    methods: {
      loadAd() {
        if (this.btnDisable)
          return
        this.btnDisable = true
        this.btnText = "正在加载广告"
        this.btnType = "primary"
        if (this.rewardAd == null) {
          this.rewardAd = uni.createRewardedVideoAd({
W
wanganxp 已提交
29
            adpid: "1507000689" //此处为测试广告位,实际开发中请在uni-ad后台申请自己的广告位后替换
30 31 32 33 34 35 36 37 38 39 40 41 42
          })
          this.rewardAd!.onError((_) => {
            this.btnType = "warn"
            this.btnText = "广告加载失败,点击重试"
            this.btnDisable = false
          })
          this.rewardAd!.onLoad((_) => {
            this.btnType = "primary"
            this.btnText = "广告加载成功,点击观看"
            this.btnDisable = false
            this.isAdLoadSuccess = true
          })
          this.rewardAd!.onClose((e) => {
W
wanganxp 已提交
43
            // 测试广告位无法通过服务器回调。实际开发中,使用自己的广告位后,需参考uni-ad文档编写服务器回调的代码,在服务端发放奖励
44 45 46 47 48 49 50 51 52 53
            this.isAdLoadSuccess = false
            uni.showToast({
              title: "激励视频" + (e.isEnded ? "" : "未") + "播放完毕",
              position: "bottom"
            })
            this.loadAd()
          })
        }
        this.rewardAd!.load()
      },
W
wanganxp 已提交
54 55
      showAd() {
        if (this.isAdLoadSuccess) {
56 57 58 59 60 61 62 63 64 65 66 67
          this.rewardAd!.show()
        } else {
          this.loadAd()
        }
      }
    }
  }
</script>

<style>

</style>