feat: 重构HTTP客户端架构和认证系统

核心改进:
- HTTP客户端:工厂函数模式,支持自定义拦截器和401/403处理
- 认证服务:函数式实现,消除this绑定问题,支持业务码+HTTP状态码双通道
- Token管理:简化为直接实例导出,移除bind()和箭头函数包装
- 路由守卫:优化逻辑,移除冗余代码,更简洁易维护

技术亮点:
- 统一401/403错误处理(业务code和HTTP status双检查)
- 自动刷新token并重试请求,保留自定义拦截器
- 分层清晰:clientAxios (Mono) -> http (应用) -> AuthService
- 支持扩展:业务代码可创建自定义HTTP实例并添加拦截器

文件变更:
- 新增 AuthService.js (函数式) 和 Login.vue
- 重构 http.js、token-manager.js、router/index.js
- 删除 TokenInput.vue、utils/auth.js 等冗余文件
- 更新所有API调用点使用直接实例导入

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-25 00:58:51 +08:00
parent cea43dd635
commit fb6d18b4f5
24 changed files with 1148 additions and 1198 deletions

View File

@@ -1,12 +1,21 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getToken } from '@gold/utils/token-manager'
import tokenManager from '@gold/utils/token-manager'
const routes = [
{
path: '/',
redirect: '/content-style/benchmark'
},
// 登录页面
{
path: '/login',
name: '登录',
component: () => import('../views/auth/Login.vue'),
meta: {
requiresAuth: false
}
},
// { path: '/home', name: '首页', component: () => import('../views/home/Home.vue') },
{
path: '/content-style',
@@ -53,7 +62,10 @@ const routes = [
children: [
{ path: '', redirect: '/system/style-settings' },
{ path: 'style-settings', name: '风格设置', component: () => import('../views/system/StyleSettings.vue') },
]
],
meta: {
requiresAuth: true
}
},
{ path: '/realtime-hot', name: '实时热点推送', component: () => import('../views/realtime/RealtimeHot.vue') },
{ path: '/capcut-import', name: '剪映导入', component: () => import('../views/capcut/CapcutImport.vue') },
@@ -67,57 +79,46 @@ const router = createRouter({
routes,
})
// 用户信息初始化标志(确保只初始化一次)
let userInfoInitialized = false
/**
* 路由导航守卫:初始化用户信息 + 登录验证
* 在首次路由跳转时,如果已登录(有 token则获取用户信息
* 如果未登录访问系统相关路由,则重定向到首页
*/
// 路由守卫
router.beforeEach(async (to, from, next) => {
const userStore = useUserStore()
// 只在首次路由跳转时初始化用户信息
if (!userInfoInitialized) {
userInfoInitialized = true
const token = getToken()
if (token) {
try {
// 如果 store 中已标记为登录,则获取用户信息
if (userStore.isLoggedIn) {
userStore.fetchUserInfo()
} else {
// 如果有 token 但 store 中未标记为登录,可能是刷新页面
// 先标记为已登录,然后获取用户信息
userStore.isLoggedIn = true
userStore.fetchUserInfo()
}
} catch (error) {
console.error('初始化用户信息失败:', error)
// 不阻止路由跳转,继续执行
}
}
}
// 检查访问系统相关路由时是否已登录
if (to.path.startsWith('/system')) {
// 等待 store 从本地存储恢复完成最多等待500ms
// 等待 Pinia store 恢复完成(最多等待 500ms
if (to.meta.requiresAuth) {
let waitCount = 0
while (!userStore.isHydrated && waitCount < 50) {
await new Promise(resolve => setTimeout(resolve, 10))
waitCount++
}
// 如果未登录,重定向到首页
if (!userStore.isLoggedIn) {
next({ path: '/content-style/benchmark', replace: true })
return
}
}
// 继续路由跳转
// 检查是否已登录(通过 token 是否有效)
const isAuthenticated = tokenManager.isLoggedIn()
// 路由访问控制
if (to.meta.requiresAuth && !isAuthenticated) {
// 需要认证但未登录,跳转到登录页并记录当前路径
next({
path: '/login',
query: { redirect: to.fullPath }
})
return
}
// 已登录用户访问登录页,重定向到首页
if (to.path === '/login' && isAuthenticated) {
next({ path: '/content-style/benchmark', replace: true })
return
}
// 首次访问且已登录时,同步用户信息到 store
if (isAuthenticated && !userStore.isLoggedIn) {
userStore.isLoggedIn = true
userStore.fetchUserInfo().catch(error => {
console.error('初始化用户信息失败:', error)
})
}
next()
})