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
+31 -12
View File
@@ -2,6 +2,7 @@ import { request } from '../../utils/request'
import { QuotaItem, UserInfo, GoodsItem, QuotaFlowItem, PagedResult, VirtualPayResult, OrderItem } from '../../types/models'
import { QUOTA_NAMES, GoodsType, GOODS_TYPE_NAMES } from '../../utils/config'
import { fenToYuan, formatDate, formatDateTime } from '../../utils/format'
import { normalizePaged } from '../../utils/paged'
const GOODS_ICONS: Record<number, string> = {
[GoodsType.MEMBER]: 'icon-crown',
@@ -48,6 +49,7 @@ Page({
goodsIcons: GOODS_ICONS,
orderVisible: false,
orderGoods: null as GoodsItem | null,
submitting: false, // 下单防连点:连点「立即支付」会重复创建虚拟支付订单
loading: true,
},
@@ -94,7 +96,7 @@ Page({
if (filter > 0) params.goods_type = filter
request<PagedResult<GoodsItem>>({ url: '/goods', data: params, auth: false })
.then((res) => {
const goods = res.list.map((g) => ({
const goods = normalizePaged<GoodsItem>(res).list.map((g) => ({
...g,
_icon: GOODS_ICONS[g.goods_type] || 'icon-crown',
_typeName: GOODS_TYPE_NAMES[g.goods_type] || '',
@@ -109,7 +111,7 @@ Page({
fetchFlow() {
request<PagedResult<QuotaFlowItem>>({ url: '/quota/flow', data: { page: 1, page_size: 50 } })
.then((res) => {
const list = res.list.map((it) => ({
const list = normalizePaged<QuotaFlowItem>(res).list.map((it) => ({
...it,
_time: formatDateTime(it.created_at),
_sign: it.change_num > 0 ? `+${it.change_num}` : `${it.change_num}`,
@@ -151,7 +153,10 @@ Page({
noop() {},
confirmPay() {
if (this.data.submitting) return
if (!this.data.orderGoods) return
this.setData({ submitting: true })
request<VirtualPayResult>({
url: '/order/virtual',
method: 'POST',
@@ -161,10 +166,10 @@ Page({
},
})
.then((res) => {
this.setData({ orderVisible: false })
this.setData({ orderVisible: false, submitting: false })
this.doPayment(res)
})
.catch(() => {})
.catch(() => this.setData({ submitting: false }))
},
doPayment(res: VirtualPayResult) {
@@ -182,35 +187,49 @@ Page({
},
pollOrder(orderNo: string) {
this.clearPayTimer()
let count = 0
const timer = setInterval(() => {
;(this as any).payTimer = setInterval(() => {
count++
request<PagedResult<OrderItem>>({ url: '/order', data: { page: 1, page_size: 50 }, showError: false })
.then((res) => {
const order = res.list.find((it) => it.order_no === orderNo)
const order = normalizePaged<OrderItem>(res).list.find((it) => it.order_no === orderNo)
if (order && order.order_status === 2) {
clearInterval(timer)
this.clearPayTimer()
wx.showToast({ title: '支付成功', icon: 'success' })
this.fetchQuota()
this.fetchUser()
} else if (order && (order.order_status === 3 || order.order_status === 4 || order.order_status === 5)) {
clearInterval(timer)
this.clearPayTimer()
const msg = order.order_status === 5 ? '订单已退款' : '订单未支付成功'
wx.showToast({ title: msg, icon: 'none' })
} else if (count >= 10) {
this.finishPayTimeout(timer)
this.finishPayTimeout()
}
})
.catch(() => {
if (count >= 10) this.finishPayTimeout(timer)
if (count >= 10) this.finishPayTimeout()
})
}, 1500)
},
finishPayTimeout(timer: ReturnType<typeof setInterval>) {
clearInterval(timer)
clearPayTimer() {
const timer = (this as any).payTimer
if (timer) {
clearInterval(timer)
;(this as any).payTimer = null
}
},
finishPayTimeout() {
this.clearPayTimer()
wx.showToast({ title: '到账可能有延迟,请稍后查看', icon: 'none' })
this.fetchQuota()
this.fetchUser()
},
onUnload() {
// 离开商城后停止支付轮询,避免对已卸载页面 setData
this.clearPayTimer()
},
})
+1 -1
View File
@@ -89,7 +89,7 @@
<view class="order-goods">{{orderGoods.goods_name}}</view>
<view class="order-desc">{{orderGoods.description}}</view>
<view class="order-amount">实付 <text class="order-price num">{{orderGoods._priceText}}</text></view>
<view class="btn btn-primary" bindtap="confirmPay">立即支付</view>
<view class="btn btn-primary {{submitting ? 'disabled' : ''}}" bindtap="confirmPay">{{submitting ? '下单中…' : '立即支付'}}</view>
</view>
</view>
</view>