<template>
<div>
<van-nav-bar
left-text="返回"
right-text="提交"
title="附件数据"
left-arrow
:fixed="true"
:placeholder="true"
@click-left="back"
@click-right="fileSubmit"
/>
<van-form>
<van-pull-refresh v-model="isLoading" @refresh="onRefresh" success-text="刷新成功">
<van-swipe-cell v-for="(item, index) in list" :key="index">
<van-card
:price="item.createdate"
currency=""
:desc="item.attachmenttitle + '.' + item.extend"
title="附件名称:"
class="goods-card"
:thumb="path[index]"
@click="clickView(path[index], index, item)"
/>
<template #right>
<van-button square text="删除" type="danger" class="delete-button" @click="delFile(item.id)" />
<van-button square type="primary" text="下载" class="delete-button" @click="downloadFile(item)" />
</template>
</van-swipe-cell>
</van-pull-refresh>
</van-form>
<!-- 文件上传 -->
<van-form @submit="onSubmit" enctype="multipart/form-data">
<van-uploader
style="float: left;margin-left: 14px;margin-top: 15px;"
v-model="fileList"
accept="file"
>
<van-button size="small" icon="plus" type="primary">上传文件</van-button>
</van-uploader>
</van-form>
<van-overlay :show="show" @click="show = false">
<van-loading size="24px" color="#1989fa" style="top: 50%;" vertical>加载中...</van-loading>
</van-overlay>
<van-image-preview v-model="showPicture" :images="images" :startPosition="startPositionIndex"/>
<a id="downLoadExcel" :href="downLoadTemplateURL" :download="filename"></a>
</div>
</template>
<script>
import { Toast } from 'vant';
export default {
name: 'chooseProples',
data() {
return {
showPicture: false,
show: false,
isLoading: false,
fileList: [],
list: [],
result: [],
id:'',
realpath: './static/img/images/wenjianjia.jpg',
pdfPath: './static/img/images/pdfIcon.jpg',
excelPath: './static/img/images/excelIcon.jpg',
wordPath: './static/img/images/wordIcon.jpg',
path: [],
images: [],
iamgeFileName: [],
startPositionIndex: 1,
num: 0,
downLoadTemplateURL: '',
filename: '',
pdfList: [],
excelList: [],
wordList: [],
};
},
created(){
this.getParams()
this.getList()
},
components:{
},
methods: {
clickView(path, index, filedata) {
if(filedata.extend == 'pdf') {
this.pdfList.some((item, i) => {
if(item.imgId == filedata.id) {
this.$router.push({
name: 'pdfPreview',
params: {
id: this.id,
url: item.url
}
})
return true;
}
})
} else if(filedata.extend == 'xlsx' || filedata.extend == 'xls') {
this.excelList.some((item, i) => {
if(item.imgId == filedata.id) {
this.$router.push({
name: 'excel',
params: {
id: this.id,
url: item.url
}
})
return true;
}
})
} else if(filedata.extend == 'docx' || filedata.extend == 'doc') {
this.wordList.some((item, i) => {
if(item.imgId == filedata.id) {
this.$router.push({
name: 'word',
params: {
id: this.id,
url: item.url
}
})
return true;
}
})
} else {
this.iamgeFileName.some((item, i) => {
if(item == filedata.id) {
this.startPositionIndex = i;
this.showPicture = true;
return true;
}
})
}
},
async getFile(res) {
for(let i = 0; i < res.length; i++) {
await this.$http.get('/aeo_smart/ICommonApi.do?viewFile_chapter', {
params: {
fileid: res[i].id,
subclassname: res[i].subclassname,
fid: res[i].id
},
responseType: 'blob'
})
.then((response) =>{
let dataInfo = response.data
let fileHeader = '';
let typeContent = dataInfo.type;
if(res[i].extend == 'png' || res[i].extend == 'gif' || res[i].extend == 'x-icon' || res[i].extend == 'jpg') {
fileHeader = "data:image/png;base64,";
typeContent = "image/" + res[i].extend;
let blob = new Blob([dataInfo], { type: typeContent })
const reader = new FileReader();
let url = '';
reader.onload = function (e) {
url = e.target.result
}
setTimeout(rese => {
this.path.push(url);
this.images.push(url);
this.iamgeFileName.push(res[i].id);
}, 500)
reader.readAsDataURL(blob);
}else if(res[i].extend == 'pdf') {
var url = window.URL.createObjectURL(dataInfo);
setTimeout(rese => {
this.pdfList.push({url: url, imgId: res[i].id})
this.path.push(this.pdfPath);
}, 500)
} else if(res[i].extend == 'xlsx' || res[i].extend == 'xls') {
var url = window.URL.createObjectURL(dataInfo);
setTimeout(rese => {
this.excelList.push({url: url, imgId: res[i].id})
this.path.push(this.excelPath);
}, 500)
} else if(res[i].extend == 'docx' || res[i].extend == 'doc') {
var url = window.URL.createObjectURL(dataInfo);
setTimeout(rese => {
this.wordList.push({url: url, imgId: res[i].id})
this.path.push(this.wordPath);
}, 500)
} else {
setTimeout(rese => {
this.path.push(this.realpath);
}, 500)
}
})
.catch(function (error) {
console.log(error);
});
}
},
back() {
this.$router.push({
name: 'voucherErrorInfo',
params: {
id: this.id
}
})
},
onRefresh() {
setTimeout(() => {
this.getList();
this.isLoading = false;
}, 1000);
},
getParams() {
let routerParams = this.$route.params.id
this.id = routerParams
},
getList () {
this.$http.post('/aeo_smart/ICommonApi.do?getVoucherErrorFile', {
fid: this.id
})
.then((response) =>{
if (response.data.issuccess === "success") {
this.path = [];
this.images = [];
this.iamgeFileName = [];
this.list = response.data.content
this.getFile(response.data.content)
}
this.isLoading = false;
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
},
fileSubmit() {
this.onSubmit()
},
async onSubmit () {
let loginUser = JSON.parse(window.localStorage.getItem('loginUser'));
let formData = new window.FormData();
if(this.fileList.length > 0) {
this.show = true;
for(let i = 0; i < this.fileList.length; i++) {
formData.append("file", this.fileList[i].file);
await this.$http.post('/aeo_smart/ICommonApi.do?saveFiles&fid=' + this.id + '&type=voucherError' + '&loginUser=' + loginUser.id + '&departid=' + loginUser.departid, formData, { headers: { "Content-Type": "multipart/form-data" }})
.then((response) =>{
if (response.data.issuccess === "success") {
formData.delete("file");
}
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
}
this.onRefresh();
this.fileList = []
setTimeout(() => {
this.show = false;
Toast.success("上传成功");
}, 1000);
}
},
delFile(id) {
this.$dialog.confirm({
message: '确定删除吗?',
}).then(() => {
this.show = true;
this.$http.get('/aeo_smart/ICommonApi.do?doDel', {
params: {
id: id
}
})
.then((response) =>{
if (response.data.issuccess === "success") {
this.onRefresh();
setTimeout(() => {
this.show = false;
Toast.success(response.data.result);
}, 1000);
}else {
this.$notify(response.data.result);
}
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
});
},
downloadFile(file) {
this.$http.get('/aeo_smart/ICommonApi.do?viewFile_chapter', {
params: {
fileid: file.id,
subclassname: file.subclassname,
fid: file.id
},
responseType: 'blob'
})
.then((response) =>{
let blob = response.data
let fileName = file.attachmenttitle + "." + file.extend;
if ('download' in document.createElement('a')) {
var url = window.URL.createObjectURL(blob);
this.downLoadTemplateURL = url;
this.filename = fileName;
setTimeout( () => {
document.querySelector("#downLoadExcel").click();
},500)
setTimeout(() => {
window.URL.revokeObjectURL(url);
}, 1000);
} else {
navigator.msSaveBlob(blob, fileName)
}
})
.catch(function (error) {
console.log(error);
});
},
}
};
</script>
<style scoped>
.van-swipe-cell {
position: relative;
box-sizing: border-box;
width: 100%;
padding-left: 16px;
overflow: hidden;
color: #323233;
font-size: 14px;
line-height: 24px;
background-color: #fff;
text-align: left;
}
.delete-button {
height: 100%;
margin-left: 1px;
}
.submit{
position: absolute;
bottom: 0;
left: 0;
}
</style>