hookehuyr

fix(路由): 修复授权跳转逻辑并优化页面导航处理

改进授权检查逻辑,避免重复跳转到授权页面
使用setTimeout延迟导航操作防止冲突
将reLaunch改为navigateTo/switchTab避免重复触发onLaunch
添加多级降级处理增强导航健壮性
/*
* @Date: 2025-06-28 10:33:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-08 17:49:20
* @LastEditTime: 2025-07-14 16:45:23
* @FilePath: /jgdl/src/app.js
* @Description: 文件描述
*/
......@@ -17,12 +17,18 @@ const App = createApp({
// 未授权状态跳转授权页面,首页不需要权限
const path = options.path;
const query = options.query;
// 缓存没有权限的地址
const router = routerStore();
router.add(path);
// if (path !== 'pages/index/index' && !wx.getStorageSync("sessionid")) {
if (!wx.getStorageSync("sessionid")) {
console.warn("没有权限");
// 检查是否有sessionid
const sessionid = wx.getStorageSync("sessionid");
// 如果没有sessionid且不是auth页面本身,才跳转到授权页面
if (!sessionid && path !== 'pages/auth/index') {
// 添加延迟避免与页面初始化冲突
setTimeout(() => {
if (path === 'pages/productDetail/index') {
Taro.navigateTo({
url: `./pages/auth/index?url=${path}&id=${query.id}`,
......@@ -32,6 +38,7 @@ const App = createApp({
url: './pages/auth/index?url=' + path,
})
}
}, 100);
}
},
onShow(options) {
......
......@@ -131,8 +131,11 @@ export default {
if (params.url === 'pages/productDetail/index') {
// 详情页的分享跳转处理 - 使用绝对路径
this.navigateToPage(`/pages/productDetail/index?id=${params.id}`);
} else if (params.url && params.url !== 'pages/auth/index') {
// 如果有指定的目标页面且不是auth页面本身,跳转到该页面
this.navigateToPage(`/${params.url}`);
} else {
// 其他页面跳转到首页
// 其他情况跳转到首页
this.navigateToHome();
}
} catch (error) {
......@@ -144,32 +147,48 @@ export default {
/**
* 跳转到指定页面
* @param {string} url - 页面路径
* @param {string} url - 目标页面路径
*/
navigateToPage(url) {
Taro.reLaunch({
url: url
setTimeout(() => {
// 使用navigateTo而不是reLaunch,避免重新触发app.js的onLaunch
Taro.navigateTo({
url
}).catch(err => {
console.error('页面跳转失败:', err);
// 降级处理:跳转到首页
console.error('navigateTo跳转失败,尝试reLaunch:', err);
// 降级处理:使用reLaunch
Taro.reLaunch({
url
}).catch(err2 => {
console.error('reLaunch跳转也失败:', err2);
// 最终降级处理:跳转到首页
this.navigateToHome();
});
});
}, 300);
},
/**
* 跳转到首页
*/
navigateToHome() {
Taro.reLaunch({
// 使用switchTab跳转到首页,避免重新触发app.js的onLaunch
Taro.switchTab({
url: `/pages/index/index`
}).catch(err => {
console.error('跳转首页失败:', err);
console.error('switchTab跳转首页失败,尝试reLaunch:', err);
// 降级处理:使用reLaunch
Taro.reLaunch({
url: `/pages/index/index`
}).catch(err2 => {
console.error('reLaunch跳转首页也失败:', err2);
// 最后的降级处理
Taro.showToast({
title: '页面跳转失败',
icon: 'error'
});
});
});
},
/**
......