page.tsx 13 KB

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