刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<script lang="ts" setup>
import type { AuditEventId, AuditEventListDTO, AuditOperation, AuditOperationQuery } 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 { formatFormLabel } from '#/components/FormGenerator/src/helper/formLabel';
 
import { getAuditActionLabel, getAuditEventTypeLabel, getSourceLayerLabel, parsePrintAuditTargets } from './auditOperation';
import { auditOperationQuery } from './auditOperationQuery';
import { AUDIT_DETAIL_PERMISSION } from './types';
 
const props = withDefaults(
  defineProps<{
    detailLoader?: AuditOperationQuery['detail'];
    detailPermission?: string;
    showFieldDiffs?: boolean;
    showRelatedEvents?: boolean;
  }>(),
  {
    detailPermission: AUDIT_DETAIL_PERMISSION,
    showFieldDiffs: true,
    showRelatedEvents: true,
  },
);
 
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 isPrintEvent = computed(() => detail.value?.eventType === 'PRINT');
const printTargetResult = computed(() => parsePrintAuditTargets(isPrintEvent.value ? detail.value?.extra : undefined));
const printTargets = computed(() => printTargetResult.value.targets);
const dmsWorkflow = computed(() => {
  const event = detail.value;
  if (!event?.extra || !['dms_wenjian_xinzeng_shenhe', 'dms_wenjian_xiuding_shenhe'].includes(event.targetTable || '')) return null;
  try {
    const context = JSON.parse(event.extra)?.workflow;
    if (!context || typeof context.taskId !== 'string' || context.taskId !== event.targetId || typeof context.operatorId !== 'string') return null;
    return {
      taskId: context.taskId as string,
      operatorId: context.operatorId as string,
      nodeName: typeof context.nodeName === 'string' ? context.nodeName : '',
    };
  } catch {
    return null;
  }
});
 
const printTargetColumns = [
  { key: 'index', title: '序号', width: 70 },
  { dataIndex: 'bizCode', key: 'bizCode', title: '业务编号', width: 180 },
  { dataIndex: 'title', key: 'title', title: '标题', width: 260 },
  { dataIndex: 'id', key: 'id', title: '目标主键', width: 200 },
];
 
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(props.detailPermission, false)) {
    closeDrawer();
    createMessage.warning('无权查看审计详情');
    return;
  }
  await loadEvent(data.operation.id);
}
 
async function loadEvent(eventId: AuditEventId) {
  state.selectedEventId = eventId;
  state.error = '';
  changeLoading(true);
  try {
    const detailLoader = props.detailLoader || auditOperationQuery.detail;
    state.detail = await detailLoader(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="业务模块">
              <span class="audit-detail-name">{{ formatFormLabel(displayText(detail.bizModule)) }}</span>
            </DescriptionsItem>
            <DescriptionsItem label="父业务模块">
              <span class="audit-detail-name">{{ displayText(detail.parentBizModule) }}</span>
            </DescriptionsItem>
            <DescriptionsItem label="业务类型">
              <span class="audit-detail-name">{{ formatFormLabel(displayText(detail.bizType)) }}</span>
            </DescriptionsItem>
            <DescriptionsItem label="业务单号" :span="2">{{ displayText(detail.bizCode) }}</DescriptionsItem>
            <DescriptionsItem label="操作原因" :span="2">{{ displayText(detail.reason) }}</DescriptionsItem>
            <DescriptionsItem v-if="dmsWorkflow" label="审批环节" :span="2">{{ displayText(dmsWorkflow.nodeName) }}</DescriptionsItem>
          </Descriptions>
        </section>
 
        <section v-if="isPrintEvent" class="audit-section">
          <h3>打印数据({{ printTargets.length }})</h3>
          <Result v-if="printTargetResult.parseError" status="warning" title="打印明细解析失败" sub-title="审计事件主信息仍然有效。" />
          <a-table
            v-else
            class="audit-print-target-table"
            size="small"
            :columns="printTargetColumns"
            :data-source="printTargets"
            :pagination="false"
            :scroll="{ x: 710 }"
            row-key="id"
            table-layout="fixed">
            <template #bodyCell="{ column, record, index }">
              <template v-if="column.key === 'index'">{{ index + 1 }}</template>
              <template v-else-if="column.key === 'bizCode'">{{ displayText(record.bizCode) }}</template>
              <template v-else-if="column.key === 'title'">
                <span class="audit-detail-name">{{ formatFormLabel(displayText(record.title)) }}</span>
              </template>
            </template>
          </a-table>
        </section>
 
        <section v-if="props.showFieldDiffs && !isPrintEvent" class="audit-section">
          <h3>字段变更</h3>
          <AuditDiffList :diffs="detail.fieldDiffList" hide-technical-field-names layout="table" mask-sensitive :parse-error="detail.fieldDiffParseError" />
        </section>
 
        <section v-if="props.showRelatedEvents" 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, record.eventType) }}</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 v-if="dmsWorkflow" label="流程实例 ID">{{ dmsWorkflow.taskId }}</DescriptionsItem>
                <DescriptionsItem v-if="dmsWorkflow" label="办理标识">{{ dmsWorkflow.operatorId }}</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-detail-name {
  overflow-wrap: anywhere;
  white-space: pre-line;
}
 
.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-print-target-table {
  :deep(.ant-table-cell) {
    vertical-align: top;
    overflow-wrap: anywhere;
  }
}
 
.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>