2026-05-04-03-21-40 增加前后端协同和NIfTI导出

This commit is contained in:
2026-05-04 03:29:54 +08:00
parent a6f3836460
commit a9b6d2d76a
15 changed files with 1040 additions and 67 deletions

67
WebSite/src/lib/api.ts Normal file
View File

@@ -0,0 +1,67 @@
import { OverviewSummary, Project, SessionState, UserRecord } from '../types';
async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
const response = await fetch(path, {
headers: {
'Content-Type': 'application/json',
...(options.headers ?? {}),
},
...options,
});
if (!response.ok) {
let message = `请求失败:${response.status}`;
try {
const data = await response.json();
if (typeof data?.message === 'string') {
message = data.message;
}
} catch {
// Keep the status-based message when the response is not JSON.
}
throw new Error(message);
}
return response.json() as Promise<T>;
}
export const api = {
getSession: () => request<SessionState>('/api/session'),
login: (account: string, password: string) =>
request<SessionState>('/api/login', {
method: 'POST',
body: JSON.stringify({ account, password }),
}),
logout: () => request<SessionState>('/api/logout', { method: 'POST' }),
getOverview: () => request<OverviewSummary>('/api/overview'),
getProjects: () => request<Project[]>('/api/projects'),
getProject: (projectId: string) => request<Project>(`/api/projects/${projectId}`),
getUsers: () => request<UserRecord[]>('/api/users'),
resetDemo: () =>
request<{ ok: boolean; projects: Project[]; users: UserRecord[] }>('/api/demo/reset', {
method: 'POST',
}),
};
export async function downloadMask(projectId: string, format: 'nii' | 'nii.gz' = 'nii.gz') {
const response = await fetch(`/api/projects/${projectId}/export-mask?format=${encodeURIComponent(format)}`, {
method: 'POST',
});
if (!response.ok) {
throw new Error(`导出失败:${response.status}`);
}
const blob = await response.blob();
const disposition = response.headers.get('Content-Disposition') ?? '';
const match = disposition.match(/filename="([^"]+)"/);
const filename = match?.[1] ?? `segmentation-mask.${format}`;
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}