From 61cfdb94e9da192058ab3db4e0b02a3bd1abdaa1 Mon Sep 17 00:00:00 2001 From: jinchao <383321154@qq.com> Date: Fri, 16 May 2025 09:21:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B6=E9=83=A8=E6=9C=BA=E5=9E=8B=E4=B8=8E?= =?UTF-8?q?=E6=9E=84=E5=9E=8B=E7=9A=84=E9=80=89=E6=8B=A9=E5=B7=B2=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Release/database/XNSim.db | Bin 278528 -> 278528 bytes XNSimHtml/components/header-tools.js | 104 ++++++++++++++++++++++--- XNSimHtml/routes/configurations.js | 71 +++++++++++++++++ XNSimHtml/routes/planes.js | 16 ++++ XNSimHtml/routes/products.js | 16 ---- XNSimHtml/server.js | 6 +- XNSimHtml/utils/configuration-utils.js | 17 +++- XNSimHtml/utils/file-utils.js | 4 +- 8 files changed, 199 insertions(+), 35 deletions(-) create mode 100644 XNSimHtml/routes/configurations.js create mode 100644 XNSimHtml/routes/planes.js delete mode 100644 XNSimHtml/routes/products.js diff --git a/Release/database/XNSim.db b/Release/database/XNSim.db index 0316178704409c2ac628725a621d34318a298bf7..8cd7cc01dfb7111c6197d1aba39fe64019cd02a2 100644 GIT binary patch delta 140 zcmZo@5Nv1=oFL68HBrWyRf<8cth+I#HHC3&3e(d6j9)e@Dm>tu{)&&8i@%Az{hbLH zLnE(kW3Y51^YlP|X7T9){LHe-mX;P428Jf4dO7*&Mg~Tvx(24YhNd9~mR1HPRwjme rh8E`21NfN}xb-S>bBapy@-p+%LE7b~-{)tRX#c~{y!{V93yT2&aeFAa delta 129 zcmZo@5Nv1=oFL68Hc`fzRg6K;dtzfsYYOAm6sD#B8DDKyRCvHQ{S_ZG7i(jzbR+NN z^Znw}rTCd;Rm{zeO^uArE%kEp(~S&_Omz)Rbq!5J42-NyEUgTU^-PRRqkyXAr&sYa dJFv!=q!yP<-^I`D1Yx%S;b-3dho6PT000g$C!YWS diff --git a/XNSimHtml/components/header-tools.js b/XNSimHtml/components/header-tools.js index d6086bf..09c9f12 100644 --- a/XNSimHtml/components/header-tools.js +++ b/XNSimHtml/components/header-tools.js @@ -7,10 +7,46 @@ class HeaderTools extends HTMLElement { } // 添加getter方法 + get selectedPlane() { + return this.shadowRoot.getElementById('planeSelect').value; + } + get selectedProduct() { return this.shadowRoot.getElementById('productSelect').value; } + // 保存选择到localStorage + saveSelection() { + const selection = { + plane: this.selectedPlane, + product: this.selectedProduct + }; + localStorage.setItem('xnsim-selection', JSON.stringify(selection)); + } + + // 从localStorage恢复选择 + restoreSelection() { + const savedSelection = localStorage.getItem('xnsim-selection'); + if (savedSelection) { + const selection = JSON.parse(savedSelection); + const planeSelect = this.shadowRoot.getElementById('planeSelect'); + const productSelect = this.shadowRoot.getElementById('productSelect'); + + // 先加载机型列表 + this.loadPlanes().then(() => { + if (selection.plane) { + planeSelect.value = selection.plane; + // 加载该机型下的构型列表 + this.loadProducts(selection.plane).then(() => { + if (selection.product) { + productSelect.value = selection.product; + } + }); + } + }); + } + } + render() { this.shadowRoot.innerHTML = ` -
- 构型: +
+ 机型: + +
+
+ 构型: @@ -112,8 +154,12 @@ class HeaderTools extends HTMLElement { } addEventListeners() { + // 加载机型列表 + this.loadPlanes(); // 加载构型列表 this.loadProducts(); + // 恢复上次的选择 + this.restoreSelection(); // 字体大小调整 this.shadowRoot.getElementById('fontSizeBtn').addEventListener('click', () => { @@ -135,17 +181,54 @@ class HeaderTools extends HTMLElement { this.dispatchEvent(new CustomEvent('online-users-click')); }); + // 机型选择 + this.shadowRoot.getElementById('planeSelect').addEventListener('change', (e) => { + this.dispatchEvent(new CustomEvent('plane-change', { + detail: { plane: e.target.value } + })); + // 当机型改变时,重新加载该机型下的构型列表 + this.loadProducts(e.target.value); + // 保存选择 + this.saveSelection(); + }); + // 构型选择 this.shadowRoot.getElementById('productSelect').addEventListener('change', (e) => { this.dispatchEvent(new CustomEvent('product-change', { detail: { product: e.target.value } })); + // 保存选择 + this.saveSelection(); }); } - async loadProducts() { + async loadPlanes() { try { - const response = await fetch('/api/products'); + const response = await fetch('/api/planes'); + if (!response.ok) { + throw new Error('获取机型列表失败'); + } + const planes = await response.json(); + const select = this.shadowRoot.getElementById('planeSelect'); + + // 清空现有选项 + select.innerHTML = ''; + + planes.forEach(plane => { + const option = document.createElement('option'); + option.value = plane.PlaneName; + option.textContent = plane.PlaneName; + select.appendChild(option); + }); + } catch (error) { + console.error('加载机型列表失败:', error); + } + } + + async loadProducts(planeName = '') { + try { + const url = planeName ? `/api/configurations?plane=${planeName}` : '/api/configurations'; + const response = await fetch(url); if (!response.ok) { throw new Error('获取构型列表失败'); } @@ -157,13 +240,12 @@ class HeaderTools extends HTMLElement { products.forEach(product => { const option = document.createElement('option'); - option.value = product.ProductName; - option.textContent = product.ProductName; + option.value = product.ConfName; + option.textContent = product.ConfName; select.appendChild(option); }); } catch (error) { console.error('加载构型列表失败:', error); - // 可以在这里添加错误提示UI } } } diff --git a/XNSimHtml/routes/configurations.js b/XNSimHtml/routes/configurations.js new file mode 100644 index 0000000..9691877 --- /dev/null +++ b/XNSimHtml/routes/configurations.js @@ -0,0 +1,71 @@ +const express = require('express'); +const router = express.Router(); +const { + getConfigurations, + getConfigurationById, + createConfiguration, + updateConfiguration, + deleteConfiguration +} = require('../utils/configuration-utils'); + +// 获取所有配置列表 +router.get('/configurations', (req, res) => { + try { + const planeName = req.query.plane; + const configs = getConfigurations(planeName); + res.json(configs); + } catch (error) { + console.error('获取配置列表失败:', error); + res.status(500).json({ error: '获取配置列表失败', details: error.message }); + } +}); + +// 根据ID获取配置 +router.get('/configurations/:id', (req, res) => { + try { + const config = getConfigurationById(req.params.id); + if (!config) { + return res.status(404).json({ error: '配置不存在' }); + } + res.json(config); + } catch (error) { + console.error('获取配置详情失败:', error); + res.status(500).json({ error: '获取配置详情失败', details: error.message }); + } +}); + +// 创建新配置 +router.post('/configurations', (req, res) => { + try { + const result = createConfiguration(req.body); + res.status(201).json(result); + } catch (error) { + console.error('创建配置失败:', error); + res.status(500).json({ error: '创建配置失败', details: error.message }); + } +}); + +// 更新配置 +router.put('/configurations/:id', (req, res) => { + try { + const configData = { ...req.body, ConfID: req.params.id }; + const result = updateConfiguration(configData); + res.json(result); + } catch (error) { + console.error('更新配置失败:', error); + res.status(500).json({ error: '更新配置失败', details: error.message }); + } +}); + +// 删除配置 +router.delete('/configurations/:id', (req, res) => { + try { + const result = deleteConfiguration(req.params.id); + res.json(result); + } catch (error) { + console.error('删除配置失败:', error); + res.status(500).json({ error: '删除配置失败', details: error.message }); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/XNSimHtml/routes/planes.js b/XNSimHtml/routes/planes.js new file mode 100644 index 0000000..824846f --- /dev/null +++ b/XNSimHtml/routes/planes.js @@ -0,0 +1,16 @@ +const express = require('express'); +const router = express.Router(); +const { getPlanes } = require('../utils/db-utils'); + +// 获取所有飞机列表 +router.get('/planes', (req, res) => { + try { + const planes = getPlanes(); + res.json(planes); + } catch (error) { + console.error('获取飞机列表失败:', error); + res.status(500).json({ error: '获取飞机列表失败', details: error.message }); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/XNSimHtml/routes/products.js b/XNSimHtml/routes/products.js deleted file mode 100644 index 0d06157..0000000 --- a/XNSimHtml/routes/products.js +++ /dev/null @@ -1,16 +0,0 @@ -const express = require('express'); -const router = express.Router(); -const { getProducts } = require('../utils/db-utils'); - -// 获取所有构型列表 -router.get('/products', (req, res) => { - try { - const products = getProducts(); - res.json(products); - } catch (error) { - console.error('获取构型列表失败:', error); - res.status(500).json({ error: '获取构型列表失败', details: error.message }); - } -}); - -module.exports = router; \ No newline at end of file diff --git a/XNSimHtml/server.js b/XNSimHtml/server.js index 74c2842..aa97853 100644 --- a/XNSimHtml/server.js +++ b/XNSimHtml/server.js @@ -20,7 +20,8 @@ const projectModelRoutes = require('./routes/project-model'); const ataChaptersRoutes = require('./routes/model-dev'); const simulationRoutes = require('./routes/run-simulation'); const udpMonitorRoutes = require('./routes/udp-monitor'); -const productsRoutes = require('./routes/products'); +const planesRoutes = require('./routes/planes'); +const configurationsRoutes = require('./routes/configurations'); const interfaceRoutes = require('./routes/interface-config'); const icdImportRoutes = require('./routes/icd-import'); const qaRoutes = require('./routes/qa'); @@ -91,7 +92,8 @@ app.use('/api', projectModelRoutes); app.use('/api', ataChaptersRoutes); app.use('/api', simulationRoutes); app.use('/api/udp-monitor', udpMonitorRoutes); -app.use('/api', productsRoutes); +app.use('/api', planesRoutes); +app.use('/api', configurationsRoutes); app.use('/api/interface', interfaceRoutes); app.use('/api/icd', icdImportRoutes); app.use('/api/qa', qaRoutes); diff --git a/XNSimHtml/utils/configuration-utils.js b/XNSimHtml/utils/configuration-utils.js index 7cfb571..f9b61de 100644 --- a/XNSimHtml/utils/configuration-utils.js +++ b/XNSimHtml/utils/configuration-utils.js @@ -1,14 +1,23 @@ const { getDBConnection } = require('./file-utils'); // 获取所有配置 -function getConfigurations() { +function getConfigurations(planeName) { try { const db = getDBConnection(true); - const configs = db.prepare(` + let query = ` SELECT * FROM Configuration - ORDER BY ConfID ASC - `).all(); + `; + + const params = []; + if (planeName) { + query += ` WHERE PlaneName = ?`; + params.push(planeName); + } + + query += ` ORDER BY ConfID ASC`; + + const configs = db.prepare(query).all(...params); return configs; } catch (error) { diff --git a/XNSimHtml/utils/file-utils.js b/XNSimHtml/utils/file-utils.js index 006b56a..75f8226 100644 --- a/XNSimHtml/utils/file-utils.js +++ b/XNSimHtml/utils/file-utils.js @@ -22,8 +22,8 @@ function getDBConnection(readonly = false) { throw new Error('无法找到数据库文件'); } - // 打开数据库连接 - dbConnection = new Database(dbPath, { readonly }); + // 打开数据库连接,始终以可读可写模式打开 + dbConnection = new Database(dbPath); return dbConnection; } catch (error) { console.error('数据库连接失败:', error);