list.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <script setup>
  2. import { ref } from 'vue'
  3. import { onShow, onReachBottom } from '@dcloudio/uni-app'
  4. import { listMyReimburse, listMyAdvance } from '@/api/bx'
  5. import { bxStatusLabel, formatMoney, dash } from '@/utils/bxLabels'
  6. import { PAGE_PATHS } from '@/config/api'
  7. const tabs = [
  8. { key: 'all', label: '全部' },
  9. { key: 'reimburse', label: '报销' },
  10. { key: 'advance', label: '借款' }
  11. ]
  12. const activeTab = ref('all')
  13. const list = ref([])
  14. const loading = ref(false)
  15. const finished = ref(false)
  16. const page = ref(1)
  17. const pageSize = 20
  18. const goBack = () => {
  19. uni.navigateBack({
  20. fail: () => uni.switchTab({ url: PAGE_PATHS.workbench })
  21. })
  22. }
  23. const goDetail = (row) => {
  24. if (row._biz === 'advance') {
  25. uni.navigateTo({ url: `${PAGE_PATHS.bxAdvanceDetail}?id=${row.id}` })
  26. } else {
  27. uni.navigateTo({ url: `${PAGE_PATHS.bxReimburseDetail}?id=${row.id}` })
  28. }
  29. }
  30. const mark = (rows, biz) =>
  31. (rows || []).map((r) => ({
  32. ...r,
  33. _biz: biz,
  34. _title: biz === 'advance' ? `借款 ${r.applyCode || ''}` : `报销 ${r.applyCode || ''}`,
  35. _sub: biz === 'advance' ? dash(r.reason) : dash(r.expenseName || r.title)
  36. }))
  37. const loadList = async (reset = false) => {
  38. if (loading.value) return
  39. if (reset) {
  40. page.value = 1
  41. finished.value = false
  42. list.value = []
  43. }
  44. if (finished.value) return
  45. loading.value = true
  46. try {
  47. const q = { page: page.value, pageSize }
  48. let rows = []
  49. if (activeTab.value === 'reimburse') {
  50. const res = await listMyReimburse(q)
  51. rows = mark(res.rows, 'reimburse')
  52. list.value = reset ? rows : list.value.concat(rows)
  53. if (list.value.length >= (res.total || 0) || rows.length < pageSize) finished.value = true
  54. else page.value += 1
  55. } else if (activeTab.value === 'advance') {
  56. const res = await listMyAdvance(q)
  57. rows = mark(res.rows, 'advance')
  58. list.value = reset ? rows : list.value.concat(rows)
  59. if (list.value.length >= (res.total || 0) || rows.length < pageSize) finished.value = true
  60. else page.value += 1
  61. } else {
  62. const [a, b] = await Promise.all([listMyReimburse(q), listMyAdvance(q)])
  63. rows = mark(a.rows, 'reimburse').concat(mark(b.rows, 'advance'))
  64. rows.sort((x, y) => String(y.applyTime || '').localeCompare(String(x.applyTime || '')))
  65. list.value = reset ? rows : list.value.concat(rows)
  66. const total = (a.total || 0) + (b.total || 0)
  67. if (list.value.length >= total || (a.rows.length < pageSize && b.rows.length < pageSize)) {
  68. finished.value = true
  69. } else page.value += 1
  70. }
  71. } catch (e) {
  72. uni.showToast({ title: e.message || '加载失败', icon: 'none' })
  73. } finally {
  74. loading.value = false
  75. }
  76. }
  77. const switchTab = (key) => {
  78. if (activeTab.value === key) return
  79. activeTab.value = key
  80. loadList(true)
  81. }
  82. onShow(() => loadList(true))
  83. onReachBottom(() => loadList(false))
  84. </script>
  85. <template>
  86. <view class="mp-page mp-page--sub">
  87. <view class="mp-header mp-header--bx">
  88. <text class="mp-subpage-back" @click="goBack">← 工作台</text>
  89. <text class="mp-header__title" style="margin-top: 16rpx">我的单据</text>
  90. </view>
  91. <view class="mp-section" style="padding-bottom: 0">
  92. <scroll-view scroll-x class="mp-tabs">
  93. <view
  94. v-for="t in tabs"
  95. :key="t.key"
  96. class="mp-tab"
  97. :class="{ 'mp-tab--active': activeTab === t.key }"
  98. @click="switchTab(t.key)"
  99. >
  100. {{ t.label }}
  101. </view>
  102. </scroll-view>
  103. </view>
  104. <view class="mp-section">
  105. <mp-empty v-if="!list.length && !loading" title="暂无单据" />
  106. <view v-else class="mp-list">
  107. <view v-for="row in list" :key="row._biz + '-' + row.id" class="mp-card" @click="goDetail(row)">
  108. <view class="mp-card__head">
  109. <view>
  110. <text class="mp-card__title">{{ row._title }}</text>
  111. <text class="mp-card__sub">{{ row._sub }}</text>
  112. </view>
  113. <text class="mp-tag" :class="'mp-tag--' + bxStatusLabel(row.status).tag">
  114. {{ bxStatusLabel(row.status).text }}
  115. </text>
  116. </view>
  117. <view class="mp-card__row">
  118. <text>申请时间</text>
  119. <text class="mp-card__row-val">{{ dash(row.applyTime) }}</text>
  120. </view>
  121. <view class="mp-card__foot">
  122. <text class="mp-card__amount">{{ formatMoney(row.amount) }}</text>
  123. </view>
  124. </view>
  125. </view>
  126. <view v-if="loading" class="mp-list-loading">加载中…</view>
  127. <view v-else-if="finished && list.length" class="mp-list-end">已加载全部</view>
  128. </view>
  129. </view>
  130. </template>