Files
miniwx/pages/index/index.js
T
ws 055549f198 refactor: 重构微信小程序从云开发迁移至后端API
将云数据库操作改为调用后端API接口
提取登录失败处理逻辑到单独方法
添加环境配置和API请求工具类
移除云开发相关配置
2026-04-21 17:30:53 +08:00

82 lines
1.6 KiB
JavaScript

// index.js
const { appointmentDB, formatRecord } = require('../../utils/api')
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'
})
}
})