refactor: 使用router.push替代window.location.href,实现SPA无刷新跳转

问题:
- 使用window.location.href会导致整页刷新,影响用户体验
- Vue应用应使用router.push实现单页应用内的路由跳转

修复:
1. http.js:
   - 导入router实例
   - 401错误:router.push('/login')
   - 403错误:router.push('/login')

2. AuthService.js:
   - 导入router实例
   - logout函数:router.push('/login')

效果:
- 页面跳转更流畅,用户体验更佳
- 保持应用状态,避免不必要的重新加载
- 符合Vue单页应用的最佳实践

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-25 01:19:56 +08:00
parent 4fc0837890
commit 36fb09feb6
4 changed files with 14 additions and 30 deletions

View File

@@ -7,6 +7,7 @@
import { createClientAxios } from '@gold/api/axios/client'
import authService from '@/services/AuthService'
import router from '@/router'
/**
* 创建应用层 HTTP 客户端实例
@@ -32,8 +33,8 @@ export function createHttpClient(options = {}) {
await authService.handleAuthError(
error,
() => {
// 刷新失败,跳转到登录页
window.location.href = '/login'
// 刷新失败,使用router跳转避免整页刷新
router.push('/login')
}
)
// 如果刷新成功AuthService返回null不跳转不抛出错误
@@ -44,8 +45,8 @@ export function createHttpClient(options = {}) {
if (on403) {
on403(error)
} else {
console.warn('403权限不足跳转到登录页')
window.location.href = '/login'
console.warn('403权限不足使用router跳转到登录页')
router.push('/login')
}
},
})