log.ts
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import axios from 'axios'
// 创建axios实例
const api = axios.create({
baseURL: 'http://localhost:8083/api/system/log',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
})
// 请求拦截器
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
api.interceptors.response.use(
(response) => {
return response.data
},
(error) => {
console.error('API请求错误:', error)
if (error.response?.status === 401) {
localStorage.removeItem('token')
localStorage.removeItem('userInfo')
window.location.href = '/login'
}
return Promise.reject(error)
}
)
// 日志接口类型定义
export interface Log {
logId: number
logType: string
logTypeText: string
userId: number
username: string
module: string
operation: string
ip: string
logTime: string
status: number
statusText: string
errorMsg: string
requestParam: string
createBy: string
createTime: string
updateBy: string
updateTime: string
}
export interface LogDetail {
logId: number
logType: string
logTypeText: string
userId: number
username: string
module: string
operation: string
ip: string
logTime: string
status: number
statusText: string
errorMsg: string
requestParam: string
createBy: string
createTime: string
updateBy: string
updateTime: string
}
export interface LogSearchParams {
pageNum: number
pageSize: number
logType?: string
userId?: number
username?: string
module?: string
operation?: string
ip?: string
status?: number
startTime?: string
endTime?: string
}
export interface ApiResponse<T> {
code: number
message: string
data: T
}
export interface PageData<T> {
records: T[]
total: number
current: number
size: number
pages: number
}
// API调用方法
export const logApi = {
// 获取日志列表
getLogList: (params: LogSearchParams): Promise<ApiResponse<PageData<Log>>> => {
return api.get('/page', { params })
},
// 获取日志详情
getLogDetail: (logId: number): Promise<ApiResponse<LogDetail>> => {
return api.get(`/${logId}`)
},
// 获取用户登录日志
getLoginLogsByUserId: (userId: number, startTime?: string, endTime?: string): Promise<ApiResponse<Log[]>> => {
return api.get(`/login/${userId}`, {
params: {
startTime,
endTime
}
})
},
// 清理过期日志
cleanExpiredLogs: (expireDays: number): Promise<ApiResponse<string>> => {
return api.delete('/clean', {
params: {
expireDays
}
})
},
// 统计日志数量
countLogs: (logType?: string, startTime?: string, endTime?: string): Promise<ApiResponse<number>> => {
return api.get('/count', {
params: {
logType,
startTime,
endTime
}
})
},
// 删除日志
deleteLog: (logId: number): Promise<ApiResponse<string>> => {
return api.delete(`/${logId}`)
},
// 批量删除日志
deleteLogs: (logIds: number[]): Promise<ApiResponse<string>> => {
return api.delete('/batch', { data: logIds })
}
}
export default logApi