Files
admin-govern/src/views/govern/device/control/offLineDataImport/index.vue

189 lines
6.9 KiB
Vue
Raw Normal View History

2024-07-22 10:35:01 +08:00
<!-- 离线数据导入 -->
<template>
2024-07-22 13:16:14 +08:00
<el-dialog v-model="dialogVisible" title="文件列表" width="70%" draggable>
2024-07-22 10:35:01 +08:00
<!-- 上传文件列表 -->
<div class="offline_data">
<div class="offline_data_btn">
<el-button
type="primary"
@click="handleUpload"
:loading="loading"
size="small"
2024-07-24 14:51:51 +08:00
:disabled="offLineFileList.length == 0 || disableHandleUpload"
>
2024-07-22 13:16:14 +08:00
开始上传
</el-button>
2024-07-24 14:51:51 +08:00
<el-button
type="primary"
@click="removeAllFile"
size="small"
:loading="loading"
:disabled="offLineFileList.length == 0"
>
2024-07-22 13:16:14 +08:00
清空文件
</el-button>
2024-07-22 10:35:01 +08:00
</div>
<div class="offline_data_list">
<el-button
:loading="loading"
style="margin-left: 10px"
size="small"
type="primary"
@click="submitUpload"
>
上传离线数据
</el-button>
2024-07-22 13:16:14 +08:00
<div>
<el-table :data="offLineFileList" style="width: 100%" height="280" border stripe :key="updateKey">
2024-07-22 13:16:14 +08:00
<el-table-column prop="name" label="文件名" align="center"></el-table-column>
<el-table-column prop="webkitRelativePath" label="文件地址" align="center"></el-table-column>
<el-table-column prop="status" label="状态" align="center" width="250">
2024-07-22 13:16:14 +08:00
<template #default="{ row }">
<el-progress v-if="row.status == 0" :percentage="0">
<el-button text>等待上传</el-button>
</el-progress>
<el-progress v-if="row.status == -1" :percentage="30" :indeterminate="true">
<el-button text>上传中...</el-button>
</el-progress>
<el-progress v-if="row.status == 1" status="success" :percentage="100">
<el-button text>上传成功</el-button>
</el-progress>
2024-07-22 13:16:14 +08:00
</template>
</el-table-column>
2024-07-22 10:35:01 +08:00
<el-table-column label="操作" width="100" fixed="right" align="center">
<template #default="{ row }">
<!-- <el-button size="small" @click="previewFile(row)">预览</el-button> -->
<el-button
size="small"
:disabled="row.status == -1"
type="danger"
text
@click="removeFile(row)"
>
移除
</el-button>
2024-07-22 10:35:01 +08:00
</template>
</el-table-column>
</el-table>
</div>
</div>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="close">取消</el-button>
<!-- <el-button type="primary" @click="close">提交</el-button> -->
</div>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import { ref, onMounted, nextTick, watch } from 'vue'
2024-07-22 10:35:01 +08:00
import JSZip from 'jszip'
import { saveAs } from 'file-saver'
import { ElMessageBox } from 'element-plus'
2024-07-22 13:16:14 +08:00
import { uploadOffLineDataFile } from '@/api/cs-device-boot/EquipmentDelivery.ts'
2024-07-22 10:35:01 +08:00
const dialogVisible = ref(false)
const loading = ref(false)
2024-07-22 13:16:14 +08:00
const offLineFileList: any = ref([])
const disableHandleUpload = ref(true)
//上传离线数据
2024-07-22 10:35:01 +08:00
const submitUpload = e => {
const input = document.createElement('input')
input.type = 'file'
input.setAttribute('allowdirs', 'true')
input.setAttribute('directory', 'true')
input.setAttribute('webkitdirectory', 'true')
input.multiple = true
document.querySelector('body').appendChild(input)
// todo 这里增加了input标签可以给它删掉
input.click()
2024-07-23 17:28:31 +08:00
input.onchange = function (e) {
2024-07-22 10:35:01 +08:00
const formData = new FormData()
const file = e.target['files']
for (let i = 0; i < file.length; i++) {
formData.append('files', file[i])
}
2024-07-22 13:16:14 +08:00
offLineFileList.value = formData.getAll('files')
offLineFileList.value.map((item: any) => {
item.status = 0
})
2024-07-24 14:51:51 +08:00
disableHandleUpload.value = false
2024-07-22 10:35:01 +08:00
}
}
2024-07-22 13:16:14 +08:00
//清空文件
const removeAllFile = () => {
offLineFileList.value = []
}
2024-07-22 10:35:01 +08:00
//移除文件
const removeFile = async (file: any) => {
2024-07-22 13:16:14 +08:00
const delIndex = offLineFileList.value.findIndex((item: any) => {
return item.webkitRelativePath == file.webkitRelativePath && item.name == file.name
})
offLineFileList.value.splice(delIndex, 1)
return
2024-07-22 10:35:01 +08:00
}
2024-07-22 13:16:14 +08:00
//开始上传
const updateKey = ref(0)
const handleUpload = () => {
loading.value = true
2024-07-23 17:28:31 +08:00
const subForm = new FormData()
subForm.append('devId', deviceId.value)
subForm.append('lineId', lineId.value)
// subForm.append('files', offLineFileList.value)
let webkitRelativePathList: any = []
offLineFileList.value.map((item: any, index: any) => {
// obj.paths.push(item.webkitRelativePath)
subForm.append(`files[${index}]`, item)
// subForm.append(`paths[${index}]`, item.webkitRelativePath)
webkitRelativePathList.push(item.webkitRelativePath)
})
subForm.append('paths', webkitRelativePathList.join(','))
offLineFileList.value.map((item: any) => {
item.status = -1
})
// 操作数据后更新视图
updateKey.value += 1
2024-07-24 14:51:51 +08:00
disableHandleUpload.value = true
2024-07-23 17:28:31 +08:00
// return;
uploadOffLineDataFile(subForm).then(res => {
2024-07-23 17:28:31 +08:00
if (res.code == 'A0000') {
offLineFileList.value.map((item: any) => {
item.status = 1
})
// 操作数据后更新视图
updateKey.value += 1
loading.value = false
disableHandleUpload.value = true
2024-07-22 13:16:14 +08:00
}
})
}
2024-07-23 17:28:31 +08:00
const deviceId: any = ref()
const lineId: any = ref()
const open = (devId: any, lineIds: any) => {
deviceId.value = devId
lineId.value = lineIds
2024-07-22 10:35:01 +08:00
dialogVisible.value = true
}
const close = () => {
dialogVisible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.offline_data {
width: 100%;
height: 400px;
.offline_data_btn {
width: 100%;
height: 40px;
display: flex;
align-items: center;
justify-content: flex-end;
}
.offline_data_list {
width: 100%;
height: 300px;
}
}
</style>