You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
8.3 KiB
TypeScript

11 months ago
/* eslint-disable no-param-reassign */
11 months ago
import { CaretDownOutlined, CaretUpOutlined, PlusOutlined } from '@ant-design/icons';
11 months ago
import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components';
import { PageContainer } from '@ant-design/pro-layout';
11 months ago
import { Button, message, Popconfirm, Switch, Image, Space } from 'antd';
11 months ago
import React, { useRef, useState } from 'react';
import { Access, useAccess } from 'umi';
11 months ago
import { addOrUpdateBannerUsingPost, changeBannerStateUsingGet, getBannerPageUsingPost, moveBannerUsingGet, removeBannerUsingGet } from '@/services/pop-b2b2c/pbcBannerController';
import UpdateBannerForm from './components/UpdateBannerForm';
import Constants from '@/constants';
11 months ago
/**
*
* @param param0
*/
11 months ago
const fetchData = async (params: API.PbcBanner_) => {
const msg = await getBannerPageUsingPost(params);
11 months ago
return {
data: msg.data?.records,
total: msg.data?.total,
success: msg.retcode,
} as any;
};
/**
*
* @param fields
*/
11 months ago
const handleUpdate = async (fields: API.PbcBanner_) => {
11 months ago
const hide = message.loading('正在保存');
try {
11 months ago
const msg = await addOrUpdateBannerUsingPost(fields);
11 months ago
hide();
if (msg.retcode) {
message.success(!fields.pbcId ? '添加成功' : '保存成功');
return true;
}
message.error(msg.retmsg);
return false;
} catch (error) {
hide();
message.error(!fields.pbcId ? '添加失败' : '保存失败');
return false;
}
};
const handleUpdateState = async (id: number, state: number) => {
const hide = message.loading('正在保存');
if (!id) return false;
try {
11 months ago
const msg = await changeBannerStateUsingGet({ pbcId: id, pbcState: state });
11 months ago
hide();
if (msg.retcode) {
message.success(!id ? '新增成功!' : '保存成功!');
return true;
}
message.error(msg.retmsg);
return false;
} catch (error) {
hide();
message.error(!id ? '新增失败,请重试!' : '保存失败,请重试!');
return false;
}
};
11 months ago
/**
*
* @param id
*/
const handleMoveBanner = async (id: number, type: string) => {
const hide = message.loading('正在保存');
if (!id) return false;
try {
const msg = await moveBannerUsingGet({
type,
pbcId: id,
});
hide();
if (msg.retcode) {
message.success('保存成功,即将刷新');
} else {
message.error(msg.retmsg ?? '保存失败,请重试');
}
return true;
} catch (error) {
hide();
message.error('保存失败,请重试');
return false;
}
};
11 months ago
/**
*
* @param id
*/
const handleRemove = async (id?: number) => {
const hide = message.loading('正在删除');
if (!id) return false;
try {
11 months ago
const msg = await removeBannerUsingGet({
11 months ago
pbcId: id,
});
hide();
if (msg.retcode) {
message.success('删除成功,即将刷新');
} else {
message.error(msg.retmsg ?? '删除失败,请重试');
}
return true;
} catch (error) {
hide();
message.error('删除失败,请重试');
return false;
}
};
// eslint-disable-next-line @typescript-eslint/ban-types
const TableList: React.FC<{}> = () => {
const access: any = useAccess();
const actionRef = useRef<ActionType>();
const [stepFormValues, setStepFormValues] = useState({});
const [updateModalVisible, handleUpdateModalVisible] = useState<boolean>(false);
11 months ago
const columns: ProColumns<API.PbcBanner_>[] = [
11 months ago
{
title: '标题',
dataIndex: 'pbcTitle',
},
{
title: '预览',
11 months ago
dataIndex: 'pbcBannerImage',
search: false,
width: 220,
render: (text) => {
const str = text?.toString()
return str && <Image style={{ objectFit: 'contain' }} src={str} width={200} height={200} />
}
},
{
title: '类型',
dataIndex: 'pbcBannerType',
valueEnum: Constants.pbcBannerType,
search: false
},
{
title: '链接',
dataIndex: 'pbcLink',
search: false
},
{
title: '排序',
dataIndex: 'pbcSort',
search: false,
width: 70,
render: (text: any, record) => <Space>
<span>{text + 1}</span>
<Space direction='vertical'>
<Button onClick={async () => {
const success = await handleMoveBanner(record.pbcId || 0, 'up');
if (success) {
actionRef.current?.reload();
}
}} icon={<CaretUpOutlined />} />
<Button onClick={async () => {
const success = await handleMoveBanner(record.pbcId || 0, 'down');
if (success) {
actionRef.current?.reload();
}
}} icon={<CaretDownOutlined />} />
</Space>
</Space>
11 months ago
},
{
title: '创建时间',
dataIndex: 'pbcCreateAt',
valueType: 'dateTime',
search: false,
},
{
title: '操作',
fixed: 'right',
valueType: 'option',
render: (text, record) => (
<span>
11 months ago
<Access key="config" accessible={access.adBannerUpdate}>
11 months ago
<Switch
11 months ago
checked={record.pbcState === 1}
11 months ago
onChange={async (value) => {
11 months ago
const success = await handleUpdateState(record.pbcId || 0, value ? 1 : 2);
11 months ago
if (success) {
if (actionRef.current) {
actionRef.current.reload();
}
}
}}
/>
</Access>
11 months ago
<Access key="config" accessible={access.adBannerAdd}>
11 months ago
<Button
type="link"
onClick={() => {
handleUpdateModalVisible(true);
setStepFormValues(record);
}}
>
</Button>
</Access>
11 months ago
<Access key="remove" accessible={access.adBannerDelete}>
11 months ago
<Popconfirm
11 months ago
title="确定删除该banner?"
11 months ago
onConfirm={async () => {
const success = await handleRemove(record.pbcId);
if (success) {
actionRef.current?.reload();
}
}}
>
<Button type="link" danger>
</Button>
</Popconfirm>
</Access>
</span>
),
},
];
return (
<PageContainer
header={{
title: '',
breadcrumb: {},
}}
>
11 months ago
<ProTable<API.PbcBanner_>
11 months ago
columns={columns}
actionRef={actionRef}
request={fetchData}
rowKey="pbcId"
size="small"
bordered
search={{
labelWidth: 'auto',
span: 6,
optionRender: ({ searchText }, { form }) => {
return [
<Button
key="button"
type="primary"
onClick={() => {
form?.submit();
}}
>
{searchText}
</Button>,
11 months ago
<Access key="add" accessible={access.adBannerAdd}>
11 months ago
<Button
icon={<PlusOutlined />}
type="primary"
onClick={() => {
handleUpdateModalVisible(true);
setStepFormValues({ id: undefined });
}}
>
</Button>
</Access>,
];
},
}}
pagination={{
defaultPageSize: 20,
showSizeChanger: true,
}}
scroll={{
y: 'calc(100vh - 320px)',
}}
dateFormatter="string"
options={false}
toolBarRender={() => []}
/>
{stepFormValues && Object.keys(stepFormValues).length ? (
11 months ago
<UpdateBannerForm
11 months ago
onSubmit={async (value: any) => {
const success = await handleUpdate(value);
if (success) {
handleUpdateModalVisible(false);
setStepFormValues({});
actionRef.current?.reload();
}
}}
onCancel={() => {
message.destroy();
handleUpdateModalVisible(false);
}}
afterClose={() => {
setStepFormValues({});
}}
updateModalVisible={updateModalVisible}
values={stepFormValues}
/>
) : null}
</PageContainer>
);
};
export default TableList;