路由表化:迁移登录/会话、报表/备份/同步路由,删除旧路由链,server.js 收为入口
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5c015f826
commit
2305900178
8 files changed
+632
-493
No files matched your search
@@ -0,0 +1,92 @@
|
||||
'use strict';
|
||||
// 报表、库存导出、完整备份与下载、飞书同步。
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { fail } = require('../../ledger');
|
||||
const { send } = require('../http');
|
||||
const { readBody } = require('../middleware/body');
|
||||
|
||||
function csvText(value) {
|
||||
const text = String(value),
|
||||
safe = /^[\s\u0000-\u001f]*[=+\-@]|^[\u0000-\u001f]/.test(text) ? "'" + text : text;
|
||||
return '"' + safe.replace(/"/g, '""') + '"';
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/report',
|
||||
handler: ({ ledger, query, actor }) => {
|
||||
const report = ledger.reportFor(query, actor);
|
||||
if (actor.role !== 'owner') report.peopleCount = null;
|
||||
return report;
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/export/inventory',
|
||||
role: 'owner',
|
||||
raw: true,
|
||||
handler: ({ res, ledger }) => {
|
||||
res.setHeader('Content-Disposition', 'attachment; filename="inventory.csv"');
|
||||
send(
|
||||
res,
|
||||
200,
|
||||
'\ufeff货盘,账面克重,已占用克重,可用克重,计数单位,账面对数,已占用对数,可用对数\r\n' +
|
||||
ledger
|
||||
.inventory()
|
||||
.map(r =>
|
||||
[
|
||||
csvText(ledger.pocketLabel(r.pocket)),
|
||||
(r.mg / 1000).toFixed(3),
|
||||
(r.reserved / 1000).toFixed(3),
|
||||
(r.available / 1000).toFixed(3),
|
||||
...(r.countUnit === 'pair'
|
||||
? ['对', r.count ?? '未登记', r.reservedCount ?? '未登记', r.availableCount ?? '未登记']
|
||||
: ['', '', '', '']),
|
||||
].join(','),
|
||||
)
|
||||
.join('\r\n'),
|
||||
'text/csv; charset=utf-8',
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
// 读取并丢弃请求体;不复核会话、不走操作号事务。
|
||||
method: 'POST',
|
||||
path: '/api/backup',
|
||||
role: 'owner',
|
||||
direct: true,
|
||||
handler: async ({ req, ledger, actor, app }) => {
|
||||
await readBody(req);
|
||||
const b = await app.doBackup();
|
||||
ledger.audit('system', '完整备份', actor, { name: b.name });
|
||||
return { name: b.name };
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/backup/*filename',
|
||||
role: 'owner',
|
||||
raw: true,
|
||||
handler: ({ res, params: { filename }, app }) => {
|
||||
if (!/^ledger-[\dTZ-]+\.sqlite$/.test(filename)) fail('备份名称无效');
|
||||
const file = path.join(app.dataDir, 'backups', filename);
|
||||
if (!fs.existsSync(file)) fail('备份不存在', 404);
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
|
||||
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
|
||||
fs.createReadStream(file).pipe(res);
|
||||
},
|
||||
},
|
||||
{
|
||||
method: 'POST',
|
||||
path: '/api/sync',
|
||||
role: 'owner',
|
||||
direct: true,
|
||||
handler: async ({ req, app }) => {
|
||||
await readBody(req);
|
||||
await app.sync.tick();
|
||||
return app.sync.status();
|
||||
},
|
||||
},
|
||||
];
|
||||
Reference in new issue
Block a user