feat: 添加登录失败处理及记录格式化功能

增加登录失败状态处理和重试机制
将记录格式化逻辑提取到工具函数
优化云数据库查询性能
This commit is contained in:
chenglijuan
2026-04-18 22:34:35 +08:00
parent 54556374b1
commit d5478429c0
7 changed files with 138 additions and 86 deletions
+28 -22
View File
@@ -1,19 +1,26 @@
// index.js
const { appointmentDB } = require('../../utils/cloud')
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 = () => {
this.onLoginReady()
app.loginReadyCallback = (userInfo) => {
if (userInfo) {
this.onLoginReady()
} else {
this.onLoginFailed()
}
}
}
},
@@ -25,20 +32,32 @@ Page({
},
onLoginReady() {
this.setData({ isLoggedIn: true })
this.setData({ isLoggedIn: true, loginFailed: false })
this.loadLatestRecord()
},
async loadLatestRecord() {
if (!this.data.isLoggedIn) {
this.setData({ latestRecord: null })
return
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: this.formatRecord(record) })
this.setData({ latestRecord: formatRecord(record) })
} else {
this.setData({ latestRecord: null })
}
@@ -48,19 +67,6 @@ Page({
}
},
formatRecord(record) {
const date = record.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 { ...record, createTime: createTimeStr }
},
goAppointment() {
wx.navigateTo({
url: '/pages/appointment/appointment'