提交 465f2d86 authored 作者: 何宗全's avatar 何宗全

修改读取文件

上级 77532c42
......@@ -14,8 +14,8 @@
</view>
</template>
<script>
import { getReportSearch } from "@/request/index.js";
import { getReportList } from "@/request/index.js";
import moment from "moment";
export default {
props: {
hiddenSearch: {
......@@ -35,9 +35,18 @@ export default {
};
},
created() {
getReportSearch().then((res) => {
this.searchFrom.reportTime = res.defaultReportTime;
this.reportList = res.reportList;
getReportList().then((res) => {
let arr = Object.keys(res).sort().reverse();
let list = []
arr.map(item=>{
list.push({
value: item,
text: moment(item).format('YYYY年MM月')
})
})
this.searchFrom.reportTime = arr.length > 0 ? arr[0] : '';
this.reportList = list;
this.searchFrom.reportTime && this.change('reportTime', this.searchFrom.reportTime )
});
},
methods: {
......
......@@ -335,15 +335,14 @@ export default {
async init() {
const allReport = await getReportList();
this.allReport = allReport;
// getReportSearch().then((res) => {
// console.log("-getReportSearch-", res);
getReportSearch().then((res) => {
console.log("-getReportSearch-", res);
this.getDetailData(res.defaultReportTime);
});
// this.getDetailData(res.defaultReportTime);
// });
},
getDetailData(reportTime) {
const result = this.allReport.filter(item => item.data.reportTime == reportTime)[0]
const result = JSON.parse(this.allReport?.[reportTime] || "{}")
this.detailData = mergeObjectsWithUnderscoreKey(result.data);
this.detailData.alarmLists = result?.data?.alarm?.hbMap || {}
......
......@@ -113,9 +113,10 @@ export const getReportList = () => {
readReportData()
.then((res) => {
const temp = res.map(item => JSON.parse(item))
// const temp = res.map(item => JSON.parse(item))
// store.commit("SET_REPORT_LIST", temp);
resolve(temp)
resolve(res)
})
.catch((error) => {
reject(error)
......
......@@ -422,7 +422,7 @@ export const readReportData = () => {
return new Promise((resolve, reject) => {
readFilesInDirectory(directoryPath)
.then((res) => {
resolve(lodash.flattenDeep(res));
resolve(res);
})
.catch((error) => reject(error));
});
......
......@@ -231,6 +231,7 @@ export async function checkFileExists(directoryPath, fileName) {
});
}
// 读取文件名和内容
export const readFilesInDirectory = async (directoryPath) => {
const promiseArr = [];
......@@ -248,7 +249,7 @@ export const readFilesInDirectory = async (directoryPath) => {
entries.forEach((entry) => {
if (entry.isFile) {
// 读取文件内容
promiseArr.push(readFileContent(entry));
promiseArr.push(readFileNameAndContent(entry));
} else if (entry.isDirectory) {
// 递归读取子目录内容
promiseArr.push(readFilesInDirectory(entry.fullPath));
......@@ -259,7 +260,13 @@ export const readFilesInDirectory = async (directoryPath) => {
.then((results) => {
// 扁平化结果数组,因为子目录可能返回一个包含多个文件内容的数组
const flatResults = results.flat(Infinity);
resolve(flatResults);
// 将结果转换为文件名(去掉后缀)和文件内容的对象
const fileContentMap = flatResults.reduce((acc, { name, content }) => {
const fileNameWithoutExtension = name.replace(/\.[^/.]+$/, "");
acc[fileNameWithoutExtension] = content;
return acc;
}, {});
resolve(fileContentMap);
})
.catch((error) => {
console.log("readFilesInDirectory:", error);
......@@ -325,6 +332,34 @@ export async function readFileContent(fileEntry) {
});
}
// 读取单个文件,返回文件名和内容
export async function readFileNameAndContent(fileEntry) {
return new Promise((resolve, reject) => {
fileEntry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = function (e) {
resolve({
name: file.name,
content: e.target.result,
});
};
reader.onerror = function (e) {
reject({
...FILE_ENUM.readFileError,
message: "读取文件失败:" + e.message,
});
};
reader.readAsText(file);
}, (error) => {
reject({
...FILE_ENUM.getFileError,
message: "获取文件失败:" + error.message,
});
});
});
};
/**
* 写入文件
* @param {*} directoryPath
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论