a2702f6d
刘淇
巡查计划
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// 监听所有页面显示(核心:检测登录状态)
onShow(() => {
checkLoginStatus();
});
// 登录检测核心函数
const checkLoginStatus = () => {
// 1. 获取当前页面信息(避免登录页循环跳转)
const pages = getCurrentPages();
if (pages.length === 0) return; // 无页面时跳过
const currentPage = pages[pages.length - 1];
const currentPath = currentPage.route; // 当前页面路径(如:pages/plan/index)
// // 2. 白名单:无需登录的页面(登录页必须加,其他按需加)
// const whiteList = ['pages/login/index', 'pages/register/index']; // 可追加首页等
// if (whiteList.includes(currentPath)) return;
// 3. 检测登录状态(仅判断token是否存在,极简版)
const token = uni.getStorageSync('jcss_token'); // 登录成功后需存token
if (!token) {
// 记录当前页面(登录后可返回)
uni.setStorageSync('loginRedirectPath', currentPath);
// 跳转到登录页(用redirectTo避免返回栈残留)
uni.redirectTo({
url: '/pages/login/index'
});
}
};
|