From 64db39320a7e81c385ed8c466b01795321f14408 Mon Sep 17 00:00:00 2001 From: JustSong Date: Sat, 10 Jun 2023 20:40:07 +0800 Subject: [PATCH] feat: now able to check all user's log --- model/log.go | 8 +++- web/src/components/LogsTable.js | 78 ++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/model/log.go b/model/log.go index bc09605537..52d02a6f00 100644 --- a/model/log.go +++ b/model/log.go @@ -35,7 +35,13 @@ func RecordLog(userId int, logType int, content string) { } func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) { - err = DB.Where("type = ?", logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error + var tx *gorm.DB + if logType == LogTypeUnknown { + tx = DB + } else { + tx = DB.Where("type = ?", logType) + } + err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error return logs, err } diff --git a/web/src/components/LogsTable.js b/web/src/components/LogsTable.js index 1c62389862..e5ec5dfa23 100644 --- a/web/src/components/LogsTable.js +++ b/web/src/components/LogsTable.js @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; -import { Button, Label, Pagination, Table } from 'semantic-ui-react'; -import { API, showError, timestamp2string } from '../helpers'; +import { Button, Label, Pagination, Select, Table } from 'semantic-ui-react'; +import { API, isAdmin, showError, timestamp2string } from '../helpers'; import { ITEMS_PER_PAGE } from '../constants'; @@ -12,6 +12,19 @@ function renderTimestamp(timestamp) { ); } +const MODE_OPTIONS = [ + { key: 'all', text: '全部用户', value: 'all' }, + { key: 'self', text: '当前用户', value: 'self' }, +]; + +const LOG_OPTIONS = [ + { key: '0', text: '全部', value: 0 }, + { key: '1', text: '充值', value: 1 }, + { key: '2', text: '消费', value: 2 }, + { key: '3', text: '管理', value: 3 }, + { key: '4', text: '系统', value: 4 } +]; + function renderType(type) { switch (type) { case 1: @@ -21,7 +34,7 @@ function renderType(type) { case 3: return ; case 4: - return ; + return ; default: return ; } @@ -33,9 +46,16 @@ const LogsTable = () => { const [activePage, setActivePage] = useState(1); const [searchKeyword, setSearchKeyword] = useState(''); const [searching, setSearching] = useState(false); + const [logType, setLogType] = useState(0); + const [mode, setMode] = useState('self'); // all, self + const showModePanel = isAdmin(); const loadLogs = async (startIdx) => { - const res = await API.get(`/api/log/self/?p=${startIdx}`); + let url = `/api/log/self/?p=${startIdx}&type=${logType}`; + if (mode === 'all') { + url = `/api/log/?p=${startIdx}&type=${logType}`; + } + const res = await API.get(url); const { success, message, data } = res.data; if (success) { if (startIdx === 0) { @@ -74,6 +94,10 @@ const LogsTable = () => { }); }, []); + useEffect(() => { + refresh().then(); + }, [mode, logType]); + const searchLogs = async () => { if (searchKeyword === '') { // if keyword is blank, load files instead. @@ -125,6 +149,19 @@ const LogsTable = () => { > 时间 + { + showModePanel && ( + { + sortLog('user_id'); + }} + width={1} + > + 用户 + + ) + } { @@ -139,7 +176,7 @@ const LogsTable = () => { onClick={() => { sortLog('content'); }} - width={11} + width={showModePanel ? 10 : 11} > 详情 @@ -157,6 +194,11 @@ const LogsTable = () => { return ( {renderTimestamp(log.created_at)} + { + showModePanel && ( + + ) + } {renderType(log.type)} {log.content} @@ -166,7 +208,31 @@ const LogsTable = () => { - + + { + showModePanel && ( + { + setLogType(value); + }} + />