|
@@ -1,18 +1,17 @@
|
|
|
<template>
|
|
|
<n-button type="primary" @click="showImportModal">导入数据</n-button>
|
|
|
<n-modal
|
|
|
- style="width: 30%"
|
|
|
v-model:show="showModal"
|
|
|
:show-icon="false"
|
|
|
preset="dialog"
|
|
|
title="导入"
|
|
|
positive-text="确认"
|
|
|
negative-text="取消"
|
|
|
- @positive-click="addTask"
|
|
|
+ @positive-click="submitCallback"
|
|
|
>
|
|
|
<n-form :model="formValue" label-width="auto" show-require-mark>
|
|
|
<n-form-item label="数据">
|
|
|
- <n-upload :default-upload="false" @change="handleChange">
|
|
|
+ <n-upload :default-upload="false" :max="1" @change="handleChange">
|
|
|
<n-button>上传文件</n-button>
|
|
|
</n-upload>
|
|
|
</n-form-item>
|
|
@@ -22,7 +21,8 @@
|
|
|
|
|
|
<script setup>
|
|
|
import { addTaskData } from "@/api";
|
|
|
-import { read } from "xlsx";
|
|
|
+import { read, utils } from "xlsx";
|
|
|
+import { useDateFormat } from "@vueuse/core";
|
|
|
|
|
|
const props = defineProps({
|
|
|
task: {
|
|
@@ -36,11 +36,35 @@ const message = useMessage();
|
|
|
// 是否展示 Modal
|
|
|
const showModal = ref(false);
|
|
|
|
|
|
+//
|
|
|
+const dataList = ref([]);
|
|
|
+
|
|
|
+/* 读取文件 */
|
|
|
+const readFile = (file) => {
|
|
|
+ return new Promise((resolve) => {
|
|
|
+ const reader = new FileReader();
|
|
|
+ reader.readAsBinaryString(file);
|
|
|
+ reader.onload = (ev) => {
|
|
|
+ resolve(ev.target.result);
|
|
|
+ };
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
// 组件状态变化的回调
|
|
|
-const handleChange = (options) => {
|
|
|
- console.log(options);
|
|
|
- const data = read(options.file.file, { type: "File" });
|
|
|
- console.log(data);
|
|
|
+const handleChange = async ({ file }) => {
|
|
|
+ const dataBinary = await readFile(file.file);
|
|
|
+ const workBook = read(dataBinary, {
|
|
|
+ type: "binary",
|
|
|
+ cellDates: true,
|
|
|
+ });
|
|
|
+ const workSheet = workBook.Sheets[workBook.SheetNames[0]];
|
|
|
+ dataList.value = utils.sheet_to_json(workSheet);
|
|
|
+ dataList.value.forEach((item) => {
|
|
|
+ item["记录时间"] = useDateFormat(
|
|
|
+ item["记录时间"],
|
|
|
+ "YYYY-MM-DD HH:mm:ss"
|
|
|
+ ).value;
|
|
|
+ });
|
|
|
};
|
|
|
|
|
|
// 表单数据
|
|
@@ -59,16 +83,27 @@ const showImportModal = () => {
|
|
|
};
|
|
|
|
|
|
//
|
|
|
+const submitCallback = () => {
|
|
|
+ if (dataList.value.length > 0) {
|
|
|
+ dataList.value.forEach((item) => {
|
|
|
+ addTask(item);
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+//
|
|
|
const addTask = async (item) => {
|
|
|
const { data: res } = await addTaskData({
|
|
|
T_task_id: props.task.T_task_id,
|
|
|
- T_sn: formValue.T_sn,
|
|
|
- T_id: item.T_id,
|
|
|
- T_t: item.T_t,
|
|
|
- T_rh: item.T_rh,
|
|
|
- T_time: item.T_time,
|
|
|
+ T_sn: item["SN"],
|
|
|
+ T_id: item["编号"],
|
|
|
+ T_t: item["温度"],
|
|
|
+ T_rh: item["湿度"],
|
|
|
+ T_time: item["记录时间"],
|
|
|
});
|
|
|
- console.log(res);
|
|
|
+ if (res.Code === 200) {
|
|
|
+ message.success(res.Msg);
|
|
|
+ }
|
|
|
};
|
|
|
</script>
|
|
|
|