Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
70 lines
2.8 KiB
TypeScript
70 lines
2.8 KiB
TypeScript
// 培训工作台本地预览配置:在主配置基础上用中间件 mock /api/v1/training/*,
|
||
// 供 training-preview.html 无后端渲染培训面板(仅本地验证用,不影响构建)。
|
||
import { fileURLToPath, URL } from 'node:url'
|
||
import { readFileSync } from 'node:fs'
|
||
import { defineConfig, mergeConfig, type Plugin } from 'vite'
|
||
import baseConfig from './vite.config'
|
||
import { resolveTrainingMock } from './src/test-utils/trainingMockApi'
|
||
|
||
// 模板二进制:harness 下用仓库内置资产文件模拟后端下载接口
|
||
const TEMPLATE_FILES: Record<string, { file: URL; type: string }> = {
|
||
demand_collection: {
|
||
file: new URL('../internal/assets/trainingtemplates/demand_collection.xlsx', import.meta.url),
|
||
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
},
|
||
question_bank: {
|
||
file: new URL('../internal/assets/trainingtemplates/question_bank.xlsx', import.meta.url),
|
||
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
},
|
||
}
|
||
|
||
// 拦截培训域接口,返回与后端一致的 { success, data } 封包
|
||
function trainingPreviewMockApi(): Plugin {
|
||
return {
|
||
name: 'training-preview-mock-api',
|
||
configureServer(server) {
|
||
server.middlewares.use((req, res, next) => {
|
||
const raw = req.url || ''
|
||
const path = raw.split('?')[0]
|
||
if (path.startsWith('/api/v1/training/')) {
|
||
// 模板文件下载:直接回真实 Excel 二进制
|
||
const fileMatch = path.match(/^\/api\/v1\/training\/templates\/(demand_collection|question_bank)\/file$/)
|
||
if (req.method === 'GET' && fileMatch) {
|
||
const entry = TEMPLATE_FILES[fileMatch[1]]
|
||
try {
|
||
const buf = readFileSync(fileURLToPath(entry.file))
|
||
res.statusCode = 200
|
||
res.setHeader('Content-Type', entry.type)
|
||
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(fileMatch[1] === 'demand_collection' ? '年度教育培训收集模板.xlsx' : '部门培训课程试题(题库导入版)模板.xlsx')}`)
|
||
res.end(buf)
|
||
return
|
||
} catch {
|
||
res.statusCode = 404
|
||
res.end(JSON.stringify({ success: false, message: 'template asset missing' }))
|
||
return
|
||
}
|
||
}
|
||
const hit = resolveTrainingMock(req.method || 'GET', path)
|
||
if (hit) {
|
||
res.statusCode = hit.status
|
||
res.setHeader('Content-Type', 'application/json; charset=utf-8')
|
||
res.end(JSON.stringify(hit.body))
|
||
return
|
||
}
|
||
}
|
||
next()
|
||
})
|
||
},
|
||
}
|
||
}
|
||
|
||
export default mergeConfig(
|
||
defineConfig({
|
||
plugins: [trainingPreviewMockApi()],
|
||
server: {
|
||
port: 5175,
|
||
},
|
||
}),
|
||
baseConfig,
|
||
)
|