合入主线登录和营业时段策略,对齐旧界面与 React 访问流程

This commit is contained in:
黄健铭 committed 2026-12-05 21:46:55 -08:00
commit 42395de23a
113 files changed
+1915 -558

No files matched your search

+38 -8
View File
@@ -1,7 +1,7 @@
'use strict';
// 路由表分发。路由字段:
// method, path 匹配条件(见 router.js)
// auth 'public'(登录前处理)| 'user'(默认;/api/ 与 /print/ 先 401/409 校验)
// auth 'public'(登录前处理)| 'session'(当前 cookie)| 'user'(默认;同时检查原页面账号)
// when(app) 可选;返回 false 时视同未声明(如演示登录只在演示模式存在)
// role: 'owner' 店主专用;默认在读请求体之前校验,ownerCheck: 'transact' 改为在事务内校验
// direct: true POST 由处理函数自行读请求体(旧代码中在通用写入流程之前处理的 POST)
@@ -9,20 +9,23 @@
// project(ctx, result) 写入结果(含幂等重放的缓存结果)发出前的投影
// contentType 非 JSON 响应类型(如打印 HTML)
// raw: true 处理函数自行写出响应(下载)
// handler(ctx) ctx = {req, res, url, params, query, body, actor, ledger, rawLedger, app}
// handler(ctx) ctx = {req, res, url, params, query, body, readBody, access, actor, ledger, rawLedger, app}
// write 路由的处理函数可设 ctx.afterRollback:事务回滚后调用一次(如登记失败审计),
// 之后照常抛出原错误
// 未匹配的 /api/、/print/ 请求:GET 直接 404;POST 照常读请求体、复核会话、进入 transact 后再 404,
// 因此请求体过大/非 JSON、会话失效、操作号无效等错误仍先于 404 返回。
const path = require('node:path');
const { fail, owner } = require('../core/ledger');
const { createRouter } = require('./router');
const { send, notFound } = require('./http');
const { readBody } = require('./middleware/body');
const { securityHeaders } = require('./middleware/security-headers');
const { checkRequest } = require('./middleware/request-guard');
const { requireActor } = require('./middleware/session');
const { requireBusinessHours, restricted, checkAccount } = require('./middleware/shop-access');
const { sendLoginPage } = require('./login-page');
const { requireActor, sessionInfo } = require('./middleware/session');
const { serveStatic } = require('./middleware/static');
const { serveWebNext, webNextEnabled, isNextPath } = require('./middleware/web-next');
const { serveWebNext, webNextEnabled, isNextPath, nextFile, DIST_DIR } = require('./middleware/web-next');
const { sendError } = require('./middleware/errors');
const UNMATCHED = {
@@ -38,7 +41,7 @@ async function runRoute(route, ctx) {
if (!route.raw) send(res, 200, data, route.contentType);
return;
}
ctx.body = await readBody(req);
ctx.body = await ctx.readBody();
const key = req.headers['idempotency-key'];
// 读取请求体期间账号可能已被停用或重置密码,写入前再次核验会话。
const checkedActor = app.user(req);
@@ -63,7 +66,7 @@ async function runRoute(route, ctx) {
send(res, 200, route.project ? route.project(ctx, result) : result);
}
// 处理顺序:安全响应头 → Host/405/来源校验 → 登录前路由 → 解析当前账号 → /api/、/print/ 的 401/409
// 处理顺序:安全响应头 → Host/405/来源/店内网络 → 登录前路由 → 解析账号、复核营业时段 → 401/409
// → 路由(含未匹配)→ 新版界面 /next/(启用时)→ 静态文件 → 统一错误响应。
function createHandler(app, routes) {
const router = createRouter(routes);
@@ -72,6 +75,7 @@ function createHandler(app, routes) {
securityHeaders(res);
try {
const { url, route: pathname, isPost } = checkRequest(req, app.production);
const access = app.access.forRequest(req);
const hit = router.match(req.method, pathname, enabled);
const ctx = {
req,
@@ -79,7 +83,15 @@ function createHandler(app, routes) {
url,
params: hit?.params ?? {},
query: Object.fromEntries(url.searchParams),
access,
body: undefined,
readBody: () =>
readBody(req, () => {
// 登录由认证后的目标账号决定。其它慢速写请求在 body 收齐时重新核对营业时段。
if (pathname === '/api/login' || pathname === '/api/demo-login' || pathname === '/api/logout') return;
const period = app.access.currentWindow();
if (restricted(period)) requireBusinessHours(app.user(req), period);
}),
actor: null,
ledger: null,
rawLedger: app.ledger,
@@ -87,12 +99,30 @@ function createHandler(app, routes) {
};
if (hit?.route.auth === 'public') return await runRoute(hit.route, ctx);
ctx.actor = app.user(req);
const next = isNextPath(pathname) && webNextEnabled(app);
const closedToActor = restricted(access.period) && ctx.actor?.role !== 'owner';
if (
!isPost &&
((pathname === '/' && (!ctx.actor || closedToActor)) ||
(closedToActor && next && nextFile(pathname) === path.join(DIST_DIR, 'index.html')))
) {
return sendLoginPage(res, sessionInfo(app.ledger, app.mode, null, access));
}
checkAccount(ctx.actor, access);
if (ctx.actor && ctx.actor.role !== 'owner' && access.period?.state === 'open') {
res.remainingBusinessTime = () => access.period.closesAt - app.access.hoursClock();
}
ctx.ledger = app.ledger.asActor(ctx.actor);
if (pathname.startsWith('/api/') || pathname.startsWith('/print/')) {
requireActor(req, ctx.actor);
if (hit?.route.auth === 'session') {
// 营业时段两个入口只看当前 cookie;店主专用入口由 runRoute 返回其原有的 403。
if (!ctx.actor && !hit.route.role) fail('请先登录', 401);
} else {
requireActor(req, ctx.actor);
}
return await runRoute(hit?.route ?? UNMATCHED[req.method], ctx);
}
if (isNextPath(pathname) && webNextEnabled(app)) return serveWebNext(res, pathname, isPost);
if (next) return serveWebNext(res, pathname, isPost);
serveStatic(res, pathname, isPost);
} catch (e) {
sendError(res, e);