Files
miniwx/app.js
T
2026-04-18 22:15:11 +08:00

46 lines
1001 B
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
}
})
},
globalData: {
userInfo: null,
isLoggedIn: false
}
})