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:
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* Token 管理兼容层
|
||||
*
|
||||
* @deprecated 此文件已废弃,请直接使用 @gold/utils/token-manager
|
||||
*
|
||||
* 为了保持向后兼容,此文件作为兼容层,所有函数都转发到 token-manager.js
|
||||
* 新代码请直接使用 @gold/utils/token-manager 中的函数
|
||||
*/
|
||||
|
||||
import {
|
||||
getToken as getTokenFromManager,
|
||||
setToken as setTokenFromManager,
|
||||
getRefreshToken as getRefreshTokenFromManager,
|
||||
getAccessToken as getAccessTokenFromManager,
|
||||
removeToken as removeTokenFromManager,
|
||||
clearAllTokens,
|
||||
getAuthHeader
|
||||
} from '@gold/utils/token-manager'
|
||||
|
||||
/**
|
||||
* 获取访问令牌
|
||||
* @deprecated 使用 @gold/utils/token-manager 中的 getToken() 或 getAccessToken()
|
||||
*/
|
||||
export const getAccessToken = getAccessTokenFromManager
|
||||
|
||||
/**
|
||||
* 获取刷新令牌
|
||||
* @deprecated 使用 @gold/utils/token-manager 中的 getRefreshToken()
|
||||
*/
|
||||
export const getRefreshToken = getRefreshTokenFromManager
|
||||
|
||||
/**
|
||||
* 设置令牌
|
||||
* @deprecated 使用 @gold/utils/token-manager 中的 setToken()
|
||||
*/
|
||||
export const setToken = setTokenFromManager
|
||||
|
||||
/**
|
||||
* 删除令牌
|
||||
* @deprecated 使用 @gold/utils/token-manager 中的 clearAllTokens() 或 removeToken()
|
||||
*/
|
||||
export const removeToken = removeTokenFromManager
|
||||
|
||||
/**
|
||||
* 格式化token(jwt格式)
|
||||
* @param {string} token - token 字符串
|
||||
* @returns {string} Bearer token
|
||||
*/
|
||||
export const formatToken = (token) => {
|
||||
return 'Bearer ' + token
|
||||
}
|
||||
|
||||
// 导出 token-manager 的其他函数,方便迁移
|
||||
export { clearAllTokens, getAuthHeader }
|
||||
@@ -252,13 +252,13 @@ export async function streamChat(options = {}) {
|
||||
if (typewriter) typewriter.flush()
|
||||
const errorMsg = err?.message || '网络请求失败'
|
||||
console.error('SSE请求错误:', err)
|
||||
|
||||
|
||||
if (typeof onError === 'function') {
|
||||
onError(err)
|
||||
} else {
|
||||
message.error(errorMsg)
|
||||
}
|
||||
|
||||
|
||||
reject(new Error(errorMsg))
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user