From 53353d3ed7f03c9f4e5439bb2d8c767fa057f288 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 18 Dec 2023 17:08:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcase=E4=BF=9D=E5=AD=98=E4=B8=8D=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E7=9A=84json=E6=95=B0=E6=8D=AE=E5=90=8E=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1033211 --user=王孝刚 【接口测试】github#27710,接口CASE中请求体Json数据如果有红叉,保存后重新打开该CASE json格式请求体数据不显示 https://www.tapd.cn/55049933/s/1444855 --- .../frontend/src/utils/format-utils.js | 160 ++++++++++-------- 1 file changed, 92 insertions(+), 68 deletions(-) diff --git a/framework/sdk-parent/frontend/src/utils/format-utils.js b/framework/sdk-parent/frontend/src/utils/format-utils.js index b7ce6b4425..620d595e50 100644 --- a/framework/sdk-parent/frontend/src/utils/format-utils.js +++ b/framework/sdk-parent/frontend/src/utils/format-utils.js @@ -1,4 +1,9 @@ -export function formatJson (json) { +export function formatJson(json) { + try { + JSON.parse(json); + } catch (e) { + return json; + } let i = 0, il = 0, tab = " ", @@ -10,133 +15,150 @@ export function formatJson (json) { for (i = 0, il = json.length; i < il; i += 1) { currentChar = json.charAt(i); switch (currentChar) { - case '{': - if (i != 0 && json.charAt(i - 1) === '$') { + case "{": + if (i != 0 && json.charAt(i - 1) === "$") { newJson += currentChar; flag = true; } else if (!inString) { newJson += currentChar + "\n" + repeat(tab, indentLevel + 1); - indentLevel += 1 + indentLevel += 1; } else { - newJson += currentChar + newJson += currentChar; } break; - case '[': + case "[": if (!inString) { newJson += currentChar + "\n" + repeat(tab, indentLevel + 1); - indentLevel += 1 + indentLevel += 1; } else { - newJson += currentChar + newJson += currentChar; } break; - case '}': + case "}": if (flag) { newJson += currentChar; flag = false; } else if (!inString) { indentLevel -= 1; - newJson += "\n" + repeat(tab, indentLevel) + currentChar + newJson += "\n" + repeat(tab, indentLevel) + currentChar; } else { - newJson += currentChar + newJson += currentChar; } break; - case ']': + case "]": if (!inString) { indentLevel -= 1; - newJson += "\n" + repeat(tab, indentLevel) + currentChar + newJson += "\n" + repeat(tab, indentLevel) + currentChar; } else { - newJson += currentChar + newJson += currentChar; } break; - case ',': + case ",": if (!inString) { - newJson += ",\n" + repeat(tab, indentLevel) + newJson += ",\n" + repeat(tab, indentLevel); } else { - newJson += currentChar + newJson += currentChar; } break; - case ':': + case ":": if (!inString) { - newJson += ": " + newJson += ": "; } else { - newJson += currentChar + newJson += currentChar; } break; - case ' ': + case " ": case "\n": case "\t": if (inString) { - newJson += currentChar + newJson += currentChar; } break; case '"': - if (i > 0 && json.charAt(i - 1) !== '\\') { - inString = !inString + if (i > 0 && json.charAt(i - 1) !== "\\") { + inString = !inString; } newJson += currentChar; break; default: newJson += currentChar; - break + break; } } return newJson; } function repeat(s, count) { - return new Array(count + 1).join(s) + return new Array(count + 1).join(s); } - - export function formatXml(text) { //去掉多余的空格 - text = '\n' + text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) { - return name + ' ' + props.replace(/\s+(\w+=)/g, " $1"); - }); + text = + "\n" + + text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) { + return name + " " + props.replace(/\s+(\w+=)/g, " $1"); + }); //把注释编码 text = text.replace(//g, function ($0, text) { - var ret = ''; + var ret = ""; //alert(ret); return ret; }); //调整格式 - var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg; + var rgx = + /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/gm; var nodeStack = []; - var output = text.replace(rgx, function ($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) { - var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/'); - //alert([all,isClosed].join('=')); - var prefix = ''; - if (isBegin == '!') { - prefix = getPrefix(nodeStack.length); - } - else { - if (isBegin != '/') { + var output = text.replace( + rgx, + function ( + $0, + all, + name, + isBegin, + isCloseFull1, + isCloseFull2, + isFull1, + isFull2 + ) { + var isClosed = + isCloseFull1 == "/" || + isCloseFull2 == "/" || + isFull1 == "/" || + isFull2 == "/"; + //alert([all,isClosed].join('=')); + var prefix = ""; + if (isBegin == "!") { prefix = getPrefix(nodeStack.length); - if (!isClosed) { - nodeStack.push(name); + } else { + if (isBegin != "/") { + prefix = getPrefix(nodeStack.length); + if (!isClosed) { + nodeStack.push(name); + } + } else { + nodeStack.pop(); + prefix = getPrefix(nodeStack.length); } } - else { - nodeStack.pop(); - prefix = getPrefix(nodeStack.length); - } + var ret = "\n" + prefix + all; + return ret; } - var ret = '\n' + prefix + all; - return ret; - }); + ); var prefixSpace = -1; var outputText = output.substring(1); //把注释还原并解码,调格式 - outputText = outputText.replace(/(\s*)/g, function ($0, prefix, text) { - if (prefix.charAt(0) == '\r') - prefix = prefix.substring(1); - text = unescape(text).replace(/\r/g, '\n'); - var ret = '\n' + prefix + ''; - //alert(ret); - return ret; - }); - return outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n'); + outputText = outputText.replace( + /(\s*)/g, + function ($0, prefix, text) { + if (prefix.charAt(0) == "\r") prefix = prefix.substring(1); + text = unescape(text).replace(/\r/g, "\n"); + var ret = "\n" + prefix + ""; + //alert(ret); + return ret; + } + ); + return outputText.replace(/\s+$/g, "").replace(/\r/g, "\r\n"); } /** @@ -147,11 +169,12 @@ export function formatXml(text) { */ export function formatTime(time, cFormat) { if (arguments.length === 0) return null; - if ((time + '').length === 10) { + if ((time + "").length === 10) { time = +time * 1000; } - let format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}', date; - if (typeof time === 'object') { + let format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}", + date; + if (typeof time === "object") { date = time; } else { date = new Date(time); @@ -163,23 +186,24 @@ export function formatTime(time, cFormat) { h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), - a: date.getDay() + a: date.getDay(), }; return format.replace(/{([ymdhisa])+}/g, (result, key) => { let value = formatObj[key]; - if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]; + if (key === "a") + return ["一", "二", "三", "四", "五", "六", "日"][value - 1]; if (result.length > 0 && value < 10) { - value = '0' + value; + value = "0" + value; } return value || 0; }); } function getPrefix(prefixIndex) { - var span = ' '; + var span = " "; var output = []; for (var i = 0; i < prefixIndex; ++i) { output.push(span); } - return output.join(''); + return output.join(""); }