feat(frontend): 构建前端 MVP,实现完整 UI 框架与 mock 数据层

- 安装 zustand + react-router-dom 依赖
- 修复 Vite 代理配置,添加 /auth 路由转发
- 实现 API 封装层(client.ts 统一 fetch、auth.ts 真实调用)
- 实现 mock 数据层(mock.ts 模拟 WebSocket 管线进度)
- 实现 4 个 zustand stores(auth/project/task/generation)
- 实现 5 个组件(StyleSelector/PromptEditor/GenerateForm/ProgressBar/AssetPreview)
- 实现 5 个页面(Login/Register/Project/Generate/Result)
- 实现路由含 ProtectedRoute 守卫
- 添加暗色主题全局样式
- 更新前端文档与 API 文档
This commit is contained in:
2026-05-24 14:18:39 +08:00
parent 302b9ba88e
commit a2736f722c
33 changed files with 2272 additions and 20 deletions
+111
View File
@@ -0,0 +1,111 @@
import { useEffect } from 'react'
import { Link, useNavigate, useParams } from 'react-router-dom'
import { useTaskStore } from '../stores/task'
import { useGenerationStore } from '../stores/generation'
import GenerateForm from '../components/GenerateForm'
import ProgressBar from '../components/ProgressBar'
export default function GeneratePage() {
const { projectId = 'proj-default' } = useParams()
const navigate = useNavigate()
const { prompt, assetType, reset: resetTask } = useTaskStore()
const {
status,
stage,
progress,
retryCount,
rejectReason,
taskId,
submit,
reset: resetGeneration,
} = useGenerationStore()
// 组件卸载时重置生成状态
useEffect(() => {
return () => resetGeneration()
}, [resetGeneration])
// 完成后自动跳转
useEffect(() => {
if (status === 'completed' && taskId) {
const timer = setTimeout(() => {
navigate(`/projects/${projectId}/tasks/${taskId}`)
}, 1500)
return () => clearTimeout(timer)
}
}, [status, taskId, projectId, navigate])
const handleSubmit = async () => {
await submit(projectId, prompt, assetType)
}
const handleReset = () => {
resetGeneration()
resetTask()
}
return (
<div className="container" style={{ paddingTop: 24, paddingBottom: 40 }}>
{/* 顶栏 */}
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 32,
}}
>
<h1 style={{ fontSize: 24 }}>新建生成</h1>
<Link
to={`/projects/${projectId}`}
className="btn-secondary"
style={{
padding: '8px 20px',
fontSize: 13,
borderRadius: 'var(--radius)',
}}
>
返回工程
</Link>
</div>
{/* 生成表单 */}
{status === 'idle' || status === 'submitting' ? (
<GenerateForm onSubmit={handleSubmit} submitting={status === 'submitting'} />
) : (
<div style={{ display: 'flex', flexDirection: 'column', gap: 24 }}>
{/* 进度条 */}
<ProgressBar
stage={stage}
progress={progress}
status={status}
retryCount={retryCount}
rejectReason={rejectReason}
/>
{/* 状态提示 */}
{status === 'running' && (
<p style={{ textAlign: 'center', color: 'var(--text-secondary)' }}>
管线执行中,请稍候...
</p>
)}
{status === 'completed' && (
<p style={{ textAlign: 'center', color: 'var(--success)' }}>
✓ 生成完成,正在跳转到结果页...
</p>
)}
{status === 'failed' && (
<div style={{ textAlign: 'center' }}>
<p style={{ color: 'var(--error)', marginBottom: 16 }}>生成失败</p>
<button className="btn-primary" onClick={handleReset}>
重新开始
</button>
</div>
)}
</div>
)}
</div>
)
}
+82
View File
@@ -0,0 +1,82 @@
import { useState } from 'react'
import { Link, useNavigate } from 'react-router-dom'
import { useAuthStore } from '../stores/auth'
export default function LoginPage() {
const navigate = useNavigate()
const { login, loading, error, clearError } = useAuthStore()
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
try {
await login(username, password)
navigate('/')
} catch {
// error 已在 store 中设置
}
}
return (
<div
style={{
minHeight: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<div className="card" style={{ width: 380, padding: 40 }}>
<h1 style={{ textAlign: 'center', marginBottom: 8, fontSize: 28 }}>gen2d</h1>
<p
style={{
textAlign: 'center',
color: 'var(--text-secondary)',
marginBottom: 32,
fontSize: 14,
}}
>
AI 驱动的 2D 游戏素材生成工具
</p>
<form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<input
type="text"
placeholder="用户名"
value={username}
onChange={e => {
setUsername(e.target.value)
clearError()
}}
required
autoFocus
/>
<input
type="password"
placeholder="密码"
value={password}
onChange={e => {
setPassword(e.target.value)
clearError()
}}
required
/>
{error && (
<div style={{ color: 'var(--error)', fontSize: 13 }}>{error}</div>
)}
<button type="submit" className="btn-primary" disabled={loading} style={{ width: '100%' }}>
{loading ? '登录中...' : '登录'}
</button>
</form>
<p style={{ textAlign: 'center', marginTop: 20, fontSize: 13, color: 'var(--text-secondary)' }}>
没有账号?{' '}
<Link to="/register">去注册</Link>
</p>
</div>
</div>
)
}
+174
View File
@@ -0,0 +1,174 @@
import { useEffect, useState } from 'react'
import { Link, useParams } from 'react-router-dom'
import { useProjectStore } from '../stores/project'
import { useAuthStore } from '../stores/auth'
import { getTasks } from '../api/project'
import type { Task } from '../api/types'
import StyleSelector from '../components/StyleSelector'
const STATUS_LABELS: Record<string, { label: string; color: string }> = {
pending: { label: '等待中', color: 'var(--text-muted)' },
running: { label: '运行中', color: 'var(--warning)' },
completed: { label: '已完成', color: 'var(--success)' },
failed: { label: '失败', color: 'var(--error)' },
}
export default function ProjectPage() {
const { projectId = 'proj-default' } = useParams()
const { user, logout } = useAuthStore()
const {
name,
style,
loading,
loadProject,
loadStyle,
updateStyle,
saveStyle,
} = useProjectStore()
const [tasks, setTasks] = useState<Task[]>([])
const [saving, setSaving] = useState(false)
useEffect(() => {
loadProject(projectId)
loadStyle(projectId)
getTasks(projectId).then(setTasks)
}, [projectId, loadProject, loadStyle])
const handleSave = async () => {
setSaving(true)
try {
await saveStyle()
} finally {
setSaving(false)
}
}
return (
<div className="container" style={{ paddingTop: 24, paddingBottom: 40 }}>
{/* 顶栏 */}
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 32,
}}
>
<h1 style={{ fontSize: 24 }}>{loading ? '加载中...' : name}</h1>
<div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
<span style={{ fontSize: 13, color: 'var(--text-secondary)' }}>
{user?.username}
</span>
<button className="btn-secondary" onClick={logout} style={{ padding: '6px 16px', fontSize: 13 }}>
退出
</button>
</div>
</div>
{/* 工程风格 */}
<section className="card" style={{ marginBottom: 24 }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 20,
}}
>
<h2 style={{ fontSize: 18 }}>工程风格</h2>
<button
className="btn-primary"
onClick={handleSave}
disabled={saving}
style={{ padding: '8px 20px', fontSize: 13 }}
>
{saving ? '保存中...' : '保存风格'}
</button>
</div>
<StyleSelector value={style} onChange={updateStyle} />
</section>
{/* 任务列表 */}
<section className="card">
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 20,
}}
>
<h2 style={{ fontSize: 18 }}>历史任务</h2>
<Link
to={`/projects/${projectId}/generate`}
className="btn-primary"
style={{
padding: '8px 20px',
fontSize: 13,
borderRadius: 'var(--radius)',
display: 'inline-block',
}}
>
新建生成
</Link>
</div>
{tasks.length === 0 ? (
<p style={{ color: 'var(--text-muted)', textAlign: 'center', padding: 32 }}>
暂无任务
</p>
) : (
<table style={{ width: '100%', borderCollapse: 'collapse' }}>
<thead>
<tr style={{ borderBottom: '1px solid var(--border)' }}>
<th style={thStyle}>提示词</th>
<th style={thStyle}>类型</th>
<th style={thStyle}>状态</th>
<th style={thStyle}>创建时间</th>
<th style={thStyle}>操作</th>
</tr>
</thead>
<tbody>
{tasks.map(task => {
const statusInfo = STATUS_LABELS[task.status] ?? STATUS_LABELS.pending
return (
<tr key={task.id} style={{ borderBottom: '1px solid var(--border)' }}>
<td style={tdStyle}>{task.prompt}</td>
<td style={tdStyle}>{task.assetType}</td>
<td style={{ ...tdStyle, color: statusInfo.color }}>{statusInfo.label}</td>
<td style={tdStyle}>
{new Date(task.createdAt).toLocaleString('zh-CN')}
</td>
<td style={tdStyle}>
{task.status === 'completed' && (
<Link
to={`/projects/${projectId}/tasks/${task.id}`}
style={{ fontSize: 13 }}
>
查看结果
</Link>
)}
</td>
</tr>
)
})}
</tbody>
</table>
)}
</section>
</div>
)
}
const thStyle: React.CSSProperties = {
textAlign: 'left',
padding: '10px 12px',
fontSize: 13,
color: 'var(--text-secondary)',
fontWeight: 600,
}
const tdStyle: React.CSSProperties = {
padding: '10px 12px',
fontSize: 13,
}
+93
View File
@@ -0,0 +1,93 @@
import { useState } from 'react'
import { Link, useNavigate } from 'react-router-dom'
import { useAuthStore } from '../stores/auth'
export default function RegisterPage() {
const navigate = useNavigate()
const { register, loading, error, clearError } = useAuthStore()
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [email, setEmail] = useState('')
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
try {
await register(username, password, email || undefined)
navigate('/login')
} catch {
// error 已在 store 中设置
}
}
return (
<div
style={{
minHeight: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<div className="card" style={{ width: 380, padding: 40 }}>
<h1 style={{ textAlign: 'center', marginBottom: 8, fontSize: 28 }}>gen2d</h1>
<p
style={{
textAlign: 'center',
color: 'var(--text-secondary)',
marginBottom: 32,
fontSize: 14,
}}
>
创建新账号
</p>
<form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<input
type="text"
placeholder="用户名(3-32 字符)"
value={username}
onChange={e => {
setUsername(e.target.value)
clearError()
}}
required
autoFocus
/>
<input
type="email"
placeholder="邮箱(可选)"
value={email}
onChange={e => {
setEmail(e.target.value)
clearError()
}}
/>
<input
type="password"
placeholder="密码(至少 6 位)"
value={password}
onChange={e => {
setPassword(e.target.value)
clearError()
}}
required
minLength={6}
/>
{error && (
<div style={{ color: 'var(--error)', fontSize: 13 }}>{error}</div>
)}
<button type="submit" className="btn-primary" disabled={loading} style={{ width: '100%' }}>
{loading ? '注册中...' : '注册'}
</button>
</form>
<p style={{ textAlign: 'center', marginTop: 20, fontSize: 13, color: 'var(--text-secondary)' }}>
已有账号?{' '}
<Link to="/login">去登录</Link>
</p>
</div>
</div>
)
}
+133
View File
@@ -0,0 +1,133 @@
import { useEffect, useState } from 'react'
import { Link, useParams } from 'react-router-dom'
import { getTask, getAssets } from '../api/generate'
import type { Asset, Task } from '../api/types'
import AssetPreview from '../components/AssetPreview'
export default function ResultPage() {
const { projectId = 'proj-default', taskId } = useParams()
const [task, setTask] = useState<Task | null>(null)
const [assets, setAssets] = useState<Asset[]>([])
const [loading, setLoading] = useState(true)
useEffect(() => {
if (!taskId) return
setLoading(true)
Promise.all([getTask(taskId), getAssets(taskId)])
.then(([t, a]) => {
setTask(t)
setAssets(a)
})
.finally(() => setLoading(false))
}, [taskId])
if (loading) {
return (
<div className="container" style={{ paddingTop: 40, textAlign: 'center' }}>
<p style={{ color: 'var(--text-secondary)' }}>加载中...</p>
</div>
)
}
if (!task) {
return (
<div className="container" style={{ paddingTop: 40, textAlign: 'center' }}>
<p style={{ color: 'var(--error)' }}>任务不存在</p>
<Link to={`/projects/${projectId}`} style={{ marginTop: 16, display: 'inline-block' }}>
返回工程
</Link>
</div>
)
}
return (
<div className="container" style={{ paddingTop: 24, paddingBottom: 40 }}>
{/* 顶栏 */}
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 32,
}}
>
<h1 style={{ fontSize: 24 }}>生成结果</h1>
<Link
to={`/projects/${projectId}`}
className="btn-secondary"
style={{
padding: '8px 20px',
fontSize: 13,
borderRadius: 'var(--radius)',
}}
>
返回工程
</Link>
</div>
{/* 任务信息 */}
<section className="card" style={{ marginBottom: 24 }}>
<h2 style={{ fontSize: 16, marginBottom: 16 }}>任务信息</h2>
<div
style={{
display: 'grid',
gridTemplateColumns: '120px 1fr',
gap: '8px 16px',
fontSize: 13,
}}
>
<span style={{ color: 'var(--text-secondary)' }}>提示词</span>
<span>{task.prompt}</span>
<span style={{ color: 'var(--text-secondary)' }}>素材类型</span>
<span>{task.assetType}</span>
<span style={{ color: 'var(--text-secondary)' }}>状态</span>
<span
style={{
color: task.status === 'completed' ? 'var(--success)' : 'var(--error)',
}}
>
{task.status === 'completed' ? '已完成' : '失败'}
</span>
<span style={{ color: 'var(--text-secondary)' }}>创建时间</span>
<span>{new Date(task.createdAt).toLocaleString('zh-CN')}</span>
{task.retryCount != null && task.retryCount > 0 && (
<>
<span style={{ color: 'var(--text-secondary)' }}>重试次数</span>
<span>{task.retryCount}</span>
</>
)}
{task.error && (
<>
<span style={{ color: 'var(--text-secondary)' }}>错误</span>
<span style={{ color: 'var(--error)' }}>{task.error}</span>
</>
)}
</div>
</section>
{/* 素材预览 */}
<section className="card" style={{ marginBottom: 24 }}>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 16,
}}
>
<h2 style={{ fontSize: 16 }}>素材预览</h2>
{assets.length > 0 && (
<button
className="btn-primary"
onClick={() => alert('下载功能即将上线')}
style={{ padding: '8px 20px', fontSize: 13 }}
>
下载素材
</button>
)}
</div>
<AssetPreview assets={assets} />
</section>
</div>
)
}