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

+5 -5
View File
@@ -1,7 +1,7 @@
const API = getApp().globalData.apiBaseUrl;
const { request, customerRequest } = require('../../utils/request');
Page({
data: { service: {}, form: {}, serviceId: '', quantity: 1, fulfillment: 'pickup', submitting: false },
onLoad(query) { wx.request({ url: API + '/public/services', success: (res) => { const services = Array.isArray(res.data) ? res.data : []; const service = services.find((item) => item.id === query.serviceId) || services.find((item) => item.priceAmount !== null) || {}; this.setData({ service, serviceId: service.id || '' }); }, fail: () => wx.showToast({ title: '服务加载失败', icon: 'none' }) }); },
onLoad(query) { request('/public/services').then((services) => { const list = Array.isArray(services) ? services : []; const service = list.find((item) => item.id === query.serviceId) || list.find((item) => item.priceAmount !== null) || {}; this.setData({ service, serviceId: service.id || '' }); }).catch(() => wx.showToast({ title: '服务加载失败', icon: 'none' })); },
input(event) { this.setData({ ['form.' + event.currentTarget.dataset.key]: event.detail.value }); },
chooseFulfillment(event) { this.setData({ fulfillment: event.currentTarget.dataset.value }); },
increase() { if (this.data.quantity < 50) this.setData({ quantity: this.data.quantity + 1 }); },
@@ -12,9 +12,9 @@ Page({
if (!form.customerName || !form.wechatId) return wx.showToast({ title: '请填写称呼和联系方式', icon: 'none' });
if (this.data.fulfillment === 'delivery' && !form.deliveryAddress) return wx.showToast({ title: '请填写配送地址', icon: 'none' });
this.setData({ submitting: true });
const send = (token) => wx.request({ url: API + '/customer/bookings', method: 'POST', header: { Authorization: 'Bearer ' + token }, data: { requestType: 'package', customerName: form.customerName, wechatId: form.wechatId, contactPreference: 'wechat', fulfillmentType: this.data.fulfillment, deliveryAddress: form.deliveryAddress, requestedTime: form.requestedTime, note: form.note, items: [{ serviceId: this.data.serviceId, quantity: this.data.quantity }] }, success: (res) => { this.setData({ submitting: false }); if (res.statusCode < 300) { wx.showToast({ title: '预约已提交' }); setTimeout(() => wx.navigateTo({ url: '/pages/bookings/index' }), 500); } else wx.showToast({ title: res.data?.error?.message || '提交失败', icon: 'none' }); }, fail: (error) => { this.setData({ submitting: false }); wx.showToast({ title: error.errMsg || '网络请求失败', icon: 'none' }); } });
const send = () => customerRequest('/customer/bookings', { method: 'POST', data: { requestType: 'package', customerName: form.customerName, wechatId: form.wechatId, contactPreference: 'wechat', fulfillmentType: this.data.fulfillment, deliveryAddress: form.deliveryAddress, requestedTime: form.requestedTime, note: form.note, items: [{ serviceId: this.data.serviceId, quantity: this.data.quantity }] }).then(() => { this.setData({ submitting: false }); wx.showToast({ title: '预约已提交' }); setTimeout(() => wx.navigateTo({ url: '/pages/bookings/index' }), 500); }).catch((error) => { this.setData({ submitting: false }); wx.showToast({ title: error.message || '提交失败', icon: 'none' }); });
const token = wx.getStorageSync('customerToken');
if (token) return send(token);
wx.login({ success: (login) => wx.request({ url: API + '/customer/session/wechat', method: 'POST', data: { code: login.code }, success: (res) => { if (res.statusCode < 300) { wx.setStorageSync('customerToken', res.data.token); send(res.data.token); } else { this.setData({ submitting: false }); wx.showToast({ title: res.data?.error?.message || '微信登录失败', icon: 'none' }); } }, fail: (error) => { this.setData({ submitting: false }); wx.showToast({ title: error.errMsg || '微信登录请求失败', icon: 'none' }); } }), fail: (error) => { this.setData({ submitting: false }); wx.showToast({ title: error.errMsg || '微信登录失败', icon: 'none' }); } });
if (token) return send();
wx.login({ success: (login) => request('/customer/session/wechat', { method: 'POST', data: { code: login.code } }).then((result) => { wx.setStorageSync('customerToken', result.token); send(); }).catch((error) => { this.setData({ submitting: false }); wx.showToast({ title: error.message || '微信登录失败', icon: 'none' }); }), fail: (error) => { this.setData({ submitting: false }); wx.showToast({ title: error.errMsg || '微信登录失败', icon: 'none' }); } });
},
});