Files
sionrui/frontend/config/api.js
sion123 fb6d18b4f5 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>
2025-11-25 00:58:51 +08:00

53 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 公共 API 基础配置
* 统一管理所有 API 的基础 URL
* 可在各个应用中通过 @gold/config/api 引用
*/
/**
* 获取基础 URL从环境变量读取
* @returns {string}
*/
function getBaseUrl() {
// 支持在浏览器环境和 Node 环境
if (typeof import.meta !== 'undefined' && import.meta.env) {
return import.meta.env.VITE_BASE_URL || ''
}
// 如果是在 Node 环境或 SSR可以从 process.env 读取
if (typeof process !== 'undefined' && process.env) {
return process.env.VITE_BASE_URL || ''
}
return ''
}
const BASE_URL = getBaseUrl()
/**
* API 基础路径配置
*/
export const API_BASE = {
// 会员端 API
APP: `${BASE_URL}`,
// 具体模块路径
APP_AI: `${BASE_URL}/api/ai`,
APP_MEMBER: `${BASE_URL}/app-api/member`,
// 特殊路径
TIKHUB_APP: `${BASE_URL}/api/tikHup`,
AI_APP: `${BASE_URL}/api/ai`,
}
/**
* 获取完整的 API 路径
* @param {string} module - 模块名称 (如 'ADMIN_AI', 'APP_MEMBER')
* @param {string} path - 接口路径 (如 '/chat/conversation/create-my')
* @returns {string} 完整的 API URL
*/
export function getApiUrl(module, path) {
const base = API_BASE[module] || API_BASE.APP
return `${base}${path.startsWith('/') ? path : '/' + path}`
}
export default API_BASE