Spaces:
Sleeping
Sleeping
File size: 7,861 Bytes
4634731 6c90f80 4634731 6c90f80 4634731 6c90f80 4634731 6c90f80 4634731 6c90f80 4634731 6c90f80 4634731 6c90f80 4634731 3933dfe 4634731 3933dfe 4634731 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import { i18n } from '../i18n'
export const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL ?? 'http://180.76.247.146:18724/api').replace(/\/$/, '')
const t = (key: string, params?: Record<string, unknown>) => i18n.global.t(key, params ?? {})
export class ApiError extends Error {
status: number
data: unknown
constructor(message: string, status: number, data: unknown) {
super(message)
this.name = 'ApiError'
this.status = status
this.data = data
}
}
function errorMessage(data: unknown, status: number): string {
if (typeof data === 'object' && data) {
const record = data as Record<string, unknown>
const detail = record.detail
if (typeof detail === 'string') return detail
if (typeof detail === 'object' && detail) {
const detailRecord = detail as Record<string, unknown>
const nestedError = detailRecord.error
if (typeof nestedError === 'object' && nestedError) {
const message = (nestedError as Record<string, unknown>).message
if (typeof message === 'string') return message
}
const message = detailRecord.message
if (typeof message === 'string') return message
}
const nestedError = record.error
if (typeof nestedError === 'object' && nestedError) {
const message = (nestedError as Record<string, unknown>).message
if (typeof message === 'string') return message
}
const message = record.message
if (typeof message === 'string') return message
}
return t('common.requestFailed', { status })
}
/** 项目关联的数据文件(详情接口) */
export interface BackendDataFile {
id: number | string
filename?: string
/** 旧字段兼容 */
name?: string
uploaded_at?: string
created_at?: string
wavenumber_min?: number
wavenumber_max?: number
size?: number
}
export interface BackendProject {
id: string
name: string
description?: string
created_at: string
last_modified?: string
status: string
data_files?: BackendDataFile[]
files?: BackendDataFile[]
/** 当前作为分析使用的数据文件 */
active_data_file?: BackendDataFile | null
data_files_count?: number
file_count?: number
files_count?: number
}
async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
const headers = new Headers(init.headers ?? {})
if (!headers.has('Content-Type') && !(init.body instanceof FormData)) {
headers.set('Content-Type', 'application/json')
}
headers.set('accept', 'application/json')
const res = await fetch(`${API_BASE_URL}${path}`, {
...init,
headers
})
const contentType = res.headers.get('content-type') ?? ''
const isJson = contentType.includes('application/json')
const data = isJson ? await res.json() : await res.text()
if (!res.ok) {
throw new ApiError(errorMessage(data, res.status), res.status, data)
}
return data as T
}
async function requestBlob(path: string, init: RequestInit = {}): Promise<Blob> {
const headers = new Headers(init.headers ?? {})
headers.set('accept', '*/*')
const res = await fetch(`${API_BASE_URL}${path}`, {
...init,
headers
})
if (!res.ok) {
const contentType = res.headers.get('content-type') ?? ''
const isJson = contentType.includes('application/json')
const data = isJson ? await res.json() : await res.text()
throw new ApiError(errorMessage(data, res.status), res.status, data)
}
return await res.blob()
}
function parseFilenameFromContentDisposition(value: string | null): string {
if (!value) return ''
// 优先解析 RFC5987 filename*=UTF-8''xxx
const star = /filename\*\s*=\s*([^;]+)/i.exec(value)
if (star?.[1]) {
const v = star[1].trim().replace(/^["']|["']$/g, '')
const parts = v.split("''")
const encoded = parts.length >= 2 ? parts.slice(1).join("''") : v
try {
return decodeURIComponent(encoded)
} catch {
return encoded
}
}
// 再解析 filename="xxx"
const normal = /filename\s*=\s*([^;]+)/i.exec(value)
if (normal?.[1]) return normal[1].trim().replace(/^["']|["']$/g, '')
return ''
}
async function requestBlobWithFilename(
path: string,
init: RequestInit = {}
): Promise<{ blob: Blob; filename: string }> {
const headers = new Headers(init.headers ?? {})
headers.set('accept', '*/*')
const res = await fetch(`${API_BASE_URL}${path}`, {
...init,
headers
})
if (!res.ok) {
const contentType = res.headers.get('content-type') ?? ''
const isJson = contentType.includes('application/json')
const data = isJson ? await res.json() : await res.text()
throw new ApiError(errorMessage(data, res.status), res.status, data)
}
const filename = parseFilenameFromContentDisposition(res.headers.get('content-disposition'))
const blob = await res.blob()
return { blob, filename }
}
export const getProjects = (): Promise<BackendProject[]> =>
request<BackendProject[]>('/projects/', { method: 'GET' })
export const getProject = (id: string): Promise<BackendProject> =>
request<BackendProject>(`/projects/${encodeURIComponent(id)}/`, { method: 'GET' })
export const deleteProject = (id: string): Promise<unknown> =>
request(`/projects/${encodeURIComponent(id)}/`, { method: 'DELETE' })
export const createProject = (payload: { name: string; description?: string }): Promise<BackendProject> =>
request<BackendProject>('/projects/', {
method: 'POST',
body: JSON.stringify(payload)
})
/** 项目数据上传(multipart 字段名需与后端一致) */
export const uploadProjectFile = async (
projectId: string,
file: File,
options?: { groupIndex?: string; wavenumberRange?: string }
): Promise<unknown> => {
const formData = new FormData()
formData.append('files', file)
const gi = options?.groupIndex?.trim()
const wr = options?.wavenumberRange?.trim()
if (gi) formData.append('group_index', gi)
if (wr) formData.append('Wavenumber range', wr)
return request(`/projects/${encodeURIComponent(projectId)}/data/upload/`, {
method: 'POST',
body: formData
})
}
/** 将指定数据文件设为当前活动文件 */
export const setProjectActiveDataFile = (
projectId: string,
fileId: number
): Promise<unknown> =>
request(`/projects/${encodeURIComponent(projectId)}/data/${fileId}/select/`, {
method: 'POST',
body: JSON.stringify({})
})
/** 删除项目下的数据文件 */
export const deleteProjectDataFile = (
projectId: string,
fileId: number
): Promise<unknown> =>
request(`/projects/${encodeURIComponent(projectId)}/data/${fileId}/delete/`, {
method: 'DELETE'
})
export type ProjectAnalysisRequest = Record<string, any>
export const getProjectAnalysisResults = (projectId: string): Promise<unknown> =>
request(`/projects/${encodeURIComponent(projectId)}/analysis/results/`, { method: 'GET' })
export const runProjectAnalysis = (
projectId: string,
payload: ProjectAnalysisRequest,
workflow?: unknown
): Promise<unknown> => {
const form = new FormData()
form.append('params', JSON.stringify(payload))
if (workflow) form.append('workflow', JSON.stringify(workflow))
return request(`/projects/${encodeURIComponent(projectId)}/analysis/`, {
method: 'POST',
body: form
})
}
export const downloadProjectResultsExport = (projectId: string, exportId: string): Promise<Blob> =>
requestBlob(`/projects/${encodeURIComponent(projectId)}/results/export/${encodeURIComponent(exportId)}/`, { method: 'GET' })
export const downloadProjectAnalysisExport = (projectId: string): Promise<Blob> =>
requestBlob(`/projects/${encodeURIComponent(projectId)}/analysis/export/`, { method: 'GET' })
export const downloadProjectAnalysisExportFile = (projectId: string): Promise<{ blob: Blob; filename: string }> =>
requestBlobWithFilename(`/projects/${encodeURIComponent(projectId)}/analysis/export/`, { method: 'GET' })
|