page.tsx 6.0 KB

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