feat(路由): 添加用户登录检查功能
在路由守卫中添加用户登录检查逻辑,并在auth.js中新增获取用户登录状态的API。同时关闭调试模式以优化生产环境。
Showing
4 changed files
with
15 additions
and
2 deletions
| ... | @@ -2,6 +2,7 @@ import { fn, fetch } from '@/api/fn'; | ... | @@ -2,6 +2,7 @@ import { fn, fetch } from '@/api/fn'; |
| 2 | 2 | ||
| 3 | const Api = { | 3 | const Api = { |
| 4 | AUTH_INFO: '/srv/?a=openid_has', | 4 | AUTH_INFO: '/srv/?a=openid_has', |
| 5 | + USER_IS_LOGIN: '/srv/?a=user_is_login', | ||
| 5 | } | 6 | } |
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| ... | @@ -10,3 +11,10 @@ const Api = { | ... | @@ -10,3 +11,10 @@ const Api = { |
| 10 | * @returns | 11 | * @returns |
| 11 | */ | 12 | */ |
| 12 | export const getAuthInfoAPI = () => fn(fetch.get(Api.AUTH_INFO)); | 13 | export const getAuthInfoAPI = () => fn(fetch.get(Api.AUTH_INFO)); |
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * @description: 判断用户是否登录 | ||
| 17 | + * @param {*} | ||
| 18 | + * @returns { data: { is_login: boolean }} | ||
| 19 | + */ | ||
| 20 | +export const getUserIsLoginAPI = () => fn(fetch.get(Api.USER_IS_LOGIN)); | ... | ... |
| 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 16:52:03 | 4 | + * @LastEditTime: 2025-03-27 16:49:19 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -44,6 +44,9 @@ export const checkWxAuth = async () => { | ... | @@ -44,6 +44,9 @@ export const checkWxAuth = async () => { |
| 44 | return true | 44 | return true |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | +// 检查用户是否已登录 | ||
| 48 | + | ||
| 49 | + | ||
| 47 | // 登录权限检查 | 50 | // 登录权限检查 |
| 48 | export const checkAuth = (to) => { | 51 | export const checkAuth = (to) => { |
| 49 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | 52 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | ... | ... |
| ... | @@ -27,6 +27,8 @@ router.beforeEach(async (to, from, next) => { | ... | @@ -27,6 +27,8 @@ router.beforeEach(async (to, from, next) => { |
| 27 | return | 27 | return |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | + // 检查用户是否已登录 | ||
| 31 | + | ||
| 30 | // 登录权限检查 | 32 | // 登录权限检查 |
| 31 | const authResult = checkAuth(to) | 33 | const authResult = checkAuth(to) |
| 32 | if (authResult !== true) { | 34 | if (authResult !== true) { | ... | ... |
-
Please register or login to post a comment