const API = getApp().globalData.apiBaseUrl; const STATUS_LABELS = { received: '待确认', contacting: '沟通中', confirmed: '已确认', completed: '已完成', cancelled: '已取消' }; const FULFILLMENT_LABELS = { pickup: '自取', delivery: '配送' }; Page({ data: { loading: true, error: '', bookings: [], cancellingId: '' }, onLoad() { this.load(); }, onShow() { this.load(); }, load() { const token = wx.getStorageSync('customerToken'); if (!token) return this.setData({ loading: false, bookings: [], error: '' }); this.setData({ loading: true, error: '' }); wx.request({ url: API + '/customer/bookings', header: { Authorization: 'Bearer ' + token }, success: (res) => { if (res.statusCode >= 300) return this.setData({ loading: false, error: res.data?.error?.message || '订单加载失败' }); const bookings = (Array.isArray(res.data) ? res.data : []).map((item) => ({ ...item, statusLabel: STATUS_LABELS[item.status] || '未知状态', fulfillmentLabel: FULFILLMENT_LABELS[item.fulfillmentType] || '待确认', serviceName: item.service?.name || item.items?.[0]?.serviceName || '红烧肉家常套餐', quantityLabel: `${item.items?.[0]?.quantity || 1} 份` })); this.setData({ bookings, loading: false }); }, fail: (error) => this.setData({ loading: false, error: error.errMsg || '网络请求失败' }) }); }, retry() { this.load(); }, contact() { wx.showToast({ title: '暂未配置联系方式', icon: 'none' }); }, cancel(e) { const id = e.currentTarget.dataset.id; if (this.data.cancellingId) return; wx.showModal({ title: '取消预约', content: '确定取消这笔预约吗?', success: (result) => { if (!result.confirm) return; this.setData({ cancellingId: id }); wx.request({ url: API + '/customer/bookings/' + id + '/cancel', method: 'POST', header: { Authorization: 'Bearer ' + wx.getStorageSync('customerToken'), 'content-type': 'application/json' }, data: {}, success: (res) => { this.setData({ cancellingId: '' }); if (res.statusCode < 300) { wx.showToast({ title: '已取消预约', icon: 'success' }); this.load(); } else wx.showToast({ title: res.data?.error?.message || '取消失败', icon: 'none' }); }, fail: (error) => { this.setData({ cancellingId: '' }); wx.showToast({ title: error.errMsg || '网络请求失败', icon: 'none' }); } }); } }); } });