product.js 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
// pages/tab/discovery/components/product/product.js
// import {imgUrlMixin} from '~/mixins/index'
import { IMG_URL } from '~/utils/constantData.js'
import USERAPI from '~/api/usersApi/index'
const videoBaseUrl = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/ai-card/'

Component({
  options: {
    addGlobalClass: true
  },
  // behaviors: [imgUrlMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    stickyTop: Number,
    actNum: Number,
    wrapactiveNavIdx: Number,
    senceData: Object, //本地数据
    apiData: Object, //后端接口数据
    isShare: {
      type: Boolean,
      value: false
    },
    origin: String, //来源
    outshowVideo: {
      type: Boolean,
      value: true
    }, //显示视频列表
    isAuthphone: Boolean,
    isShow: {
      type: Boolean
      // value: false,
    },
    phone: String,
    img_version: {
      type: null,
      // optionalTypes: [Number],
      value: 0.1
    } //图片版本号
  },

  /**
   * 组件的初始数据
   */
  data: {
    navList: ['360全景导航', 'AR实景导航', '座椅通风', '氛围灯', '数字化美容'],
    activeNavIdx: 0,
    tab: [
      '场景说明',
      '营销链路',
      '项目亮点',
      '转化漏斗',
      '合作流程'
      // "案例视频",
    ],
    heightArr: [],
    allHeight: 0,
    curTabIdx: 0,
    intoViewId: 'to-0',
    IMGURL: `${IMG_URL}index/`,
    videoBaseUrl,
    getMoreBtn: false,
    showVideo: true, //显示视频列表
    imgTopArr: []
    // navFixed:false  //步骤条定位
  },
  lifetimes: {
    ready: function () {
      this.init()
      this.getImgHeight()
      // console.log("ready");
      // //获取当前路径
      // let currentUrl = this.getCurrentPageUrl()
      // // console.log('currentUrl: ', currentUrl);
      // if(currentUrl == '/pages/tab/index/index'){
      //   this.setData({
      //     currentUrl:false
      //   })
      // }else{
      //   let {isShare} = this.data
      //   console.log(isShare,'isShare')
      //   if(isShare){
      //     this.setData({
      //       currentUrl:false
      //     })
      //   }else{
      //     this.setData({
      //       currentUrl:true
      //     })
      //   }

      // }
      //监听页面滚动的距离
      // let {stickyTop} = this.data
      // //44 nav的高度
      // let top = stickyTop/2 + 44
      wx.$eventbus.on('evt_page_scroll', async e => {
        // console.log('top: ', e.scrollTop,top);
        // if(e.scrollTop>top ){
        //   this.setData({
        //     navFixed:true
        //   })
        // }else{
        //   this.setData({
        //     navFixed:false
        //   })
        // }

        // let heightTop = this.data.stickyTop// 90 场景高度,8 场景margin-bottom
        // let prodArr = await this.getHeight('.prod-wrap')
        // // console.log('prodArr: ', prodArr);
        // heightTop += prodArr[0].height
        // let imgArr =  await this.getHeight('.img-item')
        // // console.log('imgArr: ', imgArr);
        // let imgHeightArr = []
        // imgArr.reduce((pre,cur,index)=>{
        //   imgHeightArr.push(pre+cur.height)
        //   return pre+cur.height
        // },heightTop)
        let imgTopArr = this.data.imgTopArr
        // console.log('imgTopArr: ', imgTopArr);
        if (imgTopArr.length == 0) return
        wx.nextTick(() => {
          let idx = imgTopArr.findIndex(item => item > e.scrollTop)
          if (idx > -1) {
            this.setData({
              curTabIdx: idx
            })
          }
        })
      })

      if (!this.data.outshowVideo) {
        this.setData({
          showVideo: false
        })
      }

      //如果不是分享,初始化的时候调用函数
      if (!this.data.isShare) {
        // console.log(131546)
        this.getMoreFn()
      }
    }
  },
  observers: {
    actNum: function (newval, oldval) {
      let navList
      let tab = [
        '场景说明',
        '营销链路',
        '项目亮点',
        '转化漏斗',
        '合作流程'
        // "案例视频",
      ]
      let num = this.data.actNum
      switch (num) {
        case 0:
          navList = ['数字无忧权益卡']
          break
        case 1:
          navList = ['汽车太阳膜']
          break
        case 2:
          navList = ['漆面保护膜', '改色膜']
          break
        case 3:
          navList = ['数字化美容', '座椅通风', 'AR导航', '氛围灯', '360全景导航']
          break
        case 4:
          navList = ['新能源0元换购']
          break
      }

      this.setData({
        navList,
        tab,
        intoViewId: `to-0`,
        activeNavIdx: 0
      })
      // 每次切换场景,重新获取流程图高度
      this.getImgHeight()
      this.getMoreFn()
      // this
      if (this.data.wrapactiveNavIdx) {
        this.setData({
          activeNavIdx: this.data.wrapactiveNavIdx
        })
      }
    }
    // isShow: function (newval) {
    //   let { isShare } = this.data;
    //   if (newval && !isShare) {
    //     this.setData({
    //       currentUrl: true,
    //     });
    //   }
    // },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    phoneCall(e) {
      let { phone } = e.currentTarget.dataset
      wx.$api.phoneCall(phone)
    },
    getCurrentPageUrl() {
      const pages = getCurrentPages()
      const currentPage = pages[pages.length - 1]
      const url = `/${currentPage.route}`
      return url
    },
    async init() {
      try {
        const { data, code, message } = await USERAPI.wxCodeLogin()
        if (code == '0') {
          this.loginToken = data
        } else this.reLogin(message)
        wx.hideLoading()
      } catch ({ errMsg }) {
        console.error(errMsg)
        this.reLogin()
      }
    },
    /**获取loginToken */
    reLogin(message) {
      wx.showModal({
        content: message || '获取loginToken失败',
        confirmText: '重试',
        confirmColor: '#3666FE'
      }).then(({ confirm }) => {
        if (confirm) this.init()
      })
    },
    // 视频下载
    downVideo(e) {
      let { url } = e.target.dataset
      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)
              }
            })
          }
        }
      })
    },
    saleTextChange(e) {},
    changeTab(e) {
      let idx = e.detail
      let { imgTopArr, stickyTop } = this.data
      // console.log("idx: ", imgTopArr[idx], stickyTop);
      // this.setData({
      //   intoViewId: `to-${idx}`,
      // });
      // wx.$eventbus.emit('evt_page_index',idx)
      //判断当前路径
      let currentUrl = this.getCurrentPageUrl()
      // console.log('currentUrl: ', currentUrl);
      if (currentUrl == '/pages/tab/index/index') {
        wx.pageScrollTo({
          // scrollTop: imgTopArr[idx] - stickyTop + 88 + 34
          scrollTop: imgTopArr[idx] - stickyTop + 44 + 36
        })
        // wx.pageScrollTo({ scrollTop: imgTopArr[idx] - stickyTop });
      } else {
        wx.pageScrollTo({
          scrollTop: imgTopArr[idx] - stickyTop - 10
        })
      }
    },
    // async changeTab(e){
    //   let idx = e.detail
    //   this.setData({
    //     intoViewId: `to-${idx}`
    //   })
    //   let heightTop = this.data.headerHeight + 90 - 16 - 46  // 90 场景高度,16 可能是误差 , 46 tab高度
    //   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
    //   })
    // },
    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
      })
    },
    // scrolltoupper(event) {
    //   console.log(event.detail.scrollHeight,event.detail.scrollTop)
    //   const idx = this.data.heightArr.findIndex(
    //     (item) => item > event.detail.scrollHeight
    //   );
    //   console.log('idx',idx)
    //   this.setData({
    //     curTabIdx: idx,
    //   });

    //   // console.log(this.data.curTabIdx1)
    // },
    getImgHeight() {
      let query = wx.createSelectorQuery().in(this)
      let heightArr = []
      let imgTopArr = []
      let s = 0
      let top
      let _this = this
      query
        .selectAll('.img-item')
        .boundingClientRect(react => {
          react.forEach((item, index) => {
            // console.log('item: ', item);

            // if(index == 0){
            //   console.log('item top: ', item);
            //   top = item.top
            // }
            // s = s +item.height;
            s = item.top
            // heightArr.push(s);
            imgTopArr.push(s)
          })

          // heightArr = heightArr.map((item)=>{
          //   item += top
          //   return item
          // })
          // this.data.heightArr = heightArr;
          // // console.log(heightArr[heightArr.length-1])
          // this.data.allHeight = heightArr[heightArr.length - 1];
          // console.log('imgTopArr',imgTopArr)
          _this.setData({
            imgTopArr
          })
        })
        .exec()
    },
    // 获取用户信息
    getUserInfo({ detail }) {
      // console.log(detail);

      if (!detail.code) return
      // this.setBtnLoading()
      USERAPI.getUserProfile(this.loginToken, detail)
        .then(result => {
          // console.log(result)
          let userInfo = wx.$storage.get('wx_user_info')
          // console.log('userInfo: ', userInfo);
          let { phone } = result
          // console.log('phone: ', phone);
          wx.$storage.set('wx_user_info', {
            ...userInfo,
            phone
          })
          // }
          setTimeout(() => {
            this.triggerEvent('authorized', {
              phone
              // isShare: false,
            })
          }, 1e3)
          //隐藏更多按钮,显示视频列表
          this.setData({
            getMoreBtn: true,
            isShare: false,
            outshowVideo: true
          })
          this.getMoreFn()
        })
        .catch(({ errMsg }) => {
          wx.$api.showToast(errMsg || '系统异常')
          // this.setBtnLoading(false)
        })
    },
    // 图片流程5 点击往下获取更多
    // 没登陆用户(登陆后解锁全部)
    getMoreFn() {
      let { actNum } = this.data
      // console.log("inactNumdex: ", actNum);
      let tab = [
        '场景说明',
        '营销链路',
        '项目亮点',
        '转化漏斗',
        '合作流程'
        // "案例视频",
      ]

      switch (actNum) {
        case 0:
          tab.push('案例视频')
          break
        case 1:
          tab.push('案例视频')
          break
        case 2:
          break
        case 3:
          tab.push('案例视频')
          break
      }
      this.setData({
        tab
      })
    },
    onShareFn() {
      let userInfo = wx.$storage.get('userInfo')
      // this.triggerEvent('sharehandle')
      // wx.$router.go('apply',1235)
      let { actNum, activeNavIdx, apiData } = this.data
      wx.$router.go('checkProduct', {
        actNum,
        activeNavIdx,
        apiData,
        outshowVideo: true,
        isShow: true,
        sharePhone: userInfo.phone
        // origin:'home' //跟首页类似情况
        // isShare: true
      })
    },
    // 点击获取视频的url
    getVideoUrl(e) {
      // console.log('e: ', e);
      let { title, src } = e.currentTarget.dataset
      wx.$eventbus.emit('getvideo', { title: title, src: src })
      // this.triggerEvent('getvideo', {
      //   title,
      //   src
      // })
    }
  }
})