2023-12-18 14:29:28 +08:00
|
|
|
|
<template>
|
2024-04-11 17:55:47 +08:00
|
|
|
|
<el-dialog v-model="dialogVisible" :title="title" width="50%">
|
|
|
|
|
<el-upload action="#" v-model:file-list="fileList" drag accept=".xls, .xlsx"
|
|
|
|
|
:auto-upload="false" :limit="1" :http-request="handleUpload" ref="uploadRef">
|
|
|
|
|
<div class="el-upload__text" style="padding: 40px 10px;font-size: 20px;">
|
|
|
|
|
将文件拖到此处,或 <em>点击上传</em>
|
|
|
|
|
</div>
|
|
|
|
|
<template #tip>
|
|
|
|
|
<div class="el-upload__tip" style="font-size: 16px;">
|
|
|
|
|
仅允许导入xls、xlsx格式文件。<span class="main-color" style="cursor: pointer;"
|
|
|
|
|
@click="downloadTemplate">下载模板</span>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
</el-upload>
|
2023-12-18 14:29:28 +08:00
|
|
|
|
|
2024-04-11 17:55:47 +08:00
|
|
|
|
<div style="text-align: right;margin-top: 20px;">
|
|
|
|
|
<el-button class="f18" @click="dialogVisible = false">取消</el-button>
|
|
|
|
|
<el-button class="f18" type="primary" @click="importData">确认</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
</el-dialog>
|
2023-12-18 14:29:28 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang='ts' setup>
|
2024-04-11 17:55:47 +08:00
|
|
|
|
import {ref} from 'vue'
|
|
|
|
|
import type {UploadUserFile} from 'element-plus'
|
|
|
|
|
import {ElMessage} from 'element-plus'
|
|
|
|
|
import {downBlobFile} from "@/utils/file-util";
|
|
|
|
|
import {handleHttpUploadUrl} from "@/api/file-upload";
|
2023-12-18 14:29:28 +08:00
|
|
|
|
|
|
|
|
|
const emit = defineEmits(['success', 'error'])
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
2024-04-11 17:55:47 +08:00
|
|
|
|
title: String,
|
|
|
|
|
templateUrl: String, // 模板下载地址
|
|
|
|
|
importUrl: String, // 上传接口地址
|
2023-12-18 14:29:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const dialogVisible = ref(false)
|
|
|
|
|
const fileList = ref<UploadUserFile[]>([])
|
2024-04-11 17:55:47 +08:00
|
|
|
|
const uploadRef = ref()
|
2023-12-18 14:29:28 +08:00
|
|
|
|
|
|
|
|
|
defineExpose({
|
2024-04-11 17:55:47 +08:00
|
|
|
|
open,
|
|
|
|
|
close
|
2023-12-18 14:29:28 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function open() {
|
2024-04-11 17:55:47 +08:00
|
|
|
|
dialogVisible.value = true
|
2023-12-18 14:29:28 +08:00
|
|
|
|
}
|
2024-04-11 17:55:47 +08:00
|
|
|
|
|
2023-12-18 14:29:28 +08:00
|
|
|
|
function close() {
|
2024-04-11 17:55:47 +08:00
|
|
|
|
dialogVisible.value = false
|
2023-12-18 14:29:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const downloadTemplate = () => {
|
2024-04-11 17:55:47 +08:00
|
|
|
|
downBlobFile(props.templateUrl, {}, "temp.xlsx")
|
2023-12-18 14:29:28 +08:00
|
|
|
|
}
|
|
|
|
|
const importData = () => {
|
2024-04-11 17:55:47 +08:00
|
|
|
|
// console.log(fileList.value)
|
|
|
|
|
if (fileList.value.length === 0) {
|
|
|
|
|
ElMessage.warning('请上传符合格式的xls或xlsx文件')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
uploadRef.value.submit()
|
|
|
|
|
// emit('error')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleUpload(options: any) {
|
|
|
|
|
handleHttpUploadUrl(options, props.importUrl).then((res: any) => {
|
2024-04-28 18:08:16 +08:00
|
|
|
|
if (res.code == 0) {
|
|
|
|
|
ElMessage.success('上传成功')
|
|
|
|
|
close()
|
|
|
|
|
emit('success')
|
|
|
|
|
} else {
|
|
|
|
|
ElMessage.error(res.msg ? res.msg : "上传失败")
|
|
|
|
|
emit("error");
|
|
|
|
|
}
|
2024-04-11 17:55:47 +08:00
|
|
|
|
})
|
2023-12-18 14:29:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang='scss' scoped></style>
|