SealCreateDrawer.vue
18.9 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
:title="isEdit ? '编辑用印申请' : '新增用印申请'"
width="600px"
:showFooter="true"
@ok="handleSubmit"
@close="handleCancel"
>
<BasicForm @register="registerForm" />
<!-- 文件上传区域 -->
<div class="file-upload-section">
<div class="upload-title">用印文件 <span style="color: red;">*</span></div>
<div class="file-list">
<!-- 已上传文件列表 -->
<div class="file-item" v-for="(file, index) in uploadedFiles" :key="index">
<div class="file-info">
<div class="file-name">{{ file.name }}</div>
</div>
<div class="file-actions">
<a @click="previewFile(file)" class="action-btn">预览</a>
<a @click="downloadFile(file)" class="action-btn">下载</a>
<a-popconfirm title="确定删除吗?" ok-text="是" cancel-text="否" @confirm="removeFile(index)">
<a class="action-btn delete">删除</a>
</a-popconfirm>
</div>
</div>
<!-- 上传按钮 -->
<div class="file-upload-button" @click="handleFileUpload">
<div class="upload-button-text">
<PlusOutlined />
<div>{{ uploadedFiles.length === 0 ? '上传文件' : '继续上传' }}</div>
<div class="upload-hint">支持Word、Excel、PDF格式,可上传多个文件</div>
</div>
</div>
</div>
<input
type="file"
ref="fileInput"
accept=".docx,.xls,.xlsx,.pdf"
multiple
style="display: none;"
@change="uploadSelectedFiles"
/>
</div>
<!-- 自定义底部按钮 -->
<template #footer>
<a-button @click="handleCancel">取消</a-button>
<a-button type="primary" @click="handleSubmit" :loading="submitLoading">
{{ isEdit ? '确认修改' : '确认保存' }}
</a-button>
</template>
</BasicDrawer>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { BasicForm, useForm, FormSchema } from '/@/components/Form';
import { useMessage } from '/@/hooks/web/useMessage';
import { PlusOutlined } from '@ant-design/icons-vue';
import {
getFileTypeOptions,
getSealTypeOptions,
getCompanyOptions,
getDeptOptions,
createSealApplication,
updateSealApplication
} from '/@/api/project/seal';
const emit = defineEmits(['success', 'register']);
const { createMessage } = useMessage();
// 编辑模式相关
const isEdit = ref(false);
const editId = ref<string | number>('');
// 文件上传相关
const fileInput = ref<HTMLInputElement>();
const uploadedFiles = ref<Array<{ name: string; fileUrl: string }>>([]);
const isFileUploading = ref(false);
const submitLoading = ref(false);
const uploadUrl = 'http://47.104.8.35:80/api/localStorage/upload_file_oss?name=';
// 表单配置
const formSchema: FormSchema[] = [
{
field: 'fileName',
label: '用印文件名称',
component: 'Input',
required: true,
colProps: { span: 24 },
},
{
field: 'fileType',
label: '文件分类',
component: 'Select',
required: true,
componentProps: {
options: [],
placeholder: '请选择文件分类',
},
colProps: { span: 24 },
},
{
field: 'signType',
label: '签署方式',
component: 'Select',
required: true,
componentProps: {
options: [
{ label: '电子', value: '电子' },
{ label: '纸质', value: '纸质' },
],
placeholder: '请选择签署方式',
},
colProps: { span: 24 },
},
{
field: 'count',
label: '文件份数',
component: 'InputNumber',
required: true,
componentProps: {
min: 1,
placeholder: '请输入文件份数',
},
colProps: { span: 24 },
},
{
field: 'sealTypes',
label: '印章类型',
component: 'Select',
required: true,
componentProps: {
mode: 'multiple',
options: [],
placeholder: '请选择印章类型(可多选)',
},
colProps: { span: 24 },
},
{
field: 'company',
label: '印章所属主体',
component: 'Select',
required: true,
componentProps: {
options: [],
placeholder: '请选择印章所属主体',
},
colProps: { span: 24 },
},
{
field: 'dept',
label: '用印部门',
component: 'Select',
required: true,
componentProps: {
options: [],
placeholder: '请选择用印部门',
},
colProps: { span: 24 },
},
{
field: 'note',
label: '备注',
component: 'InputTextArea',
componentProps: {
rows: 3,
placeholder: '请输入备注信息',
},
colProps: { span: 24 },
},
];
const [registerForm, { validate, resetFields, updateSchema, setFieldsValue }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const [registerDrawer, { closeDrawer }] = useDrawerInner(async (data) => {
// 重置表单
resetFields();
uploadedFiles.value = [];
// 判断是否为编辑模式
isEdit.value = !!data?.record;
// 先加载选项数据
await loadOptions();
if (isEdit.value && data.record) {
editId.value = data.record.id;
console.log('编辑模式 - 原始数据:', data.record);
// 回显表单数据
const formData = {
fileName: data.record.fileName || '',
fileType: data.record.fileTypeId || data.record.fileType || undefined,
signType: data.record.signType || '',
count: data.record.count || 1,
sealTypes: (() => {
// 处理 sealTypesId 字符串转换为数字数组
if (data.record.sealTypesId && typeof data.record.sealTypesId === 'string') {
// 将 "217,218" 转换为 [217, 218]
return data.record.sealTypesId.split(',').map(id => parseInt(id.trim())).filter(id => !isNaN(id));
} else if (Array.isArray(data.record.sealTypesId)) {
// 如果已经是数组,确保元素是数字类型
return data.record.sealTypesId.map(id => typeof id === 'string' ? parseInt(id) : id);
} else if (Array.isArray(data.record.sealTypes)) {
// 兼容旧的字段名
return data.record.sealTypes;
}
return [];
})(),
company: data.record.companyId || data.record.company || undefined,
dept: data.record.deptId || data.record.dept || undefined,
note: data.record.note || '',
};
console.log('准备回显的表单数据:', formData);
console.log('sealTypesId 原始值:', data.record.sealTypesId);
console.log('转换后的 sealTypes:', formData.sealTypes);
// 等待一下确保DOM更新
setTimeout(() => {
setFieldsValue(formData);
console.log('表单数据已设置');
}, 100);
// 回显文件数据
if (data.record.files && Array.isArray(data.record.files) && data.record.files.length > 0) {
uploadedFiles.value = data.record.files.map((fileUrl: string, index: number) => {
// 从URL中提取文件名
let fileName = '';
try {
// 尝试从URL中解析文件名
const urlObj = new URL(fileUrl);
const pathParts = urlObj.pathname.split('/');
const lastPart = pathParts[pathParts.length - 1];
// 如果URL包含编码的文件名,尝试解码
if (lastPart.includes('-')) {
const fileNamePart = lastPart.split('-').slice(1).join('-');
fileName = decodeURIComponent(fileNamePart);
} else {
fileName = decodeURIComponent(lastPart);
}
// 如果解析失败,使用默认名称
if (!fileName || fileName.length < 3) {
fileName = `${data.record.fileName || '用印文件'}_${index + 1}`;
}
} catch (error) {
// URL解析失败,使用默认名称
fileName = `${data.record.fileName || '用印文件'}_${index + 1}`;
}
return {
name: fileName,
fileUrl: fileUrl,
};
});
console.log('文件数据已回显:', uploadedFiles.value);
} else if (data.record.fileUrl) {
// 兼容单个文件字段的情况
uploadedFiles.value = [{
name: data.record.fileName || '文件',
fileUrl: data.record.fileUrl.startsWith('http') ? data.record.fileUrl : `http://47.104.8.35:80${data.record.fileUrl}`,
}];
console.log('单个文件数据已回显:', uploadedFiles.value);
} else {
console.log('没有找到文件数据');
}
} else {
editId.value = '';
console.log('新建模式');
}
});
// 加载所有选项数据
const loadOptions = async () => {
try {
// 加载文件分类选项
const fileTypeOptions = await getFileTypeOptions();
updateSchema({
field: 'fileType',
componentProps: { options: fileTypeOptions },
});
// 加载印章类型选项
const sealTypeOptions = await getSealTypeOptions();
updateSchema({
field: 'sealTypes',
componentProps: { options: sealTypeOptions },
});
// 加载公司选项
const companyOptions = await getCompanyOptions();
updateSchema({
field: 'company',
componentProps: { options: companyOptions },
});
// 加载部门选项
const deptOptions = await getDeptOptions();
updateSchema({
field: 'dept',
componentProps: { options: deptOptions },
});
} catch (error) {
console.error('加载选项数据失败:', error);
createMessage.error('加载选项数据失败');
}
};
// 文件上传处理
const handleFileUpload = () => {
if (isFileUploading.value) return;
fileInput.value?.click();
};
const uploadSelectedFiles = async (e: Event) => {
const target = e.target as HTMLInputElement;
if (!target.files || target.files.length === 0) return;
const files = Array.from(target.files);
let uploadingCount = 0; // 记录正在上传的文件数量
for (const file of files) {
// 验证文件类型
const allowedTypes = ['.doc', '.docx', '.xls', '.xlsx', '.pdf'];
const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase();
if (!allowedTypes.includes(fileExtension)) {
createMessage.error(`${file.name}: 只支持Word、Excel、PDF格式的文件`);
continue;
}
// 验证文件大小 (10MB)
if (file.size > 10 * 1024 * 1024) {
createMessage.error(`${file.name}: 文件大小不能超过10MB`);
continue;
}
// 检查是否已存在同名文件
if (uploadedFiles.value.some(f => f.name === file.name)) {
createMessage.warning(`${file.name}: 文件已存在`);
continue;
}
uploadingCount++; // 增加上传中的文件计数
isFileUploading.value = true; // 设置全局上传状态
// 显示上传开始消息
const uploadingMessage = createMessage.loading(`正在上传: ${file.name}...`, 0); // 0表示不自动关闭
try {
const formData = new FormData();
formData.append('file', file);
const response = await fetch(uploadUrl + file.name, {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`上传失败: ${response.statusText}`);
}
const data = await response.json();
if (data.data && data.data.fileUrl) {
const fileUrl = data.data.fileUrl;
// 确保URL是完整路径
const fullUrl = fileUrl.startsWith('http') ? fileUrl : `http://47.104.8.35:80${fileUrl}`;
uploadedFiles.value.push({
name: file.name,
fileUrl: fullUrl,
});
// 关闭上传中消息,显示成功消息
uploadingMessage();
createMessage.success(`${file.name} 上传成功`);
} else {
throw new Error('无法获取文件URL');
}
} catch (error) {
// 关闭上传中消息,显示错误消息
uploadingMessage();
createMessage.error(`${file.name} 上传失败: ${error.message || '未知错误'}`);
} finally {
uploadingCount--; // 减少上传中的文件计数
// 如果所有文件都上传完成,清除全局上传状态
if (uploadingCount === 0) {
isFileUploading.value = false;
}
}
}
// 清空input,以便再次选择文件
if (fileInput.value) {
fileInput.value.value = '';
}
};
// 预览文件
const previewFile = (file: any) => {
if (!file.fileUrl) {
createMessage.error('无法预览:找不到文件URL');
return;
}
window.open(file.fileUrl, '_blank');
};
// 下载文件
const downloadFile = (file: any) => {
if (!file.fileUrl) {
createMessage.error('无法下载:找不到文件URL');
return;
}
const a = document.createElement('a');
a.href = file.fileUrl;
a.download = file.name;
a.target = '_blank';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
// 删除文件
const removeFile = (index: number) => {
uploadedFiles.value.splice(index, 1);
};
// 提交表单
const handleSubmit = async () => {
try {
// 验证表单
const values = await validate();
// 验证文件上传
if (uploadedFiles.value.length === 0) {
createMessage.error('请上传用印文件');
return;
}
submitLoading.value = true;
let result;
if (isEdit.value) {
// 编辑模式 - 准备编辑数据
const editData = {
id: editId.value,
fileName: values.fileName,
fileTypeId: values.fileType,
signType: values.signType,
count: values.count,
sealTypesId: values.sealTypes,
companyId: values.company,
deptId: values.dept,
files: uploadedFiles.value.map(file => file.fileUrl),
note: values.note || '',
};
console.log('提交编辑数据到 /order/erp/seal/edit:', editData);
result = await updateSealApplication(editData);
console.log('编辑印章申请响应:', result);
createMessage.success('用印申请修改成功');
} else {
// 新建模式 - 准备新建数据
const createData = {
fileName: values.fileName,
fileTypeId: values.fileType,
signType: values.signType,
count: values.count,
sealTypesId: values.sealTypes,
companyId: values.company,
deptId: values.dept,
files: uploadedFiles.value.map(file => file.fileUrl),
note: values.note || '',
};
console.log('提交数据到 /order/erp/seal/add:', createData);
result = await createSealApplication(createData);
console.log('创建印章申请响应:', result);
createMessage.success('用印申请创建成功');
}
closeDrawer();
emit('success');
} catch (error) {
console.error(isEdit.value ? '修改用印申请失败:' : '创建用印申请失败:', error);
createMessage.error(isEdit.value ? '修改用印申请失败,请重试' : '创建用印申请失败,请重试');
} finally {
submitLoading.value = false;
}
};
// 取消操作
const handleCancel = () => {
closeDrawer();
};
</script>
<style lang="less" scoped>
.file-upload-section {
margin-top: 20px;
padding: 20px;
border-top: 1px solid #f0f0f0;
.upload-title {
font-size: 14px;
font-weight: 500;
margin-bottom: 16px;
}
.file-list {
.file-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
border: 1px solid #d9d9d9;
border-radius: 4px;
margin-bottom: 8px;
.file-info {
flex: 1;
.file-name {
font-size: 14px;
color: #333;
}
}
.file-actions {
display: flex;
gap: 8px;
.action-btn {
color: #1890ff;
cursor: pointer;
&:hover {
color: #40a9ff;
}
&.delete {
color: #ff4d4f;
&:hover {
color: #ff7875;
}
}
}
}
}
.file-upload-button {
width: 100%;
height: 100px;
border: 1px dashed #d9d9d9;
border-radius: 4px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: border-color 0.3s;
&:hover {
border-color: #1890ff;
background-color: #f6f6f6;
}
.upload-button-text {
text-align: center;
color: #666;
.anticon {
font-size: 24px;
margin-bottom: 8px;
}
.upload-hint {
font-size: 12px;
color: #999;
margin-top: 4px;
}
}
}
}
}
</style>