refactor(router): 将checkAuth函数改为异步并添加用户信息请求逻辑
将checkAuth函数从同步改为异步,并添加了请求用户信息接口的逻辑,以便在成功获取用户信息时更新本地存储。这样可以更好地处理用户登录状态的检查。
Showing
1 changed file
with
22 additions
and
22 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-26 09:55:40 | 4 | + * @LastEditTime: 2025-03-26 10:07:48 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| 8 | import { getAuthInfoAPI } from '@/api/auth' | 8 | import { getAuthInfoAPI } from '@/api/auth' |
| 9 | import { getUserInfoAPI } from '@/api/users' | 9 | import { getUserInfoAPI } from '@/api/users' |
| 10 | -import { wxInfo } from "@/utils/tools" | 10 | +import { wxInfo } from '@/utils/tools' |
| 11 | 11 | ||
| 12 | // 需要登录才能访问的路由 | 12 | // 需要登录才能访问的路由 |
| 13 | export const authRequiredRoutes = [ | 13 | export const authRequiredRoutes = [ |
| ... | @@ -29,21 +29,23 @@ export const authRequiredRoutes = [ | ... | @@ -29,21 +29,23 @@ export const authRequiredRoutes = [ |
| 29 | export const checkWxAuth = async () => { | 29 | export const checkWxAuth = async () => { |
| 30 | if (!import.meta.env.DEV && wxInfo().isWeiXin) { | 30 | if (!import.meta.env.DEV && wxInfo().isWeiXin) { |
| 31 | try { | 31 | try { |
| 32 | - const { code, data } = await getAuthInfoAPI(); | 32 | + const { code, data } = await getAuthInfoAPI() |
| 33 | if (code && !data.openid_has) { | 33 | if (code && !data.openid_has) { |
| 34 | // 直接在这里处理授权跳转 | 34 | // 直接在这里处理授权跳转 |
| 35 | const params = new URLSearchParams({ | 35 | const params = new URLSearchParams({ |
| 36 | f: 'behalo', | 36 | f: 'behalo', |
| 37 | a: 'openid', | 37 | a: 'openid', |
| 38 | - res: encodeURIComponent(location.origin + location.pathname + location.hash) | 38 | + res: encodeURIComponent( |
| 39 | - }); | 39 | + location.origin + location.pathname + location.hash, |
| 40 | + ), | ||
| 41 | + }) | ||
| 40 | 42 | ||
| 41 | if (import.meta.env.DEV) { | 43 | if (import.meta.env.DEV) { |
| 42 | - params.append('test_openid', import.meta.env.VITE_OPENID); | 44 | + params.append('test_openid', import.meta.env.VITE_OPENID) |
| 43 | } | 45 | } |
| 44 | 46 | ||
| 45 | - location.href = `/srv/?${params.toString()}`; | 47 | + location.href = `/srv/?${params.toString()}` |
| 46 | - return false; | 48 | + return false |
| 47 | } | 49 | } |
| 48 | } catch (error) { | 50 | } catch (error) { |
| 49 | console.error('微信授权检查失败:', error) | 51 | console.error('微信授权检查失败:', error) |
| ... | @@ -53,20 +55,18 @@ export const checkWxAuth = async () => { | ... | @@ -53,20 +55,18 @@ export const checkWxAuth = async () => { |
| 53 | } | 55 | } |
| 54 | 56 | ||
| 55 | // 登录权限检查 | 57 | // 登录权限检查 |
| 56 | -export const checkAuth = (to) => { | 58 | +export const checkAuth = async (to) => { |
| 57 | - // TODO: 模拟测试接口 | 59 | + try { |
| 58 | - // export const checkAuth = async (to) => { | 60 | + // 先请求用户信息接口 |
| 59 | - // try { | 61 | + const { code, data } = await getUserInfoAPI() |
| 60 | - // // 先请求用户信息接口 | 62 | + if (code) { |
| 61 | - // const { code, data } = await getUserInfoAPI(); | 63 | + // 如果成功获取用户信息,更新currentUser并允许访问 |
| 62 | - // if (code) { | 64 | + localStorage.setItem('currentUser', JSON.stringify(data)) |
| 63 | - // // 如果成功获取用户信息,更新currentUser并允许访问 | 65 | + return true |
| 64 | - // localStorage.setItem('currentUser', JSON.stringify(data)); | 66 | + } |
| 65 | - // return true; | 67 | + } catch (error) { |
| 66 | - // } | 68 | + console.error('获取用户信息失败:', error) |
| 67 | - // } catch (error) { | 69 | + } |
| 68 | - // console.error('获取用户信息失败:', error); | ||
| 69 | - // } | ||
| 70 | 70 | ||
| 71 | // 如果接口请求失败或返回401,继续原有的本地存储判断逻辑 | 71 | // 如果接口请求失败或返回401,继续原有的本地存储判断逻辑 |
| 72 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | 72 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | ... | ... |
-
Please register or login to post a comment