Files

127 lines
3.1 KiB
JavaScript

// records.js
const { appointmentDB, formatRecord } = require('../../utils/api')
const app = getApp()
Page({
data: {
records: [],
filteredRecords: [],
currentTab: 'all',
loading: true,
isLoggedIn: false,
loginFailed: false
},
onLoad() {
this._awaitLoginAndLoad()
},
async _awaitLoginAndLoad() {
const userInfo = await app.waitLogin(true)
if (userInfo) {
this.setData({ isLoggedIn: true, loginFailed: false })
this.loadRecords()
} else {
this.setData({ isLoggedIn: false, loginFailed: true, loading: false })
}
},
onShow() {
// 已加载过且已登录时刷新(从预约页返回等场景)
if (this.data._loaded && app.globalData.isLoggedIn) {
this.loadRecords()
}
},
async onRetry() {
this.setData({ loginFailed: false, loading: true })
app.silentLogin()
const userInfo = await app.waitLogin()
if (userInfo) {
this.setData({ isLoggedIn: true, loginFailed: false })
this.loadRecords()
} else {
this.setData({ loginFailed: true })
}
},
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'
})
},
showQrcode(e) {
this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id)
},
onShareAppMessage(res) {
return {
title: '访客预约',
path: '/pages/index/index'
}
},
onShareTimeline() {
return {
title: '访客预约'
}
}
})