Files
miniwx/pages/index/index.js
T

90 lines
1.9 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.onLoginReady()
} else if (app.globalData.loginFailed) {
this.onLoginFailed()
} else {
app.loginReadyCallback = (userInfo) => {
if (userInfo) {
this.onLoginReady()
} else {
this.onLoginFailed()
}
}
}
},
onShow() {
if (app.globalData.isLoggedIn) {
this.loadLatestRecord()
}
},
onLoginReady() {
this.setData({ isLoggedIn: true, loginFailed: false })
this.loadLatestRecord()
},
onLoginFailed() {
this.setData({ isLoggedIn: false, loginFailed: true })
},
onRetry() {
this.setData({ loginFailed: false })
app.silentLogin()
app.loginReadyCallback = (userInfo) => {
if (userInfo) {
this.onLoginReady()
} else {
this.onLoginFailed()
}
}
},
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)
}
})