Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
@@ -0,0 +1,389 @@
/*
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 React, { useEffect, useMemo, useState } from 'react';
import {
Button,
Checkbox,
Empty,
Input,
Modal,
Progress,
Radio,
Spin,
Tag,
Tooltip,
Upload,
} from '@douyinfe/semi-ui';
import {
Check,
ExternalLink,
Image as ImageIcon,
Plus,
ShieldCheck,
Trash2,
UserRound,
} from 'lucide-react';
import { QRCodeSVG } from 'qrcode.react';
import { useTranslation } from 'react-i18next';
import { showError, showSuccess } from '../../../../../helpers';
import { usePortraitAssets } from '../../../../../hooks/video/usePortraitAssets';
const statusColor = {
active: 'green',
draft: 'grey',
processing: 'blue',
failed: 'red',
};
const PortraitAssetPicker = ({
onSelect,
selectedAssetId,
onClearSelection,
}) => {
const { t } = useTranslation();
const [visible, setVisible] = useState(false);
const [name, setName] = useState('');
const [consent, setConsent] = useState(false);
const [selectedPortraitId, setSelectedPortraitId] = useState('');
const [busy, setBusy] = useState(false);
const [secondsLeft, setSecondsLeft] = useState(0);
const portrait = usePortraitAssets(visible);
const selectedPortrait = useMemo(
() =>
portrait.portraits.find((item) => item.id === selectedPortraitId) || null,
[portrait.portraits, selectedPortraitId],
);
useEffect(() => {
if (!selectedPortraitId && portrait.portraits.length > 0) {
setSelectedPortraitId(portrait.portraits[0].id);
}
}, [portrait.portraits, selectedPortraitId]);
useEffect(() => {
if (!portrait.session?.expires_at) {
setSecondsLeft(0);
return undefined;
}
const tick = () => {
setSecondsLeft(
Math.max(
0,
portrait.session.expires_at - Math.floor(Date.now() / 1000),
),
);
};
tick();
const timer = setInterval(tick, 1000);
return () => clearInterval(timer);
}, [portrait.session?.expires_at]);
const handleCreate = async () => {
if (!name.trim() || !consent) return;
setBusy(true);
try {
const created = await portrait.createPortrait(name.trim());
setSelectedPortraitId(created.id);
setName('');
setConsent(false);
} catch (error) {
showError(portrait.errorMessage(error));
} finally {
setBusy(false);
}
};
const handleVerify = async () => {
if (!selectedPortraitId) return;
setBusy(true);
try {
await portrait.startVerification(selectedPortraitId);
} catch (error) {
showError(portrait.errorMessage(error));
} finally {
setBusy(false);
}
};
const handleUpload = async ({ fileList }) => {
const file = fileList?.[fileList.length - 1]?.fileInstance;
if (!file || !selectedPortraitId) return;
setBusy(true);
try {
const asset = await portrait.uploadPortraitAsset(
selectedPortraitId,
file,
);
showSuccess(t('素材已提交审核'));
portrait.refreshAssetUntilTerminal(asset.id).catch(() => {});
} catch (error) {
showError(portrait.errorMessage(error));
} finally {
setBusy(false);
}
};
const assets = portrait.assets.filter(
(asset) => asset.portrait_id === selectedPortraitId,
);
const confirmDeleteAsset = (asset) => {
Modal.confirm({
title: t('删除素材'),
content: t('删除后将无法继续用于视频生成。'),
okType: 'danger',
onOk: async () => {
try {
await portrait.deleteAsset(asset.id);
if (asset.id === selectedAssetId) onClearSelection?.();
} catch (error) {
showError(portrait.errorMessage(error));
throw error;
}
},
});
};
const confirmDeletePortrait = (item) => {
Modal.confirm({
title: t('删除人像'),
content: t('该人像及其全部关联素材将被删除。'),
okType: 'danger',
onOk: async () => {
try {
const clearsSelection = portrait.assets.some(
(asset) =>
asset.id === selectedAssetId && asset.portrait_id === item.id,
);
await portrait.deletePortrait(item.id);
if (clearsSelection) onClearSelection?.();
if (selectedPortraitId === item.id) setSelectedPortraitId('');
showSuccess(t('人像已删除'));
} catch (error) {
showError(portrait.errorMessage(error));
throw error;
}
},
});
};
return (
<>
<Button
icon={<UserRound size={16} />}
theme='light'
onClick={() => setVisible(true)}
>
{t('真人人像素材')}
</Button>
<Modal
title={t('真人人像素材')}
visible={visible}
onCancel={() => setVisible(false)}
footer={null}
width={720}
bodyStyle={{ maxHeight: '72vh', overflow: 'auto' }}
>
{portrait.loading ? (
<div className='flex justify-center py-12'>
<Spin />
</div>
) : portrait.enabled === false ? (
<Empty title={t('真人人像素材未启用')} />
) : (
<div className='flex flex-col gap-5'>
<div className='flex flex-col gap-3 border-b border-solid border-semi-color-border pb-5'>
<div className='flex gap-2'>
<Input
value={name}
onChange={setName}
maxLength={128}
placeholder={t('人像名称')}
/>
<Tooltip content={t('创建人像')}>
<Button
icon={<Plus size={16} />}
onClick={handleCreate}
loading={busy}
disabled={!name.trim() || !consent}
/>
</Tooltip>
</div>
<Checkbox
checked={consent}
onChange={(event) => setConsent(event.target.checked)}
>
{t('我已获得本人授权并拥有该人像素材的合法使用权')}
</Checkbox>
</div>
{portrait.portraits.length > 0 && (
<Radio.Group
value={selectedPortraitId}
onChange={(event) => {
setSelectedPortraitId(event.target.value);
portrait.setSession(null);
}}
direction='vertical'
className='w-full'
>
{portrait.portraits.map((item) => (
<div key={item.id} className='flex items-center gap-2'>
<Radio value={item.id} className='min-w-0 flex-1'>
<span className='inline-flex min-w-0 items-center gap-2 py-1'>
<span className='truncate'>{item.name}</span>
<Tag
color={statusColor[item.status] || 'grey'}
size='small'
>
{item.status}
</Tag>
<span className='text-xs text-semi-color-text-2'>
{item.id.slice(-6)}
</span>
</span>
</Radio>
<Tooltip content={t('删除人像')}>
<Button
type='danger'
theme='borderless'
icon={<Trash2 size={16} />}
onClick={() => confirmDeletePortrait(item)}
/>
</Tooltip>
</div>
))}
</Radio.Group>
)}
{selectedPortrait &&
selectedPortrait.status !== 'active' &&
!portrait.session && (
<Button
icon={<ShieldCheck size={16} />}
onClick={handleVerify}
loading={busy}
>
{t('开始活体认证')}
</Button>
)}
{portrait.session &&
['pending', 'confirming'].includes(portrait.session.status) && (
<div className='flex flex-col items-center gap-3 border-y border-solid border-semi-color-border py-5'>
<QRCodeSVG value={portrait.session.h5_url} size={184} />
<Progress
percent={Math.round((secondsLeft / 120) * 100)}
showInfo
format={() => `${secondsLeft}s`}
style={{ width: 240 }}
/>
<Button
icon={<ExternalLink size={16} />}
onClick={() =>
window.open(
portrait.session.h5_url,
'_blank',
'noopener,noreferrer',
)
}
>
{t('打开认证页面')}
</Button>
</div>
)}
{portrait.session?.status === 'succeeded' && (
<Tag color='green' prefixIcon={<Check size={14} />}>
{t('认证通过')}
</Tag>
)}
{selectedPortrait?.status === 'active' && (
<Upload
accept='image/*'
limit={1}
uploadTrigger='custom'
showUploadList={false}
onChange={handleUpload}
>
<Button icon={<ImageIcon size={16} />} loading={busy}>
{t('上传真人素材')}
</Button>
</Upload>
)}
<div className='flex flex-col divide-y divide-semi-color-border'>
{assets.map((asset) => (
<div
key={asset.id}
className='flex min-h-[64px] items-center gap-3 py-3'
>
<div className='h-12 w-12 shrink-0 overflow-hidden rounded-[6px] bg-semi-color-fill-0'>
{asset.preview_url && (
<img
src={asset.preview_url}
alt={asset.name}
className='h-full w-full object-cover'
/>
)}
</div>
<div className='min-w-0 flex-1'>
<div className='truncate text-sm font-medium'>
{asset.name}
</div>
<Tag
color={statusColor[asset.status] || 'grey'}
size='small'
>
{asset.status}
</Tag>
</div>
{asset.status === 'active' && (
<Tooltip content={t('选择素材')}>
<Button
icon={<Check size={16} />}
onClick={() => {
onSelect(asset);
setVisible(false);
}}
/>
</Tooltip>
)}
<Tooltip content={t('删除素材')}>
<Button
type='danger'
theme='borderless'
icon={<Trash2 size={16} />}
onClick={() => confirmDeleteAsset(asset)}
/>
</Tooltip>
</div>
))}
</div>
</div>
)}
</Modal>
</>
);
};
export default PortraitAssetPicker;