|
@@ -4,6 +4,7 @@ import { useRoute, useRouter } from "vue-router";
|
|
|
import * as XLSX from "xlsx";
|
|
|
import {
|
|
|
assessmentDownloadDataRosterTemplateApi,
|
|
|
+ importAssessmentQuotaDisplayBackData,
|
|
|
postImportAssessmentTemplate
|
|
|
} from "@/api/download";
|
|
|
import { ElMessage } from "element-plus";
|
|
@@ -31,64 +32,75 @@ const tableData = ref<any>([
|
|
|
{ type: "" },
|
|
|
{ from: "" }
|
|
|
]);
|
|
|
-const handleUploadChange = (file: File) => {
|
|
|
+const handleUploadChange = async (file: File) => {
|
|
|
fileDocument.value = file; // 保存文件
|
|
|
+ const formData = new FormData();
|
|
|
+ formData.append("file", fileDocument.value);
|
|
|
+ const fileToSend = formData.get("file");
|
|
|
+ const { code, data, msg } = await importAssessmentQuotaDisplayBackData({
|
|
|
+ file: fileToSend,
|
|
|
+ type: route.query.assessmentType,
|
|
|
+ assessmentId: route.query.id
|
|
|
+ });
|
|
|
const reader = new FileReader();
|
|
|
let fileType = file.name.slice(-4);
|
|
|
if (fileType == "xlsx") {
|
|
|
- console.log(query.value.assessmentType, "打印type值");
|
|
|
- reader.onload = e => {
|
|
|
- const data = new Uint8Array(e.target?.result as ArrayBuffer);
|
|
|
- const workbook = XLSX.read(data, { type: "array" }); // 读取 Excel 文件
|
|
|
- const firstSheetName = workbook.SheetNames[0]; // 获取第一个工作表名
|
|
|
- const worksheet = workbook.Sheets[firstSheetName]; // 获取第一个工作表
|
|
|
- const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 将工作表转为 JSON
|
|
|
- if (
|
|
|
- jsonData[1].length == 6 &&
|
|
|
- (query.value.assessmentType == 0 || query.value.assessmentType == 1)
|
|
|
- ) {
|
|
|
- tableHeaders.value = jsonData[1];
|
|
|
- const arr = jsonData.slice(2).map(row => {
|
|
|
- if (row[0] && row[1] && row[2] && row[3]) {
|
|
|
- const rowData = {};
|
|
|
- tableHeaders.value.forEach((header, index) => {
|
|
|
- if (row[index]) {
|
|
|
- rowData[header] = row[index];
|
|
|
- }
|
|
|
- });
|
|
|
- return rowData;
|
|
|
- }
|
|
|
- });
|
|
|
- tableData.value = arr.filter(item => item);
|
|
|
- ElMessage.success("文件上传成功");
|
|
|
- uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
- } else if (query.value.assessmentType == 2) {
|
|
|
- tableHeaders.value = [
|
|
|
- "用户组",
|
|
|
- "用户组编号",
|
|
|
- "考核模板",
|
|
|
- "指标名称",
|
|
|
- "完成值",
|
|
|
- "得分"
|
|
|
- ]; // 表头
|
|
|
- const arr = jsonData.slice(2).map(row => {
|
|
|
- if (row[0] && row[1] && row[2] && row[3]) {
|
|
|
- const rowData = {};
|
|
|
- tableHeaders.value.forEach((header, index) => {
|
|
|
- if (row[index]) {
|
|
|
- rowData[header] = row[index];
|
|
|
- }
|
|
|
- });
|
|
|
- return rowData;
|
|
|
- }
|
|
|
- });
|
|
|
- tableData.value = arr.filter(item => item);
|
|
|
- ElMessage.success("文件上传成功");
|
|
|
- uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
- } else {
|
|
|
- ElMessage.error("文件为空或格式不正确");
|
|
|
- }
|
|
|
- };
|
|
|
+ if (data.code == 200) {
|
|
|
+ reader.onload = e => {
|
|
|
+ const data = new Uint8Array(e.target?.result as ArrayBuffer);
|
|
|
+ const workbook = XLSX.read(data, { type: "array" }); // 读取 Excel 文件
|
|
|
+ const firstSheetName = workbook.SheetNames[0]; // 获取第一个工作表名
|
|
|
+ const worksheet = workbook.Sheets[firstSheetName]; // 获取第一个工作表
|
|
|
+ const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); // 将工作表转为 JSON
|
|
|
+ if (
|
|
|
+ jsonData[1].length == 6 &&
|
|
|
+ (query.value.assessmentType == 0 || query.value.assessmentType == 1)
|
|
|
+ ) {
|
|
|
+ tableHeaders.value = jsonData[1];
|
|
|
+ const arr = jsonData.slice(2).map(row => {
|
|
|
+ if (row[0] && row[1] && row[2] && row[3]) {
|
|
|
+ const rowData = {};
|
|
|
+ tableHeaders.value.forEach((header, index) => {
|
|
|
+ if (row[index]) {
|
|
|
+ rowData[header] = row[index];
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return rowData;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tableData.value = arr.filter(item => item);
|
|
|
+ ElMessage.success("文件上传成功");
|
|
|
+ uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
+ } else if (query.value.assessmentType == 2) {
|
|
|
+ tableHeaders.value = [
|
|
|
+ "用户组",
|
|
|
+ "用户组编号",
|
|
|
+ "考核模板",
|
|
|
+ "指标名称",
|
|
|
+ "完成值",
|
|
|
+ "得分"
|
|
|
+ ]; // 表头
|
|
|
+ const arr = jsonData.slice(2).map(row => {
|
|
|
+ if (row[0] && row[1] && row[2] && row[3]) {
|
|
|
+ const rowData = {};
|
|
|
+ tableHeaders.value.forEach((header, index) => {
|
|
|
+ if (row[index]) {
|
|
|
+ rowData[header] = row[index];
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return rowData;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tableData.value = arr.filter(item => item);
|
|
|
+ ElMessage.success("文件上传成功");
|
|
|
+ uploadShow.value = false; // 隐藏上传区域,显示数据表
|
|
|
+ } else {
|
|
|
+ ElMessage.error("文件为空或格式不正确");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ ElMessage.error(data.msg);
|
|
|
+ }
|
|
|
} else {
|
|
|
ElMessage.error("请上传xlsx文件");
|
|
|
}
|
|
@@ -104,26 +116,25 @@ const postImportAssessmentTemplateApi = async data => {
|
|
|
try {
|
|
|
const formData = new FormData();
|
|
|
|
|
|
- // 将 tableData 转换为 JSON 字符串
|
|
|
- const jsonTableData = JSON.stringify(tableData.value);
|
|
|
+ // 将 tableData 转换为 XLSX 格式
|
|
|
+ // const ws = XLSX.utils.json_to_sheet(tableData.value); // 将表格数据转为工作表
|
|
|
+ // const wb = XLSX.utils.book_new(); // 创建一个新的工作簿
|
|
|
+ // XLSX.utils.book_append_sheet(wb, ws, "Sheet1"); // 将工作表添加到工作簿
|
|
|
+ // // 将工作簿转换为二进制的 XLSX 文件数据
|
|
|
+ // const xlsxData = XLSX.write(wb, { bookType: "xlsx", type: "array" });
|
|
|
|
|
|
- // 创建一个 Blob 对象,将 JSON 数据作为二进制文件上传
|
|
|
- const blob = new Blob([jsonTableData], { type: "application/json" });
|
|
|
+ // // 创建一个 Blob 对象,指定 MIME 类型为 XLSX 格式
|
|
|
+ // const blob = new Blob([xlsxData], {
|
|
|
+ // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
+ // });
|
|
|
|
|
|
- // 创建一个文件对象
|
|
|
- const file = new File([blob], "tableData.json", {
|
|
|
- type: "application/json"
|
|
|
- });
|
|
|
+ // // 创建一个文件对象
|
|
|
+ // const file = new File([blob], "tableData.xlsx", {
|
|
|
+ // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
|
+ // });
|
|
|
|
|
|
// 将生成的文件添加到 FormData 中
|
|
|
- formData.append("file", file);
|
|
|
-
|
|
|
- // 你可以在这里使用 FileReader 进一步处理文件数据
|
|
|
- const reader = new FileReader();
|
|
|
- reader.onload = function (event) {
|
|
|
- const arrayBuffer = event.target.result;
|
|
|
- // 如果需要在上传前处理文件的二进制数据,可以在这里做
|
|
|
- };
|
|
|
+ formData.append("file", fileDocument.value);
|
|
|
|
|
|
// 获取文件并执行上传
|
|
|
const fileToSend = formData.get("file");
|
|
@@ -133,12 +144,11 @@ const postImportAssessmentTemplateApi = async data => {
|
|
|
type: route.query.assessmentType,
|
|
|
assessmentId: route.query.id
|
|
|
});
|
|
|
+
|
|
|
if (data.code === 200) {
|
|
|
// 根据后端返回的 code 判断是否成功
|
|
|
ElMessage.success(data.msg);
|
|
|
// 重置上传状态
|
|
|
- // uploadShow.value = true;
|
|
|
- // $router.push("/IndexDefine/children/define");
|
|
|
router.back();
|
|
|
uploadShow.value = !uploadShow.value;
|
|
|
tableHeaders.value = []; // 清空表头
|