#
luxiaotao1123
2024-02-21 dca78d5fd0c67fd841d6841ef2cd94a10669c356
zy-asrs-flow/src/pages/system/role/index.jsx
@@ -1,67 +1,99 @@
import React, { useState, useRef, useEffect } from 'react';
import { useIntl, FormattedMessage } from '@umijs/max';
import { Button, message, Modal } from 'antd';
import { ActionType, FooterToolbar, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components';
import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined, UnlockOutlined } from '@ant-design/icons';
import { getLogininforList, removeLogininfor, exportLogininfor} from '@/services/monitor/logininfor';
import Http from '@/utils/http'
import {
    FooterToolbar,
    PageContainer,
    ProTable,
    LightFilter,
} from '@ant-design/pro-components';
import { PlusOutlined, ExportOutlined } from '@ant-design/icons';
import Http from '@/utils/http';
import Edit from './components/edit'
import { TextFilter, SelectFilter, DatetimeRangeFilter, LinkFilter } from '@/components/TableSearch'
/**
 * 删除节点
 *
 * @param selectedRows
 */
const handleRemove = async (selectedRows) => {
    if (!selectedRows) return true;
    const hide = message.loading('正在删除');
const handleSave = async (val) => {
    const hide = message.loading('正在添加');
    try {
        const resp = await removeLogininfor(selectedRows.map((row) => row.infoId).join(','));
        hide();
        const resp = await Http.doPost('api/role/save', val);
        if (resp.code === 200) {
            message.success('删除成功,即将刷新');
            message.success('添加成功');
            return true;
        } else {
            message.error(resp.msg);
            return false;
        }
        return true;
    } catch (error) {
        hide();
        message.error('删除失败,请重试');
        message.error('添加失败请重试!');
        return false;
    } finally {
        hide();
    }
};
/**
 * 导出数据
 *
 * @param id
 */
const handleUpdate = async (val) => {
    const hide = message.loading('正在更新');
    try {
        const resp = await Http.doPost('api/role/update', val);
        if (resp.code === 200) {
            message.success('更新成功');
            return true;
        } else {
            message.error(resp.msg);
            return false;
        }
    } catch (error) {
        message.error('配置失败请重试!');
        return false;
    } finally {
        hide();
    }
};
const handleRemove = async (rows) => {
    if (!rows) return true;
    const hide = message.loading('正在删除');
    try {
        const resp = await Http.doPost('api/role/remove/' + rows.map((row) => row.id).join(','));
        if (resp.code === 200) {
            message.success('删除成功');
            return true;
        } else {
            message.error(resp.msg);
            return false;
        }
    } catch (error) {
        message.error('删除失败,请重试');
        return false;
    } finally {
        hide();
    }
};
const handleExport = async () => {
    const hide = message.loading('正在导出');
    try {
        await exportLogininfor();
        hide();
        const resp = await Http.doPostBlob('api/role/export');
        const blob = new Blob([resp], { type: 'application/vnd.ms-excel' });
        window.location.href = window.URL.createObjectURL(blob);
        message.success('导出成功');
        return true;
    } catch (error) {
        hide();
        message.error('导出失败,请重试');
        return false;
    } finally {
        hide();
    }
};
const LogininforTableList = () => {
const Main = () => {
    const formTableRef = useRef();
    const actionRef = useRef();
    const [selectedRows, setSelectedRows] = useState([]);
    const [statusOptions, setStatusOptions] = useState([]);
    /** 国际化配置 */
    const intl = useIntl();
    const [modalVisible, setModalVisible] = useState(false);
    const [currentRow, setCurrentRow] = useState();
    const [searchParam, setSearchParam] = useState({});
    useEffect(() => {
@@ -69,123 +101,168 @@
    const columns = [
        {
            title: 'No',
            dataIndex: 'index',
            valueType: 'indexBorder',
            width: 48,
        },
        {
            title: '姓名',
            dataIndex: 'name',
            valueType: 'text',
            copyable: true,
            filterDropdown: (props) => <TextFilter
                name='name'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '标识',
            dataIndex: 'code',
            valueType: 'text',
            filterDropdown: (props) => <TextFilter
                name='code'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.info_id" defaultMessage="访问编号" />,
        //     dataIndex: 'infoId',
        //     valueType: 'text',
        //     hideInSearch: true,
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.user_name" defaultMessage="用户账号" />,
        //     dataIndex: 'userName',
        //     valueType: 'text',
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.ipaddr" defaultMessage="登录IP地址" />,
        //     dataIndex: 'ipaddr',
        //     valueType: 'text',
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.login_location" defaultMessage="登录地点" />,
        //     dataIndex: 'loginLocation',
        //     valueType: 'text',
        //     hideInSearch: true,
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.browser" defaultMessage="浏览器类型" />,
        //     dataIndex: 'browser',
        //     valueType: 'text',
        //     hideInSearch: true,
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.os" defaultMessage="操作系统" />,
        //     dataIndex: 'os',
        //     valueType: 'text',
        //     hideInSearch: true,
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.status" defaultMessage="登录状态" />,
        //     dataIndex: 'status',
        //     valueType: 'select',
        //     valueEnum: statusOptions,
        //     // render: (_, record) => {
        //     //     return (<DictTag enums={statusOptions} value={record.status} />);
        //     // },
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.msg" defaultMessage="提示消息" />,
        //     dataIndex: 'msg',
        //     valueType: 'text',
        //     hideInSearch: true,
        // },
        // {
        //     title: <FormattedMessage id="monitor.logininfor.login_time" defaultMessage="访问时间" />,
        //     dataIndex: 'loginTime',
        //     valueType: 'dateTime',
        // },
        {
            title: '机构',
            dataIndex: 'hostId$',
            valueType: 'text',
            filterDropdown: (props) => <LinkFilter
                name='hostId'
                major='host'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '状态',
            dataIndex: 'status$',
            valueType: 'text',
            filterDropdown: (props) => <SelectFilter
                name='status'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
                data={[
                    { label: '正常', value: 1 },
                    { label: '禁用', value: 0 },
                ]}
            />,
        },
        {
            title: '修改时间',
            dataIndex: 'updateTime$',
            valueType: 'text',
            filterDropdown: (props) => <DatetimeRangeFilter
                name='updateTime'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 140,
            valueType: 'option',
            render: (_, record) => [
                <Button
                    type="link"
                    key="edit"
                    onClick={() => {
                        setModalVisible(true);
                        setCurrentRow(record);
                    }}
                >
                    编辑
                </Button>,
                <Button
                    type="link"
                    danger
                    key="batchRemove"
                    onClick={async () => {
                        Modal.confirm({
                            title: '删除',
                            content: '确定删除该项吗?',
                            onOk: async () => {
                                const success = await handleRemove([record]);
                                if (success) {
                                    if (actionRef.current) {
                                        actionRef.current.reload();
                                    }
                                }
                            },
                        });
                    }}
                >
                    删除
                </Button>,
            ],
        },
    ];
    return (
        <PageContainer>
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    headerTitle={intl.formatMessage({
                        id: 'pages.searchTable.title',
                        defaultMessage: '信息',
                    })}
                    key="role"
                    rowKey="id"
                    actionRef={actionRef}
                    formRef={formTableRef}
                    rowKey="infoId"
                    key="logininforList"
                    search={{
                        labelWidth: 120,
                    columns={columns}
                    cardBordered
                    dateFormatter="string"
                    pagination={{ pageSize: 20 }}
                    search={false}
                    toolbar={{
                        multipleLine: true,
                        search: {
                            onSearch: (value) => {
                                setSearchParam(prevState => ({
                                    ...prevState,
                                    condition: value
                                }));
                                actionRef.current?.reload();
                            },
                        },
                        filter: (
                            <LightFilter
                                onValuesChange={(val) => {
                                }}
                            >
                            </LightFilter>
                        ),
                        actions: [
                            <Button
                                type="primary"
                                key="save"
                                onClick={async () => {
                                    setModalVisible(true)
                                }}
                            >
                                <PlusOutlined />
                                添加
                            </Button>,
                            <Button
                                key="export"
                                onClick={async () => {
                                    handleExport();
                                }}
                            >
                                <ExportOutlined />
                                导出
                            </Button>,
                        ],
                    }}
                    toolBarRender={() => [
                        <Button
                            key="remove"
                            danger
                            hidden={selectedRows?.length === 0}
                            onClick={async () => {
                                Modal.confirm({
                                    title: '是否确认删除所选数据项?',
                                    icon: <ExclamationCircleOutlined />,
                                    content: '请谨慎操作',
                                    async onOk() {
                                        const success = await handleRemove(selectedRows);
                                        if (success) {
                                            setSelectedRows([]);
                                            actionRef.current?.reloadAndRest?.();
                                        }
                                    },
                                    onCancel() { },
                                });
                            }}
                        >
                            <DeleteOutlined />
                            <FormattedMessage id="pages.searchTable.delete" defaultMessage="删除" />
                        </Button>,
                        <Button
                            type="primary"
                            key="export"
                            onClick={async () => {
                                handleExport();
                            }}
                        >
                            <PlusOutlined />
                            <FormattedMessage id="pages.searchTable.export" defaultMessage="导出" />
                        </Button>,
                    ]}
                    request={(params) =>
                        Http.doPost('/api/role/page', params, (res) => {
                    request={(params, sorter, filter) =>
                        Http.doPostPromise('/api/role/page', { ...params, ...searchParam }, (res) => {
                            return {
                                data: res.data.records,
                                total: res.data.total,
@@ -193,32 +270,40 @@
                            }
                        })
                    }
                    columns={columns}
                    rowSelection={{
                        onChange: (_, selectedRows) => {
                            setSelectedRows(selectedRows);
                        onChange: (ids, rows) => {
                            setSelectedRows(rows);
                        }
                    }}
                    columnsState={{
                        persistenceKey: 'pro-table-role',
                        persistenceType: 'localStorage',
                        defaultValue: {
                            option: { fixed: 'right', disable: true },
                        },
                        onChange(value) {
                        },
                    }}
                />
            </div>
            {selectedRows?.length > 0 && (
                <FooterToolbar
                    extra={
                        <div>
                            <FormattedMessage id="pages.searchTable.chosen" defaultMessage="已选择" />
                            已选择
                            <a style={{ fontWeight: 600 }}>{selectedRows.length}</a>
                            <FormattedMessage id="pages.searchTable.item" defaultMessage="项" />
                            项
                        </div>
                    }
                >
                    <Button
                        key="remove"
                        danger
                        onClick={async () => {
                            Modal.confirm({
                                title: '删除',
                                content: '确定删除该项吗?',
                                okText: '确认',
                                cancelText: '取消',
                                onOk: async () => {
                                    const success = await handleRemove(selectedRows);
                                    if (success) {
@@ -229,12 +314,39 @@
                            });
                        }}
                    >
                        <FormattedMessage id="pages.searchTable.batchDeletion" defaultMessage="批量删除" />
                        批量删除
                    </Button>
                </FooterToolbar>
            )}
            <Edit
                open={modalVisible}
                values={currentRow || {}}
                onCancel={
                    () => {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                    }
                }
                onSubmit={async (values) => {
                    let ok = false;
                    if (values.id) {
                        ok = await handleUpdate({ ...values })
                    } else {
                        ok = await handleSave({ ...values })
                    }
                    if (ok) {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                        if (actionRef.current) {
                            actionRef.current.reload();
                        }
                    }
                }
                }
            />
        </PageContainer>
    );
};
export default LogininforTableList;
export default Main;