刘光辉
8 小时以前 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
<script lang="ts" setup>
import type { AuditEventDetail, 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, Empty, Result } from 'ant-design-vue';
 
import AuditDiffList from '#/components/FormExtraPanel/AuditDiffList.vue';
 
import { getAuditActionLabel } from './auditOperation';
import { auditOperationQuery } from './auditOperationQuery';
import { AUDIT_DETAIL_PERMISSION } from './types';
 
interface DiffLoadResult {
  detail: AuditEventDetail | null;
  error: unknown;
}
 
interface DrawerState {
  details: AuditEventDetail[];
  error: boolean;
  operation: AuditOperation | null;
  partialError: boolean;
}
 
const state = reactive<DrawerState>({
  details: [],
  error: false,
  operation: null,
  partialError: false,
});
const { error, operation, partialError } = toRefs(state);
const { createMessage } = useMessage();
const { hasBtnP } = usePermission();
const [registerDrawer, { changeLoading, closeDrawer }] = useDrawerInner(init);
let loadSequence = 0;
 
const drawerTitle = computed(() => {
  if (!state.operation) return '字段变更详情';
  return `字段变更 · ${state.operation.bizCode || getAuditActionLabel(state.operation)}`;
});
const displayedDiffs = computed(() => state.details[0]?.fieldDiffList ?? []);
const displayedParseError = computed(() => state.details[0]?.fieldDiffParseError ?? false);
 
async function init(data: { operation: AuditOperation }) {
  state.operation = data.operation;
  state.details = [];
  state.error = false;
  state.partialError = false;
  if (!hasBtnP(AUDIT_DETAIL_PERMISSION, false)) {
    closeDrawer();
    createMessage.warning('无权查看字段变更详情');
    return;
  }
  await loadDiffs();
}
 
async function loadDiffs() {
  if (!state.operation) return;
  const currentSequence = ++loadSequence;
  const eventIds = [state.operation.id];
  state.details = [];
  state.error = false;
  state.partialError = false;
  changeLoading(true);
  try {
    const results = await Promise.all(
      eventIds.map(async (eventId): Promise<DiffLoadResult> => {
        try {
          return { detail: await auditOperationQuery.detail(eventId), error: null };
        } catch (requestError) {
          return { detail: null, error: requestError };
        }
      }),
    );
    if (currentSequence !== loadSequence) return;
 
    if (results.some((result) => result.error && isForbiddenError(result.error))) {
      closeDrawer();
      createMessage.warning('审计详情权限已失效');
      return;
    }
 
    const failedCount = results.filter((result) => result.error).length;
    state.details = results
      .flatMap((result) => (result.detail ? [result.detail] : []))
      .filter((detail) => detail.fieldDiffList.length > 0 || detail.fieldDiffParseError);
    state.partialError = failedCount > 0 && state.details.length > 0;
    state.error = failedCount > 0 && state.details.length === 0;
  } finally {
    if (currentSequence === loadSequence) changeLoading(false);
  }
}
 
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);
}
</script>
 
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :title="drawerTitle"
    width="min(820px, calc(100vw - 16px))"
    class="audit-diff-drawer"
    :keyboard="true"
    destroy-on-close>
    <div class="audit-diff-drawer-body">
      <Result v-if="error" status="error" title="字段变更加载失败" sub-title="请稍后重试。">
        <template #extra>
          <a-button type="primary" @click="loadDiffs">重试</a-button>
        </template>
      </Result>
 
      <template v-else-if="operation">
        <a-alert
          v-if="partialError"
          class="audit-diff-warning"
          type="warning"
          show-icon
          message="部分关联事件加载失败"
          description="已展示成功加载的字段变更,可稍后重试查看完整内容。">
          <template #action>
            <a-button size="small" @click="loadDiffs">重试</a-button>
          </template>
        </a-alert>
 
        <section class="audit-diff-overview">
          <Descriptions bordered size="small" :column="{ xs: 1, sm: 1, md: 2 }">
            <DescriptionsItem label="操作">{{ getAuditActionLabel(operation) }}</DescriptionsItem>
            <DescriptionsItem label="操作时间">{{ displayTime(operation.eventTime) }}</DescriptionsItem>
            <DescriptionsItem label="操作人">{{ operation.operatorName || '未知用户' }}</DescriptionsItem>
            <DescriptionsItem label="业务单号">{{ operation.bizCode || '—' }}</DescriptionsItem>
          </Descriptions>
        </section>
 
        <section class="audit-diff-content">
          <h3>字段变更</h3>
          <div class="audit-diff-summary">
            本次操作共 <strong>{{ displayedDiffs.length }}</strong> 处字段变更
          </div>
 
          <Empty v-if="!displayedDiffs.length && !displayedParseError" description="字段明细为空或暂不可访问" :image="undefined" />
          <AuditDiffList
            v-else
            :diffs="displayedDiffs"
            hide-technical-field-names
            layout="table"
            mask-sensitive
            :parse-error="displayedParseError"
            :show-total="false" />
        </section>
      </template>
    </div>
  </BasicDrawer>
</template>
 
<style lang="scss" scoped>
.audit-diff-drawer-body {
  min-width: 0;
  padding: 0 20px 24px;
}
 
.audit-diff-warning {
  margin-top: 16px;
}
 
.audit-diff-overview,
.audit-diff-content {
  padding: 20px 0;
 
  :deep(.ant-descriptions-item-content) {
    min-width: 0;
    overflow-wrap: anywhere;
  }
}
 
.audit-diff-content {
  border-top: 1px solid var(--border-color-base1);
 
  h3 {
    margin: 0 0 8px;
    font-size: 15px;
    font-weight: 600;
  }
}
 
.audit-diff-summary {
  margin-bottom: 12px;
  color: var(--text-color-secondary);
 
  strong {
    color: var(--primary-color);
  }
}
 
@media (max-width: 640px) {
  .audit-diff-drawer-body {
    padding: 0 12px 16px;
  }
 
  .audit-diff-overview,
  .audit-diff-content {
    padding: 16px 0;
  }
}
</style>