511 lines
15 KiB
React
511 lines
15 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 { useEffectEvent } from '../../../hooks/common/useEffectEvent';
|
||
import React, { useEffect, useState } from 'react';
|
||
import {
|
||
Button,
|
||
Table,
|
||
Modal,
|
||
Form,
|
||
Space,
|
||
Typography,
|
||
Tabs,
|
||
TabPane,
|
||
Popconfirm,
|
||
Select,
|
||
Empty,
|
||
Spin,
|
||
} from '@/components/ui';
|
||
import { IconPlus } from '@/components/ui/icons';
|
||
import { API, showError, showSuccess } from '../../../helpers';
|
||
import { useTranslation } from 'react-i18next';
|
||
|
||
const { Title, Text } = Typography;
|
||
|
||
const SettingModelCategory = () => {
|
||
const { t } = useTranslation();
|
||
const [loading, setLoading] = useState(false);
|
||
const [level1Categories, setLevel1Categories] = useState([]);
|
||
const [level2Categories, setLevel2Categories] = useState([]);
|
||
const [allLevel2Categories, setAllLevel2Categories] = useState([]);
|
||
|
||
// 模态框相关
|
||
const [modalVisible, setModalVisible] = useState(false);
|
||
const [editingCategory, setEditingCategory] = useState(null);
|
||
const [isLevel1Modal, setIsLevel1Modal] = useState(true);
|
||
|
||
// 二级分类选择的一级分类
|
||
const [selectedLevel1Id, setSelectedLevel1Id] = useState(null);
|
||
|
||
const loadCategories = async () => {
|
||
setLoading(true);
|
||
try {
|
||
const res = await API.get('/api/model_categories/flat');
|
||
const { success, data, message } = res.data;
|
||
if (success) {
|
||
const categoriesData = data || [];
|
||
|
||
// 分离一级和二级分类
|
||
const level1 = categoriesData.filter((cat) => cat.parent_id === 0);
|
||
const level2 = categoriesData.filter((cat) => cat.parent_id !== 0);
|
||
|
||
setLevel1Categories(level1);
|
||
setAllLevel2Categories(level2);
|
||
|
||
// 如果已选择一级分类,更新二级分类列表
|
||
if (selectedLevel1Id) {
|
||
const filteredLevel2 = level2.filter(
|
||
(cat) => cat.parent_id === selectedLevel1Id,
|
||
);
|
||
setLevel2Categories(filteredLevel2);
|
||
}
|
||
} else {
|
||
showError(message || t('加载分类失败'));
|
||
}
|
||
} catch (error) {
|
||
showError(t('加载分类失败: ') + error.message);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
const onMount = useEffectEvent(() => {
|
||
loadCategories();
|
||
});
|
||
useEffect(() => onMount(), [onMount]);
|
||
|
||
// 选择一级分类时更新二级分类列表
|
||
const handleLevel1Select = (value) => {
|
||
setSelectedLevel1Id(value);
|
||
const filteredLevel2 = allLevel2Categories.filter(
|
||
(cat) => cat.parent_id === value,
|
||
);
|
||
setLevel2Categories(filteredLevel2);
|
||
};
|
||
|
||
// 打开新建一级分类模态框
|
||
const handleAddLevel1 = () => {
|
||
setIsLevel1Modal(true);
|
||
setEditingCategory({
|
||
id: 0,
|
||
parent_id: 0,
|
||
category_name: '',
|
||
category_key: '',
|
||
description: '',
|
||
sort: 0,
|
||
status: 1,
|
||
});
|
||
setModalVisible(true);
|
||
};
|
||
|
||
// 打开编辑一级分类模态框
|
||
const handleEditLevel1 = (category) => {
|
||
setIsLevel1Modal(true);
|
||
setEditingCategory({ ...category });
|
||
setModalVisible(true);
|
||
};
|
||
|
||
// 打开新建二级分类模态框
|
||
const handleAddLevel2 = () => {
|
||
if (!selectedLevel1Id) {
|
||
showError(t('请先选择一级分类'));
|
||
return;
|
||
}
|
||
|
||
// 找到选中的一级分类名称
|
||
const selectedLevel1 = level1Categories.find(
|
||
(cat) => cat.id === selectedLevel1Id,
|
||
);
|
||
|
||
setIsLevel1Modal(false);
|
||
setEditingCategory({
|
||
id: 0,
|
||
parent_id: selectedLevel1Id,
|
||
parent_category_name: selectedLevel1?.category_name || '',
|
||
category_name: '',
|
||
category_key: '',
|
||
description: '',
|
||
sort: 0,
|
||
status: 1,
|
||
});
|
||
setModalVisible(true);
|
||
};
|
||
|
||
// 打开编辑二级分类模态框
|
||
const handleEditLevel2 = (category) => {
|
||
setIsLevel1Modal(false);
|
||
setEditingCategory({ ...category });
|
||
setModalVisible(true);
|
||
};
|
||
|
||
// 保存分类
|
||
const handleSave = async (values) => {
|
||
try {
|
||
let res;
|
||
if (editingCategory.id === 0) {
|
||
// 创建
|
||
res = await API.post('/api/model_categories/', values);
|
||
} else {
|
||
// 更新
|
||
res = await API.put('/api/model_categories/', {
|
||
...values,
|
||
id: editingCategory.id,
|
||
});
|
||
}
|
||
const { success, message } = res.data;
|
||
if (success) {
|
||
showSuccess(editingCategory.id === 0 ? t('创建成功') : t('保存成功'));
|
||
setModalVisible(false);
|
||
await loadCategories();
|
||
} else {
|
||
showError(
|
||
message || (editingCategory.id === 0 ? t('创建失败') : t('保存失败')),
|
||
);
|
||
}
|
||
} catch (error) {
|
||
showError(
|
||
(editingCategory.id === 0 ? t('创建失败: ') : t('保存失败: ')) +
|
||
error.message,
|
||
);
|
||
}
|
||
};
|
||
|
||
// 删除分类
|
||
const handleDelete = async (id) => {
|
||
try {
|
||
const res = await API.delete(`/api/model_categories/${id}`);
|
||
const { success, message } = res.data;
|
||
if (success) {
|
||
showSuccess(t('删除成功'));
|
||
await loadCategories();
|
||
} else {
|
||
showError(message || t('删除失败'));
|
||
}
|
||
} catch (error) {
|
||
showError(t('删除失败: ') + error.message);
|
||
}
|
||
};
|
||
|
||
// 重置分类
|
||
const handleReset = async () => {
|
||
try {
|
||
const res = await API.post('/api/model_categories/reset');
|
||
const { success, message } = res.data;
|
||
if (success) {
|
||
showSuccess(t('分类已重置为默认配置'));
|
||
await loadCategories();
|
||
} else {
|
||
showError(message || t('重置失败'));
|
||
}
|
||
} catch (error) {
|
||
showError(t('重置失败: ') + error.message);
|
||
}
|
||
};
|
||
|
||
// 一级分类表格列
|
||
const level1Columns = [
|
||
{
|
||
title: t('分类名称'),
|
||
dataIndex: 'category_name',
|
||
width: 150,
|
||
},
|
||
{
|
||
title: t('分类标识'),
|
||
dataIndex: 'category_key',
|
||
width: 150,
|
||
},
|
||
{
|
||
title: t('描述'),
|
||
dataIndex: 'description',
|
||
},
|
||
{
|
||
title: t('排序'),
|
||
dataIndex: 'sort',
|
||
width: 80,
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
dataIndex: 'operate',
|
||
width: 150,
|
||
render: (text, record) => (
|
||
<Space>
|
||
<Button
|
||
onClick={() => handleEditLevel1(record)}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('修改')}
|
||
</Button>
|
||
<Popconfirm
|
||
title={t('确定要删除这个分类吗?')}
|
||
content={t('删除前请确保已删除其下的所有二级分类')}
|
||
onConfirm={() => handleDelete(record.id)}
|
||
okText={t('确定')}
|
||
cancelText={t('取消')}
|
||
>
|
||
<Button type='link' size='small' danger>
|
||
{t('删除')}
|
||
</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
// 二级分类表格列
|
||
const level2Columns = [
|
||
{
|
||
title: t('分类名称'),
|
||
dataIndex: 'category_name',
|
||
width: 150,
|
||
},
|
||
{
|
||
title: t('分类标识'),
|
||
dataIndex: 'category_key',
|
||
width: 150,
|
||
},
|
||
{
|
||
title: t('描述'),
|
||
dataIndex: 'description',
|
||
},
|
||
{
|
||
title: t('排序'),
|
||
dataIndex: 'sort',
|
||
width: 80,
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
dataIndex: 'operate',
|
||
width: 150,
|
||
render: (text, record) => (
|
||
<Space>
|
||
<Button
|
||
onClick={() => handleEditLevel2(record)}
|
||
type='link'
|
||
size='small'
|
||
>
|
||
{t('修改')}
|
||
</Button>
|
||
<Popconfirm
|
||
title={t('确定要删除这个分类吗?')}
|
||
content={t('此操作不可恢复')}
|
||
onConfirm={() => handleDelete(record.id)}
|
||
okText={t('确定')}
|
||
cancelText={t('取消')}
|
||
>
|
||
<Button type='link' size='small' danger>
|
||
{t('删除')}
|
||
</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<Spin spinning={loading}>
|
||
<div>
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
justifyContent: 'space-between',
|
||
alignItems: 'center',
|
||
marginBottom: 16,
|
||
}}
|
||
>
|
||
<div>
|
||
<Title heading={5}>{t('模型分类管理')}</Title>
|
||
<Text type='tertiary'>
|
||
{t('管理模型的一级分类和二级分类,用于模型标签化管理')}
|
||
</Text>
|
||
</div>
|
||
<Popconfirm
|
||
title={t('确定要重置分类吗?')}
|
||
content={t('这将删除所有自定义分类并恢复为默认配置')}
|
||
onConfirm={handleReset}
|
||
okText={t('确定')}
|
||
cancelText={t('取消')}
|
||
>
|
||
<Button type='tertiary'>{t('重置为默认分类')}</Button>
|
||
</Popconfirm>
|
||
</div>
|
||
|
||
<Tabs type='line'>
|
||
{/* 一级分类 */}
|
||
<TabPane tab={t('一级分类')} itemKey='level1'>
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
justifyContent: 'flex-end',
|
||
marginBottom: 16,
|
||
}}
|
||
>
|
||
<Button
|
||
type='primary'
|
||
icon={<IconPlus />}
|
||
onClick={handleAddLevel1}
|
||
>
|
||
{t('新建')}
|
||
</Button>
|
||
</div>
|
||
<Table
|
||
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
|
||
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
|
||
scroll={{ x: 'max-content' }}
|
||
columns={level1Columns}
|
||
dataSource={level1Categories}
|
||
rowKey='id'
|
||
pagination={false}
|
||
empty={<Empty description={t('暂无数据')} />}
|
||
/>
|
||
</TabPane>
|
||
|
||
{/* 二级分类 */}
|
||
<TabPane tab={t('二级分类')} itemKey='level2'>
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
justifyContent: 'space-between',
|
||
alignItems: 'center',
|
||
marginBottom: 16,
|
||
}}
|
||
>
|
||
<div style={{ flex: 1, maxWidth: 300 }}>
|
||
<Select
|
||
placeholder={t('选择一级分类')}
|
||
value={selectedLevel1Id}
|
||
onChange={handleLevel1Select}
|
||
style={{ width: '100%' }}
|
||
>
|
||
{level1Categories.map((cat) => (
|
||
<Select.Option key={cat.id} value={cat.id}>
|
||
{cat.category_name}
|
||
</Select.Option>
|
||
))}
|
||
</Select>
|
||
</div>
|
||
<Button
|
||
type='primary'
|
||
icon={<IconPlus />}
|
||
onClick={handleAddLevel2}
|
||
disabled={!selectedLevel1Id}
|
||
>
|
||
{t('新建二级分类')}
|
||
</Button>
|
||
</div>
|
||
{!selectedLevel1Id && (
|
||
<div className='mb-3'>{t('请先选择一级分类')}</div>
|
||
)}
|
||
<Table
|
||
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
|
||
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
|
||
scroll={{ x: 'max-content' }}
|
||
columns={level2Columns}
|
||
dataSource={level2Categories}
|
||
rowKey='id'
|
||
pagination={false}
|
||
empty={<Empty description={t('暂无数据')} />}
|
||
/>
|
||
</TabPane>
|
||
</Tabs>
|
||
|
||
{/* 编辑/新建模态框 */}
|
||
<Modal
|
||
rootClassName='settings-form-dialog'
|
||
title={
|
||
editingCategory?.id === 0
|
||
? isLevel1Modal
|
||
? t('新建一级分类')
|
||
: t('新建二级分类')
|
||
: isLevel1Modal
|
||
? t('编辑一级分类')
|
||
: t('编辑二级分类')
|
||
}
|
||
visible={modalVisible}
|
||
onCancel={() => setModalVisible(false)}
|
||
footer={null}
|
||
width={600}
|
||
>
|
||
<Form
|
||
initValues={editingCategory}
|
||
onSubmit={(values) => handleSave(values)}
|
||
labelPosition='left'
|
||
labelWidth='120px'
|
||
>
|
||
{!isLevel1Modal && (
|
||
<Form.Input
|
||
field='parent_category_name'
|
||
label={t('一级分类')}
|
||
disabled
|
||
style={{ width: '100%' }}
|
||
/>
|
||
)}
|
||
<Form.Input
|
||
field='category_name'
|
||
label={t('分类名称')}
|
||
placeholder={
|
||
isLevel1Modal ? t('例如:文本模型') : t('例如:文本生成')
|
||
}
|
||
rules={[{ required: true, message: t('请输入分类名称') }]}
|
||
style={{ width: '100%' }}
|
||
/>
|
||
<Form.Input
|
||
field='category_key'
|
||
label={t('分类标识')}
|
||
placeholder={
|
||
isLevel1Modal ? t('例如:text') : t('例如:text_generation')
|
||
}
|
||
rules={[{ required: true, message: t('请输入分类标识') }]}
|
||
extraText={t('唯一标识,只能包含字母、数字和下划线')}
|
||
style={{ width: '100%' }}
|
||
/>
|
||
<Form.TextArea
|
||
field='description'
|
||
label={t('描述')}
|
||
placeholder={t('分类的详细描述')}
|
||
autosize={{ minRows: 2, maxRows: 4 }}
|
||
style={{ width: '100%' }}
|
||
/>
|
||
<Form.InputNumber
|
||
field='sort'
|
||
label={t('排序')}
|
||
placeholder={t('数字越小越靠前')}
|
||
style={{ width: '100%' }}
|
||
/>
|
||
<Form.Input field='parent_id' noLabel style={{ display: 'none' }} />
|
||
<div
|
||
style={{ display: 'flex', justifyContent: 'flex-end', gap: 8 }}
|
||
>
|
||
<Button onClick={() => setModalVisible(false)}>
|
||
{t('取消')}
|
||
</Button>
|
||
<Button type='primary' htmlType='submit'>
|
||
{editingCategory?.id === 0 ? t('创建') : t('保存')}
|
||
</Button>
|
||
</div>
|
||
</Form>
|
||
</Modal>
|
||
</div>
|
||
</Spin>
|
||
);
|
||
};
|
||
|
||
export default SettingModelCategory;
|