74c1725473
后端:
- POST /api/v1/generate 接收 projectId,生成 taskId,将图片保存至 generation/{projectId}/{taskId}/
- 新增 GET /api/v1/tasks/:taskId 和 GET /api/v1/tasks/:taskId/assets 查询端点
- 添加 /generation 静态文件服务,前端可通过 URL 直接访问生成的图片
- PipelineInput 增加 ProjectID/TaskID 字段
前端:
- generate.ts 对接真实后端 API,移除 mock 模式
- 401 时不再强制登出跳转,改为抛错由调用方处理
- GeneratePage 加载工程风格,传递 projectId 和完整参数
- generation store 简化为同步 API 模式,移除 WebSocket mock
- ResultPage 使用 getTask/getAssets 按 taskId 查询结果
67 lines
1.4 KiB
TypeScript
Executable File
67 lines
1.4 KiB
TypeScript
Executable File
import type { ApiResponse } from './types'
|
|
|
|
const TOKEN_KEY = 'gen2d_token'
|
|
|
|
export function getToken(): string | null {
|
|
return localStorage.getItem(TOKEN_KEY)
|
|
}
|
|
|
|
export function setToken(token: string): void {
|
|
localStorage.setItem(TOKEN_KEY, token)
|
|
}
|
|
|
|
export function clearToken(): void {
|
|
localStorage.removeItem(TOKEN_KEY)
|
|
}
|
|
|
|
export class ApiError extends Error {
|
|
code: number
|
|
constructor(code: number, message: string) {
|
|
super(message)
|
|
this.code = code
|
|
}
|
|
}
|
|
|
|
async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
|
|
const token = getToken()
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json',
|
|
...(options.headers as Record<string, string>),
|
|
}
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`
|
|
}
|
|
|
|
const res = await fetch(url, {
|
|
...options,
|
|
headers,
|
|
credentials: 'include',
|
|
})
|
|
|
|
const json: ApiResponse<T> = await res.json()
|
|
|
|
if (json.code !== 0) {
|
|
throw new ApiError(json.code, json.message)
|
|
}
|
|
|
|
return json.data
|
|
}
|
|
|
|
export function get<T>(url: string): Promise<T> {
|
|
return request<T>(url)
|
|
}
|
|
|
|
export function post<T>(url: string, body?: unknown): Promise<T> {
|
|
return request<T>(url, {
|
|
method: 'POST',
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
})
|
|
}
|
|
|
|
export function put<T>(url: string, body?: unknown): Promise<T> {
|
|
return request<T>(url, {
|
|
method: 'PUT',
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
})
|
|
}
|