diff --git a/server/routes/company.js b/server/routes/company.js index 2c1b993..068ab20 100644 --- a/server/routes/company.js +++ b/server/routes/company.js @@ -47,12 +47,12 @@ router.get('/templates', (req, res) => { res.json(companyService.listTemplates()); }); -// PATCH /api/company/templates/:templateId — 修改模板元信息 (如名称) +// PATCH /api/company/templates/:templateId — 修改模板元信息 (名称, 类型, 描述) router.patch('/templates/:templateId', (req, res) => { try { const { templateId } = req.params; - const { name } = req.body; - if (!name || !name.trim()) return res.status(400).json({ error: 'name 为必填项' }); + const { name, type, description } = req.body; + if (name !== undefined && !name.trim()) return res.status(400).json({ error: 'name 为必填项' }); const path = require('path'); const fs = require('fs'); @@ -63,29 +63,66 @@ router.patch('/templates/:templateId', (req, res) => { return res.status(404).json({ error: `模板 "${templateId}" 不存在` }); } - // 1. 更新 config.json 中的 name + // 1. 更新 config.json 中的字段 const configPath = path.join(templateDir, 'config.json'); if (fs.existsSync(configPath)) { const config = JSON.parse(fs.readFileSync(configPath, 'utf-8')); - config.name = name.trim(); + if (name !== undefined) config.name = name.trim(); + if (type !== undefined) config.type = type.trim(); + if (description !== undefined) config.description = description.trim(); fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8'); } - // 2. 更新 _registry.json 中的 name + // 2. 更新 _registry.json 中的字段 const registryPath = path.join(TEMPLATES_DIR, '_registry.json'); if (fs.existsSync(registryPath)) { try { const registry = JSON.parse(fs.readFileSync(registryPath, 'utf-8')); const entry = registry.find(t => t.id === templateId); if (entry) { - entry.name = name.trim(); + if (name !== undefined) entry.name = name.trim(); + if (type !== undefined) entry.type = type.trim(); + if (description !== undefined) entry.description = description.trim(); fs.writeFileSync(registryPath, JSON.stringify(registry, null, 2), 'utf-8'); } } catch {} } - console.log(`[Template] ✏️ 模板 ${templateId} 名称已更新为: ${name.trim()}`); - res.json({ success: true, id: templateId, name: name.trim() }); + console.log(`[Template] ✏️ 模板 ${templateId} 元信息已更新`); + res.json({ success: true, id: templateId, name: name?.trim(), type: type?.trim(), description: description?.trim() }); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + +// DELETE /api/company/templates/:templateId — 删除模板 +router.delete('/templates/:templateId', (req, res) => { + try { + const { templateId } = req.params; + const path = require('path'); + const fs = require('fs'); + const TEMPLATES_DIR = path.resolve(__dirname, '../../system/templates'); + const templateDir = path.join(TEMPLATES_DIR, templateId); + + if (!fs.existsSync(templateDir)) { + return res.status(404).json({ error: `模板 "${templateId}" 不存在` }); + } + + // 1. 删除模版文件夹 + fs.rmSync(templateDir, { recursive: true, force: true }); + + // 2. 更新 registry + const registryPath = path.join(TEMPLATES_DIR, '_registry.json'); + if (fs.existsSync(registryPath)) { + try { + let registry = JSON.parse(fs.readFileSync(registryPath, 'utf-8')); + registry = registry.filter(t => t.id !== templateId); + fs.writeFileSync(registryPath, JSON.stringify(registry, null, 2), 'utf-8'); + } catch {} + } + + console.log(`[Template] 🗑️ 模板 ${templateId} 已被删除`); + res.json({ success: true, message: `模板 ${templateId} 已删除` }); } catch (err) { res.status(500).json({ error: err.message }); } diff --git a/web/src/views/TemplateAgents.vue b/web/src/views/TemplateAgents.vue index c2325b0..97c6f2c 100644 --- a/web/src/views/TemplateAgents.vue +++ b/web/src/views/TemplateAgents.vue @@ -96,8 +96,33 @@ #{{ selectedTemplate.id }} +