feat: 1:一键成片参数动态配置
2:订单收藏
This commit is contained in:
+165
-82
@@ -6,7 +6,8 @@ import {
|
||||
ProFormTextArea,
|
||||
ProFormSelect,
|
||||
} from '@ant-design/pro-components';
|
||||
import { message } from 'antd';
|
||||
import { message, Button, InputNumber, Input, Space, Table, Tooltip } from 'antd';
|
||||
import { PlusOutlined, DeleteOutlined, QuestionCircleOutlined } from '@ant-design/icons';
|
||||
import { ordersApi } from '@/services/orders';
|
||||
|
||||
export const STATUS_OPTIONS = [
|
||||
@@ -14,11 +15,16 @@ export const STATUS_OPTIONS = [
|
||||
{ label: '禁用', value: 0 },
|
||||
];
|
||||
|
||||
export type ClipMappingItem = {
|
||||
slot: string;
|
||||
fileIndex: number | undefined;
|
||||
};
|
||||
|
||||
export type VideoTemplateFormValues = {
|
||||
id?: number;
|
||||
templateId?: string;
|
||||
templateName?: string;
|
||||
name?: string; // 兼容旧字段
|
||||
name?: string;
|
||||
description?: string;
|
||||
config?: string;
|
||||
status?: number;
|
||||
@@ -32,6 +38,31 @@ interface VideoTemplateFormProps {
|
||||
initialValues?: Partial<VideoTemplateFormValues>;
|
||||
}
|
||||
|
||||
const parseConfigToMappings = (config?: string): ClipMappingItem[] => {
|
||||
if (!config || config.trim() === '{}' || config.trim() === '[]') return [];
|
||||
try {
|
||||
const parsed = JSON.parse(config);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed.map((item: any) => ({
|
||||
slot: String(item.slot ?? ''),
|
||||
fileIndex: item.fileIndex,
|
||||
}));
|
||||
}
|
||||
return [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const mappingsToConfig = (mappings: ClipMappingItem[]): string => {
|
||||
if (!mappings || mappings.length === 0) return '{}';
|
||||
const cleaned = mappings
|
||||
.filter((m) => m.slot && m.fileIndex !== undefined)
|
||||
.map((m) => ({ slot: m.slot, fileIndex: m.fileIndex }));
|
||||
if (cleaned.length === 0) return '{}';
|
||||
return JSON.stringify(cleaned, null, 2);
|
||||
};
|
||||
|
||||
const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
title,
|
||||
visible,
|
||||
@@ -40,37 +71,107 @@ const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
initialValues,
|
||||
}) => {
|
||||
const formRef = useRef<any>();
|
||||
const [mappings, setMappings] = React.useState<ClipMappingItem[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (visible && initialValues) {
|
||||
// 延迟设置,确保表单已经渲染
|
||||
setTimeout(() => {
|
||||
// 处理字段兼容性
|
||||
const values = {
|
||||
...initialValues,
|
||||
templateName: initialValues.templateName || initialValues.name,
|
||||
config: initialValues.config || '{}', // 确保config有默认值
|
||||
};
|
||||
formRef.current?.setFieldsValue(values);
|
||||
}, 0);
|
||||
if (visible) {
|
||||
setMappings(parseConfigToMappings(initialValues?.config));
|
||||
if (initialValues) {
|
||||
setTimeout(() => {
|
||||
formRef.current?.setFieldsValue({
|
||||
...initialValues,
|
||||
templateName: initialValues.templateName || initialValues.name,
|
||||
});
|
||||
}, 0);
|
||||
}
|
||||
} else {
|
||||
setMappings([]);
|
||||
}
|
||||
}, [visible, initialValues]);
|
||||
|
||||
const handleAddMapping = () => {
|
||||
setMappings([...mappings, { slot: '', fileIndex: undefined }]);
|
||||
};
|
||||
|
||||
const handleRemoveMapping = (index: number) => {
|
||||
setMappings(mappings.filter((_, i) => i !== index));
|
||||
};
|
||||
|
||||
const handleMappingChange = (index: number, field: keyof ClipMappingItem, value: any) => {
|
||||
const next = [...mappings];
|
||||
next[index] = { ...next[index], [field]: value };
|
||||
setMappings(next);
|
||||
};
|
||||
|
||||
const handleFinish = async (values: VideoTemplateFormValues) => {
|
||||
try {
|
||||
// 确保兼容性
|
||||
const submitValues = {
|
||||
const valid = mappings.filter((m) => m.slot && m.fileIndex !== undefined);
|
||||
const slots = valid.map((m) => m.slot);
|
||||
if (new Set(slots).size !== slots.length) {
|
||||
message.error('存在重复的槽位号,请检查');
|
||||
return false;
|
||||
}
|
||||
return await onFinish({
|
||||
...values,
|
||||
name: values.templateName, // 同时设置name字段
|
||||
};
|
||||
const result = await onFinish(submitValues);
|
||||
return result;
|
||||
} catch (error) {
|
||||
name: values.templateName,
|
||||
config: mappingsToConfig(mappings),
|
||||
});
|
||||
} catch {
|
||||
message.error('操作失败');
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: (
|
||||
<Space>
|
||||
槽位号
|
||||
<Tooltip title="阿里云剪辑模板中的素材槽位编号,如 1、2、201">
|
||||
<QuestionCircleOutlined style={{ color: '#999' }} />
|
||||
</Tooltip>
|
||||
</Space>
|
||||
),
|
||||
dataIndex: 'slot',
|
||||
width: 140,
|
||||
render: (_: any, record: ClipMappingItem, index: number) => (
|
||||
<Input
|
||||
value={record.slot}
|
||||
placeholder="如: 1"
|
||||
onChange={(e) => handleMappingChange(index, 'slot', e.target.value)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: (
|
||||
<Space>
|
||||
素材序号
|
||||
<Tooltip title="对应订单素材文件列表中的位置序号(从0开始)">
|
||||
<QuestionCircleOutlined style={{ color: '#999' }} />
|
||||
</Tooltip>
|
||||
</Space>
|
||||
),
|
||||
dataIndex: 'fileIndex',
|
||||
width: 140,
|
||||
render: (_: any, record: ClipMappingItem, index: number) => (
|
||||
<InputNumber
|
||||
value={record.fileIndex}
|
||||
min={0}
|
||||
placeholder="如: 0"
|
||||
style={{ width: '100%' }}
|
||||
onChange={(val) => handleMappingChange(index, 'fileIndex', val)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
width: 60,
|
||||
render: (_: any, __: any, index: number) => (
|
||||
<Button type="link" danger icon={<DeleteOutlined />} onClick={() => handleRemoveMapping(index)} />
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<ModalForm<VideoTemplateFormValues>
|
||||
title={title}
|
||||
@@ -79,15 +180,8 @@ const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
formRef={formRef}
|
||||
onVisibleChange={onVisibleChange}
|
||||
onFinish={handleFinish}
|
||||
initialValues={{
|
||||
status: 1, // 默认启用
|
||||
config: '{}', // 默认空JSON对象
|
||||
...initialValues,
|
||||
}}
|
||||
modalProps={{
|
||||
destroyOnClose: true,
|
||||
maskClosable: false,
|
||||
}}
|
||||
initialValues={{ status: 1, ...initialValues }}
|
||||
modalProps={{ destroyOnClose: true, maskClosable: false }}
|
||||
>
|
||||
<ProForm.Group title="基本信息">
|
||||
<ProFormText
|
||||
@@ -97,10 +191,7 @@ const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
placeholder="请输入模板ID"
|
||||
rules={[
|
||||
{ required: true, message: '请输入模板ID' },
|
||||
{
|
||||
pattern: /^[a-zA-Z0-9_-]+$/,
|
||||
message: '模板ID只能包含字母、数字、下划线和横线'
|
||||
}
|
||||
{ pattern: /^[a-zA-Z0-9_-]+$/, message: '模板ID只能包含字母、数字、下划线和横线' },
|
||||
]}
|
||||
/>
|
||||
<ProFormText
|
||||
@@ -110,7 +201,7 @@ const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
placeholder="请输入模板名称"
|
||||
rules={[
|
||||
{ required: true, message: '请输入模板名称' },
|
||||
{ max: 50, message: '模板名称不能超过50个字符' }
|
||||
{ max: 50, message: '模板名称不能超过50个字符' },
|
||||
]}
|
||||
/>
|
||||
<ProFormSelect
|
||||
@@ -122,61 +213,53 @@ const VideoTemplateForm: React.FC<VideoTemplateFormProps> = ({
|
||||
/>
|
||||
</ProForm.Group>
|
||||
|
||||
<ProForm.Group title="详细配置">
|
||||
<ProForm.Group title="模板描述">
|
||||
<ProFormTextArea
|
||||
name="description"
|
||||
label="模板描述"
|
||||
label="描述"
|
||||
width="xl"
|
||||
placeholder="请输入模板描述"
|
||||
fieldProps={{
|
||||
rows: 4,
|
||||
maxLength: 200,
|
||||
showCount: true,
|
||||
}}
|
||||
/>
|
||||
<ProFormTextArea
|
||||
name="config"
|
||||
label="模板配置"
|
||||
width="xl"
|
||||
placeholder='请输入JSON格式的模板配置,例如: {"key": "value"}'
|
||||
tooltip="请输入有效的JSON格式配置,默认为空对象 {}"
|
||||
fieldProps={{
|
||||
rows: 8,
|
||||
maxLength: 2000,
|
||||
showCount: true,
|
||||
style: { fontFamily: 'monospace' },
|
||||
onBlur: (e) => {
|
||||
// 格式化JSON
|
||||
const value = e.target.value;
|
||||
if (value) {
|
||||
try {
|
||||
const json = JSON.parse(value);
|
||||
const formatted = JSON.stringify(json, null, 2);
|
||||
formRef.current?.setFieldValue('config', formatted);
|
||||
} catch (error) {
|
||||
// 格式化失败,保持原样
|
||||
}
|
||||
}
|
||||
},
|
||||
}}
|
||||
rules={[
|
||||
{ required: true, message: '请输入模板配置' },
|
||||
{
|
||||
validator: async (_, value) => {
|
||||
if (value) {
|
||||
try {
|
||||
JSON.parse(value);
|
||||
} catch (error) {
|
||||
throw new Error('请输入有效的JSON格式');
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
]}
|
||||
fieldProps={{ rows: 3, maxLength: 200, showCount: true }}
|
||||
/>
|
||||
</ProForm.Group>
|
||||
|
||||
<div style={{ marginBottom: 24 }}>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 12 }}>
|
||||
<Space>
|
||||
<span style={{ fontWeight: 500, fontSize: 16 }}>素材槽位映射配置</span>
|
||||
<Tooltip title="配置阿里云剪辑模板槽位与订单素材文件的对应关系。不配置则使用系统默认映射逻辑">
|
||||
<QuestionCircleOutlined style={{ color: '#999' }} />
|
||||
</Tooltip>
|
||||
</Space>
|
||||
<Button type="dashed" icon={<PlusOutlined />} onClick={handleAddMapping}>
|
||||
添加映射
|
||||
</Button>
|
||||
</div>
|
||||
{mappings.length > 0 ? (
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={mappings}
|
||||
rowKey={(_, index) => String(index)}
|
||||
pagination={false}
|
||||
size="small"
|
||||
bordered
|
||||
/>
|
||||
) : (
|
||||
<div
|
||||
style={{
|
||||
border: '1px dashed #d9d9d9',
|
||||
borderRadius: 6,
|
||||
padding: '24px 0',
|
||||
textAlign: 'center',
|
||||
color: '#999',
|
||||
}}
|
||||
>
|
||||
暂无映射配置,将使用系统默认映射逻辑。点击"添加映射"开始配置
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</ModalForm>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideoTemplateForm;
|
||||
export default VideoTemplateForm;
|
||||
|
||||
Reference in New Issue
Block a user