refactor: 模块化小程序配置与请求逻辑

This commit is contained in:
que01 committed 2026-09-19 12:00:54 +08:00
1 parent 5b5ba9f7da
commit 34ec893369
11 files changed
+67 -20

No files matched your search

+4 -5
View File
@@ -1,10 +1,9 @@
const API = getApp().globalData.apiBaseUrl;
const STATUS_LABELS = { received: '待确认', contacting: '沟通中', confirmed: '已确认', completed: '已完成', cancelled: '已取消' };
const FULFILLMENT_LABELS = { pickup: '自取', delivery: '配送' };
const { customerRequest } = require('../../utils/request');
const { bookingViewModel } = require('../../utils/labels');
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 || '网络请求失败' }) }); },
load() { const token = wx.getStorageSync('customerToken'); if (!token) return this.setData({ loading: false, bookings: [], error: '' }); this.setData({ loading: true, error: '' }); customerRequest('/customer/bookings').then((items) => this.setData({ bookings: (Array.isArray(items) ? items : []).map(bookingViewModel), loading: false })).catch((error) => this.setData({ loading: false, error: error.message || '网络请求失败' })); },
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' }); } }); } }); }
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 }); customerRequest('/customer/bookings/' + id + '/cancel', { method: 'POST', data: {} }).then(() => { this.setData({ cancellingId: '' }); wx.showToast({ title: '已取消预约', icon: 'success' }); this.load(); }).catch((error) => { this.setData({ cancellingId: '' }); wx.showToast({ title: error.message || '取消失败', icon: 'none' }); }); } }); }
});