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

+6
View File
@@ -0,0 +1,6 @@
const app = getApp();
module.exports = {
get apiBaseUrl() { return app.globalData.apiBaseUrl; },
get brand() { return app.globalData.brand || {}; },
};
+14
View File
@@ -0,0 +1,14 @@
const STATUS_LABELS = { received: '待确认', contacting: '沟通中', confirmed: '已确认', completed: '已完成', cancelled: '已取消' };
const FULFILLMENT_LABELS = { pickup: '自取', delivery: '配送' };
function bookingViewModel(item) {
return {
...item,
statusLabel: STATUS_LABELS[item.status] || '未知状态',
fulfillmentLabel: FULFILLMENT_LABELS[item.fulfillmentType] || '待确认',
serviceName: item.service?.name || item.items?.[0]?.serviceNameSnapshot || '未命名服务',
quantityLabel: `${item.items?.[0]?.quantity || 1} 份`,
};
}
module.exports = { STATUS_LABELS, FULFILLMENT_LABELS, bookingViewModel };
+25
View File
@@ -0,0 +1,25 @@
const config = require('./config');
function request(path, options = {}) {
return new Promise((resolve, reject) => {
wx.request({
url: config.apiBaseUrl + path,
method: options.method || 'GET',
header: { accept: 'application/json', ...(options.header || {}) },
...(options.data === undefined ? {} : { data: options.data }),
success: (response) => {
if (response.statusCode >= 200 && response.statusCode < 300) return resolve(response.data);
const error = response.data?.error?.message || '请求失败';
reject(Object.assign(new Error(error), { statusCode: response.statusCode, response }));
},
fail: reject,
});
});
}
function customerRequest(path, options = {}) {
const token = wx.getStorageSync('customerToken');
return request(path, { ...options, header: { ...(options.header || {}), Authorization: 'Bearer ' + token } });
}
module.exports = { request, customerRequest };