Files
miniwx/pages/scan/result/index.js
T
2026-04-29 18:29:50 +08:00

98 lines
2.5 KiB
JavaScript

const { appointmentDB, formatRecord } = require('../../../utils/api')
Page({
data: {
record: null,
loading: true,
error: '',
verifying: false
},
onLoad(options) {
const id = this.extractId(options)
if (!id) {
this.setData({ loading: false, error: '缺少预约记录ID' })
return
}
this.loadRecordDetail(id)
},
extractId(options) {
if (options.id) return options.id
if (!options.scene) return null
const scene = decodeURIComponent(options.scene)
return scene.startsWith('id=') ? scene.substring(3) : scene
},
async loadRecordDetail(id) {
try {
this.setData({ loading: true })
const result = await appointmentDB.getDetail(id)
if (!result) {
this.setData({ loading: false, error: '预约记录不存在' })
return
}
const statusMap = {
'pending': '待审核',
'approved': '已通过',
'rejected': '已拒绝',
'cancelled': '已取消'
}
const checkStatusMap = {
'1': '已核销',
'0': '未核销'
}
this.setData({
record: {
...formatRecord(result),
statusText: statusMap[result.status] || result.status,
checkStatusText: checkStatusMap[result.checkStatus] || '未核销',
isChecked: result.checkStatus === '1'
},
loading: false
})
} catch (err) {
console.error('加载预约记录详情失败', err)
this.setData({ loading: false, error: '加载失败,请稍后重试' })
}
},
async handleVerify() {
const { record } = this.data
if (!record || record.isChecked || this.data.verifying) return
const confirmed = await new Promise((resolve) => {
wx.showModal({
title: '确认核销',
content: '确认通知被访人访客已到达并核销此预约记录?',
confirmText: '确认核销',
confirmColor: '#1890ff',
success: (res) => resolve(res.confirm)
})
})
if (!confirmed) return
try {
this.setData({ verifying: true })
await appointmentDB.notifyHostArrival(record._id)
this.setData({
'record.isChecked': true,
'record.checkStatusText': '已核销',
verifying: false
})
wx.showToast({ title: '核销成功,已通知被访人', icon: 'success' })
} catch (err) {
console.error('核销失败', err)
this.setData({ verifying: false })
wx.showToast({ title: err.message || '核销失败,请稍后重试', icon: 'none' })
}
}
})