Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame^] | 1 | import type { FormInstance } from 'antd'; |
| 2 | import { Button, message, Modal } from 'antd'; |
| 3 | import React, { useRef, useEffect } from 'react'; |
| 4 | import { useIntl, FormattedMessage, useAccess } from '@umijs/max'; |
| 5 | import { getOnlineUserList, forceLogout } from '@/services/monitor/online'; |
| 6 | import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; |
| 7 | import { DeleteOutlined } from '@ant-design/icons'; |
| 8 | |
| 9 | |
| 10 | /* * |
| 11 | * |
| 12 | * @author whiteshader@163.com |
| 13 | * @datetime 2023/02/07 |
| 14 | * |
| 15 | * */ |
| 16 | |
| 17 | |
| 18 | const handleForceLogout = async (selectedRow: API.Monitor.OnlineUserType) => { |
| 19 | const hide = message.loading('正在强制下线'); |
| 20 | try { |
| 21 | await forceLogout(selectedRow.tokenId); |
| 22 | hide(); |
| 23 | message.success('强制下线成功,即将刷新'); |
| 24 | return true; |
| 25 | } catch (error) { |
| 26 | hide(); |
| 27 | message.error('强制下线失败,请重试'); |
| 28 | return false; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | const OnlineUserTableList: React.FC = () => { |
| 33 | const formTableRef = useRef<FormInstance>(); |
| 34 | const actionRef = useRef<ActionType>(); |
| 35 | const access = useAccess(); |
| 36 | const intl = useIntl(); |
| 37 | |
| 38 | useEffect(() => {}, []); |
| 39 | |
| 40 | const columns: ProColumns<API.Monitor.OnlineUserType>[] = [ |
| 41 | { |
| 42 | title: <FormattedMessage id="monitor.online.user.token_id" defaultMessage="会话编号" />, |
| 43 | dataIndex: 'tokenId', |
| 44 | valueType: 'text', |
| 45 | hideInSearch: true, |
| 46 | }, |
| 47 | { |
| 48 | title: <FormattedMessage id="monitor.online.user.user_name" defaultMessage="用户账号" />, |
| 49 | dataIndex: 'userName', |
| 50 | valueType: 'text', |
| 51 | }, |
| 52 | { |
| 53 | title: <FormattedMessage id="monitor.online.user.dept_name" defaultMessage="部门名称" />, |
| 54 | dataIndex: 'deptName', |
| 55 | valueType: 'text', |
| 56 | hideInSearch: true, |
| 57 | }, |
| 58 | { |
| 59 | title: <FormattedMessage id="monitor.online.user.ipaddr" defaultMessage="登录IP地址" />, |
| 60 | dataIndex: 'ipaddr', |
| 61 | valueType: 'text', |
| 62 | }, |
| 63 | { |
| 64 | title: <FormattedMessage id="monitor.online.user.login_location" defaultMessage="登录地点" />, |
| 65 | dataIndex: 'loginLocation', |
| 66 | valueType: 'text', |
| 67 | hideInSearch: true, |
| 68 | }, |
| 69 | { |
| 70 | title: <FormattedMessage id="monitor.online.user.browser" defaultMessage="浏览器类型" />, |
| 71 | dataIndex: 'browser', |
| 72 | valueType: 'text', |
| 73 | hideInSearch: true, |
| 74 | }, |
| 75 | { |
| 76 | title: <FormattedMessage id="monitor.online.user.os" defaultMessage="操作系统" />, |
| 77 | dataIndex: 'os', |
| 78 | valueType: 'text', |
| 79 | hideInSearch: true, |
| 80 | }, |
| 81 | { |
| 82 | title: <FormattedMessage id="monitor.online.user.login_time" defaultMessage="登录时间" />, |
| 83 | dataIndex: 'loginTime', |
| 84 | valueType: 'dateRange', |
| 85 | render: (_, record) => <span>{record.loginTime}</span>, |
| 86 | hideInSearch: true, |
| 87 | search: { |
| 88 | transform: (value) => { |
| 89 | return { |
| 90 | 'params[beginTime]': value[0], |
| 91 | 'params[endTime]': value[1], |
| 92 | }; |
| 93 | }, |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />, |
| 98 | dataIndex: 'option', |
| 99 | width: '60px', |
| 100 | valueType: 'option', |
| 101 | render: (_, record) => [ |
| 102 | <Button |
| 103 | type="link" |
| 104 | size="small" |
| 105 | danger |
| 106 | key="batchRemove" |
| 107 | icon=<DeleteOutlined /> |
| 108 | hidden={!access.hasPerms('monitor:online:forceLogout')} |
| 109 | onClick={async () => { |
| 110 | Modal.confirm({ |
| 111 | title: '强踢', |
| 112 | content: '确定强制将该用户踢下线吗?', |
| 113 | okText: '确认', |
| 114 | cancelText: '取消', |
| 115 | onOk: async () => { |
| 116 | const success = await handleForceLogout(record); |
| 117 | if (success) { |
| 118 | if (actionRef.current) { |
| 119 | actionRef.current.reload(); |
| 120 | } |
| 121 | } |
| 122 | }, |
| 123 | }); |
| 124 | }} |
| 125 | > |
| 126 | 强退 |
| 127 | </Button>, |
| 128 | ], |
| 129 | }, |
| 130 | ]; |
| 131 | |
| 132 | return ( |
| 133 | <div style={{ width: '100%', float: 'right' }}> |
| 134 | <ProTable<API.Monitor.OnlineUserType> |
| 135 | headerTitle={intl.formatMessage({ |
| 136 | id: 'pages.searchTable.title', |
| 137 | defaultMessage: '信息', |
| 138 | })} |
| 139 | actionRef={actionRef} |
| 140 | formRef={formTableRef} |
| 141 | rowKey="tokenId" |
| 142 | key="logininforList" |
| 143 | search={{ |
| 144 | labelWidth: 120, |
| 145 | }} |
| 146 | request={(params) => |
| 147 | getOnlineUserList({ ...params } as API.Monitor.OnlineUserListParams).then((res) => { |
| 148 | const result = { |
| 149 | data: res.rows, |
| 150 | total: res.total, |
| 151 | success: true, |
| 152 | }; |
| 153 | return result; |
| 154 | }) |
| 155 | } |
| 156 | columns={columns} |
| 157 | /> |
| 158 | </div> |
| 159 | ); |
| 160 | }; |
| 161 | |
| 162 | export default OnlineUserTableList; |