Files
new-api/web/src/components/table/RedemptionsTable.js

590 lines
16 KiB
JavaScript
Raw Normal View History

2024-03-15 16:05:33 +08:00
import React, { useEffect, useState } from 'react';
2024-03-23 21:24:39 +08:00
import {
API,
copy,
showError,
showSuccess,
timestamp2string,
renderQuota
} from '../../helpers';
import { ITEMS_PER_PAGE } from '../../constants';
2024-03-23 21:24:39 +08:00
import {
2025-04-04 12:00:38 +08:00
Button,
2025-05-23 16:58:19 +08:00
Card,
2025-04-04 12:00:38 +08:00
Divider,
2025-05-23 16:58:19 +08:00
Dropdown,
Form,
2024-03-23 21:24:39 +08:00
Modal,
Popover,
2025-05-23 16:58:19 +08:00
Space,
2024-03-23 21:24:39 +08:00
Table,
Tag,
2025-05-23 16:58:19 +08:00
Typography,
2024-03-23 21:24:39 +08:00
} from '@douyinfe/semi-ui';
2025-05-23 16:58:19 +08:00
import {
IconPlus,
IconCopy,
IconSearch,
IconEyeOpened,
IconEdit,
IconDelete,
IconStop,
IconPlay,
IconMore,
} from '@douyinfe/semi-icons';
import EditRedemption from '../../pages/Redemption/EditRedemption';
import { useTranslation } from 'react-i18next';
2025-05-23 16:58:19 +08:00
const { Text } = Typography;
function renderTimestamp(timestamp) {
2024-03-23 21:24:39 +08:00
return <>{timestamp2string(timestamp)}</>;
}
const RedemptionsTable = () => {
const { t } = useTranslation();
const renderStatus = (status) => {
switch (status) {
case 1:
return (
2025-05-23 16:58:19 +08:00
<Tag color='green' size='large' shape='circle'>
{t('未使用')}
</Tag>
);
case 2:
return (
2025-05-23 16:58:19 +08:00
<Tag color='red' size='large' shape='circle'>
{t('已禁用')}
</Tag>
);
case 3:
return (
2025-05-23 16:58:19 +08:00
<Tag color='grey' size='large' shape='circle'>
{t('已使用')}
</Tag>
);
default:
return (
2025-05-23 16:58:19 +08:00
<Tag color='black' size='large' shape='circle'>
{t('未知状态')}
</Tag>
);
}
};
2024-03-15 16:05:33 +08:00
const columns = [
{
title: t('ID'),
2024-03-23 21:24:39 +08:00
dataIndex: 'id',
2024-03-15 16:05:33 +08:00
},
{
title: t('名称'),
2024-03-23 21:24:39 +08:00
dataIndex: 'name',
2024-03-15 16:05:33 +08:00
},
{
title: t('状态'),
2024-03-15 16:05:33 +08:00
dataIndex: 'status',
key: 'status',
render: (text, record, index) => {
2024-03-23 21:24:39 +08:00
return <div>{renderStatus(text)}</div>;
},
2024-03-15 16:05:33 +08:00
},
{
title: t('额度'),
2024-03-15 16:05:33 +08:00
dataIndex: 'quota',
render: (text, record, index) => {
2024-03-23 21:24:39 +08:00
return <div>{renderQuota(parseInt(text))}</div>;
},
2024-03-15 16:05:33 +08:00
},
{
title: t('创建时间'),
2024-03-15 16:05:33 +08:00
dataIndex: 'created_time',
render: (text, record, index) => {
2024-03-23 21:24:39 +08:00
return <div>{renderTimestamp(text)}</div>;
},
2024-03-15 16:05:33 +08:00
},
{
title: t('兑换人ID'),
2024-03-15 16:05:33 +08:00
dataIndex: 'used_user_id',
render: (text, record, index) => {
return <div>{text === 0 ? t('无') : text}</div>;
2024-03-23 21:24:39 +08:00
},
2024-03-15 16:05:33 +08:00
},
{
title: '',
dataIndex: 'operate',
fixed: 'right',
2025-05-23 16:58:19 +08:00
render: (text, record, index) => {
// 创建更多操作的下拉菜单项
const moreMenuItems = [
{
node: 'item',
name: t('删除'),
icon: <IconDelete />,
type: 'danger',
onClick: () => {
Modal.confirm({
title: t('确定是否要删除此兑换码?'),
content: t('此修改将不可逆'),
onOk: () => {
manageRedemption(record.id, 'delete', record).then(() => {
removeRecord(record.key);
});
},
2024-03-23 21:24:39 +08:00
});
2025-05-23 16:58:19 +08:00
},
}
];
// 动态添加启用/禁用按钮
if (record.status === 1) {
moreMenuItems.push({
node: 'item',
name: t('禁用'),
icon: <IconStop />,
type: 'warning',
onClick: () => {
manageRedemption(record.id, 'disable', record);
},
});
} else {
moreMenuItems.push({
node: 'item',
name: t('启用'),
icon: <IconPlay />,
type: 'secondary',
onClick: () => {
manageRedemption(record.id, 'enable', record);
},
disabled: record.status === 3,
});
}
return (
<Space>
<Popover content={record.key} style={{ padding: 20 }} position='top'>
<Button
icon={<IconEyeOpened />}
theme='light'
type='tertiary'
size="small"
className="!rounded-full"
>
{t('查看')}
</Button>
</Popover>
2024-03-23 21:24:39 +08:00
<Button
2025-05-23 16:58:19 +08:00
icon={<IconCopy />}
2024-03-23 21:24:39 +08:00
theme='light'
2025-05-23 16:58:19 +08:00
type='secondary'
size="small"
className="!rounded-full"
2024-03-23 21:24:39 +08:00
onClick={async () => {
2025-05-23 16:58:19 +08:00
await copyText(record.key);
2024-03-23 21:24:39 +08:00
}}
>
2025-05-23 16:58:19 +08:00
{t('复制')}
2024-03-23 21:24:39 +08:00
</Button>
<Button
2025-05-23 16:58:19 +08:00
icon={<IconEdit />}
2024-03-23 21:24:39 +08:00
theme='light'
2025-05-23 16:58:19 +08:00
type='tertiary'
size="small"
className="!rounded-full"
onClick={() => {
setEditingRedemption(record);
setShowEdit(true);
2024-03-23 21:24:39 +08:00
}}
2025-05-23 16:58:19 +08:00
disabled={record.status !== 1}
2024-03-23 21:24:39 +08:00
>
2025-05-23 16:58:19 +08:00
{t('编辑')}
2024-03-23 21:24:39 +08:00
</Button>
2025-05-23 16:58:19 +08:00
<Dropdown
trigger='click'
position='bottomRight'
menu={moreMenuItems}
>
<Button
icon={<IconMore />}
theme='light'
type='tertiary'
size="small"
className="!rounded-full"
/>
</Dropdown>
</Space>
);
},
2024-03-23 21:24:39 +08:00
},
2024-03-15 16:05:33 +08:00
];
2023-11-09 17:08:32 +08:00
2024-03-15 16:05:33 +08:00
const [redemptions, setRedemptions] = useState([]);
const [loading, setLoading] = useState(true);
const [activePage, setActivePage] = useState(1);
const [searching, setSearching] = useState(false);
const [tokenCount, setTokenCount] = useState(ITEMS_PER_PAGE);
const [selectedKeys, setSelectedKeys] = useState([]);
const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
2024-03-15 16:05:33 +08:00
const [editingRedemption, setEditingRedemption] = useState({
2024-03-23 21:24:39 +08:00
id: undefined,
2024-03-15 16:05:33 +08:00
});
const [showEdit, setShowEdit] = useState(false);
// Form 初始值
const formInitValues = {
searchKeyword: '',
};
// Form API 引用
const [formApi, setFormApi] = useState(null);
// 获取表单值的辅助函数
const getFormValues = () => {
const formValues = formApi ? formApi.getValues() : {};
return {
searchKeyword: formValues.searchKeyword || '',
};
};
2024-03-15 16:05:33 +08:00
const closeEdit = () => {
setShowEdit(false);
2025-05-23 16:58:19 +08:00
setTimeout(() => {
setEditingRedemption({
id: undefined,
});
}, 500);
2024-03-15 16:05:33 +08:00
};
2024-03-15 16:05:33 +08:00
const setRedemptionFormat = (redeptions) => {
setRedemptions(redeptions);
};
2023-11-10 00:46:07 +08:00
const loadRedemptions = async (startIdx, pageSize) => {
2025-04-04 12:00:38 +08:00
const res = await API.get(
`/api/redemption/?p=${startIdx}&page_size=${pageSize}`,
);
2024-03-15 16:05:33 +08:00
const { success, message, data } = res.data;
if (success) {
2025-04-04 12:00:38 +08:00
const newPageData = data.items;
setActivePage(data.page);
setTokenCount(data.total);
setRedemptionFormat(newPageData);
2024-03-15 16:05:33 +08:00
} else {
2025-04-04 12:00:38 +08:00
showError(message);
2024-03-15 16:05:33 +08:00
}
setLoading(false);
};
2023-11-09 17:08:32 +08:00
2024-03-23 21:24:39 +08:00
const removeRecord = (key) => {
2024-03-15 16:05:33 +08:00
let newDataSource = [...redemptions];
if (key != null) {
2024-03-23 21:24:39 +08:00
let idx = newDataSource.findIndex((data) => data.key === key);
2023-11-09 17:08:32 +08:00
2024-03-15 16:05:33 +08:00
if (idx > -1) {
newDataSource.splice(idx, 1);
setRedemptions(newDataSource);
}
}
};
2023-11-10 00:10:41 +08:00
2024-03-15 16:05:33 +08:00
const copyText = async (text) => {
if (await copy(text)) {
showSuccess(t('已复制到剪贴板!'));
2024-03-15 16:05:33 +08:00
} else {
2025-05-23 16:58:19 +08:00
Modal.error({
title: t('无法复制到剪贴板,请手动复制'),
content: text,
size: 'large'
});
2023-11-09 17:08:32 +08:00
}
2024-03-15 16:05:33 +08:00
};
2023-11-09 17:08:32 +08:00
2024-03-15 16:05:33 +08:00
const onPaginationChange = (e, { activePage }) => {
(async () => {
if (activePage === Math.ceil(redemptions.length / pageSize) + 1) {
await loadRedemptions(activePage - 1, pageSize);
2024-03-15 16:05:33 +08:00
}
setActivePage(activePage);
})();
};
2024-03-15 16:05:33 +08:00
useEffect(() => {
loadRedemptions(0, pageSize)
2024-03-15 16:05:33 +08:00
.then()
.catch((reason) => {
showError(reason);
});
2025-05-23 16:58:19 +08:00
}, [pageSize]);
2024-03-15 16:05:33 +08:00
const refresh = async () => {
await loadRedemptions(activePage - 1, pageSize);
2024-03-15 16:05:33 +08:00
};
2023-11-09 17:08:32 +08:00
2024-03-15 16:05:33 +08:00
const manageRedemption = async (id, action, record) => {
2025-05-23 16:58:19 +08:00
setLoading(true);
2024-03-15 16:05:33 +08:00
let data = { id };
let res;
switch (action) {
case 'delete':
res = await API.delete(`/api/redemption/${id}/`);
break;
case 'enable':
data.status = 1;
res = await API.put('/api/redemption/?status_only=true', data);
break;
case 'disable':
data.status = 2;
res = await API.put('/api/redemption/?status_only=true', data);
break;
}
const { success, message } = res.data;
if (success) {
showSuccess(t('操作成功完成!'));
2024-03-15 16:05:33 +08:00
let redemption = res.data.data;
let newRedemptions = [...redemptions];
if (action === 'delete') {
} else {
record.status = redemption.status;
}
setRedemptions(newRedemptions);
} else {
showError(message);
}
2025-05-23 16:58:19 +08:00
setLoading(false);
2024-03-15 16:05:33 +08:00
};
const searchRedemptions = async (keyword = null, page, pageSize) => {
// 如果没有传递keyword参数从表单获取值
if (keyword === null) {
const formValues = getFormValues();
keyword = formValues.searchKeyword;
}
if (keyword === '') {
2025-04-04 12:00:38 +08:00
await loadRedemptions(page, pageSize);
return;
2024-03-15 16:05:33 +08:00
}
setSearching(true);
2025-04-04 12:00:38 +08:00
const res = await API.get(
`/api/redemption/search?keyword=${keyword}&p=${page}&page_size=${pageSize}`,
);
2024-03-15 16:05:33 +08:00
const { success, message, data } = res.data;
if (success) {
2025-04-04 12:00:38 +08:00
const newPageData = data.items;
setActivePage(data.page);
setTokenCount(data.total);
setRedemptionFormat(newPageData);
2024-03-15 16:05:33 +08:00
} else {
2025-04-04 12:00:38 +08:00
showError(message);
2024-03-15 16:05:33 +08:00
}
setSearching(false);
};
2024-03-15 16:05:33 +08:00
const sortRedemption = (key) => {
if (redemptions.length === 0) return;
setLoading(true);
let sortedRedemptions = [...redemptions];
sortedRedemptions.sort((a, b) => {
return ('' + a[key]).localeCompare(b[key]);
});
if (sortedRedemptions[0].id === redemptions[0].id) {
sortedRedemptions.reverse();
}
setRedemptions(sortedRedemptions);
setLoading(false);
};
2023-11-10 00:10:41 +08:00
2024-03-23 21:24:39 +08:00
const handlePageChange = (page) => {
2024-03-15 16:05:33 +08:00
setActivePage(page);
const { searchKeyword } = getFormValues();
if (searchKeyword === '') {
loadRedemptions(page, pageSize).then();
} else {
searchRedemptions(searchKeyword, page, pageSize).then();
2024-03-15 16:05:33 +08:00
}
};
2023-11-09 17:08:32 +08:00
let pageData = redemptions;
2024-03-15 16:05:33 +08:00
const rowSelection = {
2025-05-23 16:58:19 +08:00
onSelect: (record, selected) => { },
onSelectAll: (selected, selectedRows) => { },
2024-03-15 16:05:33 +08:00
onChange: (selectedRowKeys, selectedRows) => {
setSelectedKeys(selectedRows);
2024-03-23 21:24:39 +08:00
},
2024-03-15 16:05:33 +08:00
};
2023-11-09 17:08:32 +08:00
2024-03-15 16:05:33 +08:00
const handleRow = (record, index) => {
if (record.status !== 1) {
return {
style: {
2024-03-23 21:24:39 +08:00
background: 'var(--semi-color-disabled-border)',
},
2024-03-15 16:05:33 +08:00
};
} else {
return {};
}
};
2023-12-01 01:30:24 +08:00
2025-05-23 16:58:19 +08:00
const renderHeader = () => (
<div className="flex flex-col w-full">
<div className="mb-2">
<div className="flex items-center text-orange-500">
<IconEyeOpened className="mr-2" />
<Text>{t('兑换码可以批量生成和分发,适合用于推广活动或批量充值。')}</Text>
</div>
</div>
<Divider margin="12px" />
<div className="flex flex-col md:flex-row justify-between items-center gap-4 w-full">
<div className="flex gap-2 w-full md:w-auto order-2 md:order-1">
<Button
theme='light'
type='primary'
icon={<IconPlus />}
className="!rounded-full w-full md:w-auto"
onClick={() => {
setEditingRedemption({
id: undefined,
});
setShowEdit(true);
}}
>
{t('添加兑换码')}
</Button>
<Button
type='warning'
icon={<IconCopy />}
className="!rounded-full w-full md:w-auto"
onClick={async () => {
if (selectedKeys.length === 0) {
showError(t('请至少选择一个兑换码!'));
return;
}
let keys = '';
for (let i = 0; i < selectedKeys.length; i++) {
keys +=
selectedKeys[i].name + ' ' + selectedKeys[i].key + '\n';
}
await copyText(keys);
}}
>
{t('复制所选兑换码到剪贴板')}
</Button>
</div>
<Form
initValues={formInitValues}
getFormApi={(api) => setFormApi(api)}
onSubmit={() => {
setActivePage(1);
searchRedemptions(null, 1, pageSize);
}}
allowEmpty={true}
autoComplete="off"
layout="horizontal"
trigger="change"
stopValidateWithError={false}
className="w-full md:w-auto order-1 md:order-2"
>
<div className="flex flex-col md:flex-row items-center gap-4 w-full md:w-auto">
<div className="relative w-full md:w-64">
<Form.Input
field="searchKeyword"
prefix={<IconSearch />}
placeholder={t('关键字(id或者名称)')}
className="!rounded-full"
showClear
pure
/>
</div>
<div className="flex gap-2 w-full md:w-auto">
<Button
type="primary"
htmlType="submit"
loading={loading || searching}
className="!rounded-full flex-1 md:flex-initial md:w-auto"
>
{t('查询')}
</Button>
<Button
theme="light"
onClick={() => {
if (formApi) {
formApi.reset();
// 重置后立即查询使用setTimeout确保表单重置完成
setTimeout(() => {
setActivePage(1);
loadRedemptions(1, pageSize);
}, 100);
}
}}
className="!rounded-full flex-1 md:flex-initial md:w-auto"
>
{t('重置')}
</Button>
</div>
2025-05-23 16:58:19 +08:00
</div>
</Form>
2025-05-23 16:58:19 +08:00
</div>
</div>
);
2024-03-15 16:05:33 +08:00
return (
<>
2024-03-23 21:24:39 +08:00
<EditRedemption
refresh={refresh}
editingRedemption={editingRedemption}
visiable={showEdit}
handleClose={closeEdit}
></EditRedemption>
2025-05-23 16:58:19 +08:00
<Card
className="!rounded-2xl"
2025-05-23 16:58:19 +08:00
title={renderHeader()}
shadows='always'
bordered={false}
2025-04-04 12:00:38 +08:00
>
<Table
columns={columns}
dataSource={pageData}
scroll={{ x: 'max-content' }}
pagination={{
currentPage: activePage,
pageSize: pageSize,
total: tokenCount,
showSizeChanger: true,
pageSizeOptions: [10, 20, 50, 100],
formatPageText: (page) =>
t('第 {{start}} - {{end}} 条,共 {{total}} 条', {
start: page.currentStart,
end: page.currentEnd,
total: tokenCount,
}),
onPageSizeChange: (size) => {
setPageSize(size);
setActivePage(1);
const { searchKeyword } = getFormValues();
if (searchKeyword === '') {
loadRedemptions(1, size).then();
} else {
searchRedemptions(searchKeyword, 1, size).then();
}
},
onPageChange: handlePageChange,
}}
loading={loading}
rowSelection={rowSelection}
onRow={handleRow}
className="rounded-xl overflow-hidden"
size="middle"
></Table>
2025-05-23 16:58:19 +08:00
</Card>
2024-03-15 16:05:33 +08:00
</>
);
};
export default RedemptionsTable;