materials.js 5.39 KB
import { getSystemInfoSync } from '~/utils/systemInfo'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import { materialListApi, leavePageTrack } from "../../api/api"
import commonApi from "~/api/api";
import { withTabRefreshInFlight } from '../../../../services/tabRefreshService.js'
let currentTabType = 'video';
Component({
  properties: {
    navBarHeight: Number,
    /** Tab3 可见性:true 刷新素材列表,false 走 onHide 清理 */
    show: {
      type: Boolean,
      observer(n) {
        if (n) {
          this.refreshOnShow()
        } else {
          this.onHide()
        }
      }
    }
  },
  lifetimes: {
    attached() {
      this.setData({
        userInfo: wx.$util.getUserInfo() || {}
      })
    }
  },
  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: '',
    showPage: false,
    noResult: false,
    isNeedFixed: false
  },
  methods: {
    /**
     * Tab 切到培训素材时的统一入口;合并同 tick 并发。
     * @returns {Promise<void>}
     */
    refreshOnShow() {
      return withTabRefreshInFlight('materials', () => this.refreshMaterialsTab())
    },
    /**
     * 每次 show=true 刷新 materialListApi;首进走 bootstrap 动画与埋点。
     * @returns {Promise<void>}
     */
    async refreshMaterialsTab() {
      if (!this._materialsBootstrapped) {
        this._materialsBootstrapped = true
        return this.bootstrapMaterialsTab()
      }
      this.setData({
        userInfo: wx.$util.getUserInfo() || {}
      })
      this.getListData()
      this.startMaterialsInterval()
    },
    /** 素材 Tab 首次挂载:关闭提醒、延迟展示、启动 tab 尺寸轮询 */
    async bootstrapMaterialsTab() {
      this.pdfPageTrack()
      this.closeRemind()
      setTimeout(() => {
        this.setData({
          showPage: true,
          userInfo: wx.$util.getUserInfo() || {}
        })
        this.getListData()
      }, 300)
      this.startMaterialsInterval()
    },
    startMaterialsInterval() {
      if (this.intaval) return
      this.intaval = setInterval(() => {
        this.selectComponent('#tabID')?.resize()
      }, 500)
    },
    /** @deprecated 兼容外部 selectComponent 调用 */
    async onLoad() {
      return this.refreshMaterialsTab()
    },
    closeRemind() {
      let userInfo = wx.$util.getUserInfo()
      let params = {
        merchantId: userInfo?.shopMerchantId,
        staffUuid: userInfo?.uuid,
        scene: 0,
        taskId:6,
      };
      commonApi.shutDownTaskApi(params)
    },
    onChangeTab(e) {
      currentTabType = e.detail.name
      this.initData()
    },

    async getScroviewHeight() {
      let query = wx.createSelectorQuery().in(this)
      let heightArr = []
      let s = 0, navBarHeight = this.data.navBarHeight
      query
        .selectAll('.scroll_height_d_part')
        .boundingClientRect(react => {
          react.forEach(item => {
            s += item.height
            heightArr.push(s)
          })
          const last = heightArr.pop()
          let systemInfo = getSystemInfoSync()
          const scroll_height = systemInfo.safeArea.height - last - navBarHeight
          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, noResult } = this.data
      let params = {
        merchantId: userInfo.default4SStoreInfo.merchantId,
        scene: userInfo.sceneType,
        showChannel: 1,
        type: newType,
        title: searchKey,
        sortedBy: 1
      }
      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)
    },
    initData() {
      this.setData({
        [this.currentTarget]: [],
        searchKey: ''
      }, () => {
        this.getListData()
      })
    },
    pdfPageTrack() {
      const id = wx.getStorageSync('pageQueryOfcontent_trackId')
      if (id) {
        leavePageTrack({ id }).then(res => {
          wx.removeStorageSync('pageQueryOfcontent_trackId');
        })
      }
    },
    onHide() {
      clearInterval(this.intaval)
    }
  }
})