Преглед на файлове

数电发票附件上传代码调整

user5 преди 1 година
родител
ревизия
6598bd39e5

+ 38 - 7
src/api/sys/OSSService.js

@@ -139,14 +139,45 @@ export function fileName (file) {
   return fileName
 }
 
+
+export function fileNameInvoice (file) {
+	// console.log('文件名称处理')
+	if(!file.name){
+		file = file.file
+	}
+	const tmpcnt = file.name.lastIndexOf('.')
+	let fileName = file.name.substring(0, tmpcnt)
+	// 将单引号‘’都转换成',将双引号“”都转换成"
+	fileName = fileName.replace(/’|‘/g, '\'').replace(/“|”/g, '')
+	// 将中括号【】转换成[],将大括号{}转换成{}
+	fileName = fileName.replace(/【/g, '(').replace(/】/g, ')').replace(/{/g, '(').replace(/}/g, ')')
+	fileName = fileName.replace(/\[/g, '(').replace(/]/g, ')').replace(/{/g, '(').replace(/}/g, ')')
+	// 将逗号,转换成,,将:转换成:
+	fileName = fileName.replace(/,/g, ',').replace(/:/g, ':')
+	// 将中文——转换为英文-
+	fileName = fileName.replace(/—/g, '-')
+	fileName = fileName.replace(/……/g, '')
+	fileName = fileName.replace(/±/g, '')
+	fileName = fileName.replace(/#/g, '')
+	fileName = fileName.replace(/%/g, '')
+	fileName = fileName.replace(/Π/g, '')
+	fileName = fileName.replace(/π/g, '')
+	fileName = fileName.replace(/·/g, '.')
+	// console.log('文件名处理结果', fileName)
+	return fileName
+}
+
 export function beforeAvatarUpload (file, fileList, maxValue) {
-  // 文件大小校验
-  const isLt2M = file.size / 1024 / 1024 < maxValue
-  if (!isLt2M) {
-    fileList.splice(fileList.length - 1, 1)
-    // console.log('文件大小不能超过 ' + maxValue + ' MB!')
-  }
-  return isLt2M
+	if(file){
+		// 文件大小校验
+		const isLt2M = file.size / 1024 / 1024 < maxValue
+		if (!isLt2M) {
+			fileList.splice(fileList.length - 1, 1)
+			// console.log('文件大小不能超过 ' + maxValue + ' MB!')
+		}
+		return isLt2M
+	}
+	return 0
 }
 
 export function exnameFix (file, isShow, names) {

+ 15 - 0
src/views/ccpm/reimbursement/info/ReimbursementForm.vue

@@ -2133,6 +2133,21 @@
 			})
 
 		},
+		invoiceReimbursementDispose: function (data) {
+			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
+			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入
+			var includeFlag = false;
+			if(!invoiceReimbursements) {
+				invoiceReimbursements = [];
+			}
+
+			invoiceReimbursements.forEach(item => {
+				if(item.number === data.InvoiceNumber) {
+					includeFlag = true;
+				}
+			})
+			return includeFlag;
+		},
 		deleteInvoiceReimbursementDisposeData: function (data) {
 			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
 			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入

+ 213 - 80
src/views/common/InvoiceReimbursementUpLoadComponent.vue

@@ -9,6 +9,7 @@
                       $message.warning(`当前限制选择 999 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
                      }"
                :show-file-list="false"
+			   :before-upload="beforeUpload"
                :on-change="changes"
                :on-progress="uploadVideoProcess"
                :file-list="fileList">
@@ -69,7 +70,7 @@
     httpRequest,
     // eslint-disable-next-line no-unused-vars
     handleRemove,
-    fileName,
+	fileNameInvoice,
     // eslint-disable-next-line no-unused-vars
     beforeAvatarUpload,
 	  exnameFix,
@@ -88,6 +89,7 @@
         loadProgress: 0,
         fileList: [],
         dataList: [],
+        oldDataList: [],
         dataListNew: [],
         url: '',
         showViewer: false,
@@ -103,7 +105,8 @@
         createBy: '',
         showDivider: true,
         loading: false,
-        dataListLength: ''
+        dataListLength: '',
+		uploadDelFlag: false
       }
     },
     watch: {
@@ -210,6 +213,7 @@
             item.lsUrl = data.url
             item.size = data.size
             this.dataList.push(item)
+            this.oldDataList.push(item)
             this.dataListNew.push(item)
             if (this.dataListNew.length === fileList.length) {
               this.fileLoading = true
@@ -229,8 +233,58 @@
         }
       },
       async httpRequest (file) {
-        await httpRequest(file, fileName(file), this.directory, this.maxValue)
+        await httpRequest(file, fileNameInvoice(file), this.directory, this.maxValue)
       },
+		async beforeUpload(file) {
+			if(this.uploadDelFlag){
+				this.$message.warning('该文件已上传,请勿重复上传');
+				this.uploadKey = Math.random()
+
+				return true; // 取消上传
+			}else{
+				// 对文件进行判定
+				const isXml = file.type === 'text/xml'; // 假设只接受 XML 文件
+				if (!isXml) {
+					this.$message.error('只能上传 XML 文件');
+					return false; // 取消上传
+				}else{
+					try {
+						//对上传的xml文件信息进行处理并通过后端接口进行解析返回到父页面进行调整
+						const formBody = new FormData()
+						formBody.append('file', file)
+						await this.ossService.disposeXmlFile(formBody).then((data) => {
+							if(Object.keys(data).length === 0){
+								this.$message.warning('上传的数电发票格式错误');
+								this.loading = false;
+								// 在验证失败时删除已经添加的行
+								const index = this.fileList.findIndex(item => item.name === file.name);
+								if (index !== -1) {
+									this.deleteFileById(this.fileList[index], index, this.fileList);
+								}
+							}else{
+								var flag = this.$parent.invoiceReimbursementDispose(data);
+								if(!flag){
+									this.httpRequest(file);
+								}else{
+									this.$message.error('发票号:' + data.InvoiceNumber + "已上传,请勿重复上传")
+									// 在验证失败时删除已经添加的行
+									const index = this.dataListNew.findIndex(item => item.name === file.name);
+									if (index !== -1) {
+										this.deleteFileById(this.dataListNew[index], index, this.dataListNew);
+									}
+									this.loading = false;
+								}
+
+							}
+						})
+					} catch (error) {
+						console.error(error);
+					}
+				}
+				// 其他判定逻辑...
+				return true; // 允许上传
+			}
+		},
       uploadVideoProcess (event, file, fileList) {
       	var reimbursementType = ''
       	var parentHierarchy = 0
@@ -255,10 +309,20 @@
 					const formBody = new FormData()
 					formBody.append('file', file.raw)
 					this.ossService.disposeXmlFile(formBody).then((data) => {
-						if(parentHierarchy === 1){
-							this.$parent.invoiceReimbursementDisposeData(data,file)
-						}else if (parentHierarchy ===2){
-							this.$parent.$parent.invoiceReimbursementDisposeData(data,file)
+						if(Object.keys(data).length > 0){
+							if(parentHierarchy === 1){
+								this.$parent.invoiceReimbursementDisposeData(data,file)
+							}else if (parentHierarchy ===2){
+								this.$parent.$parent.invoiceReimbursementDisposeData(data,file)
+							}
+						}else{
+							this.$message.warning('上传的数电发票格式错误')
+							this.loading = false;
+							// 在验证失败时删除已经添加的行
+							const index = this.fileList.findIndex(item => item.name === file.name);
+							if (index !== -1) {
+								this.deleteFileById(this.fileList[index], index, this.fileList);
+							}
 						}
 					})
 				}
@@ -291,83 +355,73 @@
           return kb + '' + sizes[i]
         }
       },
-      async changes (file, fileList) {
-        // if (file.status !== 'ready') {
-        //   return
-        // }
-        if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
-          this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
-          return
-        }
-        if (!exnameFix(file, '1', 'xml')) {
-			for (let i = 0; i <this.dataListNew.length ; i++) {
-				this.deleteById(this.dataListNew[i],i,this.dataListNew)
+		async changes (file, fileList) {
+			this.uploadKey = Math.random()
+      	this.uploadDelFlag = false;
+			// if (file.status !== 'ready') {
+			//   return
+			// }
+			if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
+				this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
+				return
 			}
-			this.fileList = []
-			this.dataListNew = []
-			// this.$message.error('文件仅支持 xml 格式!')
-			return
-		}
-        this.dataListNew = []
-        this.dataList.forEach((item) => {
-          this.dataListNew.push(item)
-        })
-		  var fileListNames = []
-		  for (let fileItem of this.fileList) {
-			  fileListNames.push(fileItem.name)
-		  }
-        for (let item of fileList) {
-			if(fileListNames.length === 0 || fileListNames.indexOf(item.name) === -1){
-				this.fileList.push(item)
+			var verificationFileList = []
+			if(this.oldDataList){
+				for (let item of this.oldDataList) {
+					verificationFileList.push(item)
+				}
 			}
-
-		  item.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
-		  item.createBy = {
-			id: '',
-			name: ''
-		  }
-		  item.createBy.id = this.$store.state.user.id
-		  item.createBy.name = this.$store.state.user.name
-			var count = 0;
-			for (let fileDataItem of this.dataListNew) {
-				if(fileDataItem.name === item.name){
-					count ++;
+			for (let item of fileList) {
+				verificationFileList.push(item)
+			}
+			var fileCount = 0;
+			for (let item of verificationFileList) {
+				var fileItem = item;
+				if(item.raw !== undefined && item.raw !== null && item.raw !== {}){
+					fileItem = item.raw;
+				}
+				if(file.raw.name === fileItem.name){
+					fileCount ++
 				}
 			}
-			if(count === 0){
+
+
+			if(fileCount>1){
+				this.uploadDelFlag = true;
+				return
+			}
+			this.dataListNew = []
+			this.dataList.forEach((item) => {
 				this.dataListNew.push(item)
+			})
+			var fileListNames = []
+			for (let fileItem of this.fileList) {
+				fileListNames.push(fileItem.name)
 			}
-        }
-		  /*var fileListNewNames = []
-		  for (let fileItem of this.fileList) {
-			  fileListNewNames.push(fileItem.name)
-		  }
-		  for(let i = 0; i < fileList.length; i++) {
-			  let item = fileList[i]
-
-			  if (fileListNewNames.indexOf(item.name) > -1) {
-				  var count=0;
-				  for (let itemName of fileListNewNames) {
-					  if(itemName === item.name){
-						  count ++;
-					  }
-				  }
-				  if(count>0 && file.name === item.name){
-					  this.deleteFileinfo(file,i,fileList)
-				  }
-			  }
-		  }*/
-        for (let item of this.dataListNew) {
-          if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
-            item.url = item.raw.url
-            if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
-            	this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
-                item.lsUrl = data
-              })
-            }
-          }
-        }
-      },
+			for (let item of fileList) {
+				if(fileListNames.length === 0 || fileListNames.indexOf(item.name) === -1){
+					this.fileList.push(item)
+				}
+				item.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
+				item.createBy = {
+					id: '',
+					name: ''
+				}
+				item.createBy.id = this.$store.state.user.id
+				item.createBy.name = this.$store.state.user.name
+				this.dataListNew.push(item)
+			}
+			for (let item of this.dataListNew) {
+				if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
+					item.url = item.raw.url
+					if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
+						this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
+							item.lsUrl = data
+						})
+					}
+				}
+			}
+		},
       showFile (row) {
         openWindowOnUrl(row)
       },
@@ -431,10 +485,89 @@
 				  }else{
 					  formBody.append('file', row.raw)
 					  this.ossService.disposeXmlFile(formBody).then((data) => {
+						  if(data){
+							  if(parentHierarchy === 1){
+								  this.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  }else if (parentHierarchy ===2){
+								  this.$parent.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  }
+						  }else{
+							  this.$message.warning('上传的数电发票格式错误')
+							  this.loading = false;
+							  // 在验证失败时删除已经添加的行
+							  const index = this.fileList.findIndex(item => item.name === file.name);
+							  if (index !== -1) {
+								  this.deleteFileById(this.fileList[index], index, this.fileList);
+							  }
+						  }
+					  })
+				  }
+			  }
+
+		  }
+      },
+      async deleteFileById (row, index,fileList) {
+	  	// this.$refs.upload.handleRemove(this.dataListNew[index])
+        await this.dataListNew.splice(index, 1)
+		await this.dataList.splice(index, 1)
+		  if (this.commonJS.isNotEmpty(row.id)) {
+			  this.ossService.deleteMsgById(row.id)
+		  }
+		var newFileList = [];
+		for (var i = 0; i < fileList.length; i++) {
+		  if (fileList[i].name !== row.name) {
+			  newFileList.push(fileList[i])
+		  }
+		}
+		  this.fileList = newFileList;
+
+		var reimbursementType = ''
+		  var parentHierarchy = 0
+		  if(this.$parent.inputForm) {
+			  reimbursementType = this.$parent.inputForm.reimbursementType
+			  parentHierarchy = 1
+		  }else if(this.$parent.$parent.inputForm) {
+			  reimbursementType = this.$parent.$parent.inputForm.reimbursementType
+			  parentHierarchy = 2
+		  }
+		  if("1" === reimbursementType){
+			  var fileName = ''
+			  if(null === row.raw || undefined === row.raw){
+				  fileName = row.name;
+			  }else{
+				  fileName = row.raw.name;
+			  }
+
+			  const spliceLength2 = fileName.lastIndexOf(".");
+			  var fileNameSuffix = fileName.slice(spliceLength2 + 1);
+			  if(fileNameSuffix === "xml" ) {
+				  //对上传的xml文件信息进行处理并通过后端接口进行解析返回到父页面进行调整
+				  const formBody = new FormData()
+				  if(null === row.raw || undefined === row.raw){
+					  this.ossService.downLoadFileDisposeXmlFile(row.url).then((data) => {
 						  if(parentHierarchy === 1){
-							  this.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  //this.$parent.deleteInvoiceReimbursementDisposeData(data)
 						  }else if (parentHierarchy ===2){
-							  this.$parent.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  //this.$parent.$parent.deleteInvoiceReimbursementDisposeData(data)
+						  }
+					  })
+				  }else{
+					  formBody.append('file', row.raw)
+					  this.ossService.disposeXmlFile(formBody).then((data) => {
+						  if(data){
+							  if(parentHierarchy === 1){
+								  //this.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  }else if (parentHierarchy ===2){
+								  //this.$parent.$parent.deleteInvoiceReimbursementDisposeData(data)
+							  }
+						  }else{
+							  this.$message.warning('上传的数电发票格式错误')
+							  this.loading = false;
+							  // 在验证失败时删除已经添加的行
+							  const index = this.fileList.findIndex(item => item.name === file.name);
+							  if (index !== -1) {
+								  this.deleteFileById(this.fileList[index], index, this.fileList);
+							  }
 						  }
 					  })
 				  }

+ 15 - 0
src/views/consultancy/reimbursement/info/ReimbursementForm.vue

@@ -2109,6 +2109,21 @@
 			})
 
 		},
+		invoiceReimbursementDispose: function (data) {
+			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
+			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入
+			var includeFlag = false;
+			if(!invoiceReimbursements) {
+				invoiceReimbursements = [];
+			}
+
+			invoiceReimbursements.forEach(item => {
+				if(item.number === data.InvoiceNumber) {
+					includeFlag = true;
+				}
+			})
+			return includeFlag;
+		},
 		deleteInvoiceReimbursementDisposeData: function (data) {
 			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
 			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入

+ 15 - 0
src/views/cw/reimbursementApproval/info/ReimbursementForm.vue

@@ -2437,6 +2437,21 @@
 			})
 
 		},
+		invoiceReimbursementDispose: function (data) {
+			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
+			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入
+			var includeFlag = false;
+			if(!invoiceReimbursements) {
+				invoiceReimbursements = [];
+			}
+
+			invoiceReimbursements.forEach(item => {
+				if(item.number === data.InvoiceNumber) {
+					includeFlag = true;
+				}
+			})
+			return includeFlag;
+		},
 		deleteInvoiceReimbursementDisposeData: function (data) {
 			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
 			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入

+ 15 - 0
src/views/reimbursement/info/ReimbursementForm.vue

@@ -2227,6 +2227,21 @@
 					})
 
 			},
+			invoiceReimbursementDispose: function (data) {
+				var invoiceReimbursements = this.inputForm.invoiceReimbursements;
+				//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入
+				var includeFlag = false;
+				if(!invoiceReimbursements) {
+					invoiceReimbursements = [];
+				}
+
+				invoiceReimbursements.forEach(item => {
+					if(item.number === data.InvoiceNumber) {
+						includeFlag = true;
+					}
+				})
+				return includeFlag;
+			},
 			deleteInvoiceReimbursementDisposeData: function (data) {
 				var invoiceReimbursements = this.inputForm.invoiceReimbursements;
 					//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入

+ 15 - 0
src/views/zs/reimbursement/info/ReimbursementForm.vue

@@ -2083,6 +2083,21 @@
 			})
 
 		},
+		invoiceReimbursementDispose: function (data) {
+			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
+			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入
+			var includeFlag = false;
+			if(!invoiceReimbursements) {
+				invoiceReimbursements = [];
+			}
+
+			invoiceReimbursements.forEach(item => {
+				if(item.number === data.InvoiceNumber) {
+					includeFlag = true;
+				}
+			})
+			return includeFlag;
+		},
 		deleteInvoiceReimbursementDisposeData: function (data) {
 			var invoiceReimbursements = this.inputForm.invoiceReimbursements;
 			//创建判断值,若行信息存在相同的发票号,则进行数据检查调整,若不存在发票号,则新增行,并将信息写入