刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<script lang="ts" setup>
import type { AuditEventId, AuditEventListDTO, AuditOperation } from './types';
 
import { computed, reactive, toRefs } from 'vue';
 
import { useMessage, usePermission } from '@jnpf/hooks';
import { BasicDrawer, useDrawerInner } from '@jnpf/ui/drawer';
import { formatToDateTime } from '@jnpf/utils';
 
import { Descriptions, DescriptionsItem, Result } from 'ant-design-vue';
 
import AuditDiffList from '#/components/FormExtraPanel/AuditDiffList.vue';
 
import { getAuditActionLabel, getAuditEventTypeLabel, getSourceLayerLabel } from './auditOperation';
import { auditOperationQuery } from './auditOperationQuery';
import { AUDIT_DETAIL_PERMISSION } from './types';
 
type DetailError = '' | 'load' | 'not-found';
 
interface DrawerState {
  detail: Awaited<ReturnType<typeof auditOperationQuery.detail>> | null;
  error: DetailError;
  operation: AuditOperation | null;
  selectedEventId: AuditEventId | null;
  techOpenKeys: string[];
}
 
const state = reactive<DrawerState>({
  detail: null,
  error: '',
  operation: null,
  selectedEventId: null,
  techOpenKeys: ['technical'],
});
const { detail, error, selectedEventId, techOpenKeys } = toRefs(state);
const { createMessage } = useMessage();
const { hasBtnP } = usePermission();
const [registerDrawer, { changeLoading, closeDrawer }] = useDrawerInner(init);
 
const relatedEvents = computed<AuditEventListDTO[]>(() => {
  return state.operation ? [state.operation, ...state.operation.subEvents] : [];
});
 
const relatedEventColumns = [
  { key: 'action', title: '动作', width: 180 },
  { key: 'source', title: '来源', width: 110 },
  { key: 'eventTime', title: '操作时间', width: 170 },
  { key: 'diffCount', title: '变更数', width: 90 },
  { key: 'view', title: '查看', width: 100 },
];
 
async function init(data: { operation: AuditOperation }) {
  state.operation = data.operation;
  state.detail = null;
  state.error = '';
  state.techOpenKeys = ['technical'];
  if (!hasBtnP(AUDIT_DETAIL_PERMISSION, false)) {
    closeDrawer();
    createMessage.warning('无权查看审计详情');
    return;
  }
  await loadEvent(data.operation.id);
}
 
async function loadEvent(eventId: AuditEventId) {
  state.selectedEventId = eventId;
  state.error = '';
  changeLoading(true);
  try {
    state.detail = await auditOperationQuery.detail(eventId);
  } catch (requestError) {
    state.detail = null;
    if (isForbiddenError(requestError)) {
      closeDrawer();
      createMessage.warning('审计详情权限已失效');
      return;
    }
    state.error = isNotFoundError(requestError) ? 'not-found' : 'load';
  } finally {
    changeLoading(false);
  }
}
 
function isSelected(eventId: AuditEventId) {
  return String(eventId) === String(state.selectedEventId);
}
 
function getRelatedRowClassName(record: AuditEventListDTO) {
  return isSelected(record.id) ? 'is-selected' : '';
}
 
function displayText(value: unknown, fallback = '—') {
  if (value === undefined || value === null || value === '') return fallback;
  return String(value);
}
 
function displayTime(value?: string) {
  return value ? formatToDateTime(value, 'YYYY-MM-DD HH:mm:ss') : '—';
}
 
function isForbiddenError(errorValue: unknown) {
  const message = errorValue instanceof Error ? errorValue.message : '';
  return /403|forbidden|无权|权限|拒绝/i.test(message);
}
 
function isNotFoundError(errorValue: unknown) {
  const message = errorValue instanceof Error ? errorValue.message : '';
  return /404|not found|不存在|不可访问/i.test(message);
}
</script>
 
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    title="审计详情"
    width="min(820px, calc(100vw - 16px))"
    class="audit-detail-drawer"
    :keyboard="true"
    destroy-on-close>
    <div class="audit-drawer-body">
      <Result v-if="error === 'not-found'" status="404" title="审计事件不存在或已不可访问">
        <template #extra>
          <a-button @click="closeDrawer">关闭</a-button>
        </template>
      </Result>
      <Result v-else-if="error === 'load'" status="error" title="审计详情加载失败" sub-title="请稍后重试,列表筛选条件不会丢失。">
        <template #extra>
          <a-button type="primary" @click="selectedEventId !== null && loadEvent(selectedEventId)">重试</a-button>
        </template>
      </Result>
 
      <template v-else-if="detail">
        <section class="audit-section">
          <h3>操作概览</h3>
          <Descriptions class="audit-descriptions" bordered size="small" :column="{ xs: 1, sm: 1, md: 2 }">
            <DescriptionsItem label="动作">{{ getAuditActionLabel(detail) }}</DescriptionsItem>
            <DescriptionsItem label="事件类型">{{ getAuditEventTypeLabel(detail.eventType) }}</DescriptionsItem>
            <DescriptionsItem label="操作人">{{ displayText(detail.operatorName, '未知用户') }}</DescriptionsItem>
            <DescriptionsItem label="操作时间">{{ displayTime(detail.eventTime) }}</DescriptionsItem>
            <DescriptionsItem label="业务模块">{{ displayText(detail.bizModule) }}</DescriptionsItem>
            <DescriptionsItem label="业务类型">{{ displayText(detail.bizType) }}</DescriptionsItem>
            <DescriptionsItem label="业务单号" :span="2">{{ displayText(detail.bizCode) }}</DescriptionsItem>
            <DescriptionsItem label="操作原因" :span="2">{{ displayText(detail.reason) }}</DescriptionsItem>
          </Descriptions>
        </section>
 
        <section class="audit-section">
          <h3>字段变更</h3>
          <AuditDiffList :diffs="detail.fieldDiffList" hide-technical-field-names layout="table" mask-sensitive :parse-error="detail.fieldDiffParseError" />
        </section>
 
        <section class="audit-section">
          <h3>本次操作事件({{ relatedEvents.length }})</h3>
          <a-table
            class="audit-related-table"
            size="small"
            :columns="relatedEventColumns"
            :data-source="relatedEvents"
            :pagination="false"
            :row-class-name="getRelatedRowClassName"
            :scroll="{ x: 650 }"
            row-key="id"
            table-layout="fixed">
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'action'">
                <span class="audit-related-action">{{ getAuditActionLabel(record) }}</span>
              </template>
              <template v-else-if="column.key === 'source'">
                <a-tag :bordered="false">{{ getSourceLayerLabel(record.sourceLayer) }}</a-tag>
              </template>
              <template v-else-if="column.key === 'eventTime'">
                {{ displayTime(record.eventTime) }}
              </template>
              <template v-else-if="column.key === 'diffCount'"> {{ record.diffCount || 0 }} 处 </template>
              <template v-else-if="column.key === 'view'">
                <a-button type="link" size="small" :disabled="isSelected(record.id)" @click="loadEvent(record.id)">
                  {{ isSelected(record.id) ? '当前事件' : '查看' }}
                </a-button>
              </template>
            </template>
          </a-table>
        </section>
 
        <section class="audit-section audit-section-last">
          <a-collapse v-model:active-key="techOpenKeys" ghost expand-icon-position="end">
            <a-collapse-panel key="technical" header="技术信息">
              <Descriptions class="audit-descriptions audit-technical-descriptions" bordered size="small" :column="1">
                <DescriptionsItem label="IP 地址">{{ displayText(detail.ip) }}</DescriptionsItem>
                <DescriptionsItem label="IP 地区">{{ displayText(detail.ipRegion) }}</DescriptionsItem>
                <DescriptionsItem label="浏览器">{{ displayText(detail.browser) }}</DescriptionsItem>
                <DescriptionsItem label="操作系统">{{ displayText(detail.os) }}</DescriptionsItem>
                <DescriptionsItem label="请求路径">{{ displayText(detail.requestUri) }}</DescriptionsItem>
                <DescriptionsItem label="目标表">{{ displayText(detail.targetTable) }}</DescriptionsItem>
                <DescriptionsItem label="目标 ID">{{ displayText(detail.targetId) }}</DescriptionsItem>
                <DescriptionsItem label="技术服务">{{ displayText(detail.appName) }}</DescriptionsItem>
                <DescriptionsItem label="事件 ID">{{ detail.id }}</DescriptionsItem>
                <DescriptionsItem label="操作关联 ID">{{ displayText(detail.operationId) }}</DescriptionsItem>
                <DescriptionsItem label="客户端事件 ID">{{ displayText(detail.clientEventId) }}</DescriptionsItem>
              </Descriptions>
            </a-collapse-panel>
          </a-collapse>
        </section>
      </template>
    </div>
  </BasicDrawer>
</template>
 
<style lang="scss" scoped>
.audit-drawer-body {
  min-width: 0;
  padding: 0 20px 24px;
}
 
.audit-section {
  padding: 20px 0;
  border-bottom: 1px solid var(--border-color-base1);
 
  h3 {
    margin: 0 0 14px;
    font-size: 15px;
    font-weight: 600;
  }
 
  :deep(.ant-descriptions-item-content),
  :deep(.ant-list-item-meta-description) {
    min-width: 0;
    overflow-wrap: anywhere;
  }
}
 
.audit-section-last {
  padding-top: 8px;
  border-bottom: 0;
 
  :deep(.ant-collapse-header) {
    padding-inline: 0;
    font-weight: 600;
  }
 
  :deep(.ant-collapse-content-box) {
    padding-inline: 0;
  }
}
 
.audit-descriptions {
  :deep(.ant-descriptions-item-label) {
    width: 104px;
    white-space: nowrap;
  }
 
  :deep(.ant-descriptions-item-content) {
    word-break: break-word;
  }
}
 
.audit-technical-descriptions {
  :deep(.ant-descriptions-item-label) {
    width: 120px;
  }
}
 
.audit-related-table {
  :deep(.ant-table-cell) {
    vertical-align: top;
    overflow-wrap: anywhere;
  }
 
  :deep(.ant-btn-link) {
    height: auto;
    padding: 0;
  }
}
 
.audit-related-action {
  font-weight: 500;
}
 
.audit-related-table :deep(.is-selected > td) {
  background: rgb(22 119 255 / 6%);
}
 
@media (max-width: 640px) {
  .audit-drawer-body {
    padding: 0 12px 16px;
  }
 
  .audit-section {
    padding: 16px 0;
  }
}
</style>