// records.js const { appointmentDB, formatRecord } = require('../../utils/cloud') const app = getApp() Page({ data: { records: [], filteredRecords: [], currentTab: 'all', loading: true }, onLoad() { this.loadRecords() }, onShow() { // 仅从预约页返回时刷新,避免 onLoad + onShow 双重加载 if (this.data._loaded) { this.loadRecords() } }, async loadRecords() { this.setData({ loading: true }) try { const openid = app.globalData.userInfo.openid const records = await appointmentDB.getList(openid) const formatted = records.map(item => formatRecord(item)) this.setData({ records: formatted, loading: false, _loaded: true }) this.filterRecords() } catch (err) { console.error('加载预约记录失败', err) this.setData({ records: [], loading: false, _loaded: true }) this.filterRecords() } }, switchTab(e) { const tab = e.currentTarget.dataset.tab this.setData({ currentTab: tab }) this.filterRecords() }, filterRecords() { const { records, currentTab } = this.data let filtered = records if (currentTab !== 'all') { filtered = records.filter(item => item.status === currentTab) } this.setData({ filteredRecords: filtered }) }, onCancel(e) { const id = e.currentTarget.dataset.id wx.showModal({ title: '确认取消', content: '确定要取消该预约吗?', confirmColor: '#ff4d4f', success: async (res) => { if (res.confirm) { try { const openid = app.globalData.userInfo.openid const success = await appointmentDB.cancel(id, openid) if (success) { wx.showToast({ title: '已取消预约', icon: 'success' }) this.loadRecords() } else { wx.showToast({ title: '取消失败,无权限或状态不允许', icon: 'none' }) } } catch (err) { console.error('取消预约失败', err) wx.showToast({ title: '操作失败', icon: 'none' }) } } } }) }, goAppointment() { wx.navigateTo({ url: '/pages/appointment/appointment' }) } })