refactor(frontend): Rework home and video detail interactions

This commit is contained in:
2026-04-24 17:35:13 +08:00
parent 833dd3451a
commit 59af57c0fa
38 changed files with 2693 additions and 986 deletions
@@ -1,6 +1,6 @@
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
import { useUserStore } from '@/stores/user.ts'
import { showToast } from 'vant'
import { showToast } from '@/utils/toast'
import router from '@/router'
import { mockRequest, isMockEnabled } from '@/mock'
@@ -9,6 +9,25 @@ const BASE_URL = import.meta.env.VITE_API_BASE_URL || '/api'
const TIMEOUT = 30000
let handlingUnauthorized = false
const shouldRedirectToLogin = () => {
const currentRoute = router.currentRoute.value
return Boolean(currentRoute.meta?.requiresAuth)
}
const handleUnauthorized = (message = '登录已过期,请重新登录') => {
const userStore = useUserStore()
userStore.clearAuth()
if (!handlingUnauthorized) {
handlingUnauthorized = true
if (shouldRedirectToLogin() && router.currentRoute.value.path !== '/login') {
router.push('/login')
showToast(message)
return
}
}
}
// 创建 axios 实例
const service: AxiosInstance = axios.create({
baseURL: BASE_URL,
@@ -49,15 +68,7 @@ service.interceptors.response.use(
// Token 过期
if (data.code === 401) {
const userStore = useUserStore()
if (!handlingUnauthorized) {
handlingUnauthorized = true
userStore.clearAuth()
if (router.currentRoute.value.path !== '/login') {
router.push('/login')
}
showToast('登录已过期,请重新登录')
}
handleUnauthorized()
return Promise.reject(new Error('Token expired'))
}
@@ -77,15 +88,7 @@ service.interceptors.response.use(
const status = error.response.status
switch (status) {
case 401: {
const userStore = useUserStore()
if (!handlingUnauthorized) {
handlingUnauthorized = true
userStore.clearAuth()
if (router.currentRoute.value.path !== '/login') {
router.push('/login')
}
showToast('登录已过期,请重新登录')
}
handleUnauthorized()
break
}
case 403:
@@ -94,6 +97,9 @@ service.interceptors.response.use(
case 404:
showToast('请求的资源不存在')
break
case 413:
showToast('上传文件过大,请选择更小的文件')
break
case 500:
showToast('服务器错误,请稍后重试')
break
@@ -120,15 +126,7 @@ async function mockFetch<T>(method: string, url: string, data?: any, params?: an
}
if (result.code === 401) {
const userStore = useUserStore()
if (!handlingUnauthorized) {
handlingUnauthorized = true
userStore.clearAuth()
if (router.currentRoute.value.path !== '/login') {
router.push('/login')
}
showToast('登录已过期,请重新登录')
}
handleUnauthorized()
throw new Error('Token expired')
}