Files
miniwx/pages/records/records.js
T
2026-04-18 22:15:11 +08:00

106 lines
2.7 KiB
JavaScript

// records.js
const { appointmentDB } = require('../../utils/cloud')
const app = getApp()
Page({
data: {
records: [],
filteredRecords: [],
currentTab: 'all',
loading: true
},
onLoad() {
if (!app.globalData.isLoggedIn) {
wx.showToast({ title: '请先登录', icon: 'none' })
setTimeout(() => {
wx.navigateBack()
}, 1500)
return
}
this.loadRecords()
},
onShow() {
if (app.globalData.isLoggedIn) {
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 => {
const date = item.createTime
let createTimeStr = ''
if (date) {
if (typeof date === 'object' && date.$date) {
createTimeStr = new Date(date.$date).toLocaleString('zh-CN')
} else {
createTimeStr = new Date(date).toLocaleString('zh-CN')
}
}
return { ...item, createTime: createTimeStr }
})
this.setData({ records: formatted, loading: false })
this.filterRecords()
} catch (err) {
console.error('加载预约记录失败', err)
this.setData({ records: [], loading: false })
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'
})
}
})