diff --git a/frontend/app/web-gold/.env.development b/frontend/app/web-gold/.env.development index bf834e9a94..f2f68345f0 100644 --- a/frontend/app/web-gold/.env.development +++ b/frontend/app/web-gold/.env.development @@ -4,3 +4,5 @@ VITE_DEV_TOKEN=a498c8db4b4e4dbfb9e28ad2606713ec VITE_BASE_URL=/webApi # 接口地址 VITE_API_URL=/admin-api + +VITE_TENANT_ID=1 diff --git a/frontend/app/web-gold/.env.production b/frontend/app/web-gold/.env.production index 66ab5c72b4..0b876b6f47 100644 --- a/frontend/app/web-gold/.env.production +++ b/frontend/app/web-gold/.env.production @@ -3,3 +3,5 @@ VITE_TIKHUB_XHS_TOKEN=pZM4CJ484F+rxlyfLL+XmAzwMCKXVb5l2x7WsUOyCMu1rm61FiDcRQmPCQ VITE_BASE_URL=/webApi # 接口地址 VITE_API_URL=/admin-api + +VITE_TENANT_ID=1 diff --git a/frontend/app/web-gold/src/api/README.md b/frontend/app/web-gold/src/api/README.md new file mode 100644 index 0000000000..f9b2b7e96d --- /dev/null +++ b/frontend/app/web-gold/src/api/README.md @@ -0,0 +1,118 @@ +# API 统一管理说明 + +## 📁 目录结构 + +``` +api/ +├── config.js # API 基础配置(统一管理所有基础 URL) +├── index.js # 统一导出入口(所有 API 服务从这里导出) +├── http.js # Axios 实例和拦截器 +├── auth.js # 认证相关 API +├── chat.js # 聊天相关 API +├── common.js # 通用服务 API +└── tikhub/ # TikHub 相关 API + ├── index.js + ├── tikhub.js + └── types.js +``` + +## 🚀 使用方式 + +### 方式一:从统一入口导入(推荐) + +```javascript +// 导入所有 API +import { ChatMessageApi, AuthApi, CommonService, TikhubService } from '@/api' + +// 或按需导入 +import { ChatMessageApi } from '@/api' +import { AuthApi } from '@/api' +``` + +### 方式二:从具体文件导入(兼容旧代码) + +```javascript +// 仍然支持原有的导入方式 +import { ChatMessageApi } from '@/api/chat' +import { CommonService } from '@/api/common' +``` + +### 方式三:使用配置工具函数 + +```javascript +import { getApiUrl, API_BASE } from '@/api/config' + +// 获取完整 API URL +const url = getApiUrl('ADMIN_AI', '/chat/conversation/create-my') +// 结果: /admin-api/ai/chat/conversation/create-my + +// 直接使用配置 +const baseUrl = API_BASE.ADMIN_AI +``` + +## 📝 API 配置说明 + +### config.js + +所有 API 基础 URL 统一在 `config.js` 中管理: + +```javascript +export const API_BASE = { + ADMIN: '/admin-api', // 管理后台基础路径 + APP: '/app-api', // 会员端基础路径 + ADMIN_AI: '/admin-api/ai', // AI 模块(管理后台) + APP_MEMBER: '/app-api/member', // 会员模块 + TIKHUB: '/webApi/admin-api/ai/tikHup', // TikHub(管理后台) + TIKHUB_APP: '/app-api/api/tikHup', // TikHub(会员端) +} +``` + +### 添加新的 API 模块 + +1. 在 `config.js` 中添加新的基础路径: +```javascript +export const API_BASE = { + // ... 现有配置 + NEW_MODULE: `${BASE_URL}/admin-api/new-module`, +} +``` + +2. 创建新的 API 文件(如 `new-module.js`): +```javascript +import request from '@/api/http' +import { API_BASE } from '@/api/config' + +const BASE = API_BASE.NEW_MODULE + +export const NewModuleApi = { + getList: () => request.get(`${BASE}/list`), + create: (data) => request.post(`${BASE}/create`, data), +} +``` + +3. 在 `index.js` 中导出: +```javascript +export { NewModuleApi } from './new-module' +``` + +## 🔧 HTTP 实例 + +所有 API 都使用统一的 HTTP 实例(`http.js`),已配置: +- ✅ 自动 Token 注入 +- ✅ 统一错误处理 +- ✅ 请求/响应拦截器 +- ✅ 白名单机制(无需 Token 的接口) + +## 📌 注意事项 + +1. **基础 URL 配置**:所有 API 的基础 URL 都应该在 `config.js` 中定义,不要在业务文件中硬编码 +2. **统一导出**:新增 API 服务后,记得在 `index.js` 中导出 +3. **向后兼容**:保持原有的导入方式仍然可用,方便逐步迁移 + +## 🎯 最佳实践 + +1. **使用统一入口**:优先使用 `@/api` 统一导入 +2. **配置集中管理**:所有 URL 配置都在 `config.js` +3. **类型安全**:使用 TypeScript 时,可以为 API 添加类型定义 +4. **错误处理**:利用 HTTP 拦截器统一处理错误 + diff --git a/frontend/app/web-gold/src/api/auth.js b/frontend/app/web-gold/src/api/auth.js index 72f30f880c..5e9dfb87aa 100644 --- a/frontend/app/web-gold/src/api/auth.js +++ b/frontend/app/web-gold/src/api/auth.js @@ -1,8 +1,9 @@ import api from '@/api/http' import { setToken, getRefreshToken } from '@/utils/auth' +import { API_BASE } from '@/api/config' -const SERVER_BASE = import.meta.env.VITE_BASE_URL + '/app-api/member' +const SERVER_BASE = API_BASE.APP_MEMBER /** * 保存 token 的辅助函数 diff --git a/frontend/app/web-gold/src/api/chat.js b/frontend/app/web-gold/src/api/chat.js index 6fe8a16c2c..c7b63010c1 100644 --- a/frontend/app/web-gold/src/api/chat.js +++ b/frontend/app/web-gold/src/api/chat.js @@ -1,7 +1,9 @@ import request from '@/api/http' import { fetchEventSource } from '@microsoft/fetch-event-source' import { getAccessToken } from '@/utils/auth' -const SERVER_BASE_AI= import.meta.env.VITE_BASE_URL + '/admin-api/ai' +import { API_BASE } from '@/api/config' + +const SERVER_BASE_AI = API_BASE.ADMIN_AI diff --git a/frontend/app/web-gold/src/api/common.js b/frontend/app/web-gold/src/api/common.js index ea7fd9a6d4..3d8f7acf78 100644 --- a/frontend/app/web-gold/src/api/common.js +++ b/frontend/app/web-gold/src/api/common.js @@ -1,9 +1,10 @@ import http from '@/api/http' import { fetchEventSource } from '@microsoft/fetch-event-source' import { getAuthHeader } from '@/utils/token-manager' +import { API_BASE } from '@/api/config' // 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io -const SERVER_BASE = '/webApi/admin-api/ai/tikHup' +const SERVER_BASE = API_BASE.TIKHUB export const CommonService = { videoToCharacters(data) { diff --git a/frontend/app/web-gold/src/api/config.js b/frontend/app/web-gold/src/api/config.js new file mode 100644 index 0000000000..c60c2afffd --- /dev/null +++ b/frontend/app/web-gold/src/api/config.js @@ -0,0 +1,34 @@ +/** + * API 基础配置 + * 统一管理所有 API 的基础 URL + */ + +const BASE_URL = import.meta.env.VITE_BASE_URL || '' + +/** + * API 基础路径配置 + */ +export const API_BASE = { + // 会员端 API + APP: `${BASE_URL}`, + // 具体模块路径 + ADMIN_AI: `${BASE_URL}/admin-api/ai`, + APP_MEMBER: `${BASE_URL}/member`, + + // 特殊路径 + TIKHUB_APP: `${BASE_URL}/api/tikHup`, +} + +/** + * 获取完整的 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.ADMIN + return `${base}${path.startsWith('/') ? path : '/' + path}` +} + +export default API_BASE + diff --git a/frontend/app/web-gold/src/api/example.js b/frontend/app/web-gold/src/api/example.js new file mode 100644 index 0000000000..654eea3af8 --- /dev/null +++ b/frontend/app/web-gold/src/api/example.js @@ -0,0 +1,71 @@ +/** + * API 使用示例 + * 此文件仅作为参考,展示如何使用统一的 API 管理 + */ + +// ========== 方式一:从统一入口导入(推荐) ========== +import { + ChatMessageApi, + CommonService, + TikhubService, + API_BASE, + getApiUrl, + http +} from '@/api' + +// 使用示例 +async function example1() { + // 使用 ChatMessageApi + const conversationId = await ChatMessageApi.createChatConversationMy({ + roleId: 1 + }) + + // 使用 CommonService + const result = await CommonService.videoToCharacters({ videoUrl: 'xxx' }) + + // 使用 TikhubService + await TikhubService.postTikHup({ + type: 'DOUYIN_WEB_HOT_SEARCH', + methodType: 'GET', + urlParams: { keyword: '测试' } + }) + + // 使用配置 + const baseUrl = API_BASE.ADMIN_AI + const fullUrl = getApiUrl('ADMIN_AI', '/chat/conversation/create-my') + + // 直接使用 http 实例 + await http.get('/some-endpoint') +} + +// ========== 方式二:从具体文件导入(兼容旧代码) ========== +import { ChatMessageApi } from '@/api/chat' +import { CommonService } from '@/api/common' +import { API_BASE } from '@/api/config' + +async function example2() { + // 原有方式仍然可用 + await ChatMessageApi.createChatConversationMy({ roleId: 1 }) +} + +// ========== 方式三:按需导入认证 API ========== +import { + loginBySms, + sendSmsCode, + SMS_SCENE +} from '@/api' + +async function example3() { + // 发送验证码 + await sendSmsCode('13800138000', SMS_SCENE.MEMBER_LOGIN) + + // 短信登录 + await loginBySms('13800138000', '123456') +} + +export default { + example1, + example2, + example3 +} + diff --git a/frontend/app/web-gold/src/api/http.js b/frontend/app/web-gold/src/api/http.js index 7632011640..9671fcf37b 100644 --- a/frontend/app/web-gold/src/api/http.js +++ b/frontend/app/web-gold/src/api/http.js @@ -25,13 +25,8 @@ function isInWhiteList(url) { return WHITE_LIST.some((path) => url.includes(path)) } -/** - * 可选:多租户场景可在此处统一注入 tenant-id - * api.interceptors.request.use((config) => { - * config.headers['tenant-id'] = '1'; - * return config; - * }); - */ + + // 创建 axios 实例 const http = axios.create({ baseURL: '/', @@ -42,7 +37,7 @@ const http = axios.create({ http.interceptors.request.use((config) => { // 检查是否需要 token(不在白名单中且未显式设置 isToken = false) const needToken = config.headers?.isToken !== false && !isInWhiteList(config.url || '') - + config.headers['tenant-id'] = import.meta.env.VITE_TENANT_ID if (needToken) { // 使用统一的 token 管理器获取 header const authHeader = getAuthHeader() diff --git a/frontend/app/web-gold/src/api/index.js b/frontend/app/web-gold/src/api/index.js new file mode 100644 index 0000000000..d98482d424 --- /dev/null +++ b/frontend/app/web-gold/src/api/index.js @@ -0,0 +1,42 @@ +/** + * API 统一导出入口 + * 所有 API 服务都从这里导出,方便统一管理和使用 + */ + +// 配置 +export { default as API_BASE, getApiUrl } from './config' + +// HTTP 实例 +export { default as http, default as request } from './http' + +// 认证相关 API +export * from './auth' + +// 聊天相关 API +export { ChatMessageApi } from './chat' + +// 通用服务 API +export { CommonService } from './common' +export { default as CommonServiceDefault } from './common' + +// TikHub API +export { TikhubService, default as TikhubServiceDefault } from './tikhub' +export { InterfaceType, MethodType, InterfaceUrlMap, ParamType } from './tikhub/types' + +/** + * 统一导出所有 API 服务(便于按需导入) + */ +export default { + // 配置 + config: () => import('./config'), + + // HTTP 实例 + http: () => import('./http'), + + // API 服务 + auth: () => import('./auth'), + chat: () => import('./chat'), + common: () => import('./common'), + tikhub: () => import('./tikhub'), +} + diff --git a/frontend/app/web-gold/src/api/tikhub/tikhub.js b/frontend/app/web-gold/src/api/tikhub/tikhub.js index 0cef65ace9..e457ec0e2a 100644 --- a/frontend/app/web-gold/src/api/tikhub/tikhub.js +++ b/frontend/app/web-gold/src/api/tikhub/tikhub.js @@ -1,8 +1,10 @@ import http from '@/api/http' import { InterfaceType, MethodType, InterfaceUrlMap, ParamType } from './types' import qs from 'qs' +import { API_BASE } from '@/api/config' + // 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io -const SERVER_TIKHUB =import.meta.env.VITE_BASE_URL + '/app-api/api/tikHup' +const SERVER_TIKHUB = API_BASE.TIKHUB_APP /** * TikHub API 服务类 diff --git a/frontend/config/axios/config.js b/frontend/config/axios/config.js index 6a5ac126fd..c665a1eb01 100644 --- a/frontend/config/axios/config.js +++ b/frontend/config/axios/config.js @@ -2,7 +2,7 @@ const config = { /** * api请求基础路径 */ - base_url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL, + /** * 接口成功返回状态码 */