Files
sionrui/frontend/app/web-gold/src/api/common.js
2025-11-10 00:59:40 +08:00

68 lines
1.7 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.
import http from '@/api/http'
import { fetchEventSource } from '@microsoft/fetch-event-source'
import { getAuthHeader } from '@/utils/token-manager'
// 使用本地代理前缀 /tikhub开发环境通过 Vite 代理到 https://api.tikhub.io
const SERVER_BASE = '/webApi/admin-api/ai/tikHup'
export const CommonService = {
videoToCharacters(data) {
return http.post(`${SERVER_BASE}/videoToCharacters2`, data)
},
callWorkflow(data) {
return http.post(`${SERVER_BASE}/callWorkflow`, data)
},
// 流式调用 workflow
callWorkflowStream: async (options) => {
const {
data,
ctrl,
onMessage,
onError,
onClose
} = options || {}
const authHeader = getAuthHeader()
let retryCount = 0
const maxRetries = 0 // 禁用自动重试
return fetchEventSource(`${SERVER_BASE}/callWorkflow`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
...(authHeader ? { Authorization: authHeader } : {})
},
openWhenHidden: true,
body: JSON.stringify(data),
onmessage: onMessage,
onerror: (err) => {
retryCount++
console.error('SSE错误重试次数:', retryCount, err)
// 调用自定义错误处理
if (typeof onError === 'function') {
onError(err)
}
// 超过最大重试次数,停止重连
if (retryCount > maxRetries) {
throw err // 抛出错误,停止自动重连
}
},
onclose: () => {
// 调用自定义关闭处理
if (typeof onClose === 'function') {
onClose()
}
},
signal: ctrl ? ctrl.signal : undefined
})
}
}
export default CommonService