a57aae3043
- 合入 weiyuan 新功能前端:分佣 V2 / 伙伴工作台、智能路由、企业资金与分析、官网设置与预约演示等, semi-ui 已全部迁移到 antd 6 / @/components/ui,不重新引入 Semi - 分佣二级菜单与页头去重、伙伴入口探测(企业伙伴走企业中心「企业分佣」)、超管侧栏入口恢复 - 适配层 DatePicker / TimePicker 统一 onChange(Date|Date[]|null, 文本) 契约;生产构建剔除调试日志 - 图片链接刷新:同任务请求复用、后台刷新不弹全局错误、离页停止批量请求 - 恢复 bun.lock(与 package-lock.json 共存);docs 规范与 CHANGELOG 更新 - 停止跟踪 .env.playwright.local(本地登录凭据,已加入 .gitignore)
1046 lines
38 KiB
React
1046 lines
38 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import {
|
||
Download as ActionDownload,
|
||
Check as ActionCheck,
|
||
Zap as ActionZap,
|
||
RefreshCw as ActionRefreshCw,
|
||
} from '@/components/common/icons/ActionIcons';
|
||
import React, { useState, useCallback, useEffect, useContext } from 'react';
|
||
import { Navigate } from 'react-router-dom';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
Card,
|
||
Button,
|
||
Empty,
|
||
Spin,
|
||
Input,
|
||
Select,
|
||
Tabs,
|
||
Collapse,
|
||
Typography,
|
||
Tag,
|
||
Modal,
|
||
Tooltip,
|
||
Table,
|
||
Progress,
|
||
} from '@/components/ui';
|
||
import {
|
||
Image,
|
||
Settings,
|
||
Zap,
|
||
AlertCircle,
|
||
Info,
|
||
Check,
|
||
Clock,
|
||
Lock,
|
||
} from 'lucide-react';
|
||
|
||
import { useThemeMode } from '../../hooks/common/useThemeMode';
|
||
import { useIsMobile } from '../../hooks/common/useIsMobile';
|
||
import { useModelSelector } from '../../hooks/models/useModelSelector';
|
||
import { MODEL_CATEGORY } from '../../constants/model-category.constant';
|
||
import {
|
||
getImageCapability,
|
||
buildImageRequestBody,
|
||
} from '../../constants/imageModelCapabilities';
|
||
import { API, copy, showError, showSuccess } from '../../helpers';
|
||
import { StatusContext } from '../../context/Status';
|
||
import { isCreationStudioEnabled } from '../../helpers/creationStudio';
|
||
|
||
// 定义图片尺寸选项
|
||
const IMAGE_SIZES = [
|
||
{ value: '256x256', label: '256×256' },
|
||
{ value: '512x512', label: '512×512' },
|
||
{ value: '1024x1024', label: '1024×1024' },
|
||
{ value: '1024x1792', label: '1024×1792 (9:16)' },
|
||
{ value: '1792x1024', label: '1792×1024 (16:9)' },
|
||
];
|
||
|
||
// 定义图片质量选项
|
||
const IMAGE_QUALITY = [
|
||
{ value: 'standard', label: '标准' },
|
||
{ value: 'hd', label: '高清' },
|
||
];
|
||
|
||
// 任务状态定义
|
||
const TASK_STATUS = {
|
||
NOT_START: 'NOT_START',
|
||
SUBMITTED: 'SUBMITTED',
|
||
QUEUED: 'QUEUED',
|
||
IN_PROGRESS: 'IN_PROGRESS',
|
||
FAILURE: 'FAILURE',
|
||
SUCCESS: 'SUCCESS',
|
||
UNKNOWN: 'UNKNOWN',
|
||
};
|
||
|
||
// 任务状态显示名称
|
||
const TASK_STATUS_NAMES = {
|
||
[TASK_STATUS.NOT_START]: '未开始',
|
||
[TASK_STATUS.SUBMITTED]: '已提交',
|
||
[TASK_STATUS.QUEUED]: '排队中',
|
||
[TASK_STATUS.IN_PROGRESS]: '生成中',
|
||
[TASK_STATUS.FAILURE]: '失败',
|
||
[TASK_STATUS.SUCCESS]: '成功',
|
||
[TASK_STATUS.UNKNOWN]: '未知',
|
||
};
|
||
|
||
// 任务状态颜色
|
||
const TASK_STATUS_COLORS = {
|
||
[TASK_STATUS.NOT_START]: 'grey',
|
||
[TASK_STATUS.SUBMITTED]: 'blue',
|
||
[TASK_STATUS.QUEUED]: 'cyan',
|
||
[TASK_STATUS.IN_PROGRESS]: 'yellow',
|
||
[TASK_STATUS.FAILURE]: 'red',
|
||
[TASK_STATUS.SUCCESS]: 'green',
|
||
[TASK_STATUS.UNKNOWN]: 'grey',
|
||
};
|
||
|
||
const ImageGeneration = () => {
|
||
const { t } = useTranslation();
|
||
useThemeMode();
|
||
useIsMobile();
|
||
|
||
// 使用图像生成分类的模型选择器
|
||
const {
|
||
models,
|
||
loading: modelsLoading,
|
||
selectedModel,
|
||
setSelectedModel,
|
||
} = useModelSelector(MODEL_CATEGORY.IMAGE_GENERATION);
|
||
|
||
// 状态管理
|
||
const [prompt, setPrompt] = useState('');
|
||
useState('');
|
||
const [size, setSize] = useState('1024x1024');
|
||
const [quality, setQuality] = useState('standard');
|
||
const [n, setN] = useState(1);
|
||
const [showAdvanced, setShowAdvanced] = useState(false);
|
||
const [isGenerating, setIsGenerating] = useState(false);
|
||
const [error, setError] = useState('');
|
||
|
||
// 任务列表相关状态
|
||
const [tasks, setTasks] = useState([]);
|
||
const [selectedTask, setSelectedTask] = useState(null);
|
||
const [tasksLoading, setTasksLoading] = useState(false);
|
||
useState(false);
|
||
const [currentTab, setCurrentTab] = useState('generate'); // 'generate' | 'tasks'
|
||
const [pagination, setPagination] = useState({
|
||
page: 1,
|
||
pageSize: 20,
|
||
total: 0,
|
||
});
|
||
|
||
// 加载任务列表
|
||
const loadTasks = useCallback(
|
||
async (page = pagination.page, pageSize = pagination.pageSize) => {
|
||
console.log('加载任务列表...', page, pageSize);
|
||
setTasksLoading(true);
|
||
try {
|
||
const res = await API.get(
|
||
`/api/images/tasks?page=${page}&page_size=${pageSize}`,
|
||
);
|
||
const { success, data } = res.data;
|
||
|
||
if (success && data) {
|
||
console.log('任务列表加载成功:', data.items?.length, '个任务');
|
||
setTasks(data.items || []);
|
||
setPagination((prev) => ({
|
||
...prev,
|
||
page: page,
|
||
pageSize: pageSize,
|
||
total: data.total || 0,
|
||
}));
|
||
}
|
||
} catch (err) {
|
||
console.error('加载任务列表失败:', err);
|
||
} finally {
|
||
setTasksLoading(false);
|
||
}
|
||
},
|
||
[pagination.page, pagination.pageSize],
|
||
);
|
||
|
||
// 生成图片
|
||
const generateImage = useCallback(async () => {
|
||
console.log('=== 开始生成图片 ===');
|
||
console.log('prompt:', prompt);
|
||
console.log('selectedModel:', selectedModel);
|
||
|
||
if (!prompt.trim()) {
|
||
console.log('错误: 提示词为空');
|
||
setError(t('请输入提示词'));
|
||
return;
|
||
}
|
||
|
||
if (!selectedModel) {
|
||
console.log('错误: 未选择模型');
|
||
setError(t('请选择模型'));
|
||
return;
|
||
}
|
||
if (selectedModel.level_locked) {
|
||
setError(t('当前等级无权调用此模型,请选择其他模型'));
|
||
return;
|
||
}
|
||
|
||
setIsGenerating(true);
|
||
setError('');
|
||
|
||
try {
|
||
const requestData = buildImageRequestBody(selectedModel.model_name, {
|
||
prompt,
|
||
n,
|
||
size,
|
||
quality,
|
||
mode: 'text-to-image',
|
||
});
|
||
|
||
console.log('请求数据:', requestData);
|
||
|
||
// 调用API创建图像生成任务
|
||
console.log('发送API请求: POST /api/images/generations');
|
||
const res = await API.post('/api/images/generations', requestData);
|
||
console.log('API原始响应:', res);
|
||
console.log('API响应data:', res.data);
|
||
|
||
// 安全地解构响应数据
|
||
const responseData = res.data || {};
|
||
const { success, message, data } = responseData;
|
||
|
||
console.log(
|
||
'解构后 - success:',
|
||
success,
|
||
'message:',
|
||
message,
|
||
'data:',
|
||
data,
|
||
);
|
||
|
||
if (success) {
|
||
console.log('任务创建成功:', data);
|
||
showSuccess(t('图像生成任务已创建,请在任务列表查看进度'));
|
||
// 切换到任务列表tab
|
||
setCurrentTab('tasks');
|
||
// 刷新任务列表 (reset to first page)
|
||
await loadTasks(1, pagination.pageSize);
|
||
} else {
|
||
console.error('任务创建失败:', message);
|
||
setError(message || t('创建任务失败'));
|
||
showError(message || t('创建任务失败'));
|
||
}
|
||
} catch (err) {
|
||
console.error('API调用失败 - 完整错误对象:', err);
|
||
console.error('错误名称:', err.name);
|
||
console.error('错误消息:', err.message);
|
||
console.error('错误堆栈:', err.stack);
|
||
console.error('响应数据:', err.response?.data);
|
||
console.error('响应状态:', err.response?.status);
|
||
|
||
const errorMsg =
|
||
err.response?.data?.message || err.message || t('图片生成失败,请重试');
|
||
setError(errorMsg);
|
||
showError(errorMsg);
|
||
} finally {
|
||
setIsGenerating(false);
|
||
console.log('=== 生成图片结束 ===');
|
||
}
|
||
}, [
|
||
prompt,
|
||
selectedModel,
|
||
size,
|
||
quality,
|
||
n,
|
||
t,
|
||
loadTasks,
|
||
pagination.pageSize,
|
||
]);
|
||
|
||
// 任务自动刷新(轮询)
|
||
useEffect(() => {
|
||
if (currentTab === 'tasks') {
|
||
// 立即加载一次 (use current pagination state)
|
||
loadTasks();
|
||
|
||
// 每5秒刷新一次任务列表
|
||
const interval = setInterval(() => {
|
||
loadTasks();
|
||
}, 5000);
|
||
|
||
return () => clearInterval(interval);
|
||
}
|
||
}, [currentTab, loadTasks]);
|
||
|
||
// 查看任务详情
|
||
const viewTaskDetail = useCallback(async (task) => {
|
||
setSelectedTask(task);
|
||
}, []);
|
||
|
||
// 下载图片
|
||
const downloadImage = useCallback((imageUrl, index) => {
|
||
const link = document.createElement('a');
|
||
link.href = imageUrl;
|
||
link.download = `generated-image-${Date.now()}-${index}.png`;
|
||
document.body.appendChild(link);
|
||
link.click();
|
||
document.body.removeChild(link);
|
||
}, []);
|
||
|
||
// 获取推荐尺寸 — 从 capability registry 读取
|
||
const getRecommendedSizes = () => {
|
||
if (!selectedModel) return IMAGE_SIZES;
|
||
const cap = getImageCapability(selectedModel.model_name);
|
||
return cap.sizes.map((s) => ({ value: s.value, label: s.label }));
|
||
};
|
||
|
||
// 当前模型的 capability(用于 quality 字段是否可用)
|
||
const currentCapability = selectedModel
|
||
? getImageCapability(selectedModel.model_name)
|
||
: null;
|
||
|
||
// 处理分页变化
|
||
const handlePageChange = (page) => {
|
||
setPagination((prev) => ({ ...prev, page }));
|
||
};
|
||
|
||
const handlePageSizeChange = (pageSize) => {
|
||
setPagination((prev) => ({ ...prev, page: 1, pageSize }));
|
||
};
|
||
|
||
// 复制图片链接:走公共 copy,HTTP 部署下 navigator.clipboard 为 undefined,
|
||
// 直接调用 writeText 会同步抛错,.catch 降级永远走不到。
|
||
const copyImageLink = useCallback(
|
||
async (imageUrl) => {
|
||
if (await copy(imageUrl)) {
|
||
showSuccess(t('链接已复制'));
|
||
} else {
|
||
showError(t('复制失败'));
|
||
}
|
||
},
|
||
[t],
|
||
);
|
||
|
||
// 渲染图片项
|
||
const renderImageItem = (image, index) => {
|
||
// 获取图片URL,支持多种字段名
|
||
const imageUrl =
|
||
image.url ||
|
||
image.Url ||
|
||
image.b64_json ||
|
||
image.B64Json ||
|
||
image.image_url;
|
||
|
||
if (!imageUrl) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<div
|
||
key={index}
|
||
className='group relative rounded-xl overflow-hidden shadow-lg hover:shadow-2xl transition-all duration-300 hover:-translate-y-1 bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-700 hover:ring-blue-500 dark:hover:ring-blue-400'
|
||
>
|
||
<div className='relative aspect-square bg-gray-100 dark:bg-gray-800 flex items-center justify-center overflow-hidden'>
|
||
<img
|
||
src={imageUrl}
|
||
alt={`Generated ${index + 1}`}
|
||
className='w-full h-full object-contain transition-transform duration-500 group-hover:scale-105'
|
||
onError={(e) => {
|
||
e.target.src = `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><text x="50%" y="50%" text-anchor="middle" dy=".3em" fill="%23999">${t('加载失败')}</text></svg>`;
|
||
}}
|
||
/>
|
||
{/* 渐变遮罩层 */}
|
||
<div className='absolute inset-x-0 bottom-0 h-20 bg-gradient-to-t from-black/90 via-black/50 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300 flex items-center justify-end px-4 pb-2 gap-2'>
|
||
<Tooltip content={t('下载图片')}>
|
||
<Button
|
||
theme='borderless'
|
||
type='primary'
|
||
icon={
|
||
<ActionDownload
|
||
size={18}
|
||
className='text-white hover:text-blue-400 transition-colors'
|
||
/>
|
||
}
|
||
onClick={() => downloadImage(imageUrl, index)}
|
||
className='!bg-white/10 hover:!bg-white/20 !p-2 !rounded-full backdrop-blur-sm'
|
||
/>
|
||
</Tooltip>
|
||
<Tooltip content={t('复制链接')}>
|
||
<Button
|
||
theme='borderless'
|
||
type='primary'
|
||
icon={
|
||
<ActionCheck
|
||
size={18}
|
||
className='text-white hover:text-green-400 transition-colors'
|
||
/>
|
||
}
|
||
onClick={() => copyImageLink(imageUrl)}
|
||
className='!bg-white/10 hover:!bg-white/20 !p-2 !rounded-full backdrop-blur-sm'
|
||
/>
|
||
</Tooltip>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
return (
|
||
<div className='min-h-screen bg-gray-50 dark:bg-gray-900 transition-colors duration-200'>
|
||
{/* 页面标题 */}
|
||
<div className='py-6 px-4 sm:px-6 lg:px-8 border-b border-gray-200 dark:border-gray-800'>
|
||
<div className='max-w-7xl mx-auto'>
|
||
<div className='flex items-center'>
|
||
<Image className='h-8 w-8 text-blue-600 dark:text-blue-400' />
|
||
<h1 className='ml-3 text-2xl font-bold tracking-tight text-gray-900 dark:text-white'>
|
||
{t('AI 图像生成')}
|
||
</h1>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<main className='max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8'>
|
||
<Tabs type='line' activeKey={currentTab} onChange={setCurrentTab}>
|
||
{/* 图像生成Tab */}
|
||
<Tabs.TabPane
|
||
tab={
|
||
<span className='flex items-center gap-2'>
|
||
<Zap size={16} />
|
||
{t('图像生成')}
|
||
</span>
|
||
}
|
||
itemKey='generate'
|
||
>
|
||
<div className='grid grid-cols-1 lg:grid-cols-3 gap-6 mt-6'>
|
||
{/* 左侧控制面板 */}
|
||
<div className='lg:col-span-1 space-y-6'>
|
||
{/* 模型选择 */}
|
||
<Card className='overflow-hidden'>
|
||
<div className='p-6'>
|
||
<h2 className='text-lg font-medium text-gray-900 dark:text-white mb-4'>
|
||
{t('模型选择')}
|
||
</h2>
|
||
{modelsLoading ? (
|
||
<div className='flex justify-center py-4'>
|
||
<Spin />
|
||
</div>
|
||
) : models.length === 0 ? (
|
||
<Empty
|
||
description={t('暂无可用的图像生成模型')}
|
||
className='py-4'
|
||
>
|
||
<p className='text-sm text-gray-500 dark:text-gray-400 mt-2'>
|
||
{t('请在模型管理中添加图像生成类型的模型')}
|
||
</p>
|
||
</Empty>
|
||
) : (
|
||
<Select
|
||
value={selectedModel?.id}
|
||
onChange={(modelId) => {
|
||
const model = models.find((m) => m.id === modelId);
|
||
if (model && !model.level_locked) {
|
||
setSelectedModel(model);
|
||
}
|
||
}}
|
||
className='w-full'
|
||
placeholder={t('选择生成模型')}
|
||
filter
|
||
>
|
||
{models.map((model) => (
|
||
<Select.Option
|
||
key={model.id}
|
||
value={model.id}
|
||
disabled={model.level_locked}
|
||
showTick={!model.level_locked}
|
||
>
|
||
<div className='flex items-center justify-between'>
|
||
<span
|
||
className={
|
||
model.level_locked
|
||
? 'text-gray-400 dark:text-gray-500'
|
||
: ''
|
||
}
|
||
>
|
||
{model.model_name}
|
||
</span>
|
||
{model.description && (
|
||
<Typography.Text
|
||
type='secondary'
|
||
size='small'
|
||
className='ml-2'
|
||
>
|
||
{model.description}
|
||
</Typography.Text>
|
||
)}
|
||
{model.level_locked && (
|
||
<Tag
|
||
color='grey'
|
||
size='small'
|
||
prefixIcon={<Lock size={12} />}
|
||
className='ml-2'
|
||
>
|
||
{t('仅可见')}
|
||
</Tag>
|
||
)}
|
||
</div>
|
||
</Select.Option>
|
||
))}
|
||
</Select>
|
||
)}
|
||
</div>
|
||
</Card>
|
||
|
||
{/* 提示词输入 */}
|
||
<Card>
|
||
<div className='p-6'>
|
||
<h2 className='text-lg font-medium text-gray-900 dark:text-white mb-4'>
|
||
{t('提示词')}
|
||
</h2>
|
||
<Input.TextArea
|
||
value={prompt}
|
||
onChange={setPrompt}
|
||
placeholder={t('描述你想要生成的图像...')}
|
||
rows={5}
|
||
className='mb-4'
|
||
/>
|
||
|
||
<div className='mb-4'>
|
||
<Collapse
|
||
items={[
|
||
{
|
||
title: (
|
||
<div className='flex items-center cursor-pointer'>
|
||
<Settings size={16} className='mr-2' />
|
||
<span>{t('高级选项')}</span>
|
||
</div>
|
||
),
|
||
content: (
|
||
<div className='space-y-4'>
|
||
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
|
||
<div>
|
||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1'>
|
||
{t('图片数量')}
|
||
</label>
|
||
<Select
|
||
value={n}
|
||
onChange={setN}
|
||
options={[1, 2, 3, 4].map((num) => ({
|
||
value: num,
|
||
label: num.toString(),
|
||
}))}
|
||
className='w-full'
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1'>
|
||
{t('图片质量')}
|
||
</label>
|
||
<Select
|
||
value={quality}
|
||
onChange={setQuality}
|
||
options={IMAGE_QUALITY.map((o) => ({
|
||
...o,
|
||
label: t(o.label),
|
||
}))}
|
||
disabled={
|
||
!currentCapability ||
|
||
!currentCapability.sendQuality
|
||
}
|
||
className='w-full'
|
||
/>
|
||
{currentCapability &&
|
||
!currentCapability.sendQuality && (
|
||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||
{t('当前模型不支持质量选择')}
|
||
</p>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
<div>
|
||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1'>
|
||
{t('图片尺寸')}
|
||
</label>
|
||
<Select
|
||
value={size}
|
||
onChange={setSize}
|
||
options={getRecommendedSizes()}
|
||
className='w-full'
|
||
/>
|
||
</div>
|
||
</div>
|
||
),
|
||
isOpen: showAdvanced,
|
||
onItemClick: () => setShowAdvanced(!showAdvanced),
|
||
},
|
||
]}
|
||
/>
|
||
</div>
|
||
|
||
<Button
|
||
type='primary'
|
||
onClick={() => {
|
||
console.log('按钮被点击了!');
|
||
generateImage();
|
||
}}
|
||
loading={isGenerating}
|
||
disabled={
|
||
isGenerating ||
|
||
!prompt.trim() ||
|
||
!selectedModel ||
|
||
selectedModel.level_locked
|
||
}
|
||
icon={<ActionZap size={16} />}
|
||
className='w-full'
|
||
>
|
||
{isGenerating ? t('创建任务中...') : t('生成图片')}
|
||
</Button>
|
||
</div>
|
||
</Card>
|
||
|
||
{error && (
|
||
<Card className='border-red-200 bg-red-50 dark:bg-red-900/20 dark:border-red-800'>
|
||
<div className='p-4'>
|
||
<div className='flex items-start'>
|
||
<AlertCircle className='h-5 w-5 text-red-500 mr-2 flex-shrink-0' />
|
||
<p className='text-sm text-red-700 dark:text-red-300'>
|
||
{error}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
)}
|
||
</div>
|
||
|
||
{/* 右侧使用提示 */}
|
||
<div className='lg:col-span-2'>
|
||
<Card>
|
||
<div className='p-6'>
|
||
<h3 className='text-md font-medium text-gray-900 dark:text-white mb-3 flex items-center'>
|
||
<Info size={16} className='mr-2 text-blue-500' />
|
||
{t('使用说明')}
|
||
</h3>
|
||
<ul className='space-y-2 text-sm text-gray-600 dark:text-gray-300'>
|
||
<li className='flex items-start'>
|
||
<Check
|
||
size={16}
|
||
className='h-5 w-5 text-green-500 mr-2 flex-shrink-0 mt-0.5'
|
||
/>
|
||
<span>
|
||
{t('选择图像生成模型,如 qwen-image、dall-e-3 等')}
|
||
</span>
|
||
</li>
|
||
<li className='flex items-start'>
|
||
<Check
|
||
size={16}
|
||
className='h-5 w-5 text-green-500 mr-2 flex-shrink-0 mt-0.5'
|
||
/>
|
||
<span>
|
||
{t('输入详细的提示词,描述你想要生成的图像内容')}
|
||
</span>
|
||
</li>
|
||
<li className='flex items-start'>
|
||
<Check
|
||
size={16}
|
||
className='h-5 w-5 text-green-500 mr-2 flex-shrink-0 mt-0.5'
|
||
/>
|
||
<span>{t('点击"生成图片"后,系统会创建异步任务')}</span>
|
||
</li>
|
||
<li className='flex items-start'>
|
||
<Check
|
||
size={16}
|
||
className='h-5 w-5 text-green-500 mr-2 flex-shrink-0 mt-0.5'
|
||
/>
|
||
<span>{t('在"任务列表"标签页查看生成进度和结果')}</span>
|
||
</li>
|
||
<li className='flex items-start'>
|
||
<Check
|
||
size={16}
|
||
className='h-5 w-5 text-green-500 mr-2 flex-shrink-0 mt-0.5'
|
||
/>
|
||
<span>{t('生成成功后可以查看和下载图片')}</span>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</Card>
|
||
</div>
|
||
</div>
|
||
</Tabs.TabPane>
|
||
|
||
{/* 任务列表Tab */}
|
||
<Tabs.TabPane
|
||
tab={
|
||
<span className='flex items-center gap-2'>
|
||
<Clock size={16} />
|
||
{t('任务列表')}
|
||
{tasks.length > 0 && (
|
||
<Tag color='blue' size='small'>
|
||
{tasks.length}
|
||
</Tag>
|
||
)}
|
||
</span>
|
||
}
|
||
itemKey='tasks'
|
||
>
|
||
<div className='mt-6'>
|
||
<Card>
|
||
<div className='p-6'>
|
||
<div className='flex justify-between items-center mb-4'>
|
||
<h2 className='text-lg font-medium text-gray-900 dark:text-white'>
|
||
{t('我的图像生成任务')}
|
||
</h2>
|
||
<Button
|
||
type='tertiary'
|
||
icon={<ActionRefreshCw size={16} />}
|
||
onClick={() =>
|
||
loadTasks(pagination.page, pagination.pageSize)
|
||
}
|
||
loading={tasksLoading}
|
||
>
|
||
{t('刷新')}
|
||
</Button>
|
||
</div>
|
||
|
||
<Table
|
||
columns={[
|
||
{
|
||
title: t('任务ID'),
|
||
dataIndex: 'id',
|
||
width: 80,
|
||
},
|
||
{
|
||
title: t('提示词'),
|
||
dataIndex: ['properties', 'input'],
|
||
ellipsis: { showTitle: true },
|
||
render: (text) => (
|
||
<Tooltip content={text}>
|
||
<span className='truncate'>{text || '-'}</span>
|
||
</Tooltip>
|
||
),
|
||
},
|
||
{
|
||
title: t('模型'),
|
||
dataIndex: ['properties', 'origin_model_name'],
|
||
width: 150,
|
||
render: (text) => text || '-',
|
||
},
|
||
{
|
||
title: t('状态'),
|
||
dataIndex: 'status',
|
||
width: 100,
|
||
render: (status) => (
|
||
<Tag color={TASK_STATUS_COLORS[status] || 'grey'}>
|
||
{t(TASK_STATUS_NAMES[status] || status)}
|
||
</Tag>
|
||
),
|
||
},
|
||
{
|
||
title: t('进度'),
|
||
dataIndex: 'progress',
|
||
width: 120,
|
||
render: (progress) => {
|
||
const percent = parseInt(progress) || 0;
|
||
return (
|
||
<Progress
|
||
percent={percent}
|
||
showInfo
|
||
size='small'
|
||
stroke={
|
||
percent === 100
|
||
? 'var(--ui-color-success)'
|
||
: 'var(--ui-color-primary)'
|
||
}
|
||
/>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: t('创建时间'),
|
||
dataIndex: 'created_at',
|
||
width: 160,
|
||
render: (time) =>
|
||
time ? new Date(time * 1000).toLocaleString() : '-',
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
width: 100,
|
||
render: (_, record) => (
|
||
<Button
|
||
size='small'
|
||
onClick={() => viewTaskDetail(record)}
|
||
>
|
||
{t('查看')}
|
||
</Button>
|
||
),
|
||
},
|
||
]}
|
||
dataSource={tasks}
|
||
loading={tasksLoading}
|
||
pagination={{
|
||
currentPage: pagination.page,
|
||
pageSize: pagination.pageSize,
|
||
total: pagination.total,
|
||
showSizeChanger: true,
|
||
pageSizeOptions: [10, 20, 50, 100],
|
||
onPageChange: handlePageChange,
|
||
onPageSizeChange: handlePageSizeChange,
|
||
}}
|
||
empty={
|
||
<Empty
|
||
description={t('暂无任务')}
|
||
image={<Clock size={48} className='text-gray-300' />}
|
||
>
|
||
<Button
|
||
type='primary'
|
||
onClick={() => setCurrentTab('generate')}
|
||
>
|
||
{t('去生成图片')}
|
||
</Button>
|
||
</Empty>
|
||
}
|
||
/>
|
||
</div>
|
||
</Card>
|
||
</div>
|
||
</Tabs.TabPane>
|
||
</Tabs>
|
||
</main>
|
||
|
||
{/* 任务详情Modal */}
|
||
<Modal
|
||
title={t('任务详情')}
|
||
visible={!!selectedTask}
|
||
onCancel={() => setSelectedTask(null)}
|
||
footer={null}
|
||
width={900}
|
||
>
|
||
{selectedTask && (
|
||
<div>
|
||
{/* 任务基本信息 */}
|
||
<Card className='mb-4'>
|
||
<div className='p-4'>
|
||
<div className='grid grid-cols-2 gap-4'>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('任务ID')}
|
||
</Typography.Text>
|
||
<Typography.Text className='block mt-1'>
|
||
{selectedTask.id}
|
||
</Typography.Text>
|
||
</div>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('状态')}
|
||
</Typography.Text>
|
||
<div className='mt-1'>
|
||
<Tag
|
||
color={
|
||
TASK_STATUS_COLORS[selectedTask.status] || 'grey'
|
||
}
|
||
>
|
||
{t(
|
||
TASK_STATUS_NAMES[selectedTask.status] ||
|
||
selectedTask.status,
|
||
)}
|
||
</Tag>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('进度')}
|
||
</Typography.Text>
|
||
<div className='mt-1'>
|
||
<Progress
|
||
percent={parseInt(selectedTask.progress) || 0}
|
||
showInfo
|
||
size='small'
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('模型')}
|
||
</Typography.Text>
|
||
<Typography.Text className='block mt-1'>
|
||
{selectedTask.properties?.origin_model_name || '-'}
|
||
</Typography.Text>
|
||
</div>
|
||
<div className='col-span-2'>
|
||
<Typography.Text type='secondary'>
|
||
{t('提示词')}
|
||
</Typography.Text>
|
||
<Typography.Text className='block mt-1'>
|
||
{selectedTask.properties?.input || '-'}
|
||
</Typography.Text>
|
||
</div>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('创建时间')}
|
||
</Typography.Text>
|
||
<Typography.Text className='block mt-1'>
|
||
{selectedTask.created_at
|
||
? new Date(
|
||
selectedTask.created_at * 1000,
|
||
).toLocaleString()
|
||
: '-'}
|
||
</Typography.Text>
|
||
</div>
|
||
<div>
|
||
<Typography.Text type='secondary'>
|
||
{t('完成时间')}
|
||
</Typography.Text>
|
||
<Typography.Text className='block mt-1'>
|
||
{selectedTask.finish_time
|
||
? new Date(
|
||
selectedTask.finish_time * 1000,
|
||
).toLocaleString()
|
||
: '-'}
|
||
</Typography.Text>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
|
||
{/* 生成结果 */}
|
||
{selectedTask.status === TASK_STATUS.SUCCESS &&
|
||
selectedTask.data && (
|
||
<Card>
|
||
<div className='p-4'>
|
||
<Typography.Title heading={6} className='mb-4'>
|
||
{t('生成的图片')}
|
||
</Typography.Title>
|
||
{(() => {
|
||
try {
|
||
// 解析数据
|
||
const data =
|
||
typeof selectedTask.data === 'string'
|
||
? JSON.parse(selectedTask.data)
|
||
: selectedTask.data;
|
||
|
||
console.log('任务数据:', data);
|
||
|
||
// 检查数据结构
|
||
if (!data) {
|
||
console.error('数据为空');
|
||
return <Empty description={t('暂无图片数据')} />;
|
||
}
|
||
|
||
// 尝试多种数据结构
|
||
let images = null;
|
||
|
||
// 结构1: { data: [...] }
|
||
if (data.data && Array.isArray(data.data)) {
|
||
images = data.data;
|
||
}
|
||
// 结构2: { Data: [...] } (大写D)
|
||
else if (data.Data && Array.isArray(data.Data)) {
|
||
images = data.Data;
|
||
}
|
||
// 结构3: 直接是数组
|
||
else if (Array.isArray(data)) {
|
||
images = data;
|
||
}
|
||
|
||
if (!images || images.length === 0) {
|
||
console.error(
|
||
'没有找到图片数据,数据结构:',
|
||
Object.keys(data),
|
||
);
|
||
return <Empty description={t('暂无图片数据')} />;
|
||
}
|
||
|
||
console.log('找到图片:', images);
|
||
|
||
return (
|
||
<div className='grid grid-cols-2 gap-4'>
|
||
{images.map((item, index) =>
|
||
renderImageItem(item, index),
|
||
)}
|
||
</div>
|
||
);
|
||
} catch (e) {
|
||
console.error(
|
||
'解析任务数据失败:',
|
||
e,
|
||
'原始数据:',
|
||
selectedTask.data,
|
||
);
|
||
}
|
||
return <Empty description={t('暂无图片数据')} />;
|
||
})()}
|
||
</div>
|
||
</Card>
|
||
)}
|
||
|
||
{/* 失败信息 */}
|
||
{selectedTask.status === TASK_STATUS.FAILURE && (
|
||
<Card className='border-red-200 bg-red-50 dark:bg-red-900/20'>
|
||
<div className='p-4'>
|
||
<div className='flex items-start'>
|
||
<AlertCircle className='h-5 w-5 text-red-500 mr-2 flex-shrink-0 mt-0.5' />
|
||
<div>
|
||
<Typography.Title
|
||
heading={6}
|
||
className='text-red-700 dark:text-red-300 mb-2'
|
||
>
|
||
{t('任务失败')}
|
||
</Typography.Title>
|
||
<Typography.Text className='text-red-600 dark:text-red-400'>
|
||
{selectedTask.fail_reason || t('未知错误')}
|
||
</Typography.Text>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
)}
|
||
|
||
{/* 进行中提示 */}
|
||
{(selectedTask.status === TASK_STATUS.IN_PROGRESS ||
|
||
selectedTask.status === TASK_STATUS.QUEUED ||
|
||
selectedTask.status === TASK_STATUS.SUBMITTED) && (
|
||
<Card className='border-blue-200 bg-blue-50 dark:bg-blue-900/20'>
|
||
<div className='p-4'>
|
||
<div className='flex items-center'>
|
||
<Spin size='small' className='mr-2' />
|
||
<Typography.Text className='text-blue-700 dark:text-blue-300'>
|
||
{t('任务正在处理中,请稍候...')}
|
||
</Typography.Text>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
)}
|
||
</div>
|
||
)}
|
||
</Modal>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
// 新版创作台开启时独立图像页迁移到体验中心(WP D-1);关闭时保持旧页面。
|
||
// 注:本页目前未在 App.jsx 注册路由,若重新挂载也不会绕过新创作台。
|
||
const ImageGenerationPage = () => {
|
||
const [statusState] = useContext(StatusContext);
|
||
if (isCreationStudioEnabled(statusState?.status)) {
|
||
return <Navigate to='/experience-center/image-generation' replace />;
|
||
}
|
||
return <ImageGeneration />;
|
||
};
|
||
|
||
export default ImageGenerationPage;
|