refactor(router): 移除checkWxAuth函数的to参数并优化授权跳转逻辑
简化微信授权检查逻辑,移除不必要的to参数,直接在checkWxAuth函数中处理授权跳转,提高代码可读性和维护性。
Showing
2 changed files
with
17 additions
and
5 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-03-25 15:17:18 | 4 | + * @LastEditTime: 2025-03-26 08:04:21 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -25,12 +25,24 @@ export const authRequiredRoutes = [ | ... | @@ -25,12 +25,24 @@ export const authRequiredRoutes = [ |
| 25 | ] | 25 | ] |
| 26 | 26 | ||
| 27 | // 微信授权检查 | 27 | // 微信授权检查 |
| 28 | -export const checkWxAuth = async (to) => { | 28 | +export const checkWxAuth = async () => { |
| 29 | - if (!import.meta.env.DEV && wxInfo().isWeiXin && to.path !== '/auth') { | 29 | + if (!import.meta.env.DEV && wxInfo().isWeiXin) { |
| 30 | try { | 30 | try { |
| 31 | const { code, data } = await getAuthInfoAPI(); | 31 | const { code, data } = await getAuthInfoAPI(); |
| 32 | if (code && !data.openid_has) { | 32 | if (code && !data.openid_has) { |
| 33 | - return { path: '/auth', query: { href: location.hash } } | 33 | + // 直接在这里处理授权跳转 |
| 34 | + const params = new URLSearchParams({ | ||
| 35 | + f: 'behalo', | ||
| 36 | + a: 'openid', | ||
| 37 | + res: encodeURIComponent(location.origin + location.pathname + location.hash) | ||
| 38 | + }); | ||
| 39 | + | ||
| 40 | + if (import.meta.env.DEV) { | ||
| 41 | + params.append('test_openid', import.meta.env.VITE_OPENID); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + location.href = `/srv/?${params.toString()}`; | ||
| 45 | + return false; | ||
| 34 | } | 46 | } |
| 35 | } catch (error) { | 47 | } catch (error) { |
| 36 | console.error('微信授权检查失败:', error) | 48 | console.error('微信授权检查失败:', error) | ... | ... |
| ... | @@ -21,7 +21,7 @@ const router = createRouter({ | ... | @@ -21,7 +21,7 @@ const router = createRouter({ |
| 21 | // 导航守卫 | 21 | // 导航守卫 |
| 22 | router.beforeEach(async (to, from, next) => { | 22 | router.beforeEach(async (to, from, next) => { |
| 23 | // 微信授权检查 | 23 | // 微信授权检查 |
| 24 | - const wxAuthResult = await checkWxAuth(to) | 24 | + const wxAuthResult = await checkWxAuth() |
| 25 | if (wxAuthResult !== true) { | 25 | if (wxAuthResult !== true) { |
| 26 | next(wxAuthResult) | 26 | next(wxAuthResult) |
| 27 | return | 27 | return | ... | ... |
-
Please register or login to post a comment