feature/1.0版本页面优化 #1

Merged
Xubx merged 19 commits from feature/1.0版本页面优化 into DEV 2024-12-10 13:53:08 +08:00
5 changed files with 222 additions and 43 deletions
Showing only changes of commit baf4b4246b - Show all commits

70
src/utils/download.js Normal file
View File

@ -0,0 +1,70 @@
function myBrowser() {
var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf('Opera') > -1;
if (isOpera) {
return 'Opera';
} // 判断是否Opera浏览器
if (userAgent.indexOf('Firefox') > -1) {
return 'FF';
} // 判断是否Firefox浏览器
if (userAgent.indexOf('Chrome') > -1) {
return 'Chrome';
}
if (userAgent.indexOf('Safari') > -1) {
return 'Safari';
} // 判断是否Safari浏览器
if (userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 && !isOpera) {
return 'IE';
} // 判断是否IE浏览器
if (userAgent.indexOf('Trident') > -1) {
return 'Edge';
} // 判断是否Edge浏览器
}
function SaveAs5(imgURL) {
var oPop = window.open(imgURL, '', 'width=1, height=1, top=5000, left=5000');
for (; oPop.document.readyState !== 'complete'; ) {
if (oPop.document.readyState === 'complete') break;
}
oPop.document.execCommand('SaveAs');
oPop.close();
}
function download(src, fileName) {
// 创建隐藏的可下载链接
var eleLink = document.createElement('a');
if (fileName) {
eleLink.setAttribute('download', fileName);
} else {
eleLink.download = src;
}
eleLink.style.display = 'none';
// // 字符内容转变成blob地址
eleLink.href = src;
// // 触发点击
document.body.appendChild(eleLink);
eleLink.click();
// // 然后移除
document.body.removeChild(eleLink);
}
function downLoadFile(url, fileName = '') {
if (myBrowser() === 'IE' || myBrowser() === 'Edge') {
SaveAs5(url);
} else {
download(url, fileName);
}
}
function conversionFileDownload(res) {
const a = document.createElement('a');
const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
const temp = res.headers['content-disposition'].split(';')[1].split('filename=')[1];
// 对文件名乱码转义--【Node.js】使用iconv-lite解决中文乱码
const iconv = require('iconv-lite');
iconv.skipDecodeWarning = true; // 忽略警告
const fileName = iconv.decode(temp, 'utf-8');
console.log(fileName);
a.setAttribute('download', fileName);
const objectUrl = URL.createObjectURL(blob);
a.setAttribute('href', objectUrl);
a.click();
}
export { downLoadFile, conversionFileDownload };

View File

@ -261,7 +261,6 @@
this.collegeMajor = '全校';
}
//this.batch[1]null
let major = this.batch.length > 1 ? this.batch[1] : '';
this.topCollege = this.collegeMajor;
this.topBath = this.batch;
this.loading = true;

View File

@ -55,7 +55,7 @@
});
const isShow = ref(false);
const loading = ref(false);
const tableData = ref([]);
const tableData: any = ref([]);
const tableLabel = ref([
{
title: '考试批次',

View File

@ -2,24 +2,31 @@
<div style="background: #ececec; padding-top: 0px; padding-left: 20px; padding-right: 20px; padding-bottom: 20px">
<a-card title="英语四级数据导入" :bordered="false">
<a-row :gutter="2">
<a-col :xl="3" :style="{ marginBottom: '24px' }">
<a-col :xl="24" :style="{ marginBottom: '24px' }">
<div class="clearfix">
<span style="padding-top: 5px">考试批次</span>
<a-space direction="vertical" :size="12">
<a-date-picker v-model:value="month" picker="month" />
</a-space>
</a-col>
<a-col :xl="5" :style="{ marginBottom: '24px' }">
<div class="clearfix">
<!-- accept=".dbf" 属性限制文件类型 -->
<a-upload :file-list="fileList" :before-upload="beforeUpload" @remove="handleRemove">
<a-upload
:file-list="fileList"
accept=".dbf"
:max-count="1"
:customRequest="handleUpload"
:before-upload="beforeUpload"
@remove="handleRemove"
>
<a-button type="primary">
<upload-outlined />
导入文件
</a-button>
</a-upload>
<div class="clearfix">
<a-button preIcon="ant-design:export-outlined" @click="onExportXls"> 下载模板</a-button>
</div>
<!--<div class="clearfix">
<a-button preIcon="ant-design:export-outlined" @click="downloadTemplate"> 下载模板</a-button>
</div>-->
<div class="clearfix">
<a-button type="primary" :disabled="!canUpload" :loading="uploading" @click="handleUpload">
{{ uploading ? 'Uploading' : '确认上传文件' }}
@ -27,7 +34,6 @@
</div>
</div>
</a-col>
<a-col :xl="1" :style="{ marginBottom: '24px' }" />
</a-row>
<a-row :gutter="10">
<a-col :xl="24">
@ -38,15 +44,22 @@
</div>
</template>
<script setup>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import { defHttp } from '/@/utils/http/axios';
import { downloadTemplateExcel } from './dataImportApi';
import { conversionFileDownload } from '@/utils/download';
const loading = ref(false);
//
const month = ref(null);
const url = {
downLoadTemplate: '/cetDataImport/downloadTemplate',
importCet4Data: '/cetDataImport/dbfImport',
loadImportData: '/cet_4/loadImportDataList',
};
//
const fileList = ref([]);
const fileList: any = ref([]);
const uploading = ref(false);
//
@ -62,33 +75,110 @@
//
const beforeUpload = (file) => {
console.log(file);
fileList.value = [];
fileList.value = [...fileList.value, file];
let fileType = file.name.split('.').pop().toLowerCase();
if (fileType !== 'dbf') {
message.error('请上传.dbf文件');
fileList.value = fileList.value.filter((item) => item.uid !== file.uid);
}
return false; //
};
//
const handleUpload = async () => {
const formData = new FormData();
fileList.value.forEach((file) => {
formData.append('files[]', file);
});
//
if (!fileList.value || fileList.value.length === 0) {
message.error('没有选择文件');
return;
}
uploading.value = true;
const file = fileList.value[0];
if (!file) {
message.error('没有选择文件');
return;
}
// Base64
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = async () => {
const base64File = reader.result as string;
//
const payload = {
fileName: file.name,
fileContent: base64File,
batch: '',
level: 'cet4',
};
// batch
let batch: any = month.value;
if (batch) {
batch = batch.format('YYYY-MM-DD');
batch = setDayToFirst(batch); //
payload.batch = batch;
}
try {
let queryParams = {
month: month,
formData: formData,
};
const response = await defHttp.post({ url: url, data: queryParams });
uploading.value = true;
// POST
const response: any = await defHttp.post(
{
url: url.importCet4Data,
data: payload,
headers: { 'Content-Type': 'application/json' },
},
{
isTransformResponse: false,
}
);
//
fileList.value = [];
month.value = null; //
message.success('Upload successfully.');
//
if (response.success) {
message.success('上传成功');
} else {
message.error(response.message || '上传失败');
}
} catch (error) {
message.error('Upload failed.');
console.error('上传失败:', error);
message.error('上传失败');
} finally {
uploading.value = false;
}
};
reader.onerror = (error) => {
console.error('文件读取错误:', error);
message.error('文件读取失败');
};
};
/**
* 'yyyy-MM-dd' 格式的日期字符串的天部分设置为 '01'
* @param {string} dateStr - 原始日期字符串格式为 'yyyy-MM-dd'
* @returns {string} 修改后的日期字符串格式为 'yyyy-MM-01'
*/
const setDayToFirst = (dateStr) => {
// 使
const regex = /^(\d{4})-(\d{2})-(\d{2})$/;
const match = dateStr.match(regex);
if (!match) {
throw new Error("日期格式不正确,应为 'yyyy-MM-dd'");
}
const year = match[1];
const month = match[2];
// '01'
return `${year}-${month}-01`;
};
//
const dataSourceCet4 = ref([]);
//
@ -110,7 +200,7 @@
const fetchData = async () => {
loading.value = true; //
try {
const response = await defHttp.get('/api/words');
const response = await defHttp.post({ url: url.loadImportData });
console.log(response, 'response');
dataSourceCet4.value = response.data; //
} catch (error) {
@ -120,6 +210,17 @@
}
};
//const downloadTemplate = async () => {
// downloadTemplateExcel()
// .then((response: any) => {
// console.log(response, '123');
// conversionFileDownload(response);
// })
// .catch((error) => {
// console.error(error);
// });
//};
// fetchData
onMounted(fetchData);
</script>

View File

@ -0,0 +1,9 @@
import { defHttp } from '/@/utils/http/axios';
export function downloadTemplateExcel() {
return defHttp.request<Blob>({
url: '/cetDataImport/downloadTemplate',
method: 'post',
responseType: 'blob',
});
}