feat: implement gas and delivery admin systems

This commit is contained in:
david
2026-07-30 14:16:58 +08:00
parent 1093385f95
commit f5ecc0d973
252 changed files with 49385 additions and 363 deletions

View File

@@ -0,0 +1,49 @@
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const source = readFileSync(resolve(root, 'src/api/resources.ts'), 'utf8');
const routes = readFileSync(resolve(root, 'src/router/routes/modules/platform.ts'), 'utf8');
const contract = JSON.parse(
readFileSync(resolve(root, 'src/contracts/gas-resources.json'), 'utf8'),
);
const backend = new Map(contract.resources.map((item) => [item.name, item]));
const gasSource = source.slice(source.indexOf('const gasOverrides'));
const frontendDefinitions = [
...gasSource.matchAll(/define\('([^']+)',\s*'[^']+',\s*'([^']+)'/g),
].map((match) => ({ name: match[1], mode: match[2] }));
const frontendNames = frontendDefinitions.map((item) => item.name);
const embeddedResources = new Set();
for (const name of frontendNames) {
const item = backend.get(name);
if (!item) throw new Error(`前端配置了后端不存在的资源:${name}`);
if (item.path !== `/${name}`) throw new Error(`资源路径不一致:${name}`);
}
for (const item of contract.resources) {
if (!frontendNames.includes(item.name))
throw new Error(`缺少后端资源配置:${item.name}`);
const frontend = frontendDefinitions.find(
(definition) => definition.name === item.name,
);
if (frontend?.mode !== item.mode)
throw new Error(
`资源模式不一致:${item.name},后端=${item.mode},前端=${frontend?.mode}`,
);
if (!embeddedResources.has(item.name) && !routes.includes(`'/${item.name}'`))
throw new Error(`缺少后端资源路由:${item.name}`);
}
const forbidden = [
'/platform/platform_', '/gas/gas_', '/delivery/delivery_', '/staff/',
'/user/', '/ec/ec_', '/finance/fin_', '/wallet/wallet',
'delivery_task', 'delivery_track', 'delivery_track_point',
'dev_device_binding', 'dev_smart_cylinder_valve', 'dev_telemetry',
'wallet_ledger', 'wallet_recharge', 'wallet_withdrawal',
];
for (const value of forbidden) {
if (source.includes(value) || routes.includes(value))
throw new Error(`仍存在旧资源或旧路径:${value}`);
}
console.log(`契约检查通过:${frontendNames.length} 个资源`);

View File

@@ -0,0 +1,16 @@
import { execFileSync } from 'node:child_process';
import { mkdirSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const backend = resolve(root, '../../backend/api');
const output = resolve(root, 'src/contracts/gas-resources.json');
const contract = execFileSync('go', ['run', './cmd/cli', 'gas-resource-contract'], {
cwd: backend,
encoding: 'utf8',
});
JSON.parse(contract);
mkdirSync(dirname(output), { recursive: true });
writeFileSync(output, `${contract.trim()}\n`);
console.log(`已同步后端资源契约:${output}`);