Files
2026-08-09 11:23:54 +08:00

222 lines
6.5 KiB
TypeScript

import XinTable from '@/components/XinTable';
import { Badge, Image, Tag, Typography } from 'antd';
import { useEffect, useState } from 'react';
import type { IActivity } from '@/domain/iActivity';
import type { ICategory } from '@/domain/iCategory';
import type { XinTableColumn } from '@/components/XinTable/typings';
import type { ISysFileInfo } from '@/domain/iSysFile';
import { List } from '@/api/common/table';
import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
const { Title, Text } = Typography;
/**
* 从 image 字段中提取预览 URL
* image 字段可能是 ISysFileInfo 对象或 URL 字符串
*/
function getImageUrl(image: ISysFileInfo | string | null | undefined): string {
if (!image) return '';
if (typeof image === 'string') return image;
return image.preview_url || image.file_url || '';
}
/** 首页活动专区管理 */
export default function ActivityPage() {
const { t } = useTranslation();
// 分类下拉选项
const [categoryOptions, setCategoryOptions] = useState<{ label: string; value: number }[]>([]);
useEffect(() => {
List<ICategory>('/system/category', { pageSize: 1000 }).then((res) => {
const list = res.data?.data?.data ?? [];
setCategoryOptions(list.map((item) => ({
label: item.name,
value: item.id as number,
})));
}).catch(() => {
// 分类加载失败不影响页面使用
});
}, []);
const columns: XinTableColumn<IActivity>[] = [
{
title: t('system.activity.id'),
dataIndex: 'id',
hideInForm: true,
width: 80,
sorter: true,
align: 'center',
},
{
title: t('system.activity.title'),
dataIndex: 'title',
valueType: 'text',
colProps: { span: 12 },
rules: [{ required: true, message: t('system.activity.title.required') }],
},
{
title: t('system.activity.image'),
dataIndex: 'image_id',
valueType: 'image',
colProps: { span: 24 },
rules: [{ required: true, message: t('system.activity.image.required') }],
hideInSearch: true,
fieldProps: {
action: '/system/file/list/upload',
mode: 'single',
maxCount: 1,
changeType: "id"
},
render: (_value: ISysFileInfo | string, record: IActivity) => {
const url = getImageUrl(record.image);
if (!url) return '-';
return (
<Image
src={url}
width={120}
height={60}
style={{ objectFit: 'cover', borderRadius: 4 }}
/>
);
},
},
{
title: t('system.activity.description'),
dataIndex: 'description',
valueType: 'textarea',
colProps: { span: 24 },
hideInSearch: true,
fieldProps: {
rows: 3,
placeholder: t('system.activity.description.placeholder'),
},
render: (value: string) => value || '-',
},
{
title: t('system.activity.linkType'),
dataIndex: 'link_type',
valueType: 'select',
colProps: { span: 12 },
initialValue: 0,
rules: [{ required: true, message: t('system.activity.linkType.required') }],
fieldProps: {
options: [
{ label: t('system.activity.linkType.link'), value: 0 },
{ label: t('system.activity.linkType.category'), value: 1 },
],
},
render: (value: number) => {
return value === 1
? <Tag color="blue">{t('system.activity.linkType.category')}</Tag>
: <Tag>{t('system.activity.linkType.link')}</Tag>;
},
},
{
title: t('system.activity.link'),
dataIndex: 'link',
valueType: 'text',
colProps: { span: 12 },
hideInSearch: true,
fieldProps: {
placeholder: t('system.activity.link.placeholder'),
},
render: (_, record) => {
if(record.link_type === 1) {
const name = record.category?.name;
return name ? <Tag>{name}</Tag> : '-';
}
return record.link || '-'
},
dependency: {
dependencies: ['link_type'],
visible: (values) => values.link_type !== 1,
},
},
{
title: t('system.activity.category'),
dataIndex: 'category_id',
valueType: 'select',
colProps: { span: 12 },
hideInSearch: true,
hideInTable: true,
fieldProps: {
options: categoryOptions,
showSearch: true,
optionFilterProp: 'label',
placeholder: t('system.activity.category.placeholder'),
},
dependency: {
dependencies: ['link_type'],
visible: (values) => values.link_type === 1,
},
},
{
title: t('system.activity.status'),
dataIndex: 'status',
valueType: 'select',
filters: [
{ text: t('system.activity.status.normal'), value: 0 },
{ text: t('system.activity.status.disabled'), value: 1 },
],
colProps: { span: 12 },
rules: [{ required: true, message: t('system.activity.status.required') }],
fieldProps: {
options: [
{ label: t('system.activity.status.normal'), value: 0 },
{ label: t('system.activity.status.disabled'), value: 1 },
],
},
render: (value: number) => {
return value === 0
? <Badge status="success" text={t('system.activity.status.normal')} />
: <Badge status="error" text={t('system.activity.status.disabled')} />;
},
},
{
title: t('system.activity.sort'),
dataIndex: 'sort',
valueType: 'digit',
colProps: { span: 12 },
hideInSearch: true,
initialValue: 0,
fieldProps: {
min: 0,
style: { width: '100%' },
},
},
{
title: t('system.activity.createdAt'),
dataIndex: 'created_at',
render: (value: string) => (value ? dayjs(value).format('YYYY-MM-DD HH:mm') : '-'),
hideInForm: true,
hideInSearch: true,
width: 160,
},
];
return (
<>
<div className="mb-5">
<Title level={3}>{t('system.activity.page.title')}</Title>
<Text type="secondary">{t('system.activity.page.description')}</Text>
</div>
<XinTable<IActivity>
api="/system/activity"
columns={columns}
rowKey="id"
accessName="system.activity"
formProps={{
grid: true,
colProps: { span: 12 },
rowProps: { gutter: [30, 0] },
layout: 'vertical',
}}
modalProps={{ width: 800 }}
searchProps={false}
scroll={{ x: 1000 }}
/>
</>
);
}