refactor(frontend): Rework home layout and user interactions
This commit is contained in:
@@ -7,6 +7,7 @@ import { mockRequest, isMockEnabled } from '@/mock'
|
||||
// API 基础配置
|
||||
const BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api'
|
||||
const TIMEOUT = 30000
|
||||
let handlingUnauthorized = false
|
||||
|
||||
// 创建 axios 实例
|
||||
const service: AxiosInstance = axios.create({
|
||||
@@ -42,15 +43,21 @@ service.interceptors.response.use(
|
||||
|
||||
// 业务状态码判断
|
||||
if (data.code === 200) {
|
||||
handlingUnauthorized = false
|
||||
return data.data
|
||||
}
|
||||
|
||||
// Token 过期
|
||||
if (data.code === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
router.push('/login')
|
||||
showToast('登录已过期,请重新登录')
|
||||
if (!handlingUnauthorized) {
|
||||
handlingUnauthorized = true
|
||||
userStore.clearAuth()
|
||||
if (router.currentRoute.value.path !== '/login') {
|
||||
router.push('/login')
|
||||
}
|
||||
showToast('登录已过期,请重新登录')
|
||||
}
|
||||
return Promise.reject(new Error('Token expired'))
|
||||
}
|
||||
|
||||
@@ -69,12 +76,18 @@ service.interceptors.response.use(
|
||||
} else if (error.response) {
|
||||
const status = error.response.status
|
||||
switch (status) {
|
||||
case 401:
|
||||
case 401: {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
router.push('/login')
|
||||
showToast('登录已过期,请重新登录')
|
||||
if (!handlingUnauthorized) {
|
||||
handlingUnauthorized = true
|
||||
userStore.clearAuth()
|
||||
if (router.currentRoute.value.path !== '/login') {
|
||||
router.push('/login')
|
||||
}
|
||||
showToast('登录已过期,请重新登录')
|
||||
}
|
||||
break
|
||||
}
|
||||
case 403:
|
||||
showToast('没有权限访问')
|
||||
break
|
||||
@@ -102,14 +115,20 @@ async function mockFetch<T>(method: string, url: string, data?: any, params?: an
|
||||
})
|
||||
|
||||
if (result.code === 200) {
|
||||
handlingUnauthorized = false
|
||||
return result.data
|
||||
}
|
||||
|
||||
if (result.code === 401) {
|
||||
const userStore = useUserStore()
|
||||
userStore.logout()
|
||||
router.push('/login')
|
||||
showToast('登录已过期,请重新登录')
|
||||
if (!handlingUnauthorized) {
|
||||
handlingUnauthorized = true
|
||||
userStore.clearAuth()
|
||||
if (router.currentRoute.value.path !== '/login') {
|
||||
router.push('/login')
|
||||
}
|
||||
showToast('登录已过期,请重新登录')
|
||||
}
|
||||
throw new Error('Token expired')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user