Browse Source

下载数据文件接口,修复取机器人信息取不到

JiahengHe 2 months ago
parent
commit
6c05bff7e2
1 changed files with 9 additions and 14 deletions
  1. 9 14
      src/web.js

+ 9 - 14
src/web.js

@@ -148,30 +148,25 @@ function init() {
     // 下载数据文件
     app.get('/downloadDataFile', (req, res) => {
         const param = req.query;
-        const id = param.id
+        const id = Number(param.id)
         let port = -1;
-        robot.appMap.forEach((value, key) => {
-            if (value.id + "" === id + "") {
-                port = value.port
-            }
-        })
-        if (port === -1){
-            return res.status(404).send('机器人不存在!');
+        if (robot.appMap.has(id)){
+            let app = robot.appMap.get(id);
+            port = app.port
+        } else {
+            return res.status(404).send('机器人不存在!机器人id='+ id);
         }
+
         const logPath = "./db/" + port + ".html";
         const latestFile = path.resolve(logPath);
-
-        if (!latestFile) {
+        if (!fs.existsSync(latestFile)) {
             return res.status(404).send('没有找到文件。');
         }
 
         // 设置下载响应
         res.download(latestFile, err => {
             if (err) {
-                console.error('下载文件时发生错误:', err);
-                res.status(500).send('下载失败');
-            } else {
-                console.log('文件下载成功:', latestFile);
+                res.status(500).send('下载失败, 异常信息:', err);
             }
         });