page.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use client";
  2. import {fetchApi} from "@/app/_modules/func";
  3. import {DeleteOutlined, ExclamationCircleFilled, ReloadOutlined,} from "@ant-design/icons";
  4. import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
  5. import {PageContainer, ProTable} from "@ant-design/pro-components";
  6. import {Button, Modal} from "antd";
  7. import {useRouter} from "next/navigation";
  8. import {faToggleOff, faToggleOn} from "@fortawesome/free-solid-svg-icons";
  9. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  10. import {useRef, useState} from "react";
  11. //查询表格数据API
  12. const queryAPI = "/api/monitor/online/list";
  13. //强退用户API
  14. const logoutAPI = "/api/monitor/online";
  15. export default function Online() {
  16. const { push } = useRouter();
  17. // 添加用于控制强退确认模态框的状态
  18. const [logoutModalVisible, setLogoutModalVisible] = useState(false);
  19. const [logoutRecord, setLogoutRecord] = useState<any>(null);
  20. //表格列定义
  21. const columns: ProColumns[] = [
  22. {
  23. title: "序号",
  24. dataIndex: "index",
  25. valueType: "index",
  26. },
  27. {
  28. title: "会话编号",
  29. dataIndex: "tokenId",
  30. ellipsis: true,
  31. search: false,
  32. },
  33. {
  34. title: "用户名称",
  35. fieldProps: {
  36. placeholder: "请输入用户名称",
  37. },
  38. dataIndex: "userName",
  39. order: 1,
  40. },
  41. {
  42. title: "部门名称",
  43. dataIndex: "deptName",
  44. search: false,
  45. },
  46. {
  47. title: "IP地址",
  48. fieldProps: {
  49. placeholder: "请输入IP地址",
  50. },
  51. dataIndex: "ipaddr",
  52. order: 2,
  53. },
  54. {
  55. title: "登录地点",
  56. dataIndex: "loginLocation",
  57. search: false,
  58. },
  59. {
  60. title: "浏览器",
  61. dataIndex: "browser",
  62. search: false,
  63. },
  64. {
  65. title: "操作系统",
  66. dataIndex: "os",
  67. search: false,
  68. },
  69. {
  70. title: "登录时间",
  71. dataIndex: "loginTime",
  72. valueType: "dateTime",
  73. search: false,
  74. },
  75. {
  76. title: "操作",
  77. key: "option",
  78. search: false,
  79. render: (_, record) => [
  80. <Button
  81. key="deleteBtn"
  82. type="link"
  83. danger
  84. icon={<DeleteOutlined />}
  85. onClick={() => onClickLogoutRow(record)}
  86. >
  87. 强退
  88. </Button>,
  89. ],
  90. },
  91. ];
  92. //0.查询表格数据
  93. const queryTableData = async (params: any, sorter: any, filter: any) => {
  94. const searchParams = {
  95. pageNum: params.current,
  96. ...params,
  97. };
  98. delete searchParams.current;
  99. const queryParams = new URLSearchParams(searchParams);
  100. Object.keys(sorter).forEach((key) => {
  101. queryParams.append("orderByColumn", key);
  102. if (sorter[key] === "ascend") {
  103. queryParams.append("isAsc", "ascending");
  104. } else {
  105. queryParams.append("isAsc", "descending");
  106. }
  107. });
  108. return await fetchApi(`${queryAPI}?${queryParams}`, push);
  109. };
  110. //3.删除
  111. //点击删除按钮,展示删除确认框
  112. const onClickLogoutRow = (record?: any) => {
  113. setLogoutRecord(record);
  114. setLogoutModalVisible(true);
  115. };
  116. //确定强退选中的会话
  117. const executeLogoutRow = async () => {
  118. if (!logoutRecord) return;
  119. const tokenId = logoutRecord.tokenId;
  120. const body = await fetchApi(`${logoutAPI}/${tokenId}`, push, {
  121. method: "DELETE",
  122. });
  123. if (body !== undefined) {
  124. if (body.code == 200) {
  125. App.useApp().message.success("强退成功");
  126. //刷新列表
  127. if (actionTableRef.current) {
  128. actionTableRef.current.reload();
  129. }
  130. } else {
  131. App.useApp().message.error(body.msg);
  132. }
  133. }
  134. setLogoutModalVisible(false);
  135. setLogoutRecord(null);
  136. };
  137. //取消强退操作
  138. const cancelLogoutRow = () => {
  139. setLogoutModalVisible(false);
  140. setLogoutRecord(null);
  141. };
  142. //搜索栏显示状态
  143. const [showSearch, setShowSearch] = useState(true);
  144. //action对象引用
  145. const actionTableRef = useRef<ActionType>(null);
  146. //搜索表单对象引用
  147. const searchTableFormRef = useRef<ProFormInstance>(null!);
  148. //当前页数和每页条数
  149. const [page, setPage] = useState(1);
  150. const defaultPageSize = 10;
  151. const [pageSize, setPageSize] = useState(defaultPageSize);
  152. const pageChange = (page: number, pageSize: number) => {
  153. setPage(page);
  154. setPageSize(pageSize);
  155. };
  156. return (
  157. <PageContainer title={false}>
  158. <ProTable
  159. formRef={searchTableFormRef}
  160. rowKey="tokenId"
  161. columns={columns}
  162. request={async (params: any, sorter: any, filter: any) => {
  163. // 表单搜索项会从 params 传入,传递给后端接口。
  164. const data = await queryTableData(params, sorter, filter);
  165. if (data !== undefined) {
  166. return Promise.resolve({
  167. data: data.rows,
  168. success: true,
  169. total: data.total,
  170. });
  171. }
  172. return Promise.resolve({
  173. data: [],
  174. success: true,
  175. });
  176. }}
  177. pagination={{
  178. defaultPageSize: defaultPageSize,
  179. showQuickJumper: true,
  180. showSizeChanger: true,
  181. onChange: pageChange,
  182. }}
  183. search={
  184. showSearch
  185. ? {
  186. defaultCollapsed: false,
  187. searchText: "搜索",
  188. }
  189. : false
  190. }
  191. dateFormatter="string"
  192. actionRef={actionTableRef}
  193. toolbar={{
  194. actions: [],
  195. settings: [
  196. {
  197. key: "switch",
  198. icon: showSearch ? (
  199. <FontAwesomeIcon icon={faToggleOn} />
  200. ) : (
  201. <FontAwesomeIcon icon={faToggleOff} />
  202. ),
  203. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  204. onClick: (key: string | undefined) => {
  205. setShowSearch(!showSearch);
  206. },
  207. },
  208. {
  209. key: "refresh",
  210. tooltip: "刷新",
  211. icon: <ReloadOutlined />,
  212. onClick: (key: string | undefined) => {
  213. if (actionTableRef.current) {
  214. actionTableRef.current.reload();
  215. }
  216. },
  217. },
  218. ],
  219. }}
  220. />
  221. {/* 强退确认模态框 */}
  222. <Modal
  223. title={
  224. <div style={{ display: 'flex', alignItems: 'center' }}>
  225. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  226. <span>系统提示</span>
  227. </div>
  228. }
  229. open={logoutModalVisible}
  230. onOk={executeLogoutRow}
  231. onCancel={cancelLogoutRow}
  232. okText="确认"
  233. cancelText="取消"
  234. >
  235. <p>{`确定强退登录名称为“${logoutRecord?.userName}”的用户?`}</p>
  236. </Modal>
  237. </PageContainer>
  238. );
  239. }