zhangwencui
7 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
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
<script lang="ts" setup>
import type { MesPdDocumentApi } from '#/api/mes/pd/document';
 
import { markRaw, ref } from 'vue';
 
import { useVbenModal } from '#/packages/effects/common-ui/src';
import { MesPdDocumentTypeEnum } from '#/packages/constants/src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createPdDocument,
  getPdDocument,
  updatePdDocument,
} from '#/api/mes/pd/document';
import { FileUpload } from '#/components/upload';
import { $t } from '#/locales';
 
import { getPdDocumentTypeOptions } from '../data';
 
const emit = defineEmits(['success']);
 
const isUpdate = ref(false);
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    formItemClass: 'col-span-1',
    labelWidth: 100,
  },
  layout: 'horizontal',
  wrapperClass: 'grid-cols-1',
  schema: [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'projectId',
      component: 'Input',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'documentType',
      label: '资料类型',
      component: 'Select',
      componentProps: {
        options: getPdDocumentTypeOptions(),
        placeholder: '请选择资料类型',
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'documentName',
      label: '资料名称',
      component: 'Input',
      componentProps: { placeholder: '请输入资料名称' },
      rules: 'required',
    },
    {
      fieldName: 'documentVersion',
      label: '版本号',
      component: 'Input',
      componentProps: { placeholder: '如 V1.0' },
      rules: 'required',
    },
    {
      fieldName: 'fileUrl',
      label: '文件',
      component: markRaw(FileUpload),
      componentProps: {
        directory: 'mes/pd',
        maxNumber: 1,
        onChange: async (url: string) => {
          if (url) {
            await formApi?.setFieldValue('fileUrl', url);
          }
        },
      },
    },
    {
      fieldName: 'fileName',
      label: '文件名',
      component: 'Input',
      componentProps: { placeholder: '上传后自动填入,也可手动输入' },
    },
  ],
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    const data = (await formApi.getValues()) as MesPdDocumentApi.Document;
    try {
      if (isUpdate.value) {
        await updatePdDocument(data);
      } else {
        await createPdDocument(data);
      }
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
      await modalApi.close();
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    const data = modalApi.getData<{
      id?: number;
      projectId: number;
      documentType?: string;
    }>();
    isUpdate.value = !!data.id;
    modalApi.setState({ title: isUpdate.value ? '编辑资料' : '上传资料' });
    if (data.id) {
      modalApi.lock();
      try {
        const detail = await getPdDocument(data.id);
        await formApi.setValues(detail);
      } finally {
        modalApi.unlock();
      }
      return;
    }
    await formApi.setValues({
      projectId: data.projectId,
      documentType: data.documentType ?? MesPdDocumentTypeEnum.SCHEME,
    });
  },
});
</script>
 
<template>
  <Modal class="w-[720px]">
    <Form class="mx-4" />
  </Modal>
</template>