|
|
@@ -4,9 +4,9 @@ import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
|
|
|
/**
|
|
|
* 应用启动
|
|
|
*/
|
|
|
-onLaunch(() => {
|
|
|
- console.log("App Launch");
|
|
|
- checkLoginStatus();
|
|
|
+onLaunch((options: any) => {
|
|
|
+ console.log("App Launch, options:", JSON.stringify(options));
|
|
|
+ checkLoginStatus(options);
|
|
|
});
|
|
|
|
|
|
/**
|
|
|
@@ -30,22 +30,35 @@ onHide(() => {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+/**
|
|
|
+ * 免登录白名单页面
|
|
|
+ * 这些页面允许未登录用户直接访问
|
|
|
+ */
|
|
|
+const LOGIN_WHITE_LIST = [
|
|
|
+ 'pages/login/login',
|
|
|
+ 'pages/products/products',
|
|
|
+];
|
|
|
+
|
|
|
/**
|
|
|
* 检查登录状态
|
|
|
- * 如果未登录且当前不在登录页,则跳转到登录页
|
|
|
+ * 如果未登录且当前不在白名单页面,则跳转到登录页
|
|
|
*/
|
|
|
-const checkLoginStatus = () => {
|
|
|
+const checkLoginStatus = (launchOptions?: any) => {
|
|
|
const token = uni.getStorageSync('accessToken');
|
|
|
|
|
|
- // 获取当前页面路径
|
|
|
- const pages = getCurrentPages();
|
|
|
- const currentPage = pages[pages.length - 1];
|
|
|
- const currentPath = currentPage ? currentPage.route : '';
|
|
|
+ // 优先从启动参数获取目标页面路径(onLaunch时getCurrentPages为空)
|
|
|
+ let currentPath = launchOptions?.path || '';
|
|
|
+ if (!currentPath) {
|
|
|
+ const pages = getCurrentPages();
|
|
|
+ const currentPage = pages[pages.length - 1];
|
|
|
+ currentPath = currentPage ? currentPage.route : '';
|
|
|
+ }
|
|
|
|
|
|
- console.log('[App] 检查登录状态 - token:', token ? '已存在' : '不存在', ', 当前页面:', currentPath);
|
|
|
+ console.log('[App] 检查登录状态 - token:', token ? '已存在' : '不存在', ', 目标页面:', currentPath);
|
|
|
|
|
|
- // 如果没有token且不在登录页,跳转到登录页
|
|
|
- if (!token && currentPath !== 'pages/login/login') {
|
|
|
+ // 如果没有token且不在白名单页面,跳转到登录页
|
|
|
+ const isWhiteListed = LOGIN_WHITE_LIST.some(path => currentPath.indexOf(path) !== -1);
|
|
|
+ if (!token && !isWhiteListed) {
|
|
|
console.log('[App] 未登录,跳转到登录页');
|
|
|
uni.reLaunch({
|
|
|
url: '/pages/login/login'
|