|
|
|
|
@ -1,7 +1,17 @@ |
|
|
|
|
<template> |
|
|
|
|
<el-dialog :title="title" append-to-body :modelValue="importBox" width="555px" @close="closeDialog"> |
|
|
|
|
<avue-form :option="importOption" v-model="importForm" :upload-after="uploadAfter" |
|
|
|
|
:upload-error="uploadError"> |
|
|
|
|
<el-dialog |
|
|
|
|
:title="title" |
|
|
|
|
append-to-body |
|
|
|
|
:modelValue="importBox" |
|
|
|
|
width="555px" |
|
|
|
|
@close="closeDialog" |
|
|
|
|
> |
|
|
|
|
<avue-form |
|
|
|
|
:option="importOption" |
|
|
|
|
v-model="importForm" |
|
|
|
|
:upload-after="uploadAfter" |
|
|
|
|
:upload-error="uploadError" |
|
|
|
|
> |
|
|
|
|
<template #excelTemplate> |
|
|
|
|
<el-button type="primary" @click="handleTemplate"> |
|
|
|
|
点击下载<i class="el-icon-download el-icon--right"></i> |
|
|
|
|
@ -18,36 +28,40 @@ import { getToken } from '@/utils/auth'; |
|
|
|
|
import { downloadXls } from '@/utils/util'; |
|
|
|
|
export default { |
|
|
|
|
props: { |
|
|
|
|
title:{ |
|
|
|
|
type:String, |
|
|
|
|
default:'导入' |
|
|
|
|
title: { |
|
|
|
|
type: String, |
|
|
|
|
default: '导入', |
|
|
|
|
}, |
|
|
|
|
isShow:{ |
|
|
|
|
type:Boolean, |
|
|
|
|
default:false |
|
|
|
|
isShow: { |
|
|
|
|
type: Boolean, |
|
|
|
|
default: false, |
|
|
|
|
}, |
|
|
|
|
templateUrl:{ |
|
|
|
|
type:String, |
|
|
|
|
default:'' |
|
|
|
|
templateUrl: { |
|
|
|
|
type: String, |
|
|
|
|
default: '', |
|
|
|
|
}, |
|
|
|
|
templateName:{ |
|
|
|
|
type:String, |
|
|
|
|
default:'模板' |
|
|
|
|
templateName: { |
|
|
|
|
type: String, |
|
|
|
|
default: '模板', |
|
|
|
|
}, |
|
|
|
|
importUrl:{ |
|
|
|
|
type:String, |
|
|
|
|
default:'' |
|
|
|
|
importUrl: { |
|
|
|
|
type: String, |
|
|
|
|
default: '', |
|
|
|
|
}, |
|
|
|
|
showTips:{ |
|
|
|
|
type:String, |
|
|
|
|
default:'请上传 .xls,.xlsx 标准格式文件' |
|
|
|
|
} |
|
|
|
|
showTips: { |
|
|
|
|
type: String, |
|
|
|
|
default: '请上传 .xls,.xlsx 标准格式文件', |
|
|
|
|
}, |
|
|
|
|
basicImportSearch: { |
|
|
|
|
type: Object, |
|
|
|
|
default: () => {}, |
|
|
|
|
}, |
|
|
|
|
data(){ |
|
|
|
|
return{ |
|
|
|
|
importBox:false, |
|
|
|
|
importForm:{}, |
|
|
|
|
importOption:{ |
|
|
|
|
}, |
|
|
|
|
data() { |
|
|
|
|
return { |
|
|
|
|
importBox: false, |
|
|
|
|
importForm: {}, |
|
|
|
|
importOption: { |
|
|
|
|
submitBtn: false, |
|
|
|
|
emptyBtn: false, |
|
|
|
|
column: [ |
|
|
|
|
@ -61,6 +75,8 @@ export default { |
|
|
|
|
propsHttp: { |
|
|
|
|
res: 'data', |
|
|
|
|
}, |
|
|
|
|
// 传递参数 |
|
|
|
|
data: this.basicImportSearch, |
|
|
|
|
tip: this.showTips, |
|
|
|
|
action: this.importUrl, |
|
|
|
|
}, |
|
|
|
|
@ -71,29 +87,30 @@ export default { |
|
|
|
|
span: 24, |
|
|
|
|
}, |
|
|
|
|
], |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
watch: { |
|
|
|
|
isShow(newVal) { |
|
|
|
|
this.importBox = newVal; |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
mounted(){ |
|
|
|
|
console.log('isShowImport=================',this.isShow) |
|
|
|
|
this.importBox = this.isShow |
|
|
|
|
}, |
|
|
|
|
methods:{ |
|
|
|
|
mounted() { |
|
|
|
|
console.log('isShowImport=================', this.isShow); |
|
|
|
|
this.importBox = this.isShow; |
|
|
|
|
}, |
|
|
|
|
methods: { |
|
|
|
|
closeDialog(val) { |
|
|
|
|
this.importBox = false; |
|
|
|
|
this.$emit('closeDialog',val); |
|
|
|
|
this.$emit('closeDialog', val); |
|
|
|
|
}, |
|
|
|
|
handleTemplate() { |
|
|
|
|
const url = `${this.templateUrl}?${this.website.tokenHeader}=${getToken()}`; |
|
|
|
|
console.log('下载模板URL:', url); |
|
|
|
|
console.log('模板文件名:', this.templateName); |
|
|
|
|
|
|
|
|
|
exportBlob(url).then(res => { |
|
|
|
|
exportBlob(url) |
|
|
|
|
.then(res => { |
|
|
|
|
console.log('API返回状态:', res.status); |
|
|
|
|
console.log('res.data类型:', typeof res.data); |
|
|
|
|
console.log('res.data instanceof Blob:', res.data instanceof Blob); |
|
|
|
|
@ -106,7 +123,8 @@ export default { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 验证文件内容是否是有效的Excel文件 |
|
|
|
|
this.validateExcelBlob(res.data).then(isValid => { |
|
|
|
|
this.validateExcelBlob(res.data) |
|
|
|
|
.then(isValid => { |
|
|
|
|
if (!isValid) { |
|
|
|
|
console.error('下载的文件不是有效的Excel文件'); |
|
|
|
|
this.$message.error('下载的文件格式不正确'); |
|
|
|
|
@ -122,7 +140,8 @@ export default { |
|
|
|
|
console.log('开始下载文件:', fileName); |
|
|
|
|
downloadXls(res.data, fileName); |
|
|
|
|
this.$message.success('模板下载成功'); |
|
|
|
|
}).catch(error => { |
|
|
|
|
}) |
|
|
|
|
.catch(error => { |
|
|
|
|
console.error('验证文件失败:', error); |
|
|
|
|
// 即使验证失败也尝试下载 |
|
|
|
|
let fileName = this.templateName; |
|
|
|
|
@ -132,17 +151,20 @@ export default { |
|
|
|
|
downloadXls(res.data, fileName); |
|
|
|
|
this.$message.success('模板下载成功'); |
|
|
|
|
}); |
|
|
|
|
}).catch(error => { |
|
|
|
|
}) |
|
|
|
|
.catch(error => { |
|
|
|
|
console.error('下载模板失败:', error); |
|
|
|
|
this.$message.error('下载失败,请检查网络或稍后重试'); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
validateExcelBlob(blob) { |
|
|
|
|
return new Promise((resolve) => { |
|
|
|
|
return new Promise(resolve => { |
|
|
|
|
const reader = new FileReader(); |
|
|
|
|
reader.onloadend = (e) => { |
|
|
|
|
reader.onloadend = e => { |
|
|
|
|
const arr = new Uint8Array(e.target.result); |
|
|
|
|
const header = Array.from(arr.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(' '); |
|
|
|
|
const header = Array.from(arr.slice(0, 8)) |
|
|
|
|
.map(b => b.toString(16).padStart(2, '0')) |
|
|
|
|
.join(' '); |
|
|
|
|
console.log('文件头 (前8字节):', header); |
|
|
|
|
|
|
|
|
|
// 检查XLSX (ZIP格式): 50 4B 03 04 |
|
|
|
|
@ -160,17 +182,16 @@ export default { |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
uploadAfter(res, done, loading, column) { |
|
|
|
|
this.closeDialog(true) |
|
|
|
|
this.closeDialog(true); |
|
|
|
|
done(); |
|
|
|
|
}, |
|
|
|
|
uploadError(error, column){ |
|
|
|
|
console.log('error-------------',error) |
|
|
|
|
this.closeDialog() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
uploadError(error, column) { |
|
|
|
|
console.log('error-------------', error); |
|
|
|
|
this.closeDialog(); |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
<style> |
|
|
|
|
|
|
|
|
|
</style> |