| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <template>
- <div>
- <el-card v-if="cardVisible">
- <template #header>
- <div class="flex justify-between items-center pop-head">
- <div style="font-size: 16px; font-weight: 600">批量导入</div>
- <el-icon :size="18" class="mr-3" @click="() => { emits('close'); }" style="cursor: pointer;">
- <Close />
- </el-icon>
- </div>
- </template>
- <div class="upload-content">
- <el-upload ref="upload" style="width: 384px; height: 192px; border-radius: 8px" :headers="headers"
- :multiple="false" :limit="1" drag action="/skyeye-admin-api/addCameraList/uploadForm" :with-credentials="true"
- :auto-upload="false" :before-upload="beforeUpload" :on-success="handleUploadSuccess" :on-exceed="handleExceed"
- :on-change="handleChange" :on-remove="handleRemove">
- <el-icon class="el-icon--upload" style="width: 33px; height: 42px; color: #409efc;">
- <Document />
- </el-icon>
- <div class="el-upload__text">
- <div style="font-size: 12px; color: red; margin-bottom: 5px;">请下载模板并按要求填写后上传</div>
- <div style="font-size: 16px">点击或将文件拖拽到这里上传</div>
- <div style="font-size: 12px; color: rgba(0, 0, 0, 0.45); margin-top: 5px;">文件支持.xlsx .xls格式,仅支持上传一个文件</div>
- </div>
- </el-upload>
- <div style="margin-top: 72px; margin-left: 128px; display: flex">
- <el-icon :size="18" style="margin-top: 7px;">
- <Download />
- </el-icon>
- <el-tooltip content="点击下载场景字段对应的code信息" placement="top" effect="light">
- <span style="color:#409efc; margin-top: 6px; margin-right: 12px; cursor: pointer;"
- @click="handleDownloadSceneCode">场景code信息查询</span>
- </el-tooltip>
- <el-button @click="handleDownloadTemplate">下载模板</el-button>
- <el-button type="primary" @click="handleImport" :disabled="isImportEnable">导入</el-button>
- </div>
- </div>
- </el-card>
- <el-dialog v-model="DialogVisibleErr" title="Warning" width="50%" align-center @close="() => { emits('update'); }">
- <template #header>
- <el-icon :size="24" color="#f2b20a" style="margin: 0 5px 2px">
- <WarnTriangleFilled />
- </el-icon>
- <div class="header-text">添加提示</div>
- </template>
- <div class="sum-count">
- 成功上传 <span class="succ-sum">{{ sucCount }}</span> 条,
- 失败 <span class="err-sum">{{ errCount }}</span> 条
- </div>
- <div class="err-info">
- <ul v-for="(item, index) in errDetail" :key="index">
- <li v-html="item"></li>
- </ul>
- </div>
- <template #footer>
- <el-button type="primary" @click="handleErrComfirm"> 确定 </el-button>
- </template>
- </el-dialog>
- </div>
- </template>
- <script setup lang="ts">
- import { ref } from 'vue';
- import axios, { AxiosRequestConfig } from 'axios';
- import { useUserStore } from '@/store/modules/user';
- import { genFileId, ElMessage } from 'element-plus';
- import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
- import { Close, Document, WarnTriangleFilled, Download } from '@element-plus/icons-vue';
- import { useGlobSetting } from '@/hooks/setting';
- const emits = defineEmits(['close', 'update']);
- const userStore = useUserStore();
- const headers = {
- Satoken: userStore.getToken,
- Tenantid: userStore.getTenantId,
- };
- const upload = ref<UploadInstance>();
- const cardVisible = ref<boolean>(true);
- const isImportEnable = ref<boolean>(true);
- const DialogVisibleErr = ref<boolean>(false);
- const sucCount = ref<number>(0);
- const errCount = ref<number>(0);
- const errDetail = ref<string[]>([]);
- const { urlPrefix } = useGlobSetting()
- // 下载场景code信息查询表
- const handleDownloadSceneCode = async () => {
- try {
- const config: AxiosRequestConfig = {
- headers,
- responseType: 'blob',
- };
- const response = await axios.get(urlPrefix + '/addCameraList/downloadWorkspaceCodeForm', config);
- const blob = new Blob([response.data], {
- type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- });
- // 创建下载链接
- let downloadLink: HTMLAnchorElement | null = document.createElement('a');
- const url = window.URL.createObjectURL(blob);
- downloadLink.href = url;
- downloadLink.download = '场景code信息查询.xlsx';
- downloadLink.click();
- // 移除下载链接
- window.URL.revokeObjectURL(url);
- downloadLink = null;
- } catch (error) {
- console.error('Error downloading file:', error);
- }
- };
- // 下载模板
- const handleDownloadTemplate = async () => {
- try {
- const config: AxiosRequestConfig = {
- headers,
- responseType: 'blob',
- };
- const response = await axios.get('/skyeye-file-upload/skyeye/CAMERALIST_TEMPLATE/camera-upload-template.xlsx', config);
- const blob = new Blob([response.data], {
- type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- });
- // 创建下载链接
- let downloadLink: HTMLAnchorElement | null = document.createElement('a');
- const url = window.URL.createObjectURL(blob);
- downloadLink.href = url;
- downloadLink.download = '相机批量添加.xlsx';
- downloadLink.click();
- // 移除下载链接
- window.URL.revokeObjectURL(url);
- downloadLink = null;
- } catch (error) {
- console.error('Error downloading file:', error);
- }
- };
- // 导入
- const handleImport = async () => {
- upload.value!.submit();
- };
- // 上传文件之前的钩子,参数为上传的文件。即上传之前验证文件类型/后缀
- const beforeUpload = (file) => {
- const isExcel = /\.(xlsx|xls)$/.test(file.name.toLowerCase());
- if (!isExcel) {
- // 提示用户选择正确的文件类型
- ElMessage({
- message: '仅支持上传.xlsx .xls格式文件',
- type: 'error',
- });
- return false; // 阻止上传
- };
- return true; // 允许上传
- };
- const handleUploadSuccess = (response, _file, _fileList) => {
- console.log(response);
- sucCount.value = response.data.successCount;
- errCount.value = response.data.failCount;
- errDetail.value = response.data.errorList;
- try {
- if (errDetail.value.length > 0) {
- errDetail.value.forEach((item, index) => {
- if (item.indexOf('【添加失败】') >= 0) {
- errDetail.value[index] = item.replace('【添加失败】', '<span style="color: #ff4d4f">【添加失败】</span>')
- } else if (item.indexOf('【添加成功】') >= 0) {
- errDetail.value[index] = item.replace('【添加成功】', '<span style="color: #52c41a">【添加成功】</span>')
- }
- })
- };
- if (sucCount.value != 0 && errCount.value === 0 && errDetail.value.length === 0) {
- ElMessage({
- message: '添加成功', // 1.全部添加成功 —— failCount === 0
- type: 'success',
- });
- emits('update');
- } else {
- DialogVisibleErr.value = true; // 2.有错误 —— 显示错误dialog
- };
- cardVisible.value = false;
- } catch (error) {
- ElMessage({
- message: '系统错误',
- type: 'error',
- });
- emits('update');
- };
- };
- const handleErrComfirm = () => {
- DialogVisibleErr.value = false;
- emits('update');
- };
- // 当超出只能上传一个文件的限制时,自动替换上一个文件
- const handleExceed: UploadProps['onExceed'] = (files) => {
- upload.value!.clearFiles();
- const file = files[0] as UploadRawFile;
- file.uid = genFileId();
- upload.value!.handleStart(file);
- };
- const handleChange = () => {
- isImportEnable.value = false;
- };
- const handleRemove = () => {
- isImportEnable.value = true;
- };
- </script>
- <style scoped>
- .upload-content {
- margin-left: 90px;
- margin-top: 36px;
- }
- :deep(.el-dialog) {
- padding: 0px;
- border-radius: 5px;
- .el-dialog__header {
- display: flex;
- align-items: flex-end;
- height: 70px;
- padding: 0px 0px 10px 10px;
- border-bottom: 1px solid #e7e7e7;
- .header-text {
- font-size: 20px;
- }
- }
- .el-dialog__headerbtn {
- top: 22px;
- .el-dialog__close {
- color: black;
- }
- }
- .el-dialog__body {
- padding: 20px;
- .sum-count {
- margin: 10px 0 20px 20px;
- font-size: 20px;
- .succ-sum {
- color: #52c41a;
- }
- .err-sum {
- color: #ff4d4f;
- }
- }
- .err-info {
- height: 200px;
- margin-left: 20px;
- overflow: auto;
- }
- }
- .el-dialog__footer {
- margin: 0 20px 20px 0;
- }
- }
- li {
- font-size: 14px;
- margin-bottom: 2px;
- }
- </style>
|