skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
zy-asrs-flow/src/pages/system/userLogin/index.jsx
@@ -1,289 +1,289 @@
import React, { useState, useRef, useEffect } from 'react';
import { Button, message, Modal, Tag } from 'antd';
import {
    FooterToolbar,
    PageContainer,
    ProTable,
    LightFilter,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
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'
import { repairBug } from '@/utils/common-util';
const TABLE_KEY = "pro-table-userLogin";
const typeMap = {
    0: {
        color: '#87d068',
        text: '登录成功',
    },
    1: {
        color: '#f50',
        text: '登录失败',
    },
    2: {
        color: '',
        text: '退出登录',
    },
    3: {
        color: '#3b5999',
        text: '续签token',
    },
};
const handleExport = async (intl) => {
    const hide = message.loading(intl.formatMessage({ id: 'page.exporting', defaultMessage: '正在导出' }));
    try {
        const resp = await Http.doPostBlob('api/userLogin/export');
        const blob = new Blob([resp], { type: 'application/vnd.ms-excel' });
        window.location.href = window.URL.createObjectURL(blob);
        message.success(intl.formatMessage({ id: 'page.export.success', defaultMessage: '导出成功' }));
        return true;
    } catch (error) {
        message.error(intl.formatMessage({ id: 'page.export.fail', defaultMessage: '导出失败,请重试' }));
        return false;
    } finally {
        hide();
    }
};
const Main = () => {
    const intl = useIntl();
    const formTableRef = useRef();
    const actionRef = useRef();
    const [selectedRows, setSelectedRows] = useState([]);
    const [modalVisible, setModalVisible] = useState(false);
    const [currentRow, setCurrentRow] = useState();
    const [searchParam, setSearchParam] = useState({});
    useEffect(() => {
    }, []);
    const columns = [
        {
            title: intl.formatMessage({
                id: 'page.table.no',
                defaultMessage: 'No'
            }),
            dataIndex: 'index',
            valueType: 'indexBorder',
            width: 48,
        },
        {
            title: '用户',
            dataIndex: 'userId$',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <LinkFilter
                name='userId'
                major='user'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '密钥',
            dataIndex: 'token',
            valueType: 'text',
            hidden: false,
            ellipsis: true,
            width: 140,
            copyable: true,
            filterDropdown: (props) => <TextFilter
                name='token'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '登录ip',
            dataIndex: 'ip',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='ip'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '类型',
            dataIndex: 'type',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <SelectFilter
                name='type'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
                data={[
                    { label: '登录成功', value: 0 },
                    { label: '登录失败', value: 1 },
                    { label: '退出登录', value: 2 },
                    { label: '续签token', value: 3 },
                ]}
            />,
            render: (_, record) => {
                const type = typeMap[record.type]
                return <Tag color={type.color}>{type.text}</Tag>
            },
        },
        {
            title: '添加时间',
            dataIndex: 'createTime$',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <DatetimeRangeFilter
                name='createTime'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '登录系统',
            dataIndex: 'system',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='system'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '备注',
            dataIndex: 'memo',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='memo'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 140,
            hidden: true,
            valueType: 'option',
            render: (_, record) => [
            ],
        },
    ];
    return (
        <PageContainer
            header={{
                breadcrumb: {},
            }}
        >
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    key="userLogin"
                    rowKey="id"
                    actionRef={actionRef}
                    formRef={formTableRef}
                    columns={columns}
                    cardBordered
                    scroll={{ x: 1300 }}
                    dateFormatter="string"
                    pagination={{ pageSize: 16 }}
                    search={false}
                    style={{
                        marginBottom: '20px'
                    }}
                    toolbar={{
                        search: {
                            onSearch: (value) => {
                                setSearchParam(prevState => ({
                                    ...prevState,
                                    condition: value
                                }));
                                actionRef.current?.reload();
                            },
                        },
                        filter: (
                            <LightFilter
                                onValuesChange={(val) => {
                                }}
                            >
                            </LightFilter>
                        ),
                        actions: [
                            <Button
                                key="export"
                                onClick={async () => {
                                    handleExport(intl);
                                }}
                            >
                                <ExportOutlined />
                                <FormattedMessage id='page.export' defaultMessage='导出' />
                            </Button>,
                        ],
                    }}
                    request={(params, sorter, filter) =>
                        Http.doPostPromise('/api/userLogin/page', { ...params, ...searchParam }, (res) => {
                            return {
                                data: res.data.records,
                                total: res.data.total,
                                success: true,
                            }
                        })
                    }
                    rowSelection={undefined}
                    columnsState={{
                        persistenceKey: TABLE_KEY,
                        persistenceType: 'localStorage',
                        defaultValue: {
                            system: { show: repairBug(TABLE_KEY, 'system', false) },
                            option: { fixed: 'right', disable: true },
                        },
                        onChange(value) {
                        },
                    }}
                />
            </div>
            <Edit
                open={modalVisible}
                values={currentRow || {}}
                onCancel={
                    () => {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                    }
                }
                onSubmit={async (values) => {
                    setModalVisible(false);
                    setCurrentRow(undefined);
                    if (actionRef.current) {
                        actionRef.current.reload();
                    }
                }
                }
            />
        </PageContainer>
    );
};
export default Main;
import React, { useState, useRef, useEffect } from 'react';
import { Button, message, Modal, Tag } from 'antd';
import {
    FooterToolbar,
    PageContainer,
    ProTable,
    LightFilter,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
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'
import { repairBug } from '@/utils/common-util';
const TABLE_KEY = "pro-table-userLogin";
const typeMap = {
    0: {
        color: '#87d068',
        text: '登录成功',
    },
    1: {
        color: '#f50',
        text: '登录失败',
    },
    2: {
        color: '',
        text: '退出登录',
    },
    3: {
        color: '#3b5999',
        text: '续签token',
    },
};
const handleExport = async (intl) => {
    const hide = message.loading(intl.formatMessage({ id: 'page.exporting', defaultMessage: '正在导出' }));
    try {
        const resp = await Http.doPostBlob('api/userLogin/export');
        const blob = new Blob([resp], { type: 'application/vnd.ms-excel' });
        window.location.href = window.URL.createObjectURL(blob);
        message.success(intl.formatMessage({ id: 'page.export.success', defaultMessage: '导出成功' }));
        return true;
    } catch (error) {
        message.error(intl.formatMessage({ id: 'page.export.fail', defaultMessage: '导出失败,请重试' }));
        return false;
    } finally {
        hide();
    }
};
const Main = () => {
    const intl = useIntl();
    const formTableRef = useRef();
    const actionRef = useRef();
    const [selectedRows, setSelectedRows] = useState([]);
    const [modalVisible, setModalVisible] = useState(false);
    const [currentRow, setCurrentRow] = useState();
    const [searchParam, setSearchParam] = useState({});
    useEffect(() => {
    }, []);
    const columns = [
        {
            title: intl.formatMessage({
                id: 'page.table.no',
                defaultMessage: 'No'
            }),
            dataIndex: 'index',
            valueType: 'indexBorder',
            width: 48,
        },
        {
            title: '用户',
            dataIndex: 'userId$',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <LinkFilter
                name='userId'
                major='user'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '密钥',
            dataIndex: 'token',
            valueType: 'text',
            hidden: false,
            ellipsis: true,
            width: 140,
            copyable: true,
            filterDropdown: (props) => <TextFilter
                name='token'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '登录ip',
            dataIndex: 'ip',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='ip'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '类型',
            dataIndex: 'type',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <SelectFilter
                name='type'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
                data={[
                    { label: '登录成功', value: 0 },
                    { label: '登录失败', value: 1 },
                    { label: '退出登录', value: 2 },
                    { label: '续签token', value: 3 },
                ]}
            />,
            render: (_, record) => {
                const type = typeMap[record.type]
                return <Tag color={type.color}>{type.text}</Tag>
            },
        },
        {
            title: '添加时间',
            dataIndex: 'createTime$',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <DatetimeRangeFilter
                name='createTime'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '登录系统',
            dataIndex: 'system',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='system'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '备注',
            dataIndex: 'memo',
            valueType: 'text',
            hidden: false,
            width: 140,
            filterDropdown: (props) => <TextFilter
                name='memo'
                {...props}
                actionRef={actionRef}
                setSearchParam={setSearchParam}
            />,
        },
        {
            title: '操作',
            dataIndex: 'option',
            width: 140,
            hidden: true,
            valueType: 'option',
            render: (_, record) => [
            ],
        },
    ];
    return (
        <PageContainer
            header={{
                breadcrumb: {},
            }}
        >
            <div style={{ width: '100%', float: 'right' }}>
                <ProTable
                    key="userLogin"
                    rowKey="id"
                    actionRef={actionRef}
                    formRef={formTableRef}
                    columns={columns}
                    cardBordered
                    scroll={{ x: 1300 }}
                    dateFormatter="string"
                    pagination={{ pageSize: 16 }}
                    search={false}
                    style={{
                        marginBottom: '20px'
                    }}
                    toolbar={{
                        search: {
                            onSearch: (value) => {
                                setSearchParam(prevState => ({
                                    ...prevState,
                                    condition: value
                                }));
                                actionRef.current?.reload();
                            },
                        },
                        filter: (
                            <LightFilter
                                onValuesChange={(val) => {
                                }}
                            >
                            </LightFilter>
                        ),
                        actions: [
                            <Button
                                key="export"
                                onClick={async () => {
                                    handleExport(intl);
                                }}
                            >
                                <ExportOutlined />
                                <FormattedMessage id='page.export' defaultMessage='导出' />
                            </Button>,
                        ],
                    }}
                    request={(params, sorter, filter) =>
                        Http.doPostPromise('/api/userLogin/page', { ...params, ...searchParam }, (res) => {
                            return {
                                data: res.data.records,
                                total: res.data.total,
                                success: true,
                            }
                        })
                    }
                    rowSelection={undefined}
                    columnsState={{
                        persistenceKey: TABLE_KEY,
                        persistenceType: 'localStorage',
                        defaultValue: {
                            system: { show: repairBug(TABLE_KEY, 'system', false) },
                            option: { fixed: 'right', disable: true },
                        },
                        onChange(value) {
                        },
                    }}
                />
            </div>
            <Edit
                open={modalVisible}
                values={currentRow || {}}
                onCancel={
                    () => {
                        setModalVisible(false);
                        setCurrentRow(undefined);
                    }
                }
                onSubmit={async (values) => {
                    setModalVisible(false);
                    setCurrentRow(undefined);
                    if (actionRef.current) {
                        actionRef.current.reload();
                    }
                }
                }
            />
        </PageContainer>
    );
};
export default Main;