You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
// app.js
|
|
const { isAuthenticated } = require('./utils/token');
|
|
|
|
App({
|
|
onLaunch() {
|
|
// 展示本地存储能力
|
|
const logs = wx.getStorageSync('logs') || []
|
|
logs.unshift(Date.now())
|
|
wx.setStorageSync('logs', logs)
|
|
|
|
// 登录
|
|
wx.login({
|
|
success: res => {
|
|
// 发送 res.code 到后台换取 openId, sessionKey, unionId
|
|
}
|
|
})
|
|
|
|
// 检查token有效性并自动跳转
|
|
this.checkTokenAndRedirect();
|
|
},
|
|
|
|
globalData: {
|
|
userInfo: null
|
|
},
|
|
|
|
/**
|
|
* 检查token有效性并自动跳转到相应页面
|
|
*/
|
|
checkTokenAndRedirect() {
|
|
if (isAuthenticated()) {
|
|
console.log('用户已认证,跳转到首页');
|
|
// 如果用户已经登录,跳转到首页
|
|
wx.switchTab({
|
|
url: '/pages/index/index',
|
|
fail: (error) => {
|
|
console.error('跳转到首页失败:', error);
|
|
}
|
|
});
|
|
} else {
|
|
console.log('用户未认证,跳转到登录页');
|
|
// 如果用户未登录,跳转到登录页
|
|
wx.redirectTo({
|
|
url: '/pages/login/login',
|
|
fail: (error) => {
|
|
console.error('跳转到登录页失败:', error);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
})
|