From b377e208b417d2d8ef3b182245216c14b4a33e84 Mon Sep 17 00:00:00 2001 From: jinchao <383321154@qq.com> Date: Mon, 12 May 2025 16:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=B3=BB=E7=BB=9F=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E9=A1=B5=E9=9D=A2=EF=BC=8C=E5=BE=85=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Release/database/XNSim.db | Bin 176128 -> 192512 bytes XNSimHtml/components/system-log.js | 118 +++++++++++++---------------- XNSimHtml/main.html | 3 +- XNSimHtml/routes/system-log.js | 26 +++++++ XNSimHtml/server.js | 2 + XNSimHtml/utils/db-utils.js | 95 ++++++++++++++++++++++- 6 files changed, 177 insertions(+), 67 deletions(-) create mode 100644 XNSimHtml/routes/system-log.js diff --git a/Release/database/XNSim.db b/Release/database/XNSim.db index 8e84bde44ccf3a31ca3232831b1c87aac3f3b8a0..4d904410a4500706173f71baf06b6e2bb4c3e26a 100644 GIT binary patch delta 370 zcmZp8z}4`8dxEs!6$SZtd|({%DNj5MO6s)6h~8yjyhZr8cYIERPPdSavbvImS80BDjL4gdfE diff --git a/XNSimHtml/components/system-log.js b/XNSimHtml/components/system-log.js index 6937026..5370d75 100644 --- a/XNSimHtml/components/system-log.js +++ b/XNSimHtml/components/system-log.js @@ -35,11 +35,21 @@ class SystemLog extends HTMLElement { background: #f5f7fa; border-radius: 8px; align-items: center; + width: 100%; + } + + .toolbar-right { + display: flex; + align-items: center; + gap: 16px; + margin-left: auto; } .search-box { - flex: 1; + width: 320px; + min-width: 180px; position: relative; + flex-shrink: 0; } .search-box input { @@ -69,6 +79,7 @@ class SystemLog extends HTMLElement { .filter-group { display: flex; gap: 12px; + flex-shrink: 0; } .filter-select { @@ -89,6 +100,7 @@ class SystemLog extends HTMLElement { .action-buttons { display: flex; gap: 8px; + flex-shrink: 0; } .btn { @@ -133,7 +145,7 @@ class SystemLog extends HTMLElement { .log-header { display: grid; - grid-template-columns: 180px 120px 120px 1fr; + grid-template-columns: 180px 120px 120px 120px 1fr; padding: 12px 16px; background: #f5f7fa; border-bottom: 1px solid #e4e7ed; @@ -148,7 +160,7 @@ class SystemLog extends HTMLElement { .log-item { display: grid; - grid-template-columns: 180px 120px 120px 1fr; + grid-template-columns: 180px 120px 120px 120px 1fr; padding: 12px 16px; border-bottom: 1px solid #e4e7ed; font-size: 14px; @@ -219,6 +231,11 @@ class SystemLog extends HTMLElement { color: #c0c4cc; cursor: not-allowed; } + + .icon-small { + width: 16px; + height: 16px; + }
@@ -226,31 +243,29 @@ class SystemLog extends HTMLElement { 搜索
-
- - -
-
- - +
+
+ + +
+
+ +
@@ -258,6 +273,7 @@ class SystemLog extends HTMLElement {
时间
级别
来源
+
用户
内容
@@ -300,12 +316,6 @@ class SystemLog extends HTMLElement { this.loadLogs(); }); - // 导出按钮事件 - const exportBtn = this.shadowRoot.querySelector('#exportBtn'); - exportBtn.addEventListener('click', () => { - this.exportLogs(); - }); - // 分页按钮事件 const prevPage = this.shadowRoot.querySelector('#prevPage'); const nextPage = this.shadowRoot.querySelector('#nextPage'); @@ -319,29 +329,11 @@ class SystemLog extends HTMLElement { async loadLogs() { try { - // 这里应该调用后端API获取日志数据 - // 示例数据 - const logs = [ - { - timestamp: '2024-03-20 10:00:00', - level: 'info', - source: '系统', - content: '系统启动成功' - }, - { - timestamp: '2024-03-20 10:01:00', - level: 'warning', - source: '数据库', - content: '数据库连接池接近最大连接数' - }, - { - timestamp: '2024-03-20 10:02:00', - level: 'error', - source: 'API', - content: 'API请求超时' - } - ]; - + const response = await fetch('/api/system-log/logs'); + if (!response.ok) { + throw new Error('网络响应失败'); + } + const logs = await response.json(); this.renderLogs(logs); } catch (error) { console.error('加载日志失败:', error); @@ -352,10 +344,11 @@ class SystemLog extends HTMLElement { const logList = this.shadowRoot.querySelector('#logList'); logList.innerHTML = logs.map(log => `
-
${log.timestamp}
+
${log.time || ''}
${this.getLevelText(log.level)}
-
${log.source}
-
${log.content}
+
${log.source || ''}
+
${log.user || ''}
+
${log.log || ''}
`).join(''); } @@ -379,11 +372,6 @@ class SystemLog extends HTMLElement { this.loadLogs(); } - exportLogs() { - // 实现日志导出功能 - console.log('导出日志'); - } - changePage(delta) { // 实现分页功能 console.log('切换页面:', delta); diff --git a/XNSimHtml/main.html b/XNSimHtml/main.html index 597b564..3a43cd0 100644 --- a/XNSimHtml/main.html +++ b/XNSimHtml/main.html @@ -634,7 +634,8 @@ '个人中心': 'user', '用户管理': 'users', '模型开发': 'cube', - '服务开发': 'settings' + '服务开发': 'settings', + '系统日志': 'file-text' }; return iconMap[title] || parentTool || 'con'; diff --git a/XNSimHtml/routes/system-log.js b/XNSimHtml/routes/system-log.js new file mode 100644 index 0000000..baef9f1 --- /dev/null +++ b/XNSimHtml/routes/system-log.js @@ -0,0 +1,26 @@ +const express = require('express'); +const router = express.Router(); +const { getSystemLogs, addSystemLog } = require('../utils/db-utils'); + +// 获取所有系统日志 +router.get('/logs', async (req, res) => { + try { + const logs = await getSystemLogs(); + res.json(logs); + } catch (error) { + res.status(500).json({ error: '无法获取系统日志' }); + } +}); + +// 添加系统日志 +router.post('/logs', async (req, res) => { + const { level, user, log, source } = req.body; + try { + const result = await addSystemLog({ level, user, log, source }); + res.json(result); + } catch (error) { + res.status(500).json({ error: '无法添加系统日志' }); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/XNSimHtml/server.js b/XNSimHtml/server.js index dd86ba9..c1d195e 100644 --- a/XNSimHtml/server.js +++ b/XNSimHtml/server.js @@ -26,6 +26,7 @@ const icdImportRoutes = require('./routes/icd-import'); const qaRoutes = require('./routes/qa'); const todoRoutes = require('./routes/todos'); const userRoutes = require('./routes/users'); +const systemLogRoutes = require('./routes/system-log'); const app = express(); const PORT = process.env.PORT || 3000; @@ -93,6 +94,7 @@ app.use('/api/icd', icdImportRoutes); app.use('/api/qa', qaRoutes); app.use('/api/todos', todoRoutes); app.use('/api', userRoutes); +app.use('/api/system-log', systemLogRoutes); // 接口配置页面路由 app.get('/interface-config', (req, res) => { diff --git a/XNSimHtml/utils/db-utils.js b/XNSimHtml/utils/db-utils.js index 37a8068..b612906 100644 --- a/XNSimHtml/utils/db-utils.js +++ b/XNSimHtml/utils/db-utils.js @@ -1443,6 +1443,97 @@ function getUsers() { } } +// 获取所有系统日志 +function getSystemLogs() { + try { + const xnCorePath = getXNCorePath(); + if (!xnCorePath) { + throw new Error('XNCore环境变量未设置,无法获取数据库路径'); + } + + const dbPath = xnCorePath + '/database/XNSim.db'; + if (!dbPath) { + throw new Error('无法找到数据库文件'); + } + + // 打开数据库连接 + const db = new Database(dbPath, { readonly: true }); + + const logs = db.prepare(` + SELECT id, time, level, user, source, log + FROM SystemLog + ORDER BY time DESC + `).all(); + + db.close(); + + return logs; + } catch (error) { + console.error('获取系统日志失败:', error); + throw error; + } +} + +// 添加系统日志 +function addSystemLog(logData) { + try { + // 验证必填字段 + if (!logData.level || !logData.source) { + throw new Error('日志级别和来源是必填字段'); + } + + const xnCorePath = getXNCorePath(); + if (!xnCorePath) { + throw new Error('XNCore环境变量未设置,无法获取数据库路径'); + } + + const dbPath = xnCorePath + '/database/XNSim.db'; + if (!dbPath) { + throw new Error('无法找到数据库文件'); + } + + // 打开数据库连接 + const db = new Database(dbPath); + + // 获取当前本地时间 + const now = new Date(); + const year = now.getFullYear(); + const month = String(now.getMonth() + 1).padStart(2, '0'); + const day = String(now.getDate()).padStart(2, '0'); + const hours = String(now.getHours()).padStart(2, '0'); + const minutes = String(now.getMinutes()).padStart(2, '0'); + const seconds = String(now.getSeconds()).padStart(2, '0'); + const localDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; + + // 在插入日志时添加时间 + const result = db.prepare(` + INSERT INTO SystemLog (level, user, source, log, time) + VALUES (?, ?, ?, ?, ?) + `).run( + logData.level, + logData.user || null, + logData.source, + logData.log || null, + localDateTime + ); + + db.close(); + + if (result.changes > 0) { + return { + success: true, + id: result.lastInsertRowid, + message: '系统日志添加成功' + }; + } else { + throw new Error('系统日志添加失败'); + } + } catch (error) { + console.error('添加系统日志失败:', error); + throw error; + } +} + module.exports = { getATAChapters, getModelsByChapterId, @@ -1467,5 +1558,7 @@ module.exports = { addTodo, updateTodoStatus, deleteTodo, - getUsers + getUsers, + getSystemLogs, + addSystemLog }; \ No newline at end of file