teachApply.js 4.3 KB
import api from '~/api/api'
import {
  TEACH_LIST_FIELDS,
  isStrategyApiSuccess,
  parseTeachingApplicationPageData,
} from '~/utils/teachApplyItem'

const PAGE_SIZE = 10

Page({
  data: {
    navBarHeight: 0,
    keyword: '',
    tabList: ['待审批(0)', '全部(0)'],
    tabIndex: 0,
    listFields: TEACH_LIST_FIELDS,
    displayList: [],
    pageNum: 1,
    pageSize: PAGE_SIZE,
    total: 0,
    hasMore: true,
    loading: false,
    triggered: false,
    nothing: false,
  },

  navbarReady(e) {
    this.setData({ navBarHeight: e.detail.height })
  },

  onBack() {
    wx.navigateBack()
  },

  onShow() {
    this.refreshAll()
  },

  getKeyword() {
    return String(this.data.keyword ?? '').trim()
  },

  onKeywordInput(e) {
    this.setData({ keyword: e.detail.value })
  },

  clearKeyword() {
    this.setData({ keyword: '' }, () => this.refreshAll())
  },

  search(e) {
    const keyword = String(e?.detail?.value ?? this.data.keyword ?? '').trim()
    this.setData({ keyword }, () => this.refreshAll())
  },

  onRefresh() {
    this.setData({ triggered: true })
    this.refreshAll().finally(() => {
      this.setData({ triggered: false })
    })
  },

  tabClick(e) {
    const tabIndex = e.detail
    if (tabIndex === this.data.tabIndex) return
    this.setData({ tabIndex }, () => this.reloadList())
  },

  onReachBottom() {
    const { hasMore, loading } = this.data
    if (!hasMore || loading) return
    this.loadList(false)
  },

  buildQueryParams({ pageNum, pageSize, tab } = {}) {
    const { tabIndex, pageSize: defaultPageSize } = this.data
    const keyWord = this.getKeyword()
    const params = {
      pageNum: pageNum ?? 1,
      pageSize: pageSize ?? defaultPageSize,
      keyWord,
      keyword: keyWord,
    }
    const tabVal = tab !== undefined ? tab : tabIndex === 0 ? 0 : undefined
    if (tabVal === 0) params.tab = 0
    return params
  },

  buildListParams(pageNum) {
    return this.buildQueryParams({ pageNum })
  },

  buildCountParams(tab) {
    return this.buildQueryParams({ pageNum: 1, pageSize: 1, tab })
  },

  updateTabListLabels(pending, all) {
    this.setData({
      tabList: [`待审批(${pending})`, `全部(${all})`],
    })
  },

  async fetchTabCounts() {
    try {
      const [pendingRes, allRes] = await Promise.all([
        api.getTeachingApplicationPage(this.buildCountParams(0)),
        api.getTeachingApplicationPage(this.buildCountParams()),
      ])
      const pickTotal = (res) => parseTeachingApplicationPageData(res).total
      this.updateTabListLabels(pickTotal(pendingRes), pickTotal(allRes))
    } catch (e) {
      // 计数失败不影响列表
    }
  },

  async refreshAll() {
    await Promise.all([this.fetchTabCounts(), this.reloadList()])
  },

  async reloadList() {
    this.setData({ pageNum: 1, hasMore: true, displayList: [] })
    await this.loadList(true)
  },

  async loadList(reset = true) {
    if (this.data.loading) return

    const pageNum = reset ? 1 : this.data.pageNum
    this.setData({ loading: true })

    try {
      const res = await api.getTeachingApplicationPage(
        this.buildListParams(pageNum)
      )
      const { list, total } = parseTeachingApplicationPageData(res)
      if (!list.length && !isStrategyApiSuccess(res)) {
        wx.showToast({
          title: res?.message || '加载失败',
          icon: 'none',
        })
        this.setData({ loading: false, hasMore: false, nothing: true })
        return
      }
      const displayList = reset ? list : [...this.data.displayList, ...list]
      const hasMore = displayList.length < total

      this.setData({
        displayList,
        total,
        pageNum: pageNum + 1,
        hasMore,
        nothing: !hasMore,
        loading: false,
      })
    } catch (e) {
      wx.showToast({ title: '加载失败', icon: 'none' })
      this.setData({
        loading: false,
        hasMore: false,
        nothing: true,
      })
    }
  },

  openDetail(e) {
    const { item } = e.currentTarget.dataset
    const applicationId = item?.applicationId || item?.id
    if (!applicationId) return
    wx.navigateTo({
      url: `/pages/teachApplyDetail/teachApplyDetail?id=${applicationId}`,
    })
  },

  goAddTeach() {
    wx.navigateTo({
      url: '/pages/teachApplyCreate/teachApplyCreate',
      events: {
        refreshList: () => this.refreshAll(),
      },
    })
  },
})