front/debug-inparams.html
2026-01-24 11:55:01 +08:00

136 lines
7.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Debug inParams Processing</title>
</head>
<body>
<h1>Debug inParams Processing</h1>
<div>
<h2>Input inParams:</h2>
<textarea id="input" rows="10" cols="80">{"\r\n \"clientNo\": \"string\",\r\n \"trackNo\": \"string\",\r\n \"serviceNo\": \"string\",\r\n \"productCode\": \"string\",\r\n \"weight\": 3,\r\n \"length\": 3,\r\n \"width\": 3,\r\n \"height\": 3,\r\n \"vatNo\": \"string\",\r\n \"iossNo\": \"string\",\r\n \"eoriNo\": \"string\",\r\n \"warehouseId\": \"string\",\r\n \"isCod\": \"false\",\r\n \"codCurrency\": \"string\",\r\n \"codSum\": \"string\",\r\n \"receiver\": {\r\n \"name\": \"string\",\r\n \"company\": \"string\",\r\n \"phone\": \"string\",\r\n \"email\": \"string\",\r\n \"country\": \"string\",\r\n \"state\": \"string\",\r\n \"city\": \"string\",\r\n \"street\": \"string\",\r\n \"houseNo\": \"string\",\r\n \"address\": \"string\",\r\n \"address2\": \"string\",\r\n \"postalCode\": \"string\"\r\n },\r\n \"sender\": {\r\n \"name\": \"string\",\r\n \"company\": \"string\",\r\n \"phone\": \"string\",\r\n \"email\": \"string\",\r\n \"country\": \"string\",\r\n \"state\": \"string\",\r\n \"city\": \"string\",\r\n \"street\": \"string\",\r\n \"address\": \"string\",\r\n \"address2\": \"string\",\r\n \"postalCode\": \"string\"\r\n },\r\n \"items\": [\r\n {\r\n \"nameEn\": \"string\",\r\n \"nameCn\": \"string\",\r\n \"value\": \"fake_simple_value\",\r\n \"currency\": \"string\",\r\n \"weight\": 3,\r\n \"quantity\": 3,\r\n \"hsCode\": \"string\",\r\n \"sku\": \"string\",\r\n \"url\": \"string\",\r\n \"originCountry\": \"string\",\r\n \"note\": \"string\"\r\n }\r\n ]\r\n}"</textarea>
</div>
<div>
<button onclick="processData()">Process Data</button>
</div>
<div>
<h2>Output:</h2>
<pre id="output"></pre>
</div>
<div>
<h2>Console Log:</h2>
<pre id="console"></pre>
</div>
<script>
// 实现 processInParamsData 函数
function processInParamsData(inParams) {
console.log('processInParamsData called with:', typeof inParams, inParams?.substring?.(0, 100));
if (!inParams) {
console.log('processInParamsData: input is empty');
return '';
}
// 如果是对象,直接格式化
if (typeof inParams === 'object') {
console.log('processInParamsData: input is object, formatting directly');
const result = JSON.stringify(inParams, null, 2);
console.log('processInParamsData: object result length:', result.length);
return result;
}
// 如果不是字符串,返回原值
if (typeof inParams !== 'string') {
console.log('processInParamsData: input is not string, converting to string');
return String(inParams);
}
console.log('processInParamsData: processing string input, length:', inParams.length);
try {
// 步骤1: 处理转义字符 - 将字面量的转义序列转换为实际字符
let processed = inParams;
console.log('processInParamsData: before escape processing, first 100 chars:', processed.substring(0, 100));
// 按顺序处理转义字符(重要:顺序不能错)
processed = processed.replace(/\\r\\n/g, '\n'); // Windows 换行符转义 -> 实际换行
processed = processed.replace(/\\n/g, '\n'); // Unix 换行符转义 -> 实际换行
processed = processed.replace(/\\r/g, '\r'); // 回车符转义 -> 实际回车
processed = processed.replace(/\\t/g, '\t'); // 制表符转义 -> 实际制表符
processed = processed.replace(/\\\\/g, '\\'); // 反斜杠转义 -> 实际反斜杠
processed = processed.replace(/\\"/g, '"'); // 双引号转义 -> 实际双引号
console.log('processInParamsData: after escape processing, first 100 chars:', processed.substring(0, 100));
// 步骤2: 解析处理后的字符串为 JSON 对象
console.log('processInParamsData: attempting to parse JSON...');
const parsedObject = JSON.parse(processed);
console.log('processInParamsData: JSON parsed successfully, keys:', Object.keys(parsedObject));
// 步骤3: 格式化为美观的 JSON 字符串(2 空格缩进)
const result = JSON.stringify(parsedObject, null, 2);
console.log('processInParamsData: formatted result length:', result.length);
console.log('processInParamsData: formatted result first 200 chars:', result.substring(0, 200));
return result;
} catch (error) {
console.warn('processInParamsData 处理失败:', error);
console.warn('processInParamsData: original input:', inParams);
// 如果处理失败,返回原始数据
return inParams;
}
}
// 处理数据的函数
function processData() {
const input = document.getElementById('input').value;
const output = document.getElementById('output');
const consoleOutput = document.getElementById('console');
// 清空之前的输出
output.textContent = '';
consoleOutput.textContent = '';
// 重定向console.log到页面
const logs = [];
const originalLog = console.log;
const originalWarn = console.warn;
console.log = function(...args) {
logs.push('[LOG] ' + args.join(' '));
originalLog.apply(console, args);
};
console.warn = function(...args) {
logs.push('[WARN] ' + args.join(' '));
originalWarn.apply(console, args);
};
try {
// 处理数据
const result = processInParamsData(input);
// 显示结果
output.textContent = result;
consoleOutput.textContent = logs.join('\n');
} catch (error) {
output.textContent = 'Error: ' + error.message;
consoleOutput.textContent = logs.join('\n') + '\n[ERROR] ' + error.message;
} finally {
// 恢复原始的console方法
console.log = originalLog;
console.warn = originalWarn;
}
}
// 页面加载完成后自动处理一次
window.onload = function() {
processData();
};
</script>
</body>
</html>