0372cbe243
- 高优先级: 花名册导入模板必填项标注、性别自动识别、导入结果反馈、证据链Excel导出、身份证搜索修复、试用期区分与转正提醒、薪税批次流程优化 - 中优先级: 专项附加扣除批量导入、文本模板库完善(Word下载/复制/使用说明)、用工体检评分标准说明、考勤页面导入入口 - 所有导入模板表头标注必填项(*后缀)并含示例行 - 导入逻辑统一改用getField兼容*后缀列名 - 批次归档时强制重算所有条目个税和社保,解决多未归档批次并存时累计计算不准问题 - 更新需求梳理文档
62 lines
2.2 KiB
TypeScript
62 lines
2.2 KiB
TypeScript
import { Router, Response, NextFunction } from 'express'
|
|
import { authMiddleware, AuthRequest } from '../middleware/auth'
|
|
import { getAllTemplates, getTemplateById, getTemplatesByCategory, renderTemplate } from '../services/template.service'
|
|
|
|
const router = Router()
|
|
|
|
/** 获取所有模板列表 */
|
|
router.get('/', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const category = req.query.category as string | undefined
|
|
const templates = category ? getTemplatesByCategory(category) : getAllTemplates()
|
|
res.json({ success: true, data: templates })
|
|
} catch (err) {
|
|
next(err)
|
|
}
|
|
})
|
|
|
|
/** 获取模板详情 */
|
|
router.get('/:id', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const template = getTemplateById(req.params.id)
|
|
if (!template) {
|
|
return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
|
|
}
|
|
res.json({ success: true, data: template })
|
|
} catch (err) {
|
|
next(err)
|
|
}
|
|
})
|
|
|
|
/** 渲染模板 */
|
|
router.post('/:id/render', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const { variables } = req.body as { variables: Record<string, string> }
|
|
const content = renderTemplate(req.params.id, variables || {})
|
|
if (!content) {
|
|
return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
|
|
}
|
|
res.json({ success: true, data: { content } })
|
|
} catch (err) {
|
|
next(err)
|
|
}
|
|
})
|
|
|
|
/** 下载模板(Word .doc 格式) */
|
|
router.get('/:id/download', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
try {
|
|
const template = getTemplateById(req.params.id)
|
|
if (!template) {
|
|
return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '模板不存在' } })
|
|
}
|
|
const encoded = encodeURIComponent(template.name + '.doc')
|
|
res.setHeader('Content-Type', 'application/msword')
|
|
res.setHeader('Content-Disposition', `attachment; filename="${encoded}"; filename*=UTF-8''${encoded}`)
|
|
res.send(template.content)
|
|
} catch (err) {
|
|
next(err)
|
|
}
|
|
})
|
|
|
|
export default router
|