feat: 功能
This commit is contained in:
113
frontend/api/services.js
Normal file
113
frontend/api/services.js
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* 公共 API 服务
|
||||
* 封装可在 monorepo 各个应用中复用的 API 调用
|
||||
*
|
||||
* 使用方式:
|
||||
* import { createApiService } from '@gold/config/api/services'
|
||||
*
|
||||
* const apiService = createApiService({
|
||||
* http: axiosInstance,
|
||||
* getAuthHeader: () => 'Bearer token',
|
||||
* baseUrl: API_BASE.TIKHUB_APP
|
||||
* })
|
||||
*
|
||||
* await apiService.videoToCharacters({ fileLinkList: [...] })
|
||||
*/
|
||||
|
||||
import { API_BASE } from '@gold/config/api'
|
||||
|
||||
/**
|
||||
* 创建 API 服务实例
|
||||
* @param {Object} options - 配置选项
|
||||
* @param {Object} options.http - HTTP 客户端实例(如 axios)
|
||||
* @param {Function} options.getAuthHeader - 获取 Authorization header 的函数
|
||||
* @param {string} options.baseUrl - API 基础 URL(可选,默认使用 TIKHUB_APP)
|
||||
* @returns {Object} API 服务对象
|
||||
*/
|
||||
export function createApiService(options = {}) {
|
||||
const { http, getAuthHeader, baseUrl } = options
|
||||
|
||||
if (!http) {
|
||||
throw new Error('createApiService: http 实例是必需的')
|
||||
}
|
||||
|
||||
// 确定 API 基础路径
|
||||
// 如果没有提供 baseUrl,尝试使用 TIKHUB_APP 或 TIKHUB
|
||||
const apiBaseUrl = baseUrl || API_BASE.TIKHUB_APP || API_BASE.TIKHUB || ''
|
||||
|
||||
/**
|
||||
* 视频转字符(音频转文字)
|
||||
* @param {Object} data - 请求数据
|
||||
* @param {string[]} data.fileLinkList - 音频文件链接列表
|
||||
* @returns {Promise<Object>} 响应数据
|
||||
*/
|
||||
async function videoToCharacters(data) {
|
||||
const url = `${apiBaseUrl}/videoToCharacters2`
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
// 添加 Authorization header(如果提供了 getAuthHeader 函数)
|
||||
if (getAuthHeader) {
|
||||
const authHeader = getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers.Authorization = authHeader
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 tenant-id(从环境变量或默认值)
|
||||
const tenantId =
|
||||
(typeof import.meta !== 'undefined' && import.meta.env?.VITE_TENANT_ID) ||
|
||||
(typeof process !== 'undefined' && process.env?.VITE_TENANT_ID) ||
|
||||
'1'
|
||||
|
||||
if (tenantId) {
|
||||
headers['tenant-id'] = tenantId
|
||||
}
|
||||
|
||||
return await http.post(url, data, { headers })
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用工作流
|
||||
* @param {Object} data - 请求数据
|
||||
* @returns {Promise<Object>} 响应数据
|
||||
*/
|
||||
async function callWorkflow(data) {
|
||||
const url = `${apiBaseUrl}/callWorkflow`
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
if (getAuthHeader) {
|
||||
const authHeader = getAuthHeader()
|
||||
if (authHeader) {
|
||||
headers.Authorization = authHeader
|
||||
}
|
||||
}
|
||||
|
||||
const tenantId =
|
||||
(typeof import.meta !== 'undefined' && import.meta.env?.VITE_TENANT_ID) ||
|
||||
(typeof process !== 'undefined' && process.env?.VITE_TENANT_ID) ||
|
||||
'1'
|
||||
|
||||
if (tenantId) {
|
||||
headers['tenant-id'] = tenantId
|
||||
}
|
||||
|
||||
return await http.post(url, data, { headers })
|
||||
}
|
||||
|
||||
return {
|
||||
videoToCharacters,
|
||||
callWorkflow,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认导出(便于直接使用)
|
||||
*/
|
||||
export default createApiService
|
||||
|
||||
Reference in New Issue
Block a user