This commit is contained in:
2026-09-13 01:51:09 +08:00
parent b1a664a768
commit afd0973ce5
32 changed files with 935 additions and 171 deletions
+57 -5
View File
@@ -1,6 +1,7 @@
import { request } from '../../utils/request'
import { PagedResult, OrderItem } from '../../types/models'
import { fenToYuan, formatDateTime, formatCountdown } from '../../utils/format'
import { normalizePaged, mergePaged, hasMorePaged } from '../../utils/paged'
const STATUS_FILTERS = [
{ name: '全部', value: 0 },
@@ -20,6 +21,7 @@ Page({
activeFilter: 0,
list: [] as OrderItem[],
loading: true,
loadingMore: false,
page: 1,
pageSize: 20,
hasMore: true,
@@ -27,6 +29,15 @@ Page({
onShow() {
this.refresh()
this.startCountdown()
},
onHide() {
this.stopCountdown()
},
onUnload() {
this.stopCountdown()
},
refresh() {
@@ -35,19 +46,56 @@ Page({
},
onReachBottom() {
if (!this.data.hasMore || this.data.loading) return
if (!this.data.hasMore || this.data.loading || this.data.loadingMore) return
this.fetchList(this.data.page + 1)
},
// 待支付倒计时需要每秒走动,仅在有倒计时项时才 setData
startCountdown() {
this.stopCountdown()
;(this as any).tickTimer = setInterval(() => this.tickCountdown(), 1000)
},
stopCountdown() {
const timer = (this as any).tickTimer
if (timer) {
clearInterval(timer)
;(this as any).tickTimer = null
}
},
tickCountdown() {
const list = this.data.list
if (!list.length) return
const now = Math.floor(Date.now() / 1000)
let changed = false
const next = list.map((it: any) => {
if (it.order_status !== 1 || !it.expire_time) return it
const left = it.expire_time - now
const text = left > 0 ? formatCountdown(left) : ''
if (text === it._countdown) return it
changed = true
return { ...it, _countdown: text }
})
if (changed) this.setData({ list: next })
},
fetchList(page: number) {
const params: Record<string, any> = { page, page_size: this.data.pageSize }
const filter = this.data.activeFilter
if (filter > 0) params.status = filter
// 请求序号:切换筛选时旧响应可能后到,序号不匹配直接丢弃
const seq = ((this as any).reqSeq || 0) + 1
;(this as any).reqSeq = seq
if (page > 1) this.setData({ loadingMore: true })
request<PagedResult<OrderItem>>({ url: '/order', data: params })
.then((res) => {
if ((this as any).reqSeq !== seq) return
const { list: raw, total } = normalizePaged<OrderItem>(res)
const now = Math.floor(Date.now() / 1000)
const list = res.list.map((it) => ({
const list = raw.map((it) => ({
...it,
_statusText: STATUS_TEXT[it.order_status] || '',
_statusType: STATUS_TYPE[it.order_status] || 'gray',
@@ -59,15 +107,19 @@ Page({
_canCancel: it.order_status === 1 && it.pay_channel !== 2,
_payChannelText: it.pay_channel === 2 ? '虚拟支付' : '',
}))
const merged = page === 1 ? list : this.data.list.concat(list)
const merged = mergePaged(this.data.list, list, page === 1)
this.setData({
list: merged,
hasMore: merged.length < res.total,
hasMore: hasMorePaged(merged, total, this.data.pageSize),
loading: false,
loadingMore: false,
page,
})
})
.catch(() => this.setData({ loading: false }))
.catch(() => {
if ((this as any).reqSeq !== seq) return
this.setData({ loading: false, loadingMore: false })
})
},
onFilterTap(e: WechatMiniprogram.TouchEvent) {