detail.js 7.75 KB
import {
  getBindtapData,
  pxToRpx,
  getLastScreenHeight
} from '../../utils/util';

let app = getApp();

let rewardedVideoAd = null

Page({
  data: {
    isOverShare: true,
    authorizeVisible: false,
    total: 0,
    page: 1,
    size: 10,
    dataList: [],
    productList: [], // 产品列表
    indexInfo: {},
    userInfo: {},
    options: {},
    vidHeight: 0, // 视频高度
    isVideoEnded: false,
    detailData: {}, // 详情列表
    adConfigure: {}, //广告位配置
  },
  onShareAppMessage(res) {
    console.log("res:", res);
    if (res.from === 'button') {
      // 来自页面内转发按钮
    } else {
      // 打点
      app.stat({
        classify: "share",
        action: this.data.detailData.type == 1 ? "hide_detail_page_share" : "detail_page_share",
        primaryCode: this.data.detailData.videoCode,
      });
    }
    let detailData = this.data.detailData;
    let title = detailData && detailData.videoName || "";
    let path = `pages/detail/detail?c=${detailData.videoCode}`;
    let imageUrl = detailData && detailData.thumbnail || "";
    return {
      title,
      path,
      imageUrl
    }

  },
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    let _this = this;
    this.setData({
      options
    });

    this.queryAdConfigure().then((result) => {
      this.queryVideoDetail().then((result) => {
        _this.onReplayHandler();
      }).catch((err) => {});;
      this.initData();
    });

    wx.aldstat.sendEvent('进入页面', {
      '页面名字': '视频详情页'
    });

    if (wx.createRewardedVideoAd) {
      rewardedVideoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-3e849f70e28e6c21'
      })
      rewardedVideoAd.onLoad(() => {
        console.log('onLoad event emit');
      })
      rewardedVideoAd.onError((err) => {
        console.log('onError event emit', err)
      })
      rewardedVideoAd.onClose((res) => {
        console.log('onClose event emit', res)
      })
    }
    this.checkShowVideoAd();
  },

  checkShowVideoAd() {
    if (app.globalData.adConfigure && app.globalData.adConfigure.configureN > 0) {
      if (app.globalData.videoPlayTimes >= app.globalData.adConfigure.configureN) {
        console.log("app.globalData.videoPlayTimes:", app.globalData.videoPlayTimes);
        // if (app.globalData.videoPlayTimes > 1) {
        app.globalData.videoPlayTimes = 0;

        rewardedVideoAd.show()
        // 用户触发广告后,显示激励视频广告
        if (rewardedVideoAd) {
          rewardedVideoAd.show().catch(() => {
            // 失败重试
            rewardedVideoAd.load()
              .then(() => rewardedVideoAd.show())
              .catch(err => {
                console.log('激励视频 广告显示失败')
              })
          })
        }
      }
    }
  },

  /**
   * 基础方法
   * 授权完毕重拉数据用
   */
  initData() {
    this.queryVideoMore();
  },

  /**
   * 到达底部
   * 做加载更多操作
   */
  onReachBottom() {
    if (this.data.dataList.length < this.data.total) {
      this.setData({
        page: this.data.page + 1
      });
      this.queryVideoMore();
    }
  },

  // 重置页面列表 点击搜索条件时需要
  resetPage() {
    this.setData({
      page: 1,
      dataList: []
    })
  },

  /**
   * 到个人列表页面
   */
  toPersonalList() {
    let detailData = this.data.detailData;
    if (detailData && detailData.accountCode) {
      detailData.c = detailData.accountCode;
      app.router.push({
        path: "personalList",
        query: detailData
      })
    }
  },

  /**
   * 跳转详情页面
   */
  onDetailHandler(evt) {
    let curItem = getBindtapData(evt);
    app.router.push({
      path: "detail",
      query: {
        c: curItem.videoCode || ""
      }
    })
  },

  /**
   * 播放完毕
   */
  onVideoEndedHandler(evt) {
    this.setData({
      isVideoEnded: true
    })
    // 打点
    app.stat({
      classify: "video",
      action: this.data.detailData.type == 1 ? "hide_play_end" : "play_end",
      primaryCode: this.data.detailData.videoCode,
    });
  },

  /**
   * 播放开始
   */
  onVideoPlayHandler(evt) {
    app.globalData.videoPlayTimes++;
    // 打点
    app.stat({
      classify: "video",
      action: this.data.detailData.type == 1 ? "hide_play" : "play",
      primaryCode: this.data.detailData.videoCode,
    });
  },

  /**
   * 重播
   */
  onReplayHandler() {
    let video = wx.createVideoContext("vid", this);
    console.log("video:", video);
    video.seek(0);
    video.play();
    this.setData({
      isVideoEnded: false
    })
    // // 打点
    // app.stat({
    //   classify: "video",
    //   action: this.data.detailData.type == 1 ? "hide_play" : "play",
    //   primaryCode: this.data.detailData.videoCode,
    // });
  },

  onShareVideoEnd() {
    app.stat({
      classify: "share",
      action: "detail_page_button_end_share",
      primaryCode: this.data.detailData.videoCode,
    });
  },

  onShareFriend() {
    app.stat({
      classify: "share",
      action: "detail_page_button_friend_share",
      primaryCode: this.data.detailData.videoCode,
    });
  },

  onShareGroup() {
    app.stat({
      classify: "share",
      action: "detail_page_button_group_share",
      primaryCode: this.data.detailData.videoCode,
    });
  },

  /**
   * 返回首页
   */
  toIndex() {
    app.router.push({
      openType: "reLaunch",
      path: "index"
    })
  },

  /**
   * 请求DataList
   */
  queryVideoMore() {
    return new Promise((resolve, reject) => {
      app.post({
        sid: false,
        url: app.api.videoMore,
        data: {
          page: this.data.page,
          size: this.data.size,
        },
      }).then((result) => {
        let dataList = result.list;
        dataList = this.data.dataList.concat(dataList);
        // console.log("dataList:", dataList);
        this.setData({
          dataList: dataList,
          total: result.total
        })
        resolve();
      })
    });
  },


   /**广告配置 */
   queryAdConfigure() {
    return new Promise((resolve, reject) => {
      app.post({
        sid: false,
        url: app.api.adConfigure,
        data: {}
      }).then((result) => {
        this.setData({
          adConfigure: result
        })
        app.globalData.adConfigure = result;
        resolve(result);
      }).catch((err) => {
        reject(err)
      });
    });
  },

  /**
   * 视频详情
   */
  queryVideoDetail() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.videoDetail,
        data: {
          videoCode: this.data.options.c
        }
      }).then((result) => {
        this.setData({
          detailData: result
        })
        let {
          width,
          height
        } = result;
        let vidHeight = (height / width) * 750;
        if (vidHeight < 800) {
          vidHeight = 800;
        } else {
          vidHeight = getLastScreenHeight(132);
        }
        console.log("vidHeight:", vidHeight);
        // vidHeight = pxToRpx(vidHeight);
        // let vidHeight = getLastScreenHeight(132);

        this.setData({
          vidHeight: vidHeight,
        });

        resolve(result);
      }).catch((err) => {
        reject(err);
      });
    });
  },

  /**
   * 隐藏蒙层
   */
  hideMask() {
    this.setData({
      productDetailVisible: false,
      authorizeVisible: false,
    })
  },
  /**
   * 子组件事件
   * @param {*} evt
   */
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      // 隐藏蒙层
      case "_evt_hide_mask":
        this.hideMask();
        break;

        /**
         * 重拉数据已在
         */
      case "_evt_auth_complete":
        // this.initData();
        this.hideMask();
        break;

      default:
        break;
    }
  },
})