const orderApi = require('../../api/order') const auth = require('../../utils/auth') const navigation = require('../../utils/navigation') const actionGuard = require('../../utils/action-guard') const theme = require('../../utils/theme') const { resolveLegacyImage } = require('../../utils/file') const { formatDateTime, formatPrice, getOrderStatusClass, getOrderStatusText } = require('../../utils/format') const { runWithLoading } = require('../../utils/loading') /** * orders page logic. * Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks. */ Page({ data: Object.assign(theme.pageThemeData(getApp().globalData.shopTheme), { loading: false, isLoggedIn: false, orders: [] }), /** * `onShow` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ onShow() { this.loadOrders() }, async loadOrders() { const isLoggedIn = auth.isLoggedIn() this.setData({ isLoggedIn, loading: isLoggedIn }) /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ 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), shopNameText: order.shopName ? `所属店铺:${order.shopName}` : '', 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` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleOpenAuth() { navigation.openAuth('/pages/orders/index') }, /** * `handleBack` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ handleBack() { navigation.goBack('/pages/profile/index') }, async handleCancelOrder(event) { const orderId = event.currentTarget.dataset.orderId /** * `if` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ 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` page method. Use this from WXML event bindings or lifecycle hooks. * Call guide: read or update page data with `this.setData(...)` inside the method when state changes. */ 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' }) } } })