OperationLogs.vue 11 KB

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