252 lines
7.4 KiB
JavaScript
252 lines
7.4 KiB
JavaScript
'use strict';
|
||
|
||
const path = require('node:path');
|
||
const urllib = require('urllib');
|
||
|
||
const DEFAULT_FILE_URL = 'https://ye4s.file.gzxunren.com/exports/receiving/completed/100000/20260405/%E6%94%B6%E8%B4%A7%E5%AE%8C%E6%88%90_R20547058.xlsx';
|
||
const DEFAULT_FILE_NAME = '收货完成_R20547058.xlsx';
|
||
|
||
function parseArgs(argv) {
|
||
const options = {
|
||
url: process.env.WEBHOOK_DEBUG_URL || '',
|
||
textUrl: process.env.WEBHOOK_DEBUG_TEXT_URL || '',
|
||
fileUrl: process.env.WEBHOOK_DEBUG_FILE_URL || '',
|
||
mode: process.env.WEBHOOK_DEBUG_MODE || 'both',
|
||
warehouseId: Number(process.env.WEBHOOK_DEBUG_WAREHOUSE_ID || 100000),
|
||
storeId: Number(process.env.WEBHOOK_DEBUG_STORE_ID || 1000016),
|
||
receivingId: Number(process.env.WEBHOOK_DEBUG_RECEIVING_ID || 202604050001),
|
||
receivingNumber: process.env.WEBHOOK_DEBUG_RECEIVING_NUMBER || 'SIM202604050001',
|
||
fileResourceUrl: process.env.WEBHOOK_DEBUG_REPORT_URL || DEFAULT_FILE_URL,
|
||
fileName: process.env.WEBHOOK_DEBUG_REPORT_FILE || DEFAULT_FILE_NAME,
|
||
timeout: Number(process.env.WEBHOOK_DEBUG_TIMEOUT || 10000),
|
||
};
|
||
|
||
for (const arg of argv) {
|
||
if (!arg.startsWith('--')) {
|
||
continue;
|
||
}
|
||
|
||
const [rawKey, ...rest] = arg.slice(2).split('=');
|
||
const value = rest.join('=');
|
||
switch (rawKey) {
|
||
case 'url':
|
||
options.url = value || options.url;
|
||
break;
|
||
case 'text-url':
|
||
options.textUrl = value || options.textUrl;
|
||
break;
|
||
case 'file-url':
|
||
options.fileUrl = value || options.fileUrl;
|
||
break;
|
||
case 'mode':
|
||
options.mode = value || options.mode;
|
||
break;
|
||
case 'warehouse-id':
|
||
options.warehouseId = Number(value || options.warehouseId);
|
||
break;
|
||
case 'store-id':
|
||
options.storeId = Number(value || options.storeId);
|
||
break;
|
||
case 'receiving-id':
|
||
options.receivingId = Number(value || options.receivingId);
|
||
break;
|
||
case 'receiving-number':
|
||
options.receivingNumber = value || options.receivingNumber;
|
||
break;
|
||
case 'report-url':
|
||
options.fileResourceUrl = value || options.fileResourceUrl;
|
||
break;
|
||
case 'report-file':
|
||
options.fileName = value || options.fileName;
|
||
break;
|
||
case 'timeout':
|
||
options.timeout = Number(value || options.timeout);
|
||
break;
|
||
case 'help':
|
||
options.help = true;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!options.textUrl) {
|
||
options.textUrl = options.url;
|
||
}
|
||
if (!options.fileUrl) {
|
||
options.fileUrl = options.url;
|
||
}
|
||
|
||
return options;
|
||
}
|
||
|
||
function printHelp() {
|
||
console.log(`用法:\n npm run debug:webhook:receiving -- --url=http://127.0.0.1:7071/notic\n npm run debug:webhook:receiving -- --text-url=http://127.0.0.1:7071/notic --file-url=http://127.0.0.1:7071/notic2\n\n说明:\n - 默认发送两次请求:text + file\n - 默认模拟 warehouseId=100000, storeId=1000016 的收货完成通知\n - 可通过 --mode=text 或 --mode=file 只发送一种\n`);
|
||
}
|
||
|
||
function buildBasePayload(options) {
|
||
return {
|
||
traceId: `debug_${Date.now()}_${Math.random().toString(16).slice(2, 8)}`,
|
||
event: 'receiving_completed',
|
||
businessName: 'receiving',
|
||
warehouseId: Number(options.warehouseId),
|
||
storeId: Number(options.storeId),
|
||
businessId: Number(options.receivingId),
|
||
occurredAt: new Date().toISOString(),
|
||
subject: {
|
||
id: Number(options.warehouseId),
|
||
type: 'warehouse',
|
||
name: `测试仓${options.warehouseId}`,
|
||
code: '',
|
||
config: {
|
||
max_notice_webhook_count: 5,
|
||
},
|
||
},
|
||
store: {
|
||
id: Number(options.storeId),
|
||
name: `测试货主${options.storeId}`,
|
||
businessLicense: `BL-${options.storeId}`,
|
||
storeRemask: `货主${options.storeId}备注`,
|
||
},
|
||
sender: {
|
||
id: 90001,
|
||
name: '调试脚本',
|
||
},
|
||
receivers: [],
|
||
metadata: {
|
||
debug: true,
|
||
sourceScript: path.basename(__filename),
|
||
receivingId: Number(options.receivingId),
|
||
receivingNumber: options.receivingNumber,
|
||
warehouseId: Number(options.warehouseId),
|
||
storeId: Number(options.storeId),
|
||
stats: {
|
||
total_details: 10,
|
||
matched_details: 10,
|
||
unmatched_details: 0,
|
||
total_quantity: 120,
|
||
},
|
||
completionReport: null,
|
||
},
|
||
};
|
||
}
|
||
|
||
function buildTextPayload(options) {
|
||
const payload = buildBasePayload(options);
|
||
const title = `收货单${options.receivingNumber}已完成`;
|
||
const content = `收货单${options.receivingNumber}已完成,匹配商品10件,未匹配商品0件,总数量120,完成人调试脚本。`;
|
||
payload.message = {
|
||
type: 'text',
|
||
title,
|
||
content,
|
||
url: '',
|
||
fileName: '',
|
||
mimeType: '',
|
||
body: content,
|
||
};
|
||
return payload;
|
||
}
|
||
|
||
function buildFilePayload(options) {
|
||
const payload = buildBasePayload(options);
|
||
const title = `收货单${options.receivingNumber}已完成`;
|
||
const content = `收货单${options.receivingNumber}已完成,匹配商品10件,未匹配商品0件,总数量120,完成人调试脚本。`;
|
||
payload.message = {
|
||
type: 'file',
|
||
title,
|
||
content,
|
||
url: options.fileResourceUrl,
|
||
fileName: options.fileName,
|
||
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
body: content,
|
||
};
|
||
payload.metadata.completionReport = {
|
||
fileKey: options.fileName,
|
||
url: options.fileResourceUrl,
|
||
};
|
||
return payload;
|
||
}
|
||
|
||
async function postJson(url, data, timeout) {
|
||
const response = await urllib.request(url, {
|
||
method: 'POST',
|
||
headers: {
|
||
'content-type': 'application/json',
|
||
},
|
||
data: JSON.stringify(data),
|
||
timeout: Number(timeout) || 10000,
|
||
dataType: 'text',
|
||
});
|
||
|
||
return {
|
||
status: response.status,
|
||
body: String(response.data || ''),
|
||
};
|
||
}
|
||
|
||
async function main() {
|
||
const options = parseArgs(process.argv.slice(2));
|
||
if (options.help) {
|
||
printHelp();
|
||
return;
|
||
}
|
||
|
||
if (!options.textUrl && !options.fileUrl) {
|
||
throw new Error('请通过 --url 或 --text-url/--file-url 指定接收端地址');
|
||
}
|
||
|
||
const tasks = [];
|
||
if ([ 'both', 'text' ].includes(options.mode) && options.textUrl) {
|
||
tasks.push({
|
||
label: 'text',
|
||
url: options.textUrl,
|
||
payload: buildTextPayload(options),
|
||
});
|
||
}
|
||
if ([ 'both', 'file' ].includes(options.mode) && options.fileUrl) {
|
||
tasks.push({
|
||
label: 'file',
|
||
url: options.fileUrl,
|
||
payload: buildFilePayload(options),
|
||
});
|
||
}
|
||
|
||
if (tasks.length === 0) {
|
||
throw new Error(`mode=${options.mode} 下没有可发送的 webhook 请求`);
|
||
}
|
||
|
||
const results = [];
|
||
for (const task of tasks) {
|
||
console.log(`\n[send] ${task.label.toUpperCase()} -> ${task.url}`);
|
||
const response = await postJson(task.url, task.payload, options.timeout);
|
||
console.log(`[response] status=${response.status} body=${response.body}`);
|
||
results.push({
|
||
label: task.label,
|
||
url: task.url,
|
||
status: response.status,
|
||
body: response.body,
|
||
traceId: task.payload.traceId,
|
||
event: task.payload.event,
|
||
});
|
||
}
|
||
|
||
const failedCount = results.filter(item => Number(item.status) < 200 || Number(item.status) >= 300).length;
|
||
console.log('\n[summary]');
|
||
console.log(JSON.stringify({
|
||
options: {
|
||
...options,
|
||
timeout: Number(options.timeout),
|
||
},
|
||
results,
|
||
failedCount,
|
||
}, null, 2));
|
||
|
||
if (failedCount > 0) {
|
||
process.exitCode = 1;
|
||
}
|
||
}
|
||
|
||
main().catch(error => {
|
||
console.error('[debug-send-receiving-webhooks] failed:', error.message);
|
||
process.exit(1);
|
||
}); |