index.js 3.73 KB
// pages/viewVideo/index.js
import { IMG_URL } from "~/utils/constantData.js";
import { senceData } from "~/pages/tab/index/constant/data";
// 导入方法
import { behavior as computedBehavior } from "~/utils/lib/miniprogram-computed";
import utils from '~/utils/util.js'

Page({
  noPageAuth: true,
  // 混入
  behaviors: [computedBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    isShare:false,//是否是被分享出去
    videoSrc: "",
    senceData,
    tabActive: 0, //场景值
    videoIndex: 0, //场景中的视频下表
  },
  // 使用
  computed: {
    videoitem(data) {
      let { senceData, tabActive, videoIndex } = data;
      return senceData[tabActive].videoList[videoIndex];
    },
    videoLeft(data) {
      let { senceData, tabActive, videoIndex } = data;
      // return senceData[tabActive].videoList.filter((item,index) => index != videoIndex)
      return senceData[tabActive].videoList;
    },
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {isShare} = options;
    this.setData({
      isShare:isShare, //
    })
    this.shareDataFn(options)
    
  },
  //接受跳转的路由参数(小程序页面跳转)
  onRoute(options) {
   this.shareDataFn(options)
  },
  // 分享数据函数
  shareDataFn(options){
    if (!options || utils.isEmptyObj(options)) return
    let {tabActive,videoIndex} = options
        // console.log("options: ", options, !!options);
    // console.log('tabActive,videoIndex: ', tabActive,videoIndex);
    this.setData({
      tabActive: tabActive,
      videoIndex: videoIndex,
    });
    //
    
    //分享的数据
    this.changeShareData()
  },
  changeShareData(){
    let {senceData,tabActive,videoIndex,videoitem} = this.data
    // // 设置转发标题
    // let userInfoObj = wx.$storage.get("userInfo");
    // // #服务商账号#取该分享者商户的联系人字段,无则取商户简称,无则取商户全称
    // let name = userInfoObj.merchantName;
    this.shareTitle = `给您分享一个${videoitem.name } 视频`;

    //分享的数据
    let senceItem = senceData[tabActive]
    this.shareQuery = `sceneCode=${senceItem?.sceneCode}&isShare=${true}&tabActive=${tabActive}&videoIndex=${videoIndex}`
  },
  changeVideo(e) {
    let { index } = e.currentTarget.dataset;
    this.setData({
      videoIndex: index,
    });
    this.setpagesTitle()
    this.changeShareData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setpagesTitle()
  },
  //设置标题
  setpagesTitle(){
    //设置标题
    let {videoitem} = this.data
      // wx.setNavigationBarTitle({
      //   title: videoitem.name,
      // });
      utils.setNavigationBarTitle(videoitem.name)
  },
  // 视频下载
  downVideo() {
    let {videoitem} = this.data
    let url = videoitem.videoUrl
    console.log(url)
    wx.$api.showToast('正在下载视频...')
    wx.downloadFile({
      url: url,
      success(res) {
        // console.log(res,111)
        if (res.statusCode === 200) {
          wx.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              setTimeout(() => {
                wx.$api.showToast('下载成功!')
              }, 2000)
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});