const express = require('express'); const path = require('path'); const fs = require('fs'); const yaml = require('js-yaml'); // 加载路由配置 function loadRoutes() { try { const fileContents = fs.readFileSync('routes.yaml', 'utf8'); return yaml.load(fileContents); } catch (e) { console.error('加载路由配置失败:', e); process.exit(1); } } const config = loadRoutes(); const app = express(); const PORT = config.server.port || 4155; // 静态文件服务 app.use('/order-classes', express.static('order-classes')); // 为所有订单班提供独立的静态文件服务 const orderClassDirs = ['wenlu', 'food', 'finance', 'health', 'chemical', 'environmental', 'transportation', 'energy', 'visual-design', 'civil', 'developer', 'manufacturing']; orderClassDirs.forEach(dir => { // 每个订单班的资源路径 app.use(`/order-class/${dir}/css`, express.static(`order-classes/${dir}/css`)); app.use(`/order-class/${dir}/js`, express.static(`order-classes/${dir}/js`)); app.use(`/order-class/${dir}/data`, express.static(`order-classes/${dir}/data`)); app.use(`/order-class/${dir}/images`, express.static(`order-classes/${dir}/images`)); app.use(`/order-class/${dir}/agent-avatars`, express.static(`order-classes/${dir}/agent-avatars`)); }); // 日志中间件 app.use((req, res, next) => { console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`); next(); }); // 主路由 - 显示订单班选择页面 app.get('/', (req, res) => { const orderClass = req.query.class; if (orderClass) { // 如果指定了订单班,重定向到对应路由 res.redirect(`/order-class/${orderClass}`); } else { // 显示选择页面 res.send(generateIndexPage()); } }); // 处理 /order-class/wenlu/*.html 形式的请求 app.get('/order-class/wenlu/:page', (req, res) => { const page = req.params.page; const filePath = path.join(__dirname, 'order-classes', 'wenlu', page); if (fs.existsSync(filePath)) { let html = fs.readFileSync(filePath, 'utf8'); res.send(html); } else { res.status(404).send(generate404Page(`wenlu/${page}`)); } }); // 处理 /order-class/*.html 形式的请求(wenlu 子页面)- 兼容旧路径 app.get('/order-class/:page', (req, res, next) => { const page = req.params.page; // 只处理 .html 文件 if (!page.endsWith('.html')) { // 如果不是 .html 文件,继续下一个路由 next(); return; } const filePath = path.join(__dirname, 'order-classes', 'wenlu', page); if (fs.existsSync(filePath)) { let html = fs.readFileSync(filePath, 'utf8'); res.send(html); } else { res.status(404).send(generate404Page(page)); } }); // 订单班路由 app.get('/order-class/:className', (req, res) => { const className = req.params.className; const classConfig = config.order_classes[className]; if (!classConfig) { res.status(404).send(generate404Page(className)); return; } // 如果订单班已完成,返回其页面 if (classConfig.status === 'completed') { const filePath = path.join(__dirname, classConfig.path, classConfig.entry); if (fs.existsSync(filePath)) { // 读取文件并修改相对路径 let html = fs.readFileSync(filePath, 'utf8'); // 保持相对路径,让浏览器自己解析 // 不需要修改路径,因为我们已经设置了静态文件服务 res.send(html); } else { res.send(generateComingSoonPage(classConfig)); } } else { res.send(generateComingSoonPage(classConfig)); } }); // 处理订单班内部页面(如文旅的overview.html等) app.get('/order-classes/:className/:page', (req, res) => { const { className, page } = req.params; const filePath = path.join(__dirname, 'order-classes', className, page); if (fs.existsSync(filePath)) { let html = fs.readFileSync(filePath, 'utf8'); // 保持相对路径不变 res.send(html); } else { res.status(404).send(generate404Page(`${className}/${page}`)); } }); // 已删除 /order-class/pages/:page 路由,因为导航链接已修复为相对路径 // 生成首页(选择页面) function generateIndexPage() { const orderClasses = Object.entries(config.order_classes); const completedClasses = orderClasses.filter(([_, c]) => c.status === 'completed'); const pendingClasses = orderClasses.filter(([_, c]) => c.status === 'pending'); return ` 订单班AI生成方案展示系统

订单班AI生成方案展示系统

基于多Agent协作的智能方案生成平台

已完成的订单班方案

${completedClasses.map(([key, config]) => `
${getClassIcon(key)}

${config.name}

${config.description}

${config.agents.length || 0} 位AI专家
查看方案
`).join('')}

即将推出

${pendingClasses.map(([key, config]) => `
${getClassIcon(key)}

${config.name}

${config.description}

开发中...
`).join('')}
`; } // 生成404页面 function generate404Page(resource) { return ` 页面未找到

404

页面未找到

资源 "${resource}" 不存在

返回首页
`; } // 生成即将推出页面 function generateComingSoonPage(classConfig) { return ` ${classConfig.name} - 即将推出
${getClassIcon(classConfig.name)}

${classConfig.name}

${classConfig.title}

${classConfig.description}

🚀 正在开发中

我们的AI专家团队正在为您准备专业的解决方案

返回首页
`; } // 获取订单班图标 function getClassIcon(key) { const icons = { 'wenlu': '🚗', 'food': '🍽️', 'finance': '💰', 'health': '🏥', 'chemical': '⚗️', 'environmental': '🌱', 'transportation': '🚚', 'energy': '⚡', 'visual-design': '🎨', 'civil': '🏗️', 'developer': '💻', 'manufacturing': '🏭' }; return icons[key] || '📦'; } // 启动服务器 app.listen(PORT, () => { console.log(` ╔═══════════════════════════════════════════════════════╗ ║ 订单班AI生成方案展示系统 - 服务器已启动 ║ ╠═══════════════════════════════════════════════════════╣ ║ ║ ║ 访问地址: ║ ║ • http://localhost:${PORT} ║ ║ ║ ║ 路由示例: ║ ║ • 首页: http://localhost:${PORT}/ ║ ║ • 文旅: http://localhost:${PORT}/order-class/wenlu ║ ║ • 食品: http://localhost:${PORT}/order-class/food ║ ║ ║ ║ 快捷访问: ║ ║ • http://localhost:${PORT}/?class=wenlu ║ ║ • http://localhost:${PORT}/?class=food ║ ║ ║ ╚═══════════════════════════════════════════════════════╝ `); }); // 优雅关闭 process.on('SIGINT', () => { console.log('\n正在关闭服务器...'); process.exit(0); });