Преглед изворни кода

优化返回结构解析,保持原样输出

mxd пре 3 година
родитељ
комит
3b62a0d163
2 измењених фајлова са 18 додато и 4 уклоњено
  1. 4 3
      src/scripts/service/magic-api.js
  2. 14 1
      src/scripts/utils.js

+ 4 - 3
src/scripts/service/magic-api.js

@@ -4,7 +4,7 @@ import request from '../request.js'
 import bus from '../bus.js'
 import $i from '../i18n.js'
 import modal from '../../components/common/dialog/magic-modal.js'
-import { definition2TreeList, getSizeUnit } from '../utils.js'
+import { definition2TreeList, getSizeUnit, formatJson } from '../utils.js'
 import { parseJson } from '../parsing/parser.js'
 import { nextTick } from 'vue'
 function sendApiTestRequest(opened, requestConfig) {
@@ -27,7 +27,8 @@ function sendApiTestRequest(opened, requestConfig) {
 			reader.readAsText(data)
 			reader.onload = function () {
 				try {
-					resolve(JSON.parse(this.result))
+					JSON.parse(this.result)
+					resolve(this.result)
 				} catch (e) {
 					resolve(data)
 				}
@@ -46,7 +47,7 @@ function sendApiTestRequest(opened, requestConfig) {
 			opened.item.responseBodyDefinition = null
 			opened.responseBodyTree = null
 		} else {
-			opened.item.responseBody = JSON.stringify(data)
+			opened.item.responseBody = formatJson(data)
 			let definition = parseJson(opened.item.responseBody, opened.item.responseBodyDefinition)
 			opened.item.responseBodyDefinition = definition
 			opened.responseBodyTree = definition2TreeList(definition)

+ 14 - 1
src/scripts/utils.js

@@ -1,4 +1,5 @@
 import $i from './i18n.js'
+import Beautifier from './beautifier/javascript/beautifier.js'
 /**
  * 排序数组对象
  * @param {*} arr 排序的数据
@@ -256,4 +257,16 @@ export function loadPlugin(url) {
 }
 export function rand(n, m){
 	return Math.floor(Math.random()*(m-n+1)+n)
-}
+}
+
+export function formatJson(val, defaultVal) {
+    if (val) {
+        if (typeof val == 'string') {
+            return new Beautifier(val).beautify()
+        }
+        if (val) {
+            return JSON.stringify(val, null, 4);
+        }
+    }
+    return defaultVal || ''
+};