From 987af2519cf2fca82640b422609a974a9e2e12ed Mon Sep 17 00:00:00 2001
From: TommyLemon <1184482681@qq.com>
Date: Fri, 8 Nov 2024 00:50:25 +0800
Subject: [PATCH] =?UTF-8?q?=E9=9B=B6=E4=BB=A3=E7=A0=81=E5=9B=9E=E5=BD=92?=
 =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9A=E8=A7=A3=E5=86=B3=20Response=20JSON?=
 =?UTF-8?q?=20=E8=B6=85=E8=BF=87=202=20=E5=B1=82=E7=9A=84=20key=20?=
 =?UTF-8?q?=E5=85=89=E6=A0=87=E6=82=AC=E6=B5=AE=E6=B3=A8=E9=87=8A=E8=B7=AF?=
 =?UTF-8?q?=E5=BE=84=E9=94=99=E8=AF=AF=EF=BC=8C=E6=96=AD=E8=A8=80=E6=80=BB?=
 =?UTF-8?q?=E6=98=AF=E6=BC=8F=E6=8A=A5=E7=AD=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 apijson/CodeUtil.js     |   2 +-
 apijson/JSONResponse.js |   2 +-
 index.html              |   4 +-
 js/main.js              | 132 +++++++++++++++++++++++++++++++++++-----
 js/server.js            |   2 +
 5 files changed, 123 insertions(+), 19 deletions(-)

diff --git a/apijson/CodeUtil.js b/apijson/CodeUtil.js
index 078d2e6..4a5a4ed 100644
--- a/apijson/CodeUtil.js
+++ b/apijson/CodeUtil.js
@@ -135,7 +135,7 @@ var CodeUtil = {
         if (isExtract && standardObj != null && (isReq || depth != 1
           || [JSONResponse.KEY_CODE, JSONResponse.KEY_MSG, JSONResponse.KEY_THROW].indexOf(key) < 0)) {
           comment = line.substring(cIndex + ccLen).trim();
-          // standardObj = CodeUtil.updateStandardPart(standardObj, names, key, value, comment)
+          // standardObj = CodeUtil.updateStandardByPath(standardObj, names, key, value, comment)
         }
 
         line = line.substring(0, cIndex).trim();
diff --git a/apijson/JSONResponse.js b/apijson/JSONResponse.js
index 96a9ea4..912225f 100644
--- a/apijson/JSONResponse.js
+++ b/apijson/JSONResponse.js
@@ -1758,7 +1758,7 @@ var JSONResponse = {
       return null;
     }
 
-    tgt[pathKeys[pathKeys.length - 1]] = val;
+    tgt[pathKeys[depth - 1]] = val;
 
     return target;
   },
diff --git a/index.html b/index.html
index ec7e64c..17d4e56 100755
--- a/index.html
+++ b/index.html
@@ -370,7 +370,7 @@
 " >
 {
     "User": {
-        "id": 82001
+        "id": "82001"
     }, // 以上查一个对象,以下查一个数组,Comment.userId = User.id。在键值对后按 Enter 回车键自动智能生成补全提示
     "[]": {
          Comment : {
@@ -918,7 +918,7 @@
 <script type="text/x-template" id="item-template">
     <ul class="json-item">
         <vue-val :theme="theme" :field="key" :val="val" :isend="index == objLength(jsondata) - 1" :thiz="JSONResponse.isObject(jsondata) != true || StringUtil.isEmpty(jsondata._$_this_$_, true) ? {} : (JSON.parse(jsondata._$_this_$_) || {})"
-                 v-show="onRenderJSONItem(val, key, thiz == null ? null : thiz._$_path_$_)"
+                 v-show="onRenderJSONItem(val, key, (JSONResponse.isObject(jsondata) != true || StringUtil.isEmpty(jsondata._$_this_$_, true) ? {} : (thiz || JSON.parse(jsondata._$_this_$_) || {}))._$_path_$_)"
                  v-for="(val, key, index) in jsondata"></vue-val>
     </ul>
 </script>
diff --git a/js/main.js b/js/main.js
index 62119d2..3b24b41 100755
--- a/js/main.js
+++ b/js/main.js
@@ -197,12 +197,12 @@
             if (val[0] instanceof Object && (val[0] instanceof Array == false)) {  // && JSONObject.isArrayKey(key, null, isRestful)) {
               // alert('onRenderJSONItem  key = ' + key + '; val = ' + JSON.stringify(val))
 
-              var ckey = key.substring(0, key.lastIndexOf('[]'));
+              var ckey = key == null ? null : key.substring(0, key.lastIndexOf('[]'));
 
-              var aliaIndex = ckey.indexOf(':');
+              var aliaIndex = ckey == null ? -1 : ckey.indexOf(':');
               var objName = aliaIndex < 0 ? ckey : ckey.substring(0, aliaIndex);
 
-              var firstIndex = objName.indexOf('-');
+              var firstIndex = objName == null ? -1 : objName.indexOf('-');
               var firstKey = firstIndex < 0 ? objName : objName.substring(0, firstIndex);
 
               for (var i = 0; i < val.length; i++) {
@@ -239,7 +239,7 @@
                           var pathKeys = StringUtil.split(pathUri, '/');
                           var target = App.isMLEnabled ? JSONResponse.getStandardByPath(standardObj, pathKeys) : JSONResponse.getValByPath(standardObj, pathKeys);
                           var real = JSONResponse.getValByPath(responseObj, pathKeys);
-                          var cmp = App.isMLEnabled ? JSONResponse.compareWithStandard(target, real, path) : JSONResponse.compareWithBefore(target, real, path);
+                          var cmp = App.isMLEnabled ? JSONResponse.compareWithStandard(target, real, pathUri) : JSONResponse.compareWithBefore(target, real, pathUri);
                           cmp.path = pathUri;
                           var cmpShowObj = JSONResponse.getCompareShowObj(cmp);
                           thiz[k] = [cmpShowObj.compareType, cmpShowObj.compareColor, cmpShowObj.compareMessage];
@@ -270,7 +270,7 @@
             }
           }
           else if (val instanceof Object) {
-            var aliaIndex = key.indexOf(':');
+            var aliaIndex = key == null ? -1 : key.indexOf(':');
             var objName = aliaIndex < 0 ? key : key.substring(0, aliaIndex);
 
             // var newVal = JSON.parse(JSON.stringify(val))
@@ -299,7 +299,7 @@
                     var target = App.isMLEnabled ? JSONResponse.getStandardByPath(standardObj, pathKeys) : JSONResponse.getValByPath(standardObj, pathKeys);
                     var real = JSONResponse.getValByPath(responseObj, pathKeys);
             //              c = JSONResponse.compareWithBefore(target, real, path);
-                    var cmp = App.isMLEnabled ? JSONResponse.compareWithStandard(target, real, path) : JSONResponse.compareWithBefore(target, real, path);
+                    var cmp = App.isMLEnabled ? JSONResponse.compareWithStandard(target, real, pathUri) : JSONResponse.compareWithBefore(target, real, pathUri);
                     cmp.path = pathUri;
                     var cmpShowObj = JSONResponse.getCompareShowObj(cmp);
                     thiz[k] = [cmpShowObj.compareType, cmpShowObj.compareColor, cmpShowObj.compareMessage];
@@ -1519,16 +1519,118 @@ https://github.com/Tencent/APIJSON/issues
             } catch (ex) {
               log(ex)
             }
+
+            var path = null;
+            var key = null;
+            var thiz = {
+               _$_path_$_: null,
+               _$_table_$_: null
+            };
+
             if (isSingle || ret instanceof Array || (ret instanceof Object == false)) {
-              this.jsonhtml = ret
+              var val = ret;
+              if (isSingle != true && val instanceof Array && val[0] instanceof Object && (val[0] instanceof Array == false)) {
+                  // alert('onRenderJSONItem  key = ' + key + '; val = ' + JSON.stringify(val))
+                  var ckey = key == null ? null : key.substring(0, key.lastIndexOf('[]'));
+
+                  var aliaIndex = ckey == null ? -1 : ckey.indexOf(':');
+                  var objName = aliaIndex < 0 ? ckey : ckey.substring(0, aliaIndex);
+
+                  var firstIndex = objName == null ? -1 : objName.indexOf('-');
+                  var firstKey = firstIndex < 0 ? objName : objName.substring(0, firstIndex);
+
+                  for (var i = 0; i < val.length; i++) {
+                    var vi = val[i]
+
+                    if (vi instanceof Object && vi instanceof Array == false && JSONObject.isTableKey(firstKey, val, isRestful)) {
+                      // var newVal = JSON.parse(JSON.stringify(val[i]))
+                      if (vi == null) {
+                        continue
+                      }
+
+                      var curPath = '' + i;
+                      var curTable = firstKey;
+                      var thiz = {
+                        _$_path_$_: curPath,
+                        _$_table_$_: curTable
+                      };
+
+                      var newVal = {};
+                      for (var k in vi) {
+                        newVal[k] = vi[k]; //提升性能
+                        if (this.isFullAssert) {
+                            try {
+                              var tr = this.currentRemoteItem.TestRecord || {};
+                              var d = this.currentRemoteItem.Document || {};
+                              var standard = this.isMLEnabled ? tr.standard : tr.response;
+                              var standardObj = StringUtil.isEmpty(standard, true) ? null : JSON.parse(standard);
+                              var tests = this.tests[String(this.currentAccountIndex)] || {};
+                              var responseObj = (tests[d.id] || {})[0]
+
+                              var pathUri = (StringUtil.isEmpty(curPath, false) ? '' : curPath + '/') + k;
+                              var pathKeys = StringUtil.split(pathUri, '/');
+                              var target = this.isMLEnabled ? JSONResponse.getStandardByPath(standardObj, pathKeys) : JSONResponse.getValByPath(standardObj, pathKeys);
+                              var real = JSONResponse.getValByPath(responseObj, pathKeys);
+                              var cmp = this.isMLEnabled ? JSONResponse.compareWithStandard(target, real, pathUri) : JSONResponse.compareWithBefore(target, real, pathUri);
+                              cmp.path = pathUri;
+                              var cmpShowObj = JSONResponse.getCompareShowObj(cmp);
+                              thiz[k] = [cmpShowObj.compareType, cmpShowObj.compareColor, cmpShowObj.compareMessage];
+                              var countKey = '_$_' + cmpShowObj.compareColor + 'Count_$_';
+                              thiz[countKey] = thiz[countKey] == null ? 1 : thiz[countKey] + 1;
+                            } catch (e) {
+                              thiz[k] = [JSONResponse.COMPARE_ERROR, 'red', e.message];
+                              var countKey = '_$_redCount_$_';
+                              thiz[countKey] = thiz[countKey] == null ? 1 : thiz[countKey] + 1;
+                            }
+                        }
+
+                        delete vi[k]
+                      }
+
+                      vi._$_this_$_ = JSON.stringify(thiz)
+                      for (var k in newVal) {
+                        vi[k] = newVal[k]
+                      }
+                    }
+
+                  }
+                }
+
+                this.jsonhtml = val;
             }
             else {
-              this.jsonhtml = Object.assign({
-                _$_this_$_: JSON.stringify({
-                  _$_path_$_: null,
-                  _$_table_$_: null
-                })
-              }, ret)
+                for (var k in ret) {
+                  if (this.isFullAssert) {
+                      try {
+                        var tr = this.currentRemoteItem.TestRecord || {};
+                        var d = this.currentRemoteItem.Document || {};
+                        var standard = this.isMLEnabled ? tr.standard : tr.response;
+                        var standardObj = StringUtil.isEmpty(standard, true) ? null : JSON.parse(standard);
+                        var tests = this.tests[String(this.currentAccountIndex)] || {};
+                        var responseObj = (tests[d.id] || {})[0]
+
+                        var pathUri = k;
+                        var pathKeys = StringUtil.split(pathUri, '/');
+                        var target = this.isMLEnabled ? JSONResponse.getStandardByPath(standardObj, pathKeys) : JSONResponse.getValByPath(standardObj, pathKeys);
+                        var real = JSONResponse.getValByPath(responseObj, pathKeys);
+                //              c = JSONResponse.compareWithBefore(target, real, path);
+                        var cmp = this.isMLEnabled ? JSONResponse.compareWithStandard(target, real, pathUri) : JSONResponse.compareWithBefore(target, real, pathUri);
+                        cmp.path = pathUri;
+                        var cmpShowObj = JSONResponse.getCompareShowObj(cmp);
+                        thiz[k] = [cmpShowObj.compareType, cmpShowObj.compareColor, cmpShowObj.compareMessage];
+                        var countKey = '_$_' + cmpShowObj.compareColor + 'Count_$_';
+                        thiz[countKey] = thiz[countKey] == null ? 1 : thiz[countKey] + 1;
+                      } catch (e) {
+                        thiz[k] = [JSONResponse.COMPARE_ERROR, 'red', e.message];
+                        var countKey = '_$_redCount_$_';
+                        thiz[countKey] = thiz[countKey] == null ? 1 : thiz[countKey] + 1;
+                      }
+                  }
+                }
+
+                this.jsonhtml = Object.assign({
+                    _$_this_$_: JSON.stringify(thiz)
+                }, ret)
             }
 
           }
@@ -1844,7 +1946,7 @@ https://github.com/Tencent/APIJSON/issues
         var name = item == null ? '' : StringUtil.get(item.name);
         target.value = text = before + name + after
         if (target == vScript) { // 不这样会自动回滚
-          App.scripts[App.scriptType][App.scriptBelongId][App.isPreScript ? 'pre' : 'post'].script = text
+          this.scripts[this.scriptType][this.scriptBelongId][this.isPreScript ? 'pre' : 'post'].script = text
         }
         else if (target == vInput) {
           inputted = target.value;
@@ -1870,7 +1972,7 @@ https://github.com/Tencent/APIJSON/issues
           }
 
           if (isInputValue != true) {
-            App.showOptions(target, text, before + name + (isSingle ? "'" : '"') + ': ', after.substring(3), true);
+            this.showOptions(target, text, before + name + (isSingle ? "'" : '"') + ': ', after.substring(3), true);
           }
         } else {
           target.selectionStart = selectionStart;
diff --git a/js/server.js b/js/server.js
index 5bbcad8..06e9bc7 100644
--- a/js/server.js
+++ b/js/server.js
@@ -79,11 +79,13 @@ app.use(async ctx => {
   ctx.set('Access-Control-Allow-Headers', "*");
   ctx.set('Access-Control-Allow-Credentials', 'true');
   ctx.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,PUT,DELETE,OPTIONS,TRACE');
+//    ctx.set('Access-Control-Expose-Headers', "*");
 
   if (ctx.method == null || ctx.method.toUpperCase() == 'OPTIONS') {
      ctx.status = 200;
      return;
   }
+
   if (ctx.path == '/test/start' || (isLoading != true && ctx.path == '/test')) {
     if (isLoading && ctx.path == '/test/start') {
       ctx.status = 200;