This commit is contained in:
张成
2025-11-21 16:53:49 +08:00
commit 8309808835
286 changed files with 32656 additions and 0 deletions

127
tool/funTool.js Normal file
View File

@@ -0,0 +1,127 @@
const fs = require("fs");
const request = require("request");
const path = require("path");
const { sys_parameter } = require("../middleware/baseModel");
module.exports = {
async getParameterByKey(key) {
let res = await sys_parameter.findOne({ where: { key: key } });
if (res) {
return res.value;
}
return "";
},
delay(seconds = 1, callback) {
let promise = new Promise((resolve, reject) => {
setTimeout(() => {
if (callback) {
callback();
}
resolve(true);
}, seconds * 1000);
});
return promise;
},
getParantNode(row, moduleRows) {
let nodeIds = [];
if (row) {
nodeIds.push(row.id);
}
let nodes = moduleRows.filter((p) => p.id === row.parent_id);
if (nodes && nodes.length > 0) {
let node = nodes[0];
nodeIds.push(node.id);
if (node.parent_id) {
let sonNodeIds = this.getParantNode(node, moduleRows);
if (sonNodeIds && sonNodeIds.length > 0) {
nodeIds.push(...sonNodeIds);
}
}
}
return nodeIds;
},
readFileList(dir, filesList = []) {
const files = fs.readdirSync(dir);
files.forEach((item, index) => {
var fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
this.readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
filesList.push(fullPath);
}
});
return filesList;
},
mkdirsSync(dirname) {
if (fs.existsSync(dirname)) {
return true;
} else {
if (this.mkdirsSync(path.dirname(dirname))) {
fs.mkdirSync(dirname);
return true;
}
}
},
isExist(path) {
let promise = new Promise((resolve, reject) => {
fs.access(path, function (err) {
if (err && err.code == "ENOENT") {
resolve(false);
}
resolve(true);
});
});
return promise;
},
downloadFile(file_url) {
let promise = new Promise((resolve, reject) => {
let fileName = path.basename(file_url);
let received_bytes = 0;
let total_bytes = 0;
let req = request({
method: "GET",
uri: file_url,
});
let filePath = path.join(__dirname, `../downVideo/${fileName}`);
let out = fs.createWriteStream(filePath);
req.pipe(out);
req.on("response", function (data) {
// Change the total bytes value to get progress later.
total_bytes = parseInt(data.headers["content-length"]);
});
req.on("data", function (chunk) {
// Update the received bytes
received_bytes += chunk.length;
console.log("downloadFile", parseFloat((received_bytes / total_bytes) * 100).toFixed(2) + "%");
});
req.on("end", function () {
resolve(filePath);
});
req.on("error", function (e) {
resolve(false);
});
});
return promise;
},
};