8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
215 lines
5.1 KiB
React
215 lines
5.1 KiB
React
import React from 'react';
|
||
import { Tag, Button, Space, Popover, Dropdown } from '@/components/ui';
|
||
import { IconMore } from '@/components/ui/icons';
|
||
import {
|
||
renderQuota,
|
||
timestamp2string,
|
||
isAdmin,
|
||
hoverOrClickTrigger,
|
||
} from '../../../helpers';
|
||
import {
|
||
REDEMPTION_STATUS,
|
||
REDEMPTION_STATUS_MAP,
|
||
REDEMPTION_ACTIONS,
|
||
} from '../../../constants/redemption.constants';
|
||
|
||
/**
|
||
* Check if redemption code is expired
|
||
*/
|
||
export const isExpired = (record) => {
|
||
return (
|
||
record.status === REDEMPTION_STATUS.UNUSED &&
|
||
record.expired_time !== 0 &&
|
||
record.expired_time < Math.floor(Date.now() / 1000)
|
||
);
|
||
};
|
||
|
||
/**
|
||
* Render timestamp
|
||
*/
|
||
const renderTimestamp = (timestamp) => {
|
||
return <>{timestamp2string(timestamp)}</>;
|
||
};
|
||
|
||
/**
|
||
* Render redemption code status
|
||
*/
|
||
const renderStatus = (status, record, t) => {
|
||
if (isExpired(record)) {
|
||
return (
|
||
<Tag color='orange' shape='circle'>
|
||
{t('已过期')}
|
||
</Tag>
|
||
);
|
||
}
|
||
|
||
const statusConfig = REDEMPTION_STATUS_MAP[status];
|
||
if (statusConfig) {
|
||
return (
|
||
<Tag color={statusConfig.color} shape='circle'>
|
||
{t(statusConfig.text)}
|
||
</Tag>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<Tag color='black' shape='circle'>
|
||
{t('未知状态')}
|
||
</Tag>
|
||
);
|
||
};
|
||
|
||
/**
|
||
* Get redemption code table column definitions
|
||
*/
|
||
export const getRedemptionsColumns = ({
|
||
t,
|
||
manageRedemption,
|
||
copyText,
|
||
setEditingRedemption,
|
||
setShowEdit,
|
||
refresh,
|
||
redemptions,
|
||
activePage,
|
||
showDeleteRedemptionModal,
|
||
}) => {
|
||
return [
|
||
{
|
||
title: t('ID'),
|
||
dataIndex: 'id',
|
||
},
|
||
{
|
||
title: t('名称'),
|
||
dataIndex: 'name',
|
||
},
|
||
{
|
||
title: t('状态'),
|
||
dataIndex: 'status',
|
||
key: 'status',
|
||
render: (text, record) => {
|
||
return <div>{renderStatus(text, record, t)}</div>;
|
||
},
|
||
},
|
||
{
|
||
title: t('额度'),
|
||
dataIndex: 'quota',
|
||
render: (text) => {
|
||
return (
|
||
<div>
|
||
<Tag color='grey' shape='circle'>
|
||
{renderQuota(parseInt(text))}
|
||
</Tag>
|
||
</div>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
title: t('创建时间'),
|
||
dataIndex: 'created_time',
|
||
render: (text) => {
|
||
return <div>{renderTimestamp(text)}</div>;
|
||
},
|
||
},
|
||
{
|
||
title: t('过期时间'),
|
||
dataIndex: 'expired_time',
|
||
render: (text) => {
|
||
return <div>{text === 0 ? t('永不过期') : renderTimestamp(text)}</div>;
|
||
},
|
||
},
|
||
{
|
||
title: t('兑换人ID'),
|
||
dataIndex: 'used_user_id',
|
||
render: (text) => {
|
||
return <div>{text === 0 ? t('无') : text}</div>;
|
||
},
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
dataIndex: 'operate',
|
||
fixed: 'right',
|
||
width: 205,
|
||
render: (text, record) => {
|
||
// Create dropdown menu items for more operations
|
||
const moreMenuItems = [
|
||
{
|
||
node: 'item',
|
||
name: t('删除'),
|
||
type: 'danger',
|
||
onClick: () => {
|
||
showDeleteRedemptionModal(record);
|
||
},
|
||
},
|
||
];
|
||
|
||
if (record.status === REDEMPTION_STATUS.UNUSED && !isExpired(record)) {
|
||
moreMenuItems.push({
|
||
node: 'item',
|
||
name: t('禁用'),
|
||
type: 'warning',
|
||
onClick: () => {
|
||
manageRedemption(record.id, REDEMPTION_ACTIONS.DISABLE, record);
|
||
},
|
||
});
|
||
} else if (!isExpired(record)) {
|
||
moreMenuItems.push({
|
||
node: 'item',
|
||
name: t('启用'),
|
||
type: 'secondary',
|
||
onClick: () => {
|
||
manageRedemption(record.id, REDEMPTION_ACTIONS.ENABLE, record);
|
||
},
|
||
disabled: record.status === REDEMPTION_STATUS.USED,
|
||
});
|
||
}
|
||
|
||
return (
|
||
<Space>
|
||
<Popover
|
||
// 触屏必须 click:这是查看兑换码明文的唯一入口
|
||
trigger={hoverOrClickTrigger()}
|
||
content={record.key}
|
||
style={{ padding: 20 }}
|
||
position='top'
|
||
>
|
||
<Button type='tertiary' size='small'>
|
||
{t('查看')}
|
||
</Button>
|
||
</Popover>
|
||
<Button
|
||
size='small'
|
||
onClick={async () => {
|
||
await copyText(record.key);
|
||
}}
|
||
>
|
||
{t('复制')}
|
||
</Button>
|
||
{isAdmin() && (
|
||
<>
|
||
<Button
|
||
type='tertiary'
|
||
size='small'
|
||
onClick={() => {
|
||
setEditingRedemption(record);
|
||
setShowEdit(true);
|
||
}}
|
||
disabled={record.status !== REDEMPTION_STATUS.UNUSED}
|
||
>
|
||
{t('编辑')}
|
||
</Button>
|
||
<Dropdown
|
||
trigger='click'
|
||
position='bottomRight'
|
||
menu={moreMenuItems}
|
||
>
|
||
<Button type='tertiary' size='small' icon={<IconMore />} />
|
||
</Dropdown>
|
||
</>
|
||
)}
|
||
</Space>
|
||
);
|
||
},
|
||
},
|
||
];
|
||
};
|