page.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. "use client";
  2. import {fetchApi, fetchFile} from "@/app/_modules/func";
  3. import {
  4. ClearOutlined,
  5. DeleteOutlined,
  6. ExclamationCircleFilled,
  7. ReloadOutlined,
  8. UnlockOutlined,
  9. } from "@ant-design/icons";
  10. import type {ActionType, ProColumns, ProFormInstance,} from "@ant-design/pro-components";
  11. import {PageContainer, ProTable,} from "@ant-design/pro-components";
  12. import {App, Button, Modal, Space, Tag} from "antd";
  13. import {useRouter} from "next/navigation";
  14. import {faCheck, faDownload, faToggleOff, faToggleOn, faXmark,} from "@fortawesome/free-solid-svg-icons";
  15. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  16. import {useRef, useState} from "react";
  17. import globalMessage from "@/app/_modules/globalMessage";
  18. export default function OperLog() {
  19. const { push } = useRouter();
  20. // 添加用于控制删除确认模态框的状态
  21. const [deleteModalVisible, setDeleteModalVisible] = useState(false);
  22. // 添加用于控制清空确认模态框的状态
  23. const [clearModalVisible, setClearModalVisible] = useState(false);
  24. // 添加用于控制解锁确认模态框的状态
  25. const [unlockModalVisible, setUnlockModalVisible] = useState(false);
  26. //表格列定义
  27. const columns: ProColumns[] = [
  28. {
  29. title: "访问编号",
  30. dataIndex: "infoId",
  31. search: false,
  32. },
  33. {
  34. title: "用户名称",
  35. fieldProps: {
  36. placeholder: "请输入用户名称",
  37. },
  38. dataIndex: "userName",
  39. order: 3,
  40. sorter: true,
  41. },
  42. {
  43. title: "登录地址",
  44. fieldProps: {
  45. placeholder: "请输入登录地址",
  46. },
  47. dataIndex: "ipaddr",
  48. order: 4,
  49. },
  50. {
  51. title: "登录地点",
  52. dataIndex: "loginLocation",
  53. search: false,
  54. },
  55. {
  56. title: "浏览器",
  57. dataIndex: "browser",
  58. search: false,
  59. },
  60. {
  61. title: "操作系统",
  62. dataIndex: "os",
  63. search: false,
  64. },
  65. {
  66. title: "登录状态",
  67. fieldProps: {
  68. placeholder: "请选择登录状态",
  69. },
  70. dataIndex: "status",
  71. valueType: "select",
  72. order: 2,
  73. render: (_, record) => {
  74. return (
  75. <Space>
  76. <Tag
  77. color={record.status == 0 ? "green" : "red"}
  78. icon={
  79. record.status == 0 ? (
  80. <FontAwesomeIcon icon={faCheck} />
  81. ) : (
  82. <FontAwesomeIcon icon={faXmark} />
  83. )
  84. }
  85. >
  86. {_}
  87. </Tag>
  88. </Space>
  89. );
  90. },
  91. valueEnum: {
  92. 0: {
  93. text: "成功",
  94. status: "0",
  95. },
  96. 1: {
  97. text: "失败",
  98. status: "1",
  99. },
  100. },
  101. },
  102. {
  103. title: "操作信息",
  104. dataIndex: "msg",
  105. search: false,
  106. },
  107. {
  108. title: "登录时间",
  109. dataIndex: "loginTime",
  110. valueType: "dateTime",
  111. search: false,
  112. sorter: true,
  113. },
  114. {
  115. title: "操作时间",
  116. fieldProps: {
  117. placeholder: ["开始日期", "结束日期"],
  118. },
  119. dataIndex: "loginTimeRange",
  120. valueType: "dateRange",
  121. hideInTable: true,
  122. order: 1,
  123. search: {
  124. transform: (value) => {
  125. return {
  126. "params[beginTime]": `${value[0]} 00:00:00`,
  127. "params[endTime]": `${value[1]} 23:59:59`,
  128. };
  129. },
  130. },
  131. },
  132. ];
  133. //查询日志数据
  134. const getLog = async (params: any, sorter: any, filter: any) => {
  135. const searchParams = {
  136. pageNum: params.current,
  137. ...params,
  138. };
  139. delete searchParams.current;
  140. const queryParams = new URLSearchParams(searchParams);
  141. Object.keys(sorter).forEach((key) => {
  142. queryParams.append("orderByColumn", key);
  143. if (sorter[key] === "ascend") {
  144. queryParams.append("isAsc", "ascending");
  145. } else {
  146. queryParams.append("isAsc", "descending");
  147. }
  148. });
  149. const body = await fetchApi(
  150. `/api/monitor/logininfor/list?${queryParams}`,
  151. push
  152. );
  153. console.log("data:", body);
  154. return body;
  155. };
  156. //删除按钮是否可用,选中行时才可用
  157. const [rowCanDelete, setRowCanDelete] = useState(false);
  158. //选中行操作
  159. const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
  160. const [selectedRow, setSelectedRow] = useState(undefined as any);
  161. //解锁按钮是否可用
  162. const [rowCanUnlock, setRowCanUnlock] = useState(false);
  163. const rowSelection = {
  164. onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
  165. setSelectedRowKeys(newSelectedRowKeys);
  166. setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);
  167. if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
  168. console.log("row:", selectedRows[0]);
  169. setSelectedRow(selectedRows[0]);
  170. setRowCanUnlock(true);
  171. } else {
  172. setRowCanUnlock(false);
  173. setSelectedRow(undefined);
  174. }
  175. },
  176. };
  177. //点击删除按钮
  178. const onClickDeleteRow = () => {
  179. setDeleteModalVisible(true);
  180. };
  181. //确定删除选中的日志数据
  182. const executeDeleteRow = async () => {
  183. const body = await fetchApi(
  184. `/api/monitor/logininfor/${selectedRowKeys.join(",")}`,
  185. push,
  186. {
  187. method: "DELETE",
  188. }
  189. );
  190. if (body !== undefined) {
  191. if (body.code == 200) {
  192. globalMessage.success("删除成功");
  193. //删除按钮变回不可点击
  194. setRowCanDelete(false);
  195. //选中行数据重置为空
  196. setSelectedRowKeys([]);
  197. //刷新列表
  198. if (actionRef.current) {
  199. actionRef.current.reload();
  200. }
  201. } else {
  202. globalMessage.error(body.msg);
  203. }
  204. }
  205. setDeleteModalVisible(false);
  206. };
  207. //取消删除操作
  208. const cancelDeleteRow = () => {
  209. setDeleteModalVisible(false);
  210. };
  211. //点击清空按钮
  212. const onClickClear = () => {
  213. setClearModalVisible(true);
  214. };
  215. //确定清空日志数据
  216. const executeClear = async () => {
  217. const body = await fetchApi("/api/monitor/logininfor/clean", push, {
  218. method: "DELETE",
  219. });
  220. if (body !== undefined) {
  221. if (body.code == 200) {
  222. globalMessage.success("清空成功");
  223. //选中行数据重置为空
  224. setSelectedRowKeys([]);
  225. //刷新列表
  226. if (actionRef.current) {
  227. actionRef.current.reload();
  228. }
  229. } else {
  230. globalMessage.error(body.msg);
  231. }
  232. }
  233. setClearModalVisible(false);
  234. };
  235. //取消清空操作
  236. const cancelClear = () => {
  237. setClearModalVisible(false);
  238. };
  239. //点击解锁按钮
  240. const onClickUnlock = () => {
  241. setUnlockModalVisible(true);
  242. };
  243. //确定解锁用户
  244. const executeUnlock = async () => {
  245. if (!selectedRow) return;
  246. const body = await fetchApi(
  247. `/api/monitor/logininfor/unlock/${selectedRow.userName}`,
  248. push
  249. );
  250. if (body !== undefined) {
  251. if (body.code == 200) {
  252. globalMessage.success("解锁成功");
  253. //刷新列表
  254. if (actionRef.current) {
  255. actionRef.current.reload();
  256. }
  257. } else {
  258. globalMessage.error(body.msg);
  259. }
  260. }
  261. setUnlockModalVisible(false);
  262. };
  263. //取消解锁操作
  264. const cancelUnlock = () => {
  265. setUnlockModalVisible(false);
  266. };
  267. //搜索栏显示状态
  268. const [showSearch, setShowSearch] = useState(true);
  269. //action对象引用
  270. const actionRef = useRef<ActionType>(null);
  271. //表单对象引用
  272. const formRef = useRef<ProFormInstance>(null!);
  273. //当前页数和每页条数
  274. const [page, setPage] = useState(1);
  275. const defaultPageSize = 10;
  276. const [pageSize, setPageSize] = useState(defaultPageSize);
  277. const pageChange = (page: number, pageSize: number) => {
  278. setPage(page);
  279. setPageSize(pageSize);
  280. };
  281. //导出日志文件
  282. const exportTable = async () => {
  283. if (formRef.current) {
  284. const formData = new FormData();
  285. const data = {
  286. pageNum: page,
  287. pageSize: pageSize,
  288. ...formRef.current.getFieldsValue(),
  289. };
  290. Object.keys(data).forEach((key) => {
  291. if (data[key] !== undefined) {
  292. formData.append(key, data[key]);
  293. }
  294. });
  295. await fetchFile(
  296. "/api/monitor/logininfor/export",
  297. push,
  298. {
  299. method: "POST",
  300. body: formData,
  301. },
  302. `logininfor_${new Date().getTime()}.xlsx`
  303. );
  304. }
  305. };
  306. return (
  307. <PageContainer title={false}>
  308. <ProTable
  309. formRef={formRef}
  310. rowKey="infoId"
  311. rowSelection={{
  312. selectedRowKeys,
  313. ...rowSelection,
  314. }}
  315. columns={columns}
  316. request={async (params: any, sorter: any, filter: any) => {
  317. // 表单搜索项会从 params 传入,传递给后端接口。
  318. const data = await getLog(params, sorter, filter);
  319. if (data !== undefined) {
  320. return Promise.resolve({
  321. data: data.rows,
  322. success: true,
  323. total: data.total,
  324. });
  325. }
  326. return Promise.resolve({
  327. data: [],
  328. success: true,
  329. });
  330. }}
  331. pagination={{
  332. defaultPageSize: defaultPageSize,
  333. showQuickJumper: true,
  334. showSizeChanger: true,
  335. onChange: pageChange,
  336. }}
  337. search={
  338. showSearch
  339. ? {
  340. defaultCollapsed: false,
  341. searchText: "搜索",
  342. }
  343. : false
  344. }
  345. dateFormatter="string"
  346. actionRef={actionRef}
  347. toolbar={{
  348. actions: [
  349. <Button
  350. key="danger"
  351. danger
  352. icon={<DeleteOutlined />}
  353. disabled={!rowCanDelete}
  354. onClick={onClickDeleteRow}
  355. >
  356. 删除
  357. </Button>,
  358. <Button
  359. key="clear"
  360. danger
  361. icon={<ClearOutlined />}
  362. onClick={onClickClear}
  363. >
  364. 清空
  365. </Button>,
  366. <Button
  367. key="unlock"
  368. icon={<UnlockOutlined />}
  369. disabled={!rowCanUnlock}
  370. onClick={onClickUnlock}
  371. >
  372. 解锁
  373. </Button>,
  374. <Button
  375. key="export"
  376. type="primary"
  377. icon={<FontAwesomeIcon icon={faDownload} />}
  378. onClick={exportTable}
  379. >
  380. 导出
  381. </Button>,
  382. ],
  383. settings: [
  384. {
  385. key: "switch",
  386. icon: showSearch ? (
  387. <FontAwesomeIcon icon={faToggleOn} />
  388. ) : (
  389. <FontAwesomeIcon icon={faToggleOff} />
  390. ),
  391. tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
  392. onClick: (key?: string | undefined) => {
  393. setShowSearch(!showSearch);
  394. },
  395. },
  396. {
  397. key: "refresh",
  398. tooltip: "刷新",
  399. icon: <ReloadOutlined />,
  400. onClick: (key?: string | undefined) => {
  401. if (actionRef.current) {
  402. actionRef.current.reload();
  403. }
  404. },
  405. },
  406. ],
  407. }}
  408. />
  409. {/* 删除确认模态框 */}
  410. <Modal
  411. title={
  412. <div style={{ display: 'flex', alignItems: 'center' }}>
  413. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  414. <span>系统提示</span>
  415. </div>
  416. }
  417. open={deleteModalVisible}
  418. onOk={executeDeleteRow}
  419. onCancel={cancelDeleteRow}
  420. okText="确认"
  421. cancelText="取消"
  422. >
  423. <p>{`确定删除访问编号为“${selectedRowKeys.join(",")}”的数据项?`}</p>
  424. </Modal>
  425. {/* 清空确认模态框 */}
  426. <Modal
  427. title={
  428. <div style={{ display: 'flex', alignItems: 'center' }}>
  429. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  430. <span>系统提示</span>
  431. </div>
  432. }
  433. open={clearModalVisible}
  434. onOk={executeClear}
  435. onCancel={cancelClear}
  436. okText="确认"
  437. cancelText="取消"
  438. >
  439. <p>是否确认清空所有操作日志数据项?</p>
  440. </Modal>
  441. {/* 解锁确认模态框 */}
  442. <Modal
  443. title={
  444. <div style={{ display: 'flex', alignItems: 'center' }}>
  445. <ExclamationCircleFilled style={{ color: '#faad14', marginRight: 8 }} />
  446. <span>系统提示</span>
  447. </div>
  448. }
  449. open={unlockModalVisible}
  450. onOk={executeUnlock}
  451. onCancel={cancelUnlock}
  452. okText="确认"
  453. cancelText="取消"
  454. >
  455. <p>{`确定解锁用户"${selectedRow?.userName}"数据项?`}</p>
  456. </Modal>
  457. </PageContainer>
  458. );
  459. }