Files
miniwx/app.js
T
chenglijuan d5478429c0 feat: 添加登录失败处理及记录格式化功能
增加登录失败状态处理和重试机制
将记录格式化逻辑提取到工具函数
优化云数据库查询性能
2026-04-18 22:34:35 +08:00

53 lines
1.2 KiB
JavaScript

// app.js
App({
onLaunch() {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
traceUser: true
})
}
// 自动静默登录:调用云函数获取 openid
this.silentLogin()
},
silentLogin() {
wx.cloud.callFunction({
name: 'login',
data: {},
success: (res) => {
const openid = res.result.openid
const userInfo = res.result.userInfo || {}
userInfo.openid = openid
this.globalData.userInfo = userInfo
this.globalData.isLoggedIn = true
wx.setStorageSync('userInfo', userInfo)
// 通知当前页面刷新
if (this.loginReadyCallback) {
this.loginReadyCallback(userInfo)
}
},
fail: (err) => {
console.error('静默登录失败', err)
this.globalData.isLoggedIn = false
this.globalData.loginFailed = true
// 通知页面登录失败
if (this.loginReadyCallback) {
this.loginReadyCallback(null)
}
}
})
},
globalData: {
userInfo: null,
isLoggedIn: false,
loginFailed: false
}
})