hookehuyr

fix(auth): 添加token过期检查并修复toast样式导入

在auth.js中添加了token过期检查功能,确保用户信息在token过期时不会被恢复。同时,在fn.js中修复了toast样式未导入的问题,确保toast显示正常。
1 /* 1 /*
2 * @Date: 2022-05-18 22:56:08 2 * @Date: 2022-05-18 22:56:08
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2023-02-10 15:13:01 4 + * @LastEditTime: 2025-03-21 18:05:34
5 - * @FilePath: /data-table/src/api/fn.js 5 + * @FilePath: /mlaj/src/api/fn.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
8 import axios from '@/utils/axios'; 8 import axios from '@/utils/axios';
9 import qs from 'Qs' 9 import qs from 'Qs'
10 import { showSuccessToast, showFailToast } from 'vant'; 10 import { showSuccessToast, showFailToast } from 'vant';
11 +import 'vant/lib/toast/style'
11 12
12 /** 13 /**
13 * 网络请求功能函数 14 * 网络请求功能函数
...@@ -49,7 +50,7 @@ export const uploadFn = (api) => { ...@@ -49,7 +50,7 @@ export const uploadFn = (api) => {
49 // tslint:disable-next-line: no-console 50 // tslint:disable-next-line: no-console
50 console.warn(res); 51 console.warn(res);
51 if (!res.data.show) return false; 52 if (!res.data.show) return false;
52 - Toast({ 53 + showFailToast({
53 icon: 'close', 54 icon: 'close',
54 message: res.data.msg, 55 message: res.data.msg,
55 }); 56 });
......
1 +/*
2 + * @Date: 2025-03-20 21:11:31
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2025-03-22 10:08:20
5 + * @FilePath: /mlaj/src/contexts/auth.js
6 + * @Description: 文件描述
7 + */
1 import { ref, provide, inject, onMounted } from 'vue' 8 import { ref, provide, inject, onMounted } from 'vue'
2 9
3 // 创建认证上下文的key 10 // 创建认证上下文的key
...@@ -10,12 +17,30 @@ export function provideAuth() { ...@@ -10,12 +17,30 @@ export function provideAuth() {
10 const currentUser = ref(null) 17 const currentUser = ref(null)
11 const loading = ref(true) 18 const loading = ref(true)
12 19
20 + // 检查token是否过期(24小时)
21 + const checkTokenExpiration = () => {
22 + const loginTimestamp = localStorage.getItem('loginTimestamp')
23 + if (loginTimestamp) {
24 + const now = Date.now()
25 + const diff = now - parseInt(loginTimestamp)
26 + if (diff > 24 * 60 * 60 * 1000) { // 24小时
27 + // 清理登录状态并返回false表示token已过期
28 + logout()
29 + return false
30 + }
31 + }
32 + return true
33 + }
34 +
13 // 初始化时检查本地存储的用户信息 35 // 初始化时检查本地存储的用户信息
14 onMounted(() => { 36 onMounted(() => {
15 const savedUser = localStorage.getItem('currentUser') 37 const savedUser = localStorage.getItem('currentUser')
16 if (savedUser) { 38 if (savedUser) {
39 + // 只有在token未过期的情况下才恢复用户信息
40 + if (checkTokenExpiration()) {
17 currentUser.value = JSON.parse(savedUser) 41 currentUser.value = JSON.parse(savedUser)
18 } 42 }
43 + }
19 loading.value = false 44 loading.value = false
20 }) 45 })
21 46
...@@ -23,6 +48,7 @@ export function provideAuth() { ...@@ -23,6 +48,7 @@ export function provideAuth() {
23 const login = (userData) => { 48 const login = (userData) => {
24 currentUser.value = userData 49 currentUser.value = userData
25 localStorage.setItem('currentUser', JSON.stringify(userData)) 50 localStorage.setItem('currentUser', JSON.stringify(userData))
51 + localStorage.setItem('loginTimestamp', Date.now().toString())
26 return true 52 return true
27 } 53 }
28 54
...@@ -30,6 +56,7 @@ export function provideAuth() { ...@@ -30,6 +56,7 @@ export function provideAuth() {
30 const logout = () => { 56 const logout = () => {
31 currentUser.value = null 57 currentUser.value = null
32 localStorage.removeItem('currentUser') 58 localStorage.removeItem('currentUser')
59 + localStorage.removeItem('loginTimestamp')
33 } 60 }
34 61
35 // 提供认证上下文 62 // 提供认证上下文
......