文件上传

This commit is contained in:
cwchen 2025-10-22 11:07:47 +08:00
parent 2000837d10
commit 7c31fc85a9
3 changed files with 235 additions and 38 deletions

View File

@ -74,15 +74,21 @@ export default {
},
data() {
return {
files: this.fileList,
files: [...this.fileList], // 使
previewImageUrl: '',
previewImageName: '',
previewFileName: '',
previewFileType: ''
previewFileType: '',
isUploading: false //
}
},
methods: {
beforeUpload(file) {
//
if (this.isUploading) {
return false;
}
//
const fileExtension = file.name.split('.').pop().toLowerCase();
const isAllowedType = this.allowedTypes.includes(fileExtension);
@ -107,7 +113,13 @@ export default {
},
//
handleFileChange(file, fileList) {
this.files = fileList;
//
if (file.status === 'removed' || file.status === 'fail') {
return;
}
// 使使 fileList files
this.files = this.formatFileList(fileList);
//
if (file.raw && fileList.length === 1) {
@ -122,41 +134,180 @@ export default {
//
this.clearPreview();
}
console.log('文件列表更新:', this.files.length, '个文件');
// this.$emit('file-change', this.files);
//
if (this.autoUpload && file.status === 'ready') {
if(Object.keys(this.fileUploadRule).length !== 0){
if (this.autoUpload && file.status === 'ready' && !this.isUploading) {
if (this.fileUploadRule.fields_json) {
// ocr
this.uploadFile(file, '识别中');
} else {
// ocr
this.uploadFile(file, '上传中');
}
}
},
//
formatFileList(fileList) {
return fileList.map(file => {
//
const formattedFile = {
uid: file.uid,
name: file.name,
size: file.size,
type: file.type,
status: file.status,
raw: file.raw
};
// percentage
if (file.percentage !== undefined && file.percentage !== null) {
formattedFile.percentage = Math.max(0, Math.min(100, file.percentage));
} else if (file.status === 'uploading') {
formattedFile.percentage = 0;
} else if (file.status === 'success') {
formattedFile.percentage = 100;
}
//
if (file.response) {
formattedFile.response = file.response;
formattedFile.url = file.url;
}
if (file.statusText) {
formattedFile.statusText = file.statusText;
}
return formattedFile;
});
},
//
uploadFile(file,text){
console.log(file);
this.$bus.$emit('startUpload', text);
async uploadFile(file, text) {
//
this.isUploading = true;
const formData = new FormData();
formData.append('file', file.raw);
formData.append('params', JSON.stringify(this.fileUploadRule));
uploadSmallFileByOcr(formData).then(res => {
console.log(res);
this.$message.success(res.msg);
// 0
this.updateFileStatus(file.uid, 'uploading', text, null, 0);
try {
this.$bus.$emit('startUpload', text);
const res = await uploadSmallFileByOcr(formData);
console.log('上传成功:', res);
this.$bus.$emit('endUpload');
}).catch(err => {
this.$message.error(err.msg);
// 100
this.updateFileStatus(file.uid, 'success', '', res.data, 100);
console.log('上传成功后的文件列表:', this.files);
//
this.$emit('file-change', this.getCurrentFiles());
} catch (err) {
this.$bus.$emit('endUpload');
})
//
this.removeFailedFile(file.uid);
} finally {
//
this.isUploading = false;
}
},
//
removeFailedFile(fileUid) {
console.log('移除上传失败的文件:', fileUid);
const fileIndex = this.files.findIndex(item => item.uid === fileUid);
if (fileIndex !== -1) {
//
this.files.splice(fileIndex, 1);
console.log('移除失败文件后的文件列表:', this.files);
//
this.clearPreview();
//
this.$emit('file-change', this.getCurrentFiles());
}
},
//
updateFileStatus(fileUid, status, statusText, responseData = null, percentage = null) {
console.log('更新文件状态:', fileUid, status, statusText, '进度:', percentage);
console.log('更新前的文件列表:', this.files);
const fileIndex = this.files.findIndex(item => item.uid === fileUid);
if (fileIndex !== -1) {
const updatedFile = {
...this.files[fileIndex],
status: status
};
if (statusText) {
updatedFile.statusText = statusText;
}
// percentage 0-100
if (percentage !== null && percentage >= 0 && percentage <= 100) {
updatedFile.percentage = percentage;
} else if (status === 'uploading') {
// 0
updatedFile.percentage = 0;
} else if (status === 'success') {
// 100
updatedFile.percentage = 100;
}
if (responseData) {
//
updatedFile.response = responseData;
updatedFile.response.businessType = this.fileUploadRule?.fileUploadType;
}
// 使 Vue.set
this.$set(this.files, fileIndex, updatedFile);
console.log('更新后的文件列表:', this.files);
} else {
console.warn('未找到要更新的文件:', fileUid);
}
},
//
getCurrentFiles() {
const currentFiles = this.files.map(file => {
const fileObj = {
uid: file.uid,
name: file.name,
size: file.size,
type: file.type,
status: file.status
};
// percentage
if (file.percentage !== undefined) {
fileObj.percentage = file.percentage;
}
//
if (file.raw) {
fileObj.raw = file.raw;
}
//
if (file.response) {
fileObj.response = file.response;
fileObj.response.businessType = this.fileUploadRule?.fileUploadType;
}
return fileObj;
});
console.log('getCurrentFiles 返回:', currentFiles);
return currentFiles;
},
//
handleExceed(files, fileList) {
console.log('文件超出限制处理');
//
if (files.length > 0) {
//
@ -166,13 +317,15 @@ export default {
const newFile = files[0];
this.beforeUpload(newFile); //
//
// percentage
const newFileObj = {
name: newFile.name,
size: newFile.size,
type: newFile.type,
raw: newFile,
uid: Date.now() // uid
uid: Date.now(), // uid
status: 'ready',
percentage: 0 //
};
//
@ -185,13 +338,36 @@ export default {
this.generateDocumentPreview(newFile);
}
this.$emit('file-change', this.files);
// this.$message.success('');
console.log('handleExceed 后的文件列表:', this.files);
//
this.$emit('file-change', this.getCurrentFiles());
//
if (this.autoUpload && !this.isUploading) {
if (this.fileUploadRule.fields_json) {
this.uploadFile(newFileObj, '识别中');
} else {
this.uploadFile(newFileObj, '上传中');
}
}
}
},
//
handleRemove(file, fileList) {
this.files = fileList;
console.log('移除文件:', file);
console.log('移除前的文件列表:', this.files);
//
if (this.isUploading) {
return false;
}
// 使
this.files = this.formatFileList(fileList);
console.log('移除后的文件列表:', this.files);
//
if (fileList.length === 0) {
@ -206,8 +382,10 @@ export default {
this.clearPreview();
}
this.$emit('file-change', fileList);
//
this.$emit('file-change', this.getCurrentFiles());
},
//
isImageFile(file) {
return file && file.type && file.type.startsWith('image/');
@ -307,10 +485,6 @@ export default {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'bmp': 'image/bmp',
'webp': 'image/webp',
'svg': 'image/svg+xml',
'pdf': 'application/pdf',
'doc': 'application/msword',
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
@ -323,7 +497,9 @@ export default {
watch: {
fileList: {
handler(newVal) {
this.files = newVal;
// console.log('fileList :', newVal);
// 使
this.files = this.formatFileList(newVal);
//
if (newVal.length === 1 && newVal[0] && newVal[0].raw) {
if (this.isImageFile(newVal[0].raw)) {
@ -340,7 +516,6 @@ export default {
},
}
</script>
<style scoped lang="scss">
.upload-container {
width: 100%;

View File

@ -7,7 +7,7 @@
<el-form :model="form" :rules="rules" ref="basicInfoForm" label-width="110px" label-position="top">
<!-- 营业执照 -->
<el-form-item label="营业执照" prop="fileList">
<UploadFile :fileList="form.fileList" :fileUploadRule="fileUploadRule" />
<UploadFile :fileList="form.fileList" :fileUploadRule="fileUploadRule" @file-change="handleFileChange"/>
</el-form-item>
<el-form-item label="企业名称" prop="enterpriseName">
<el-input v-model="form.enterpriseName" placeholder="自动提取"></el-input>
@ -55,6 +55,14 @@ export default {
// ocrRuleList: ['business_license', 'face_id_card_portrait', 'national_emblem_id_card', 'account_opening_license'],
ocrRuleList: ['business_license'],
fileUploadList: [],
ocrResult: {
"企业名称": "enterpriseName",
"统一社会信用代码": "enterpriseCode",
"注册资本": "registeredCapital",
"营业期限": "businessTerm",
"住所": "residence",
"经营范围": "businessScope",
},
rules: {
fileList: [
{ required: true, message: '请上传营业执照', trigger: 'blur' }
@ -105,7 +113,6 @@ export default {
fields_json: foundItem.raw.remark,
suffix: 'mainDatabase'
} : null;
console.log(item);
this.fileUploadList.push(item)
},
@ -115,6 +122,23 @@ export default {
this.ocrRule(item)
})
},
//
handleFileChange(file) {
this.form.fileList = file;
console.log(file[0].response);
if(file instanceof Array && file.length > 0){
//
const response = file[0].response;
if(response.ocrResult){
// ocr
this.ocrResult.forEach(item => {
this.form[item.value] = response.ocrResult[item.label];
});
}
this.$refs.basicInfoForm.validate();
}
}
},
computed: {
fileUploadRule() {
@ -126,7 +150,6 @@ export default {
'dict.type.identification_tag': {
handler(newVal) {
if (newVal && newVal.length > 0) {
console.log('字典数据加载完成:', newVal);
this.addOcrRule();
}
},

View File

@ -247,7 +247,6 @@ export default {
//
handleAdd() {
console.log('新增企业')
this.$router.push({
name: 'EnterpriseForm',
query: {