book-min/miniprogram/app.ts

41 lines
819 B
TypeScript
Raw Normal View History

2024-12-18 16:15:08 +08:00
// app.ts
App<IAppOption>({
globalData: {
userInfo: null,
isLoggedIn: false
},
onLaunch() {
// 检查登录状态
const token = wx.getStorageSync('token');
const userInfo = wx.getStorageSync('userInfo');
if (token && userInfo) {
this.globalData.isLoggedIn = true;
this.globalData.userInfo = userInfo;
} else {
// 未登录则跳转到登录页
wx.redirectTo({
url: '/pages/login/index'
});
}
// 登录
wx.login({
success: res => {
console.log('微信登录成功code:', res.code);
},
});
},
// 检查登录状态的方法
checkLogin() {
if (!this.globalData.isLoggedIn) {
wx.redirectTo({
url: '/pages/login/index'
});
return false;
}
return true;
}
});