华为快应用接入广告

华为快应用接入广告,第1张

华为快应用接入广告文档:
接入广告文档及dome
华为广告api文档

代码:
template:一个隐私协议d框一个广告d框

  
  
    
      温馨提示
    
    
      
        欢迎使用本应用!本应用非常重视您的隐私和个人信息保护。在您使用本应用前请认真阅读
        {{userSet}}
        及
        {{contentSet}}
        您同意并接受全部条款后可开始使用本应用
      
    
    
      同意并继续
    
    
      不同意退出
    
  

  
  
  
    
      
      是否离开快应用?
    
    
      
        
        {{native.adData.source}}:{{native.adData.title}}
        广告
      
    
    
      离开
      {{native.btnTxt}}
    
  

进入页面判断缓存,先加载隐私协议d框,status = 1则d出d框,先不调用广告请求,点击同意后请求广告,status = 2则不d隐私协议直接调用广告请求,退出应用时走自己的退出逻辑,如果d框没有关闭就退出则走 refuseClick() 方法退出,点击不同意隐私协议也走refuseClick()方法退出应用,否则就d出广告d框,d出广告d框前先调用上报广告曝光,请求一次广告只能上报一次曝光,不能多次调用展示,点击叉时调用showNativeAd(),重新请求广告,下次退出 *** 作再次上报曝光,点击打开贼进入广告落地页,点击关闭则调用refuseClick()退出方法,然后销毁广告
JS:

 module.exports = {
    data: {
      show: false,
      isShow: false,
      status:”“,
   
      //用于广告
      provider: "",
      native: {
        adUnitId: adUnitId,
        isShow: false,
        adData: null,
        isShowImg: false,
        isShowData: true,
        isShowBtn: true,
        btnTxt: "",
        adImgSrc: "",
      },
      clickType: false,
    },

    onInit: function () {
      // console.log('初始化');
    },
    onReady() {
      // console.log('创建完成');
      let that = this
      that.isInitStatus(() => {
        if (that.show === false) {
          that.showNativeAd();
        }
      })
    },
    isInitStatus(c) {
      let that = this
      //查看是否有缓存
      storage.get({
        key: 'status',
        success: function (data) {
          that.status = data
          if (that.status === '2') {
            that.show = false
            c()
          } else {
            that.show = true
            c()
          }
        }
      })
    },
    onShow() {
    
    },
    onHide() {
    
    },
    //获取华为广告权限
    getAdProvider() {
      this.provider = ad.getProvider();
      console.log(this.provider);
    },
    //判断是否包含createType
    isDownloadAd(creativeType) {
      let downloadTypes = [103, 106, 107, 108, 101, 102, 110];
      return downloadTypes.includes(creativeType);
    },
    //获取广告
    showNativeAd() {
      //判断广告权限
      this.getAdProvider();
      if (this.provider !== "huawei") {
        return;
      }
      //创建广告组件
      nativeAd = ad.createNativeAd({ adUnitId: this.native.adUnitId });
      //广告加载成功回调
      nativeAd.onLoad(data => {
        console.info("ad data loaded: " + JSON.stringify(data));
        this.native.adData = data.adList[0];
        if (this.native.adData) {
          if (this.native.adData.imgUrlList) {
            this.native.adImgSrc = this.native.adData.imgUrlList[0];
            console.info(" this.native.adImgSrc =" + this.native.adImgSrc);
            this.native.isShowImg = true;
          } else {
            this.native.isShowImg = false;
            this.native.adImgSrc = "";
          }
          if (this.native.adData.clickBtnTxt) {
            this.native.btnTxt = this.native.adData.clickBtnTxt;
            this.native.isShowBtn = true;
          } else {
            this.native.isShowBtn = false;
            this.native.btnTxt = "";
          }

          this.native.isShow = true;
          // this.reportNativeShow();
        }
      });
      //监听广告错误事件。
      nativeAd.onError(e => {
        console.error("load ad error:" + JSON.stringify(e));
        this.native.isShowImg = false;
        this.native.isShow = false;
      });
      //拉取广告数据
      nativeAd.load();
    },
    //广告按钮事件
    clickAdBtn(evt) {
      evt.stopPropagation();
      // 判断是否带下载按钮
      let createType = this.native.adData.creativeType;
      let ans = this.isDownloadAd(createType);
      console.info("clickAdBtn dealdownload ans=" + ans);
      this.reportNativeClick();
      // if (ans) {
      //   this.dealdownload();
      // } else {
      //   this.reportNativeClick();
      // }
    },
    //下载事件
    dealdownload: function () {
      let downloadstatus = nativeAd.getAppStatus({
        adId: this.native.adData.adId
      });
      console.log("downloadstatus downloadstatus = " + downloadstatus);
      if (downloadstatus === "DOWNLOAD") {//下载
        //下载未开始,应用初始状态。
        const resultCode = nativeAd.startDownload({
          adId: this.native.adData.adId
        });
        console.log("dealdownload startDownload  resultCode= " + resultCode);
        const progress = nativeAd.getDownloadProgress({
          adId: this.native.adData.adId
        });
        console.log("getDownloadProgress progress = " + progress);
        nativeAd.onDownloadProgress(data => {
          console.log("onDownloadProgress data = ", data);
          this.native.btnTxt = data.progress + "%";
        });
        nativeAd.onStatusChanged(data => {
          console.log("onStatusChanged data = ", data);
          let status = data.status;
          this.listenNativeAdDownloadStatus(status);
        });
      } else if (downloadstatus === "DOWNLOADING") {//下载中
        this.native.btnTxt = "暂停";
        nativeAd.pauseDownload({
          adId: this.native.adData.adId
        });
      } else if (downloadstatus === "PAUSE") {//暂停下载
        this.native.btnTxt = "继续下载";
        nativeAd.resumeDownload({
          adId: this.native.adData.adId
        });
      } else {
        this.reportNativeClick();
      }
    },
    //上报广告曝光,一个广告只有一次上报有效,adId 为 load 方法获取的广告数据的 adId 字段。
    reportNativeShow() {
      if (nativeAd) {
        nativeAd.reportAdShow({ adId: this.native.adData.adId });
      }
    },
    //广告点击
    reportNativeClick() {
      this.clickType = true
      nativeAd.reportAdClick({
        adId: this.native.adData.adId
      });
    },
    listenNativeAdDownloadStatus(downloadstatus) {
      if (downloadstatus === "INSTALLED") {
        this.native.btnTxt = "查看";
      }
    },

    //跳转隐私政策
    userClick() {
      router.push({
        uri: '/Page_agreement',
      });
    },
    //关闭隐私d框,添加缓存状态
    btnClick() {
      this.showNativeAd()
      this.show = false
      this.status = '2'
      storage.set({
        key: 'status',
        value: this.status,
        success: function (data) {
          console.log('缓存成功----', data)
        }
      })
    },
    //关闭广告d框
    close() {
      this.showNativeAd()
      this.isShow = false
      this.clickType = false
    },
    //退出按钮动作
    onBackPress() {
      console.log(this.native.adData);
      if (this.show === true) {
        this.refuseClick()
      } else if (this.native.adData === null) {
        this.refuseClick()
      } else {
        this.reportNativeShow();
        this.isShow = true
      }
      return true
    },
    //退出快应用
    refuseClick() {
      let that = this
      this.native.isShow = false;
      that.clickType = false
      // 添加桌面快捷方式
      shortcut.hasInstalled({
        success: function (ret) {
          if (ret === true) {
            that.$app.exit()
            // 桌面图标已创建    
          } else {
            // 桌面图标未创建
            shortcut.install({
              message: '是否将应用添加到桌面',
              success: function (ret) {
                that.$app.exit()
              },
              fail: function (erromsg, errocode) {
                that.$app.exit()
              }.bind(that),
            })
          }
        }.bind(that),
        fail: function (erromsg, errocode) {
          that.$app.exit()
          // console.log('hasInstalled fail ret---' + erromsg);
        }.bind(that),
        complete: function () {
          that.$app.exit()
        }
      })
      // this.$app.exit();
    },


    onDestroy() {
      // console.log('销毁-----');
      if (nativeAd) {
        nativeAd.cancelDownload({
          adId: this.native.adData.adId
        });
        nativeAd.destroy();
      }
    },
  }

效果图:
协议d框:

广告d框:

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/940127.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-17
下一篇2022-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存