liu
2026-09-29 982c97ab82f3beb1b4febcf014562eda34b9fc05
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
<script lang="ts" setup>
import type { ArchiveDocumentApi } from '#/api/archive/document';
 
import type { FormType } from '../data';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createDocument,
  getDocument,
  updateDocument,
} from '#/api/archive/document';
 
import { useFormSchema } from '../data';
 
defineOptions({ name: 'ArchiveDocumentForm' });
 
const emit = defineEmits(['success']);
 
const formType = ref<FormType>('create');
 
const getTitle = computed(() => {
  if (formType.value === 'create') {
    return $t('ui.actionTitle.create', ['档案']);
  }
  if (formType.value === 'edit') {
    return $t('ui.actionTitle.edit', ['档案']);
  }
  return '档案详情';
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 120,
  },
  wrapperClass: 'grid-cols-2',
  layout: 'vertical',
  schema: [],
  showDefaultActions: false,
});
 
/** 只提交后端 SaveReqVO 接受的字段,避免回传 no/status/source* 等只读字段 */
async function buildSubmitData() {
  const values = (await formApi.getValues()) as ArchiveDocumentApi.Document;
  return {
    id: values.id,
    title: values.title,
    categoryId: values.categoryId,
    secretLevel: values.secretLevel,
    carrierType: values.carrierType,
    shelfNo: values.shelfNo,
    layerNo: values.layerNo,
    columnNo: values.columnNo,
    boxNo: values.boxNo,
    storePlace: values.storePlace,
    archiveDate: values.archiveDate,
    summary: values.summary,
    tags: values.tags,
    remark: values.remark,
    // 附件为全量替换语义:清空全部附件时必须显式传空数组
    blobIds: values.blobIds ?? [],
  } as ArchiveDocumentApi.Document;
}
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    const data = await buildSubmitData();
    try {
      await (formType.value === 'create'
        ? createDocument(data)
        : updateDocument(data));
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    const data = modalApi.getData<{ formType: FormType; id?: number }>();
    formType.value = data.formType;
    formApi.setState({ schema: useFormSchema(formType.value) });
    formApi.setDisabled(formType.value === 'detail');
    if (data?.id) {
      modalApi.lock();
      try {
        const detail = await getDocument(data.id);
        // 已有附件转成上传组件可识别的文件对象,提交时按 valueKey=id 提取为 blobId
        const blobIds = (detail.attachmentList ?? []).map((item) => ({
          uid: String(item.id),
          name: item.name || '',
          url: item.url || '',
          status: 'done',
          id: item.id,
        }));
        await formApi.setValues({ ...detail, blobIds }, false);
      } finally {
        modalApi.unlock();
      }
    } else {
      await formApi.resetForm();
    }
  },
});
</script>
 
<template>
  <Modal
    :title="getTitle"
    class="w-3/4"
    :close-on-click-modal="false"
    :show-confirm-button="formType !== 'detail'"
  >
    <Form class="mx-3" />
  </Modal>
</template>