合入主线登录和营业时段策略,对齐旧界面与 React 访问流程
This commit is contained in:
commit
42395de23a
113 files changed
+1915
-558
No files matched your search
@@ -2,7 +2,7 @@
|
||||
const { fail } = require('../../core/ledger');
|
||||
const LIMIT = 300000;
|
||||
// 按字节累计、超限即拒绝;收齐后一次性按 UTF-8 解码,避免多字节字符被块边界截断。
|
||||
async function readBody(req) {
|
||||
async function readBody(req, afterRead) {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
@@ -11,6 +11,7 @@ async function readBody(req) {
|
||||
if (size > LIMIT) fail('内容过大', 413);
|
||||
chunks.push(buffer);
|
||||
}
|
||||
afterRead?.();
|
||||
const raw = Buffer.concat(chunks, size).toString('utf8');
|
||||
try {
|
||||
return JSON.parse(raw || '{}');
|
||||
|
||||
@@ -9,7 +9,7 @@ function sendError(res, e) {
|
||||
: e.code?.startsWith('SQLITE')
|
||||
? '保存失败,请保留操作内容并联系维护者'
|
||||
: e.message || '操作失败',
|
||||
...(e.code === 'ACCOUNT_CHANGED' ? { code: e.code } : {}),
|
||||
...(['ACCOUNT_CHANGED', 'BUSINESS_HOURS'].includes(e.code) ? { code: e.code } : {}),
|
||||
...(e.code === 'EMPLOYEE_HAS_BUSINESS' ? { code: e.code, businessUsage: e.businessUsage } : {}),
|
||||
...(e.code === 'RECOVERY_MATERIAL_IN_USE' ? { code: e.code, blockedUsages: e.blockedUsages } : {}),
|
||||
});
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
const crypto = require('node:crypto');
|
||||
const TeamMembers = require('../../domains/team/team-members');
|
||||
const ShopSettings = require('../../domains/team/shop-settings');
|
||||
const { fail } = require('../../core/ledger');
|
||||
const { restricted } = require('./shop-access');
|
||||
const { today, fail } = require('../../core/ledger');
|
||||
const { sha } = require('../hash');
|
||||
|
||||
function sessionToken(req) {
|
||||
@@ -27,6 +28,25 @@ function sessionUser(ledger, u) {
|
||||
label: TeamMembers.label(ledger, member) || u.name,
|
||||
};
|
||||
}
|
||||
function sessionInfo(ledger, mode, actor, { outside, period }) {
|
||||
const setup = !ledger.one('SELECT id FROM users LIMIT 1');
|
||||
const onlyOwner = outside || restricted(period);
|
||||
return {
|
||||
user: actor,
|
||||
setup,
|
||||
mode,
|
||||
today: today(),
|
||||
...(period && { accessWindow: actor?.role === 'owner' ? null : period }),
|
||||
brand: { storeName: setup ? '店铺台账' : ShopSettings.read(ledger).storeName },
|
||||
loginOptions: ShopSettings.loginOptions(ledger)
|
||||
.filter(
|
||||
row =>
|
||||
(!onlyOwner || row.member === 'owner') &&
|
||||
ledger.one('SELECT user_id FROM team_user_members WHERE member=?', row.member),
|
||||
)
|
||||
.map(({ member, label }) => ({ member, label })),
|
||||
};
|
||||
}
|
||||
function createSessions(ledger, { production }) {
|
||||
function user(req) {
|
||||
const cookie = sessionToken(req);
|
||||
@@ -64,4 +84,4 @@ function requireActor(req, actor) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
module.exports = { sessionToken, sessionUser, createSessions, requireActor };
|
||||
module.exports = { sessionToken, sessionUser, sessionInfo, createSessions, requireActor };
|
||||
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
const { fail } = require('../../core/ledger');
|
||||
const { accessWindow } = require('../../domains/team/business-hours');
|
||||
const ShopSettings = require('../../domains/team/shop-settings');
|
||||
|
||||
function restricted(period) {
|
||||
return period?.state === 'closed' || period?.state === 'unset';
|
||||
}
|
||||
|
||||
function requireBusinessHours(account, period) {
|
||||
if (restricted(period) && account?.role !== 'owner') {
|
||||
throw Object.assign(new Error(period.message || '员工营业时间尚未设置,请联系店主。'), {
|
||||
status: 403,
|
||||
code: 'BUSINESS_HOURS',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function checkAccount(account, { outside, period }) {
|
||||
requireBusinessHours(account, period);
|
||||
if (outside && account && account.role !== 'owner') fail('员工账号请连接店内 Wi-Fi 后使用', 403);
|
||||
}
|
||||
|
||||
function createShopAccess(ledger, { shopAccessPolicy, hoursPolicy, hoursClock = Date.now }) {
|
||||
const currentWindow = () =>
|
||||
hoursPolicy ? accessWindow(ledger.meta(ShopSettings.KEY)?.businessHours, hoursClock()) : null;
|
||||
function forRequest(req) {
|
||||
const zone = req.headers['x-ledger-access-zone'];
|
||||
if (shopAccessPolicy && zone !== 'store' && zone !== 'owner') fail('请通过店铺正式网址访问', 403);
|
||||
return { outside: shopAccessPolicy && zone === 'owner', period: currentWindow() };
|
||||
}
|
||||
return { currentWindow, forRequest, hoursClock };
|
||||
}
|
||||
|
||||
module.exports = { createShopAccess, restricted, requireBusinessHours, checkAccount };
|
||||
@@ -7,6 +7,9 @@ const PUBLIC_DIR = path.join(__dirname, '..', '..', 'public');
|
||||
const ASSETS = {
|
||||
'/': 'index.html',
|
||||
'/private-operations-ui.js': 'private-operations-ui.js',
|
||||
'/business-hours-lock.js': 'business-hours-lock.js',
|
||||
'/login-diagnostics.js': 'login-diagnostics.js',
|
||||
'/login-ui.js': 'login-ui.js',
|
||||
'/app.js': 'app.js',
|
||||
'/ledger-values.js': 'ledger-values.js',
|
||||
'/report-rules.js': 'report-rules.js',
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// 路径解码后必须仍落在 web/dist 之内,否则按未知地址 404。
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { send, HTML } = require('../http');
|
||||
const { fail } = require('../../core/ledger');
|
||||
const DIST_DIR = path.join(__dirname, '..', '..', 'web', 'dist');
|
||||
const TYPES = {
|
||||
@@ -37,8 +38,7 @@ function readFile(file) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
function serveWebNext(res, pathname, isPost, { distDir = DIST_DIR } = {}) {
|
||||
if (isPost) fail('地址不存在', 404);
|
||||
function nextFile(pathname, distDir = DIST_DIR) {
|
||||
let relative;
|
||||
try {
|
||||
relative = decodeURIComponent(pathname.slice('/next'.length));
|
||||
@@ -52,11 +52,17 @@ function serveWebNext(res, pathname, isPost, { distDir = DIST_DIR } = {}) {
|
||||
const last = relative.split('/').pop();
|
||||
const asset = last.includes('.');
|
||||
const file = asset ? target : path.join(root, 'index.html');
|
||||
return file;
|
||||
}
|
||||
function serveWebNext(res, pathname, isPost, { distDir = DIST_DIR } = {}) {
|
||||
if (isPost) fail('地址不存在', 404);
|
||||
const file = nextFile(pathname, distDir);
|
||||
const type = TYPES[path.extname(file).toLowerCase()];
|
||||
const body = type ? readFile(file) : null;
|
||||
if (!body) fail('地址不存在', 404);
|
||||
if (type === HTML) return send(res, 200, body, type);
|
||||
// 直接写出原始字节:通用 send() 会把 JSON 类型的内容再序列化一次。
|
||||
res.writeHead(200, { 'Content-Type': type });
|
||||
res.end(body);
|
||||
}
|
||||
module.exports = { serveWebNext, webNextEnabled, isNextPath, DIST_DIR };
|
||||
module.exports = { serveWebNext, webNextEnabled, isNextPath, nextFile, DIST_DIR };
|
||||
Reference in new issue
Block a user