Skip to content

Commit

Permalink
优雅代码
Browse files Browse the repository at this point in the history
  • Loading branch information
QwQ-wuwuwu committed Jun 19, 2024
1 parent 247642a commit 5774fd4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
16 changes: 3 additions & 13 deletions src/frontend/src/controllers/API/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ export async function getLogsApi({page, pageSize, userIds, groupId, start, end,
action?:string
}):Promise<{data:any[], total:number}> {
const uids = userIds?.reduce((pre,val) => `${pre}&operator_ids=${val}`, '') || ''
if(!start && end) return await axios.get(
`/api/v1/audit?page=${page}&limit=${pageSize}&group_ids=${groupId}${uids}` +
`&end_time=${end}&system_id=${moduleId}&event_type=${action}`
)
if(!end && start) return await axios.get(
`/api/v1/audit?page=${page}&limit=${pageSize}&group_ids=${groupId}${uids}` +
`&start_time=${start}&system_id=${moduleId}&event_type=${action}`
)
if(!start && !end) return await axios.get(
`/api/v1/audit?page=${page}&limit=${pageSize}&group_ids=${groupId}${uids}` +
`&system_id=${moduleId}&event_type=${action}`
)
const startStr = start ? `&start_time=${start}` : ''
const endStr = end ? `&end_time=${end}` : ''
return await axios.get(
`/api/v1/audit?page=${page}&limit=${pageSize}&group_ids=${groupId}${uids}` +
`&start_time=${start}&end_time=${end}&system_id=${moduleId}&event_type=${action}`
`&system_id=${moduleId}&event_type=${action}` + startStr + endStr
)
}
// 系统模块
Expand Down
46 changes: 20 additions & 26 deletions src/frontend/src/pages/LogPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TableHeader,
TableRow
} from "../../components/bs-ui/table";
import { formatDate } from "@/util/utils";

const useGroups = () => {
const [groups, setGroups] = useState([])
Expand All @@ -37,39 +38,32 @@ export default function index() {
const { users, reload, loadMore, searchUser } = useUsers()
const { groups, loadData } = useGroups()
const { modules, loadModules } = useModules()
const { page, pageSize, data: logs, total, setPage, filterData } = useTable({ pageSize: 13 }, (param) =>
const { page, pageSize, data: logs, total, setPage, filterData } = useTable({ pageSize: 20 }, (param) =>
getLogsApi({...param})
)

const [actions, setActions] = useState<any[]>([])
const [keys, setKeys] = useState({
const init = {
userIds: [],
groupId: '',
start: undefined,
end: undefined,
moduleId: '',
action: ''
})
}

const handleActionOpen = () => {
keys.moduleId !== '' ? getActionsByModuleApi(keys.moduleId).then(res => setActions(res.data))
: getActionsApi().then(res => setActions(res.data))
const [actions, setActions] = useState<any[]>([])
const [keys, setKeys] = useState({...init})

const handleActionOpen = async () => {
setActions((keys.moduleId ? await getActionsByModuleApi(keys.moduleId) : await getActionsApi()).data)
}
const handleSearch = () => {
const uids = keys.userIds.map(u => u.value)
const startTime = keys.start ? keys.start.toISOString().replace('T',' ').split('.')[0] : undefined
const endTime = keys.end ? keys.end.toISOString().replace('T',' ').split('.')[0] : undefined
const startTime = keys.start ? formatDate(keys.start, 'yyyy-MM-dd HH:mm:ss') : undefined
const endTime = keys.end ? formatDate(keys.start, 'yyyy-MM-dd HH:mm:ss') : undefined
filterData({...keys, userIds:uids, start:startTime, end:endTime})
}
const handleReset = () => {
setKeys({
userIds: [],
groupId: '',
start: undefined,
end: undefined,
moduleId: '',
action: ''
})
setKeys({...init})
}

return <div className="relative">
Expand Down Expand Up @@ -156,14 +150,14 @@ export default function index() {
{logs.map(log => (
<TableRow>
<TableCell className="font-medium max-w-md truncate">{log.id}</TableCell>
<TableHead>{log.operator_name}</TableHead>
<TableHead>{log.create_time}</TableHead>
<TableHead>{log.system_ids}</TableHead>
<TableHead>{log.event_type}</TableHead>
<TableHead>{log.object_type}</TableHead>
<TableHead>{log.object_name}</TableHead>
<TableHead>{log.ip_address}</TableHead>
<TableHead>{log.note}</TableHead>
<TableCell>{log.operator_name}</TableCell>
<TableCell>{log.create_time}</TableCell>
<TableCell>{log.system_ids}</TableCell>
<TableCell>{log.event_type}</TableCell>
<TableCell>{log.object_type}</TableCell>
<TableCell>{log.object_name}</TableCell>
<TableCell>{log.ip_address}</TableCell>
<TableCell>{log.note}</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
14 changes: 14 additions & 0 deletions src/frontend/src/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ export function formatMilliseconds(ms: number, format: string): string {
return formattedString;
}

// Date转换为目标格式
export function formatDate(date: Date, format: string): string {
const addZero = (num) => num < 10 ? `0${num}` : `${num}`
const replacements = {
'yyyy': date.getFullYear(),
'MM': addZero(date.getMonth() + 1),
'dd': addZero(date.getDate()),
'HH': addZero(date.getHours()),
'mm': addZero(date.getMinutes()),
'ss': addZero(date.getSeconds())
}
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, (match) => replacements[match])
}

export function toTitleCase(str: string | undefined): string {
if (!str) return "";
let result = str
Expand Down

0 comments on commit 5774fd4

Please sign in to comment.