91 lines
2.0 KiB
JavaScript
91 lines
2.0 KiB
JavaScript
// index.js
|
|
const { appointmentDB, formatRecord } = require('../../utils/api')
|
|
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
isLoggedIn: false,
|
|
loginFailed: false,
|
|
latestRecord: null,
|
|
recordLoading: false
|
|
},
|
|
|
|
onLoad() {
|
|
// 首页不阻塞,直接渲染;有缓存登录态时立即加载数据
|
|
if (app.globalData.isLoggedIn) {
|
|
this.setData({ isLoggedIn: true })
|
|
this.loadLatestRecord()
|
|
}
|
|
},
|
|
|
|
async onShow() {
|
|
// 每次显示时等待登录完成,再加载最新数据
|
|
const userInfo = await app.waitLogin(true)
|
|
if (userInfo) {
|
|
this.setData({ isLoggedIn: true, loginFailed: false })
|
|
this.loadLatestRecord()
|
|
} else {
|
|
this.setData({ isLoggedIn: false, loginFailed: true })
|
|
}
|
|
},
|
|
|
|
async onRetry() {
|
|
this.setData({ loginFailed: false })
|
|
app.silentLogin()
|
|
const userInfo = await app.waitLogin()
|
|
if (userInfo) {
|
|
this.setData({ isLoggedIn: true, loginFailed: false })
|
|
this.loadLatestRecord()
|
|
} else {
|
|
this.setData({ loginFailed: true })
|
|
}
|
|
},
|
|
|
|
async loadLatestRecord() {
|
|
this.setData({ recordLoading: true })
|
|
try {
|
|
const openid = app.globalData.userInfo.openid
|
|
const record = await appointmentDB.getLatest(openid)
|
|
if (record) {
|
|
this.setData({ latestRecord: formatRecord(record) })
|
|
} else {
|
|
this.setData({ latestRecord: null })
|
|
}
|
|
} catch (err) {
|
|
console.error('加载最新预约失败', err)
|
|
this.setData({ latestRecord: null })
|
|
} finally {
|
|
this.setData({ recordLoading: false })
|
|
}
|
|
},
|
|
|
|
goAppointment() {
|
|
wx.navigateTo({
|
|
url: '/pages/appointment/appointment'
|
|
})
|
|
},
|
|
|
|
goRecords() {
|
|
wx.navigateTo({
|
|
url: '/pages/records/records'
|
|
})
|
|
},
|
|
|
|
showQrcode(e) {
|
|
this.selectComponent('#qrcodeModal').show(e.currentTarget.dataset.id)
|
|
},
|
|
|
|
onShareAppMessage(res) {
|
|
return {
|
|
title: '访客预约',
|
|
path: '/pages/index/index'
|
|
}
|
|
},
|
|
|
|
onShareTimeline() {
|
|
return {
|
|
title: '访客预约'
|
|
}
|
|
}
|
|
})
|