OperationLogs.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class="operation-logs">
  3. <!-- 筛选条件 -->
  4. <el-card class="filter-card">
  5. <el-form :inline="true" :model="filterForm" class="filter-form">
  6. <el-form-item label="用户ID">
  7. <el-input v-model="filterForm.userId" placeholder="请输入用户ID" clearable></el-input>
  8. </el-form-item>
  9. <el-form-item label="操作类型">
  10. <el-select v-model="filterForm.operationType" placeholder="请选择" clearable>
  11. <el-option label="登录" value="login"></el-option>
  12. <el-option label="登出" value="logout"></el-option>
  13. <el-option label="创建" value="create"></el-option>
  14. <el-option label="更新" value="update"></el-option>
  15. <el-option label="删除" value="delete"></el-option>
  16. <el-option label="查询" value="query"></el-option>
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item label="时间范围">
  20. <el-date-picker
  21. v-model="dateRange"
  22. type="daterange"
  23. range-separator="至"
  24. start-placeholder="开始日期"
  25. end-placeholder="结束日期"
  26. value-format="yyyy-MM-dd">
  27. </el-date-picker>
  28. </el-form-item>
  29. <el-form-item>
  30. <el-button type="primary" @click="handleQuery" :loading="loading">查询</el-button>
  31. <el-button @click="handleReset">重置</el-button>
  32. </el-form-item>
  33. </el-form>
  34. </el-card>
  35. <!-- 日志列表 -->
  36. <el-card>
  37. <el-table :data="logs" v-loading="loading" stripe>
  38. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  39. <el-table-column prop="userId" label="用户ID" width="100"></el-table-column>
  40. <el-table-column prop="nickname" label="用户名" width="120"></el-table-column>
  41. <el-table-column prop="operationType" label="操作类型" width="100">
  42. <template slot-scope="scope">
  43. <el-tag :type="getOperationTypeTag(scope.row.operationType)" size="small">
  44. {{ scope.row.operationType }}
  45. </el-tag>
  46. </template>
  47. </el-table-column>
  48. <el-table-column prop="description" label="操作描述" min-width="200"></el-table-column>
  49. <el-table-column prop="requestPath" label="请求路径" min-width="180"></el-table-column>
  50. <el-table-column prop="requestMethod" label="方法" width="80"></el-table-column>
  51. <el-table-column prop="responseStatus" label="状态" width="80">
  52. <template slot-scope="scope">
  53. <el-tag :type="scope.row.responseStatus === 200 ? 'success' : 'danger'" size="small">
  54. {{ scope.row.responseStatus }}
  55. </el-tag>
  56. </template>
  57. </el-table-column>
  58. <el-table-column prop="clientIp" label="IP地址" width="130"></el-table-column>
  59. <el-table-column prop="createdAt" label="操作时间" width="160">
  60. <template slot-scope="scope">
  61. {{ formatDate(scope.row.createdAt) }}
  62. </template>
  63. </el-table-column>
  64. <el-table-column label="操作" width="80" fixed="right">
  65. <template slot-scope="scope">
  66. <el-button type="text" size="small" @click="handleViewDetail(scope.row)">
  67. 详情
  68. </el-button>
  69. </template>
  70. </el-table-column>
  71. </el-table>
  72. <!-- 分页 -->
  73. <el-pagination
  74. @current-change="handlePageChange"
  75. :current-page="pagination.page"
  76. :page-size="pagination.size"
  77. :total="pagination.total"
  78. layout="total, prev, pager, next"
  79. class="pagination">
  80. </el-pagination>
  81. </el-card>
  82. <!-- 详情对话框 -->
  83. <el-dialog title="操作日志详情" :visible.sync="detailVisible" width="600px">
  84. <el-descriptions :column="1" border v-if="currentLog">
  85. <el-descriptions-item label="ID">{{ currentLog.id }}</el-descriptions-item>
  86. <el-descriptions-item label="用户ID">{{ currentLog.userId }}</el-descriptions-item>
  87. <el-descriptions-item label="用户名">{{ currentLog.nickname }}</el-descriptions-item>
  88. <el-descriptions-item label="操作类型">{{ currentLog.operationType }}</el-descriptions-item>
  89. <el-descriptions-item label="操作描述">{{ currentLog.description }}</el-descriptions-item>
  90. <el-descriptions-item label="请求路径">{{ currentLog.requestPath }}</el-descriptions-item>
  91. <el-descriptions-item label="请求方法">{{ currentLog.requestMethod }}</el-descriptions-item>
  92. <el-descriptions-item label="请求参数">{{ currentLog.requestParams || '-' }}</el-descriptions-item>
  93. <el-descriptions-item label="响应状态">{{ currentLog.responseStatus }}</el-descriptions-item>
  94. <el-descriptions-item label="客户端IP">{{ currentLog.clientIp }}</el-descriptions-item>
  95. <el-descriptions-item label="User-Agent">{{ currentLog.userAgent || '-' }}</el-descriptions-item>
  96. <el-descriptions-item label="操作时间">{{ formatDate(currentLog.createdAt) }}</el-descriptions-item>
  97. </el-descriptions>
  98. </el-dialog>
  99. </div>
  100. </template>
  101. <script>
  102. import axios from '@/utils/request'
  103. import { getOperationLogs } from '@/api/operationLog'
  104. export default {
  105. name: 'OperationLogs',
  106. data() {
  107. return {
  108. loading: false,
  109. logs: [],
  110. filterForm: {
  111. userId: null,
  112. operationType: null
  113. },
  114. dateRange: null,
  115. pagination: {
  116. page: 1,
  117. size: 20,
  118. total: 0
  119. },
  120. detailVisible: false,
  121. currentLog: null
  122. }
  123. },
  124. mounted() {
  125. this.fetchLogs()
  126. },
  127. methods: {
  128. async fetchLogs() {
  129. this.loading = true
  130. try {
  131. const params = {
  132. page: this.pagination.page,
  133. size: this.pagination.size,
  134. userId: this.filterForm.userId || undefined,
  135. operationType: this.filterForm.operationType || undefined,
  136. startDate: this.dateRange ? this.dateRange[0] : undefined,
  137. endDate: this.dateRange ? this.dateRange[1] : undefined
  138. }
  139. const res = await getOperationLogs(params)
  140. if (res.code === 200) {
  141. this.logs = res.data.records || []
  142. this.pagination.total = res.data.total || 0
  143. }
  144. } catch (e) {
  145. this.$message.error('获取日志失败')
  146. } finally {
  147. this.loading = false
  148. }
  149. },
  150. handleQuery() {
  151. this.pagination.page = 1
  152. this.fetchLogs()
  153. },
  154. handleReset() {
  155. this.filterForm = { userId: null, operationType: null }
  156. this.dateRange = null
  157. this.pagination.page = 1
  158. this.fetchLogs()
  159. },
  160. handlePageChange(page) {
  161. this.pagination.page = page
  162. this.fetchLogs()
  163. },
  164. handleViewDetail(row) {
  165. this.currentLog = row
  166. this.detailVisible = true
  167. },
  168. getOperationTypeTag(type) {
  169. const tags = {
  170. login: 'success',
  171. logout: 'info',
  172. create: 'primary',
  173. update: 'warning',
  174. delete: 'danger',
  175. query: ''
  176. }
  177. return tags[type] || ''
  178. },
  179. formatDate(dateStr) {
  180. if (!dateStr) return '-'
  181. return new Date(dateStr).toLocaleString('zh-CN')
  182. }
  183. }
  184. }
  185. </script>
  186. <style scoped>
  187. .operation-logs {
  188. padding: 20px;
  189. }
  190. .filter-card {
  191. margin-bottom: 20px;
  192. }
  193. .filter-form {
  194. margin-bottom: 0;
  195. }
  196. .pagination {
  197. margin-top: 20px;
  198. text-align: right;
  199. }
  200. </style>