mySpecialTaskList.js 2.34 KB
import api from '~/api/index/index'
import dayjs from 'dayjs'

const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/supervisor-miniprogram/'

Page({
  data: {
    IMG_URL,
    navBarHeight: 84,
    statusMap: {
      0: { name: '待完成', color: '#2C66F7', background: '#2c66f71a' },
      1: { name: '已完成', color: '#35C376', background: '#35c3761a' }
    },
    list: [],
    total: 0,
    pageNum: 1,
    pageSize: 10,
    nothing: false,
    flag: false
  },

  navbarReady(e) {
    const navBarHeight = e.detail?.height
    if (!navBarHeight) return
    this.setData({ navBarHeight })
  },

  onLoad() {
    this.reload()
  },

  onShow() {},

  reload() {
    this.setData(
      {
        list: [],
        total: 0,
        pageNum: 1,
        nothing: false,
        flag: false
      },
      () => {
        this.getList(true)
      }
    )
  },

  onBottom() {
    this.getList()
  },

  getList(reload) {
    const { pageNum, pageSize, nothing } = this.data
    if (nothing) return
    const params = {
      pageNum,
      pageSize
    }

    wx.$util.showLoading()
    api
      .getSpecialTaskList(params)
      .then(res => {
        wx.$util.hideLoading()
        const { code, data, message } = res || {}
        if (code != 0) {
          wx.$util.showToast(message || '加载失败')
          this.setData({ flag: true })
          return
        }

        const pageList = Array.isArray(data?.list) ? data.list : []
        pageList.forEach(val => {
					val.completeTime = val.completeTime ? dayjs(val.completeTime).format('YYYY-MM-DD HH:mm:ss') : ''
        })

        const nextList = reload ? pageList : this.data.list.concat(pageList)
        const total = data?.total || 0
        const isNothing = pageNum == data?.pages

        this.setData({
          list: nextList,
          total,
          pageNum: pageNum + 1,
          nothing: !!isNothing,
          flag: true
        })
      })
      .catch(() => {
        wx.$util.hideLoading()
        this.setData({ flag: true })
      })
  },

  viewDetail(e) {
    const item = e.currentTarget?.dataset?.item || {}
    wx.navigateTo({
      url: `/packageIndex/pages/specialTaskApply/specialTaskApply?id=${item.id}`
    })
  },

  goCreate() {
    wx.navigateTo({
      url: '/packageIndex/pages/specialTaskApply/specialTaskApply'
    })
  },

  back() {
    wx.navigateBack()
  }
})