book-min/miniprogram/app.ts

41 lines
819 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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;
}
});