trainingMaterials.js 3.68 KB

const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import { materialListApi, leavePageTrack } from "./api/api"
let currentTabType = 'video'
Page({
  data: {
    IMG_URL,
    userInfo: {},
    menuList: [{
      title: '视频',
      name: 'video',
      icon: 'default_video',
      list: []
    }, {
      title: 'PDF',
      name: 'PDF',
      icon: 'default_pdf',
      list: []
    }, {
      title: '图片',
      name: 'image',
      icon: 'default_img',
      list: []
    }],
    searchKey: '',
    sortValue: 0,
    sortMap: {
      0: 'sort_default',
      1: 'sort_desc',
      2: 'sort_asc',
    },
    noResult: false
  },
  onLoad() {
    currentTabType = 'video'
  },
  async onShow() {
    this.pdfPageTrack()
    this.getScroviewHeight()
    this.getListData()
    this.intaval = setInterval(() => {
      this.selectComponent('#tabID').resize();
    }, 500);
  },
  navbarReady(e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
  },
  onChangeTab(e) {
    currentTabType = e.detail.name
    this.initData()
  },
  async getScroviewHeight() {
    let query = wx.createSelectorQuery().in(this)
    let heightArr = []
    let s = 0
    query
      .selectAll('.scroll_height_d_part')
      .boundingClientRect(react => {
        react.forEach(item => {
          s += item.height
          heightArr.push(s)
        })
        const last = heightArr.pop()
        let systemInfo = wx.getSystemInfoSync()
        const scroll_height = systemInfo.safeArea.height - last * 3
        this.setData({
          scroll_height
        })
      })
      .exec()

  },
  getListData() {
    const userInfo = wx.$util.getUserInfo()
    // 登录信息映射值: 0 会员权益升级 1 精品升级 3 售后众筹 ,2次新车转化
    // 0-图片;1-视频;2-pdf;
    const newType = currentTabType == 'video' ? 1 : currentTabType == 'PDF' ? 2 : 0
    let { searchKey, sortValue, noResult } = this.data
    let params = {
      merchantId: userInfo.default4SStoreInfo.merchantId,
      scene: userInfo.sceneType,
      showChannel: 0,
      type: newType,
      title: searchKey,
      sortedBy: sortValue ? 2 : 1
    }
    if (sortValue == 2) params.sortedOrder = 'asc'
    if (sortValue == 1) params.sortedOrder = 'desc'
    let list = [], listNameMap = {
      0: 'menuList[2].list',
      1: 'menuList[0].list',
      2: 'menuList[1].list',
    }
    const target = listNameMap[newType]
    this.currentTarget = target
    materialListApi(params).then(res => {
      if (res && res.data) {
        res.data.forEach(el => {
         
          if (el.createdDate) {
            let date = el.createdDate + ''
            date = date.replace(/-/g, '/').split(' ')[0] // 日期格式处理
            el.createdDate = date
          } 
        })
      }
      list = res.data
      noResult = list.length ? false : true
      this.setData({
        [target]: list,
        noResult
      })
    })
  },
  onChangeSearch(e) {

  },
  onSearch(e) {
    this.setData({
      searchKey: e.detail
    }, () => {
      this.getListData()
    })
  },
  onCancel(e) {
    this.onSearch(e)
  },
  toFilterSort() {
    let { sortValue } = this.data
    this.setData({
      sortValue: sortValue == 2 ? 1 : 2
    }, () => {
      this.getListData()
    })
  },
  initData() {
    this.setData({
      [this.currentTarget]: [],
      sortValue: 0,
      searchKey: ''
    }, () => {
      this.getListData()
    })
  },
  pdfPageTrack() {
    const id = wx.getStorageSync('pageQueryOfcontent_trackId')
    if (id) {
      leavePageTrack({ id }).then(res => {
        wx.removeStorageSync('pageQueryOfcontent_trackId');
      })
    }
  },
  onHide() {
    clearInterval(this.intaval)
  }
})