feat:导出面板动态编写一半
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import websocket from '@/utils/websocket'
|
||||
import type { Agent, IApiStepTask, IRawPlanResponse, IRawStepTask } from '@/stores'
|
||||
import { withRetry } from '@/utils/retry'
|
||||
import request from '@/utils/request'
|
||||
import { useConfigStoreHook } from '@/stores'
|
||||
|
||||
export interface ActionHistory {
|
||||
ID: string
|
||||
@@ -770,6 +772,209 @@ class Api {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出功能 ====================
|
||||
|
||||
/**
|
||||
* 导出任务为指定格式
|
||||
*/
|
||||
exportTask = async (params: {
|
||||
task_id: string
|
||||
export_type: string
|
||||
user_id?: string
|
||||
}): Promise<{
|
||||
record_id: number
|
||||
file_name: string
|
||||
file_url: string
|
||||
file_size: number
|
||||
export_type: string
|
||||
}> => {
|
||||
if (!websocket.connected) {
|
||||
throw new Error('WebSocket未连接')
|
||||
}
|
||||
|
||||
const rawResponse = await websocket.send('export', {
|
||||
task_id: params.task_id,
|
||||
export_type: params.export_type,
|
||||
user_id: params.user_id || 'anonymous',
|
||||
})
|
||||
|
||||
const response = this.extractResponse<{
|
||||
record_id: number
|
||||
file_name: string
|
||||
file_url: string
|
||||
file_size: number
|
||||
export_type: string
|
||||
}>(rawResponse)
|
||||
|
||||
if (response) {
|
||||
console.log('导出成功:', response)
|
||||
return response
|
||||
}
|
||||
throw new Error('导出失败')
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出记录列表
|
||||
*/
|
||||
getExportList = async (params: {
|
||||
task_id: string
|
||||
}): Promise<{
|
||||
list: Array<{
|
||||
id: number
|
||||
task_id: string
|
||||
user_id: string
|
||||
export_type: string
|
||||
file_name: string
|
||||
file_path: string
|
||||
file_url: string
|
||||
file_size: number
|
||||
created_at: string
|
||||
}>
|
||||
total: number
|
||||
}> => {
|
||||
if (!websocket.connected) {
|
||||
throw new Error('WebSocket未连接')
|
||||
}
|
||||
|
||||
const rawResponse = await websocket.send('get_export_list', {
|
||||
task_id: params.task_id,
|
||||
})
|
||||
|
||||
const response = this.extractResponse<{
|
||||
list: Array<{
|
||||
id: number
|
||||
task_id: string
|
||||
user_id: string
|
||||
export_type: string
|
||||
file_name: string
|
||||
file_path: string
|
||||
file_url: string
|
||||
file_size: number
|
||||
created_at: string
|
||||
}>
|
||||
total: number
|
||||
}>(rawResponse)
|
||||
|
||||
if (response) {
|
||||
return response
|
||||
}
|
||||
return { list: [], total: 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载导出文件
|
||||
*/
|
||||
downloadExport = async (recordId: number): Promise<void> => {
|
||||
const configStore = useConfigStoreHook()
|
||||
const baseURL = configStore.config.apiBaseUrl || ''
|
||||
const url = `${baseURL}/api/export/${recordId}/download`
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('下载失败')
|
||||
}
|
||||
|
||||
// 获取文件名从 Content-Disposition 头
|
||||
const contentDisposition = response.headers.get('Content-Disposition')
|
||||
let fileName = 'download'
|
||||
if (contentDisposition) {
|
||||
const match = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/)
|
||||
if (match) {
|
||||
fileName = match[1].replace(/['"]/g, '')
|
||||
}
|
||||
}
|
||||
|
||||
// 创建 Blob 并下载
|
||||
const blob = await response.blob()
|
||||
const downloadUrl = window.URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = downloadUrl
|
||||
link.download = fileName
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
window.URL.revokeObjectURL(downloadUrl)
|
||||
} catch (error) {
|
||||
console.error('下载失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览导出文件
|
||||
*/
|
||||
previewExport = async (recordId: number): Promise<{
|
||||
content?: string
|
||||
file_url?: string
|
||||
file_name?: string
|
||||
type: string
|
||||
}> => {
|
||||
const configStore = useConfigStoreHook()
|
||||
const baseURL = configStore.config.apiBaseUrl || ''
|
||||
const url = `${baseURL}/api/export/${recordId}/preview`
|
||||
|
||||
const response = await request<{
|
||||
content?: string
|
||||
file_url?: string
|
||||
file_name?: string
|
||||
type: string
|
||||
}>({
|
||||
url,
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成分享链接
|
||||
*/
|
||||
shareExport = async (recordId: number): Promise<{
|
||||
share_url: string
|
||||
file_name: string
|
||||
expired_at: string | null
|
||||
}> => {
|
||||
const configStore = useConfigStoreHook()
|
||||
const baseURL = configStore.config.apiBaseUrl || ''
|
||||
const url = `${baseURL}/api/export/${recordId}/share`
|
||||
|
||||
const response = await request<{
|
||||
share_url: string
|
||||
file_name: string
|
||||
expired_at: string | null
|
||||
}>({
|
||||
url,
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除导出记录
|
||||
*/
|
||||
deleteExport = async (recordId: number): Promise<boolean> => {
|
||||
const configStore = useConfigStoreHook()
|
||||
const baseURL = configStore.config.apiBaseUrl || ''
|
||||
const url = `${baseURL}/api/export/${recordId}`
|
||||
|
||||
try {
|
||||
await request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
})
|
||||
console.log('删除导出记录成功:', recordId)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('删除导出记录失败:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Api()
|
||||
|
||||
Reference in New Issue
Block a user