OperationLogs.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div class="operation-logs admin-page">
  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 :max-height="tableHeight" :data="logs" v-loading="loading" stripe>
  38. <el-table-column prop="id" label="ID" width="80"></el-table-column>
  39. <el-table-column prop="nickname" label="用户名" width="120"></el-table-column>
  40. <el-table-column prop="operationType" label="操作类型" width="100">
  41. <template slot-scope="scope">
  42. <el-tag :type="getOperationTypeTag(scope.row.operationType)" size="small">
  43. {{ scope.row.operationType }}
  44. </el-tag>
  45. </template>
  46. </el-table-column>
  47. <el-table-column prop="description" label="操作描述" min-width="200"></el-table-column>
  48. <el-table-column prop="requestPath" label="请求路径" min-width="180"></el-table-column>
  49. <el-table-column prop="requestMethod" label="方法" width="80"></el-table-column>
  50. <el-table-column prop="responseStatus" label="状态" width="80">
  51. <template slot-scope="scope">
  52. <el-tag :type="scope.row.responseStatus === 200 ? 'success' : 'danger'" size="small">
  53. {{ scope.row.responseStatus }}
  54. </el-tag>
  55. </template>
  56. </el-table-column>
  57. <el-table-column prop="clientIp" label="IP地址" width="130"></el-table-column>
  58. <el-table-column prop="createdAt" label="操作时间" width="160">
  59. <template slot-scope="scope">
  60. {{ formatDate(scope.row.createdAt) }}
  61. </template>
  62. </el-table-column>
  63. <el-table-column label="操作" width="80" fixed="right">
  64. <template slot-scope="{ row }">
  65. <el-button size="mini" @click="handleViewDetail(row)">详情</el-button>
  66. </template>
  67. </el-table-column>
  68. </el-table>
  69. <!-- 分页 -->
  70. <el-pagination
  71. @current-change="handlePageChange"
  72. :current-page="pagination.page"
  73. :page-size="pagination.size"
  74. :total="pagination.total"
  75. layout="total, prev, pager, next"
  76. class="pagination">
  77. </el-pagination>
  78. </el-card>
  79. <!-- 详情对话框 -->
  80. <el-dialog title="操作日志详情" :visible.sync="detailVisible" width="600px">
  81. <el-descriptions :column="1" border v-if="currentLog">
  82. <el-descriptions-item label="ID">{{ currentLog.id }}</el-descriptions-item>
  83. <el-descriptions-item label="用户名">{{ currentLog.nickname || currentLog.userName || '-' }}</el-descriptions-item>
  84. <el-descriptions-item label="操作类型">{{ currentLog.operationType }}</el-descriptions-item>
  85. <el-descriptions-item label="操作描述">{{ currentLog.description }}</el-descriptions-item>
  86. <el-descriptions-item label="请求路径">{{ currentLog.requestPath }}</el-descriptions-item>
  87. <el-descriptions-item label="请求方法">{{ currentLog.requestMethod }}</el-descriptions-item>
  88. <el-descriptions-item label="请求参数">{{ currentLog.requestParams || '-' }}</el-descriptions-item>
  89. <el-descriptions-item label="响应状态">{{ currentLog.responseStatus }}</el-descriptions-item>
  90. <el-descriptions-item label="客户端IP">{{ currentLog.clientIp }}</el-descriptions-item>
  91. <el-descriptions-item label="User-Agent">{{ currentLog.userAgent || '-' }}</el-descriptions-item>
  92. <el-descriptions-item label="操作时间">{{ formatDate(currentLog.createdAt) }}</el-descriptions-item>
  93. </el-descriptions>
  94. </el-dialog>
  95. </div>
  96. </template>
  97. <script>
  98. import axios from '@/utils/request'
  99. import { getOperationLogs } from '@/api/operationLog'
  100. export default {
  101. name: 'OperationLogs',
  102. computed: {
  103. tableHeight() {
  104. return window.innerHeight - 340
  105. }
  106. },
  107. data() {
  108. return {
  109. loading: false,
  110. logs: [],
  111. filterForm: {
  112. userId: null,
  113. operationType: null
  114. },
  115. dateRange: null,
  116. pagination: {
  117. page: 1,
  118. size: 20,
  119. total: 0
  120. },
  121. detailVisible: false,
  122. currentLog: null
  123. }
  124. },
  125. mounted() {
  126. this.fetchLogs()
  127. },
  128. methods: {
  129. async fetchLogs() {
  130. this.loading = true
  131. try {
  132. const params = {
  133. page: this.pagination.page,
  134. size: this.pagination.size,
  135. userId: this.filterForm.userId || undefined,
  136. operationType: this.filterForm.operationType || undefined,
  137. startDate: this.dateRange ? this.dateRange[0] : undefined,
  138. endDate: this.dateRange ? this.dateRange[1] : undefined
  139. }
  140. const res = await getOperationLogs(params)
  141. if (res.code === 200) {
  142. this.logs = res.data.records || []
  143. this.pagination.total = res.data.total || 0
  144. }
  145. } catch (e) {
  146. this.$message.error('获取日志失败')
  147. } finally {
  148. this.loading = false
  149. }
  150. },
  151. handleQuery() {
  152. this.pagination.page = 1
  153. this.fetchLogs()
  154. },
  155. handleReset() {
  156. this.filterForm = { userId: null, operationType: null }
  157. this.dateRange = null
  158. this.pagination.page = 1
  159. this.fetchLogs()
  160. },
  161. handlePageChange(page) {
  162. this.pagination.page = page
  163. this.fetchLogs()
  164. },
  165. handleViewDetail(row) {
  166. this.currentLog = row
  167. this.detailVisible = true
  168. },
  169. getOperationTypeTag(type) {
  170. const tags = {
  171. login: 'success',
  172. logout: 'info',
  173. create: 'primary',
  174. update: 'warning',
  175. delete: 'danger',
  176. query: ''
  177. }
  178. return tags[type] || ''
  179. },
  180. formatDate(dateStr) {
  181. if (!dateStr) return '-'
  182. return new Date(dateStr).toLocaleString('zh-CN')
  183. }
  184. }
  185. }
  186. </script>
  187. <style scoped>
  188. .operation-logs {
  189. padding: 20px;
  190. }
  191. .filter-card {
  192. margin-bottom: 20px;
  193. }
  194. .filter-form {
  195. margin-bottom: 0;
  196. }
  197. .pagination {
  198. margin-top: 20px;
  199. text-align: right;
  200. }
  201. </style>