import React, {Component} from 'react'; import { Button, Col, Divider, Input, Layout, Modal, notification, PageHeader, Row, Select, Space, Table, Tag, Tooltip, Typography } from "antd"; import qs from "qs"; import request from "../../common/request"; import {differTime, itemRender} from "../../utils/utils"; import {message} from "antd/es"; import {PROTOCOL_COLORS} from "../../common/constants"; import {DisconnectOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons"; import Monitor from "../access/Monitor"; import Logout from "../user/Logout"; import dayjs from "dayjs"; const confirm = Modal.confirm; const {Content} = Layout; const {Search} = Input; const {Title, Text} = Typography; const routes = [ { path: '', breadcrumbName: '首页', }, { path: 'onlineSession', breadcrumbName: '在线会话', } ]; class OnlineSession extends Component { inputRefOfClientIp = React.createRef(); state = { items: [], total: 0, queryParams: { pageIndex: 1, pageSize: 10, protocol: '', userId: undefined, assetId: undefined }, loading: false, selectedRowKeys: [], delBtnLoading: false, users: [], assets: [], accessVisible: false, sessionWidth: 1024, sessionHeight: 768, sessionProtocol: '' }; componentDidMount() { this.loadTableData(); this.handleSearchByNickname(''); this.handleSearchByAssetName(''); } async loadTableData(queryParams) { this.setState({ loading: true }); queryParams = queryParams || this.state.queryParams; queryParams['status'] = 'connected'; // queryParams let paramsStr = qs.stringify(queryParams); let data = { items: [], total: 0 }; try { let result = await request.get('/sessions/paging?' + paramsStr); if (result.code === 1) { data = result.data; } else { message.error(result.message); } } catch (e) { } finally { const items = data.items.map(item => { return {'key': item['id'], ...item} }) this.setState({ items: items, total: data.total, queryParams: queryParams, loading: false }); } } handleChangPage = (pageIndex, pageSize) => { let queryParams = this.state.queryParams; queryParams.pageIndex = pageIndex; queryParams.pageSize = pageSize; this.setState({ queryParams: queryParams }); this.loadTableData(queryParams) }; handleSearchByClientIp = clientIp => { let query = { ...this.state.queryParams, 'pageIndex': 1, 'pageSize': this.state.queryParams.pageSize, 'clientIp': clientIp, } this.loadTableData(query); } handleChangeByProtocol = protocol => { let query = { ...this.state.queryParams, 'pageIndex': 1, 'pageSize': this.state.queryParams.pageSize, 'protocol': protocol, } this.loadTableData(query); } handleSearchByNickname = async nickname => { const result = await request.get(`/users/paging?pageIndex=1&pageSize=100&nickname=${nickname}`); if (result.code !== 1) { message.error(result.message, 10); return; } this.setState({ users: result.data.items }) } handleChangeByUserId = userId => { let query = { ...this.state.queryParams, 'pageIndex': 1, 'pageSize': this.state.queryParams.pageSize, 'userId': userId, } this.loadTableData(query); } handleSearchByAssetName = async assetName => { const result = await request.get(`/assets/paging?pageIndex=1&pageSize=100&name=${assetName}`); if (result.code !== 1) { message.error(result.message, 10); return; } this.setState({ assets: result.data.items }) } handleChangeByAssetId = (assetId, options) => { let query = { ...this.state.queryParams, 'pageIndex': 1, 'pageSize': this.state.queryParams.pageSize, 'assetId': assetId, } this.loadTableData(query); } batchDis = async () => { this.setState({ delBtnLoading: true }) try { let result = await request.post('/sessions/' + this.state.selectedRowKeys.join(',') + '/disconnect'); if (result.code === 1) { message.success('操作成功', 3); this.setState({ selectedRowKeys: [] }) await this.loadTableData(this.state.queryParams); } else { message.error('删除失败 :( ' + result.message, 10); } } finally { this.setState({ delBtnLoading: false }) } } showMonitor = (record) => { this.setState({ connectionId: record.connectionId, sessionProtocol: record.protocol, accessVisible: true, sessionWidth: record.width, sessionHeight: record.height, sessionTitle: `${record.username}@${record.ip}:${record.port} ${record.width}x${record.height}` }) } render() { const columns = [{ title: '序号', dataIndex: 'id', key: 'id', render: (id, record, index) => { return index + 1; } }, { title: '来源IP', dataIndex: 'clientIp', key: 'clientIp' }, { title: '用户昵称', dataIndex: 'creatorName', key: 'creatorName' }, { title: '资产名称', dataIndex: 'assetName', key: 'assetName' }, { title: '连接协议', dataIndex: 'protocol', key: 'protocol', render: (text, record) => { const title = `${record.username}@${record.ip}:${record.port}`; return ( {text} ) } }, { title: '接入时间', dataIndex: 'connectedTime', key: 'connectedTime', render: (text, record) => { return ( {dayjs(text).fromNow()} ) } }, { title: '接入时长', dataIndex: 'connectedTime', key: 'connectedTime', render: (text, record) => { return differTime(new Date(record['connectedTime']), new Date()); } }, { title: '操作', key: 'action', render: (text, record) => { return (
) }, } ]; const selectedRowKeys = this.state.selectedRowKeys; const rowSelection = { selectedRowKeys: selectedRowKeys, onChange: (selectedRowKeys, selectedRows) => { this.setState({selectedRowKeys}); }, }; const hasSelected = selectedRowKeys.length > 0; const userOptions = this.state.users.map(d => {d.nickname}); const assetOptions = this.state.assets.map(d => {d.name}); return ( <> ]} subTitle="查询实时在线会话" >
在线会话列表
`总计 ${total} 条` }} loading={this.state.loading} /> { this.state.accessVisible ? { message.destroy(); this.setState({accessVisible: false}) }} > : undefined } ); } } export default OnlineSession;