Files
sionrui/frontend/api/services.js
2025-11-12 22:45:29 +08:00

114 lines
2.9 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 服务
* 封装可在 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