117 lines
3.0 KiB
JavaScript
117 lines
3.0 KiB
JavaScript
const orderApi = require('../../api/order')
|
|
const auth = require('../../utils/auth')
|
|
const navigation = require('../../utils/navigation')
|
|
const actionGuard = require('../../utils/action-guard')
|
|
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: `\u00a5${formatPrice(order.actualAmount)}`,
|
|
items: await Promise.all((order.items || []).map(async item => ({
|
|
...item,
|
|
productImageUrl: await resolveLegacyImage(item.productImage),
|
|
priceText: `\u00a5${formatPrice(item.price)}`,
|
|
appointmentText: formatDateTime(item.appointmentTime)
|
|
})))
|
|
})))
|
|
|
|
this.setData({
|
|
orders: formattedOrders
|
|
})
|
|
} catch (error) {
|
|
this.setData({
|
|
orders: []
|
|
})
|
|
wx.showToast({
|
|
title: error.message || '\u8ba2\u5355\u5217\u8868\u52a0\u8f7d\u5931\u8d25',
|
|
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
|
|
}
|
|
|
|
try {
|
|
await actionGuard.run(this, `order:cancel:${orderId}`, async () => {
|
|
const confirmed = await new Promise(resolve => {
|
|
wx.showModal({
|
|
title: '\u53d6\u6d88\u8ba2\u5355',
|
|
content: '\u786e\u8ba4\u53d6\u6d88\u8fd9\u7b14\u8ba2\u5355\u5417\uff1f',
|
|
success: modalRes => resolve(Boolean(modalRes.confirm))
|
|
})
|
|
})
|
|
|
|
if (!confirmed) {
|
|
return
|
|
}
|
|
|
|
await runWithLoading(() => orderApi.cancelOrder(orderId))
|
|
wx.showToast({
|
|
title: '\u8ba2\u5355\u5df2\u53d6\u6d88',
|
|
icon: 'none'
|
|
})
|
|
await this.loadOrders()
|
|
}, {
|
|
message: '\u64cd\u4f5c\u8fdb\u884c\u4e2d\uff0c\u8bf7\u52ff\u91cd\u590d\u70b9\u51fb'
|
|
})
|
|
} catch (error) {
|
|
if (actionGuard.isGuardError(error)) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: error.message || '\u53d6\u6d88\u8ba2\u5355\u5931\u8d25',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
})
|