'use strict'; /** * 调试脚本:为指定账号铸造 JWT 并调用系统接口完成收货。 * 用法: * node script/debug-complete-receiving.cjs --receiving-id=1512 --account-id=1 * node script/debug-complete-receiving.cjs --receiving-id=1512 --account-id=1 --host=127.0.0.1 --port=6060 */ const http = require('node:http'); const jwt = require('jsonwebtoken'); const JWT_SECRET = 'E5#ex.g@,GfL_8?'; const JWT_EXPIRES_IN = 14400; function parseArgs(argv) { const options = { receivingId: 0, accountId: 1, host: '127.0.0.1', port: 6060, remark: '', }; for (const arg of argv) { if (!arg.startsWith('--')) { continue; } const [ rawKey, ...rest ] = arg.slice(2).split('='); const value = rest.join('='); switch (rawKey) { case 'receiving-id': options.receivingId = Number(value || options.receivingId); break; case 'account-id': options.accountId = Number(value || options.accountId); break; case 'host': options.host = value || options.host; break; case 'port': options.port = Number(value || options.port); break; case 'remark': options.remark = value || ''; break; default: break; } } return options; } function postSystemMethod(options, token, method, body) { return new Promise((resolve, reject) => { const bodyJson = JSON.stringify(body); const path = `/system?method=${encodeURIComponent(method)}&v=1×tamp=${Math.floor(Date.now() / 1000)}`; const req = http.request({ hostname: options.host, port: options.port, path, method: 'POST', headers: { platform: 'web', project: 'wms', Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(bodyJson), }, }, res => { let data = ''; res.on('data', chunk => { data += chunk; }); res.on('end', () => { resolve({ status: res.statusCode, body: data }); }); }); req.on('error', reject); req.write(bodyJson); req.end(); }); } async function main() { const options = parseArgs(process.argv.slice(2)); if (!options.receivingId) { console.error('缺少 --receiving-id'); process.exit(1); } const token = jwt.sign({ id: options.accountId, platform_code: 'web', current_store_id: 100000, }, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN }); console.log(`[debug-complete-receiving] accountId=${options.accountId} receivingId=${options.receivingId}`); const result = await postSystemMethod(options, token, 'storage.receiving.completeReceiving', { id: options.receivingId, remark: options.remark || undefined, }); console.log(`HTTP ${result.status}`); try { console.log(JSON.stringify(JSON.parse(result.body), null, 2).substring(0, 3000)); } catch (_) { console.log(result.body.substring(0, 1000)); } } main().catch(error => { console.error('[debug-complete-receiving] failed:', error.message); process.exit(1); });