product.js 4.57 KB
import {imgUrlMixin} from '~/mixins/index'
const videoBaseUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/'
import {IMG_VERSION}  from '~/utils/constantData'
Component({
  behaviors: [imgUrlMixin],
  
  properties: {
    headerHeight: Number,
    senceData:Object,
    actNum: Number,
    navIdx: Number,
    isShare:{
      type: Boolean,
      value: false
    }
  },

  data: {
    videoBaseUrl,
    img_version:IMG_VERSION(),
    navList:['数字无忧权益卡'],
    activeNavIdx: 0, //特色产品
    tab:['适用场景','营销链路','项目亮点','转化漏斗','合作流程','合作案例'],
    curTabIdx:0, //适用产品
    actNum: 0, //场景
    showAuth: false,
    throttle: false
  },
  lifetimes:{
    ready: function (){
      wx.$eventbus.on('evt_page_scroll',(e)=>{
        wx.$util.getViewInfo('.header',async (res)=> {
          let heightTop = this.data.isShare ? 0 : (res?.height || 0)
          let prodArr = await this.getHeight('.prod-wrap')
          heightTop += prodArr[0].height
          let imgArr =  await this.getHeight('.img-item')
          let imgHeightArr = []
          imgArr.reduce((pre,cur,index)=>{
            imgHeightArr.push(pre+cur.height)
            return pre+cur.height
          },heightTop)
          let idx = imgHeightArr.findIndex(item => item > e.scrollTop)
          if(idx>-1 &&  !this.data.throttle){
            this.setData({
              curTabIdx: idx
            })
          }
        })
      })
    },
  },

  observers: {
    actNum: function () {
      let num = this.data.actNum;
      let navList = []
      let tab = ['适用场景','营销链路','项目亮点','转化漏斗','合作流程']
      switch (num) {
        case 0:
          navList = ["数字无忧权益卡"];
          tab.push("合作案例");
          break;
        case 1:
          navList = ["汽车太阳膜"];
          tab.push("合作案例");
          break;
        case 2:
          navList = ["漆面保护膜", "改色膜"];
          break;
        case 3:
          navList = [
            "数字化美容",
            "座椅通风",
            "AR导航",
            "氛围灯",
            "360全景导航",
          ];
          tab.push("合作案例");
          break;
      }
      this.setData({
        navList,
        curTabIdx: 0,
        activeNavIdx:this.data.isShare  ? this.data.navIdx :  0,
        tab
      })
    },
  },

  methods: {
    changeTab(e){
      let {idx} = e.currentTarget.dataset
      this.setData({
        curTabIdx: idx
      })
      this.data.throttle = true
      wx.$util.getViewInfo('.header',async (res)=> {
        let heightTop = this.data.isShare ? 0 : (res?.height || 0)
        let prodArr = await this.getHeight('.prod-wrap')
        heightTop += prodArr[0].height
        let imgArr =  await this.getHeight('.img-item')
        for(let i=0;i<idx;i++){
          heightTop += imgArr[i].height
        }
        wx.pageScrollTo({
          scrollTop: heightTop,
          duration: 0 
        })
        setTimeout(()=>{
          this.data.throttle = false
        },500)
      })     
    },
    getHeight(cla){
      return new Promise((resolve,reject)=>{
        let query = wx.createSelectorQuery().in(this)
        query.selectAll(cla).boundingClientRect((rect) => {
          resolve(rect)
        }).exec()
      })
    },
    changeNav(event){
      this.setData({
        activeNavIdx: event.target.dataset.index
      })
    },
    downVideo(e) {
      let {url} = e.target.dataset
      this.setData({
        curVideoUrl: url
      })
      wx.getSetting({
        success: (res)=> {
          if(res.authSetting['scope.writePhotosAlbum'] === false){
            this.setData({
              showAuth: true
            })
          }else{
            this.toDown()
          }
        }
      })
    },
    toDown(){
      wx.$api.showToast('正在下载视频...')
      wx.downloadFile({
        url: this.data.curVideoUrl,
        success (res) {
          if (res.statusCode === 200) {
            wx.saveVideoToPhotosAlbum({
              filePath:  res.tempFilePath,
              success (resp) {
                wx.$api.showToast('下载成功!')
              }
            })
          }
        }
      })
    },
    authFail(){
      wx.showToast({
        title: "下载视频前需要您授权'添加到相册'权限",
        icon: "none",
        duration: 2500,
      });
    },
    authSuccess(){
      this.toDown()
    },
    toSharePage() {
      let { actNum, activeNavIdx ,senceData} = this.data;
      wx.$router.go("checkProduct", { actNum, activeNavIdx ,senceData, fromLocal:1});
    },
  }
})