const { request, customerRequest } = require("../../utils/request"); const { bookingViewModel } = require("../../utils/labels"); const { getTemplate } = require("../../utils/templates"); Page({ data: { loading: true, error: "", bookings: [], cancellingId: "", template: getTemplate("personal"), }, 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: "" }); Promise.all([customerRequest("/customer/bookings"), request("/public/brand")]) .then(([items, brand]) => { getApp().globalData.brand = brand; const template = getTemplate(brand.templateKey); this.setData({ template, bookings: (Array.isArray(items) ? items : []).map((item) => bookingViewModel(item, template), ), loading: false, }); }) .catch((error) => this.setData({ loading: false, error: error.message || "网络请求失败" })); }, retry() { this.load(); }, contact() { const brand = getApp().globalData.brand || {}; if (brand.contactPhone) return wx.makePhoneCall({ phoneNumber: brand.contactPhone }); if (brand.contactWechat) return wx.showModal({ title: "联系品牌方", content: "微信号:" + brand.contactWechat, showCancel: false, }); 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 }); 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" }); }); }, }); }, });