ai.js 3.76 KB
// packageEquity/pages/aiExcercise/ai/ai.js
import carLifeApi from '../../../carEquityApi'
import NP from 'number-precision'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusEnum: {
      0: '未完成',
      1: '已完成',
      2: '对练失败',
    },
    pageNum: 1,
    searchValue: '',
    flag: true,
    list: [],
    navBarHeight: 0
  },
  // 头部返回上一级
  back() {
    wx.$router.back()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    this.setData({
      pageNum: 1,
      list: [],
      flag: false,
      nothing: false
    })
    this.getSparringPage()
  },
  jumpDetail(e) {
    const {staffid} = e.target.dataset
    wx.$router.go(`packageEquity.detail?staffid=${staffid}`)
  },
  jumpResult(e) {
    const {staffid,status} = e.target.dataset
    wx.$router.go(`packageEquity.result?staffid=${staffid}&status=${status}`)
  },
  jumpExcercise(e) {
    const {staffid,count,type,prompt} = e.target.dataset
    wx.$router.go(`packageEquity.list?staffid=${staffid}&tryCount=${count}&roleType=${type}&promptOnOff=${prompt}`)
  },
  search() {
    this.setData({
      pageNum: 1,
      list: [],
      flag: false,
      nothing: false
    })
    this.getSparringPage()
  },
  tabClick(e) {
    this.status = [null, 1, 2, -1][e.detail]
    this.search()
  },
  async getSparringPage() {
    try {
      const {
        searchValue,
        pageNum,
      } = this.data, {
          merchantId
        } = wx.$util.getUserInfo(),
        params = {
          taskName: searchValue,
          currentPage: pageNum,
          pageSize: 10,
          yqMerchantId: merchantId
        }
      wx.$util.showLoading()
      const {
        code,
        data,
        message
      } = await carLifeApi.getSparringPage(params)
      if (code == 0) {
        wx.hideLoading()
        const list = pageNum == 1 ? data.list : [...this.data.list, ...data.list]
        list.forEach(item=>{
          item.task.contrast = new Date(item.task.startTime).getTime() < Date.now()
        })
        this.setData({
          total: data.total,
          flag: true,
          list: pageNum == 1 ? data.list : [...this.data.list, ...data.list],
        })
      } else throw message
    } catch (error) {
      wx.hideLoading()
      this.setData({
        flag: true
      })
      console.log(error)
      wx.$util.showToast(error)
    }
  },
  input(e) {
    let {
      value
    } = e.detail
    this.setData({
      searchValue: value
    })
  },
  handleReset() {
    this.setData({
      searchValue: ''
    })
    this.search()
  },
  truncateDecimal(number, decimalPlaces = 2) {
    let [integer, decimals] = String(number).split('.')
    if (!decimals) decimals = '00'
    if (decimals.length > 2) decimals = decimals.substring(0, decimalPlaces)
    if (decimals.length == 1) decimals += '0'
    return `${integer}.${decimals}`
  },
  navbarReady(e) {
    let navBarHeight = e.detail.height
    this.setData({
      navBarHeight
    })
    wx.createSelectorQuery().select('#search').boundingClientRect(rect => {
      this.setData({
        searchHeight: rect.height
      }) 
    }).exec()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
 

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },
  onBottomRefresh() {
    let {
      total,
      pageNum
    } = this.data
    wx.$util.onReachBottom({
      total,
      pageNum
    }, res => {
      this.getSparringPage()
    })
  }
})