fix(路由): 修复授权跳转逻辑并优化页面导航处理
改进授权检查逻辑,避免重复跳转到授权页面 使用setTimeout延迟导航操作防止冲突 将reLaunch改为navigateTo/switchTab避免重复触发onLaunch 添加多级降级处理增强导航健壮性
Showing
2 changed files
with
38 additions
and
12 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-06-28 10:33:00 | 2 | * @Date: 2025-06-28 10:33:00 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-07-08 17:49:20 | 4 | + * @LastEditTime: 2025-07-14 16:45:23 |
| 5 | * @FilePath: /jgdl/src/app.js | 5 | * @FilePath: /jgdl/src/app.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -17,12 +17,18 @@ const App = createApp({ | ... | @@ -17,12 +17,18 @@ const App = createApp({ |
| 17 | // 未授权状态跳转授权页面,首页不需要权限 | 17 | // 未授权状态跳转授权页面,首页不需要权限 |
| 18 | const path = options.path; | 18 | const path = options.path; |
| 19 | const query = options.query; | 19 | const query = options.query; |
| 20 | + | ||
| 20 | // 缓存没有权限的地址 | 21 | // 缓存没有权限的地址 |
| 21 | const router = routerStore(); | 22 | const router = routerStore(); |
| 22 | router.add(path); | 23 | router.add(path); |
| 23 | - // if (path !== 'pages/index/index' && !wx.getStorageSync("sessionid")) { | 24 | + |
| 24 | - if (!wx.getStorageSync("sessionid")) { | 25 | + // 检查是否有sessionid |
| 25 | - console.warn("没有权限"); | 26 | + const sessionid = wx.getStorageSync("sessionid"); |
| 27 | + | ||
| 28 | + // 如果没有sessionid且不是auth页面本身,才跳转到授权页面 | ||
| 29 | + if (!sessionid && path !== 'pages/auth/index') { | ||
| 30 | + // 添加延迟避免与页面初始化冲突 | ||
| 31 | + setTimeout(() => { | ||
| 26 | if (path === 'pages/productDetail/index') { | 32 | if (path === 'pages/productDetail/index') { |
| 27 | Taro.navigateTo({ | 33 | Taro.navigateTo({ |
| 28 | url: `./pages/auth/index?url=${path}&id=${query.id}`, | 34 | url: `./pages/auth/index?url=${path}&id=${query.id}`, |
| ... | @@ -32,6 +38,7 @@ const App = createApp({ | ... | @@ -32,6 +38,7 @@ const App = createApp({ |
| 32 | url: './pages/auth/index?url=' + path, | 38 | url: './pages/auth/index?url=' + path, |
| 33 | }) | 39 | }) |
| 34 | } | 40 | } |
| 41 | + }, 100); | ||
| 35 | } | 42 | } |
| 36 | }, | 43 | }, |
| 37 | onShow(options) { | 44 | onShow(options) { | ... | ... |
| ... | @@ -131,8 +131,11 @@ export default { | ... | @@ -131,8 +131,11 @@ export default { |
| 131 | if (params.url === 'pages/productDetail/index') { | 131 | if (params.url === 'pages/productDetail/index') { |
| 132 | // 详情页的分享跳转处理 - 使用绝对路径 | 132 | // 详情页的分享跳转处理 - 使用绝对路径 |
| 133 | this.navigateToPage(`/pages/productDetail/index?id=${params.id}`); | 133 | this.navigateToPage(`/pages/productDetail/index?id=${params.id}`); |
| 134 | + } else if (params.url && params.url !== 'pages/auth/index') { | ||
| 135 | + // 如果有指定的目标页面且不是auth页面本身,跳转到该页面 | ||
| 136 | + this.navigateToPage(`/${params.url}`); | ||
| 134 | } else { | 137 | } else { |
| 135 | - // 其他页面跳转到首页 | 138 | + // 其他情况跳转到首页 |
| 136 | this.navigateToHome(); | 139 | this.navigateToHome(); |
| 137 | } | 140 | } |
| 138 | } catch (error) { | 141 | } catch (error) { |
| ... | @@ -144,32 +147,48 @@ export default { | ... | @@ -144,32 +147,48 @@ export default { |
| 144 | 147 | ||
| 145 | /** | 148 | /** |
| 146 | * 跳转到指定页面 | 149 | * 跳转到指定页面 |
| 147 | - * @param {string} url - 页面路径 | 150 | + * @param {string} url - 目标页面路径 |
| 148 | */ | 151 | */ |
| 149 | navigateToPage(url) { | 152 | navigateToPage(url) { |
| 150 | - Taro.reLaunch({ | 153 | + setTimeout(() => { |
| 151 | - url: url | 154 | + // 使用navigateTo而不是reLaunch,避免重新触发app.js的onLaunch |
| 155 | + Taro.navigateTo({ | ||
| 156 | + url | ||
| 152 | }).catch(err => { | 157 | }).catch(err => { |
| 153 | - console.error('页面跳转失败:', err); | 158 | + console.error('navigateTo跳转失败,尝试reLaunch:', err); |
| 154 | - // 降级处理:跳转到首页 | 159 | + // 降级处理:使用reLaunch |
| 160 | + Taro.reLaunch({ | ||
| 161 | + url | ||
| 162 | + }).catch(err2 => { | ||
| 163 | + console.error('reLaunch跳转也失败:', err2); | ||
| 164 | + // 最终降级处理:跳转到首页 | ||
| 155 | this.navigateToHome(); | 165 | this.navigateToHome(); |
| 156 | }); | 166 | }); |
| 167 | + }); | ||
| 168 | + }, 300); | ||
| 157 | }, | 169 | }, |
| 158 | 170 | ||
| 159 | /** | 171 | /** |
| 160 | * 跳转到首页 | 172 | * 跳转到首页 |
| 161 | */ | 173 | */ |
| 162 | navigateToHome() { | 174 | navigateToHome() { |
| 163 | - Taro.reLaunch({ | 175 | + // 使用switchTab跳转到首页,避免重新触发app.js的onLaunch |
| 176 | + Taro.switchTab({ | ||
| 164 | url: `/pages/index/index` | 177 | url: `/pages/index/index` |
| 165 | }).catch(err => { | 178 | }).catch(err => { |
| 166 | - console.error('跳转首页失败:', err); | 179 | + console.error('switchTab跳转首页失败,尝试reLaunch:', err); |
| 180 | + // 降级处理:使用reLaunch | ||
| 181 | + Taro.reLaunch({ | ||
| 182 | + url: `/pages/index/index` | ||
| 183 | + }).catch(err2 => { | ||
| 184 | + console.error('reLaunch跳转首页也失败:', err2); | ||
| 167 | // 最后的降级处理 | 185 | // 最后的降级处理 |
| 168 | Taro.showToast({ | 186 | Taro.showToast({ |
| 169 | title: '页面跳转失败', | 187 | title: '页面跳转失败', |
| 170 | icon: 'error' | 188 | icon: 'error' |
| 171 | }); | 189 | }); |
| 172 | }); | 190 | }); |
| 191 | + }); | ||
| 173 | }, | 192 | }, |
| 174 | 193 | ||
| 175 | /** | 194 | /** | ... | ... |
-
Please register or login to post a comment