109 lines
2.6 KiB
JavaScript
109 lines
2.6 KiB
JavaScript
const orderApi = require('../../api/order')
|
|
const auth = require('../../utils/auth')
|
|
const navigation = require('../../utils/navigation')
|
|
const { resolveLegacyImage } = require('../../utils/file')
|
|
const { formatDateTime, formatPrice, getOrderStatusClass, getOrderStatusText } = require('../../utils/format')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
isLoggedIn: false,
|
|
orders: []
|
|
},
|
|
|
|
onShow() {
|
|
this.loadOrders()
|
|
},
|
|
|
|
async loadOrders() {
|
|
const isLoggedIn = auth.isLoggedIn()
|
|
this.setData({
|
|
isLoggedIn,
|
|
loading: isLoggedIn
|
|
})
|
|
|
|
if (!isLoggedIn) {
|
|
this.setData({
|
|
orders: [],
|
|
loading: false
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
const orders = await orderApi.getOrderList()
|
|
const list = Array.isArray(orders) ? orders : []
|
|
const formattedOrders = await Promise.all(list.map(async order => ({
|
|
...order,
|
|
statusText: getOrderStatusText(order),
|
|
statusClass: getOrderStatusClass(order),
|
|
actualAmountText: `¥${formatPrice(order.actualAmount)}`,
|
|
items: await Promise.all((order.items || []).map(async item => ({
|
|
...item,
|
|
productImageUrl: await resolveLegacyImage(item.productImage),
|
|
priceText: `¥${formatPrice(item.price)}`,
|
|
appointmentText: formatDateTime(item.appointmentTime)
|
|
})))
|
|
})))
|
|
|
|
this.setData({
|
|
orders: formattedOrders
|
|
})
|
|
} catch (error) {
|
|
this.setData({
|
|
orders: []
|
|
})
|
|
wx.showToast({
|
|
title: error.message || '订单列表加载失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
loading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
handleOpenAuth() {
|
|
navigation.openAuth('/pages/orders/index')
|
|
},
|
|
|
|
handleBack() {
|
|
navigation.goBack('/pages/profile/index')
|
|
},
|
|
|
|
async handleCancelOrder(event) {
|
|
const orderId = event.currentTarget.dataset.orderId
|
|
if (!orderId) {
|
|
return
|
|
}
|
|
|
|
const result = await new Promise(resolve => {
|
|
wx.showModal({
|
|
title: '取消订单',
|
|
content: '确认取消这笔订单吗?',
|
|
success: modalRes => resolve(Boolean(modalRes.confirm))
|
|
})
|
|
})
|
|
|
|
if (!result) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
await runWithLoading(() => orderApi.cancelOrder(orderId))
|
|
wx.showToast({
|
|
title: '订单已取消',
|
|
icon: 'none'
|
|
})
|
|
this.loadOrders()
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '取消订单失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|