uniapp+微信小程序 激励广告

防忘

首先在onready里面准备好代码  包括广告准备就绪  准备出错,广告关闭

        watch: {
            showAd(newVal, oldVal) {

                let that = this;
                if (newVal === true) {
                    console.log('看广告', newVal)
                    // that.rewardedVideoAd.show()
                    //     .then(() => console.log('激励视频 广告显示'))


                    that.rewardedVideoAd.show()
                        .catch(() => {
                            that.rewardedVideoAd.load()
                                .then(() => that.rewardedVideoAd.show().catch(err => {

                                    //直接给密码
                                    that.getCode()
                                    that.hint = "获取成功"
                                    // utils.showError("暂时无法使用该下载资源");
                                    return false;
                                }))
                                .catch(err => {

                                    //直接给密码
                                    that.getCode()
                                    that.hint = "获取成功"
                                    // utils.showError("暂时无法使用该下载资源");
                                    return false;
                                    console.log('激励视频 广告显示失败')
                                })
                        })

                }

            }
        }

在watch里面监听showAd的变化 从而展示广告

 

        data() {

            return {

                showAd: false,
                canShowAd: false,
                rewardedVideoAd: null,

            }
        },
        onReady() {
            let that = this;


            this.rewardedVideoAd = wx.createRewardedVideoAd({
                adUnitId: '广告位ID',
                multiton: true
            })

            this.rewardedVideoAd.onLoad(() => {
                that.canShowAd = true;

                uni.hideLoading()
                console.log('视频准备好了')
            })

            this.rewardedVideoAd.onError((err) => {
                console.log('视频出错', err)
            })
            try {
                if (this.rewardedVideoAd.closeHandler) {
                    this.rewardedVideoAd.offClose(this.rewardedVideoAd.closeHandler);
                    console.log("---videoAd.offClose 卸载成功---");
                }
            } catch (e) {
                console.log("---videoAd.offClose 卸载失败---", e);
            }

            this.rewardedVideoAd.closeHandler = function(res) {
                // 用户点击了【关闭广告】按钮
                that.showAd = falseif (res && res.isEnded || res === undefined) {
                    // 正常播放结束
                    console.log("播放完毕");
                } else {

                    console.log("提前关闭")

                }
            };
            this.rewardedVideoAd.onClose(this.rewardedVideoAd.closeHandler);
        },

触发,点击按钮使showAd为true

广告播放完毕关闭后,再触发需要的操作方法就可以了

cancelShowAd是我用来判断流程的方法,假设我没有广告可以看,那么我就不需要看广告这一步,直接进行别的操作就行了

posted @ 2023-11-06 16:05  顾笙言啊顾笙言  阅读(437)  评论(0编辑  收藏  举报