Browse Source

fix: 解析试卷非标json报错问题

zhangjie 1 year ago
parent
commit
93292432b5
4 changed files with 10 additions and 18 deletions
  1. 2 1
      package.json
  2. 7 4
      src/modules/card/components/CardBuildDialog.vue
  3. 0 12
      src/plugins/utils.js
  4. 1 1
      yarn.lock

+ 2 - 1
package.json

@@ -18,6 +18,7 @@
     "element-ui": "^2.14.1",
     "js-md5": "^0.7.3",
     "jsbarcode": "^3.11.3",
+    "json5": "^2.2.3",
     "lodash": "^4.17.21",
     "qs": "^6.11.0",
     "vue": "^2.6.14",
@@ -53,4 +54,4 @@
       "git add"
     ]
   }
-}
+}

+ 7 - 4
src/modules/card/components/CardBuildDialog.vue

@@ -68,6 +68,7 @@
 </template>
 
 <script>
+import json5 from "json5";
 import { mapState, mapMutations, mapActions } from "vuex";
 import { saveCard, cardConfigInfos } from "../api";
 import { tikuPaperDetail } from "../../exam/api";
@@ -78,7 +79,7 @@ import { getCardHeadModel } from "../../../../card/elementModel";
 import TopicElementPreview from "../../../../card/components/TopicElementPreview";
 import CardView from "../../../../card/components/CardView.vue";
 import CardHeadSample from "../../../../card/elements/card-head/CardHead";
-import { deepCopy, objTypeOf, removeRichTextValue } from "@/plugins/utils";
+import { deepCopy, objTypeOf } from "@/plugins/utils";
 // ceshi
 // import paperData from "./paper.json";
 
@@ -153,12 +154,14 @@ export default {
           uuid: this.presetData.uuid,
           attachmentId: res.attachmentId,
         };
+
         const answerJson = res.answerJson
-          ? JSON.parse(removeRichTextValue(res.answerJson))
+          ? json5.parse(res.answerJson)
           : { details: [] };
         const paperJson = res.paperJson
-          ? JSON.parse(removeRichTextValue(res.paperJson))
-          : {};
+          ? json5.parse(res.paperJson)
+          : { details: [] };
+
         this.rebuildPaperQuestionNumber(answerJson);
         this.rebuildPaperQuestionNumber(paperJson);
         this.parsePaperAnswers(paperJson, answerJson);

+ 0 - 12
src/plugins/utils.js

@@ -450,15 +450,3 @@ export function numberToChinese(num) {
     })
     .join("十");
 }
-
-/**
- * 移除富文本json字符串中的type=text的value值
- * @param {string} data 富文本json字符串
- * @returns 字符串
- */
-export function removeRichTextValue(data) {
-  return data.replace(
-    /"type":"text","value":"(.*?)"/g,
-    '"type":"text","value":""'
-  );
-}

+ 1 - 1
yarn.lock

@@ -3942,7 +3942,7 @@ json5@^1.0.1:
   dependencies:
     minimist "^1.2.0"
 
-json5@^2.1.2, json5@^2.2.2:
+json5@^2.1.2, json5@^2.2.2, json5@^2.2.3:
   version "2.2.3"
   resolved "https://registry.npmmirror.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
   integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==