d5478429c0
增加登录失败状态处理和重试机制 将记录格式化逻辑提取到工具函数 优化云数据库查询性能
82 lines
1.6 KiB
JavaScript
82 lines
1.6 KiB
JavaScript
// index.js
|
|
const { appointmentDB, formatRecord } = require('../../utils/cloud')
|
|
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
isLoggedIn: false,
|
|
loginFailed: false,
|
|
latestRecord: null
|
|
},
|
|
|
|
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() {
|
|
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 })
|
|
}
|
|
},
|
|
|
|
goAppointment() {
|
|
wx.navigateTo({
|
|
url: '/pages/appointment/appointment'
|
|
})
|
|
},
|
|
|
|
goRecords() {
|
|
wx.navigateTo({
|
|
url: '/pages/records/records'
|
|
})
|
|
}
|
|
})
|