88 lines
2.4 KiB
JavaScript

const auth = require('../../../utils/auth');
const { formatTime } = require('../../../utils/format');
const PAGE_SIZE = 10;
// orderStatus 枚举 → 显示文案 & 样式 key
const STATUS_MAP = {
0: { text: '待支付', key: 'pending' },
1: { text: '已支付', key: 'success' },
2: { text: '已取消', key: 'cancelled' },
3: { text: '已退款', key: 'refunded' },
4: { text: '已发货', key: 'success' },
5: { text: '已完成', key: 'success' }
};
function formatOrder(item) {
const statusInfo = STATUS_MAP[item.orderStatus] || { text: item.orderStatus || '-', key: 'default' };
return {
...item,
statusText: statusInfo.text,
statusKey: statusInfo.key,
payTimeStr: item.paymentTime ? formatTime(item.paymentTime) : (item.createdTime ? formatTime(item.createdTime) : '-'),
actualAmount: item.actualAmount != null ? item.actualAmount : '-',
refundAmount: item.refundAmount != null && item.refundAmount > 0 ? item.refundAmount : ''
};
}
Page({
data: {
loading: false,
noMore: false,
pageNo: 1,
total: 0,
orders: []
},
onLoad() {
this.loadOrders(true);
},
onPullDownRefresh() {
this.loadOrders(true).finally(() => wx.stopPullDownRefresh());
},
async loadOrders(refresh = false) {
if (this.data.loading) return;
const pageNo = refresh ? 1 : this.data.pageNo;
if (!refresh && this.data.noMore) return;
this.setData({ loading: true });
try {
const res = await auth.fetchMyOrders({ pageNo, pageSize: PAGE_SIZE });
const rows = (res && res.rows) || [];
const total = (res && res.total) || 0;
const formatted = rows.map(formatOrder);
const newOrders = refresh ? formatted : [...this.data.orders, ...formatted];
const noMore = newOrders.length >= total;
this.setData({
orders: newOrders,
pageNo: pageNo + 1,
total,
noMore
});
} catch (err) {
wx.showToast({ title: err?.message || '加载失败', icon: 'none' });
} finally {
this.setData({ loading: false });
}
},
onBack() {
if (getCurrentPages().length > 1) {
wx.navigateBack();
} else {
wx.switchTab({ url: '/pages/mine/mine' });
}
},
onOrderTap(e) {
const { id } = e.currentTarget.dataset;
wx.navigateTo({ url: `/subpackages/mine/order-detail/order-detail?id=${id}` });
},
onReachBottom() {
this.loadOrders(false);
}
});