zhongshujie
3 小时以前 e33672cf85da88d515d5fe6ccc0a139c3cfaa5db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import MG from "@/assets/js/middleGround/WebMiddleGroundApi";
import { tokenKey } from "@/assets/js/config";
import getPublicImage from "@/assets/js/middleGround/tool";
// 获取题目列表
const getQuestionList = async (
  page,
  questionList,
  activeBook,
  bookQuestionsList
) => {
  if (!bookQuestionsList || !Array.isArray(bookQuestionsList) || bookQuestionsList.length === 0) {
    console.warn("getQuestionList: 传入的 bookQuestionsList 为空或无效,将返回空列表。");
    return [];
  }
  const token = localStorage.getItem(tokenKey);
  let collectList = [];
  if (token) collectList = await getCollectList(activeBook);
  const oldAnswerData = localStorage.getItem(activeBook.name + "oldAnswerData");
  let oldData = null;
  let oldList = [];
  if (oldAnswerData) {
    oldData = JSON.parse(oldAnswerData);
  }
  if (oldData && oldData[page]) {
    oldList = oldData[page];
  }
  let cardList = [
    {
      catalogName: "单选题",
      infoList: [],
    },
    {
      catalogName: "判断题",
      infoList: [],
    },
    {
      catalogName: "多选题",
      infoList: [],
    },
    {
      catalogName: "填空题",
      infoList: [],
    },
    {
      catalogName: "简答题",
      infoList: [],
    },
    {
      catalogName: "材料题",
      infoList: [],
    },
  ];
  let singleChoiceArr = []; // 单选
  let judgeArr = []; // 判断
  let shortArr = []; // 简答
  let multipleChoiceArr = []; // 多选
  let completionArr = []; // 填空
  let materialArr = []; // 材料
  // 11
  for (let bindex = 0; bindex < questionList.length; bindex++) {
    const qitem = questionList[bindex];
    let foundItems = bookQuestionsList.find((citem) => citem.id == qitem);
    if (
      foundItems &&
      foundItems.Embedded_QuestionBank_QuestionType == "material"
    ) {
      foundItems.childList = bookQuestionsList.filter(
        (ditem) =>
          ditem.productLinkInfo[0].LinkPath == foundItems.productLinkPath
      );
    }
 
    let foundlist = [];
    foundlist.push(foundItems);
    if (foundlist.length > 0 && foundlist != undefined) {
      foundlist.forEach((item, index) => {
        const questionObj = handleQuestion(item, index + 1, oldList, collectList);
        if (item.Embedded_QuestionBank_QuestionType == "judge") {
          questionObj.type = "判断题";
          judgeArr.push(questionObj);
        } else if (item.Embedded_QuestionBank_QuestionType == "singleChoice") {
          questionObj.type = "单选题";
          singleChoiceArr.push(questionObj);
        } else if (
          item.Embedded_QuestionBank_QuestionType == "multipleChoice"
        ) {
          questionObj.type = "多选题";
          multipleChoiceArr.push(questionObj);
        } else if (item.Embedded_QuestionBank_QuestionType == "completion") {
          questionObj.type = "填空题";
          completionArr.push(questionObj);
        } else if (item.Embedded_QuestionBank_QuestionType == "shortAnswer") {
          questionObj.type = "简答题";
          shortArr.push(questionObj);
        } else if (item.Embedded_QuestionBank_QuestionType == "material") {
          questionObj.type = "材料题";
          materialArr.push(questionObj);
        }
      });
    }
  }
 
  // for (let qindex = 0; qindex < questionList.length; qindex++) {
  //   const qitem = questionList[qindex];
  //   let query = {
  //     storeInfo: activeBook.storeRefcode,
  //     path: "*",
  //     cmsPath: activeBook.rootCmsItemId,
  //     cmsType: "*",
  //     productId: activeBook.bookId,
  //     queryType: "*",
  //     itemIds: qitem + "",
  //     itemFields: {
  //       Embedded_QuestionBank_Stem: [],
  //       Embedded_QuestionBank_AnalysisCon: [],
  //       Embedded_QuestionBank_Answer: [],
  //       Embedded_QuestionBank_Option: [],
  //       Embedded_QuestionBank_QuestionType: [],
  //       Embedded_QuestionBank_StemStyle: [],
  //       Embedded_QuestionBank_OptionStyle: [],
  //       Embedded_QuestionBank_KnowledgePoint: [],
  //       Embedded_QuestionBank_Difficulty: [],
  //     },
  //   };
  //   const res = await MG.store.getProductDetail(query);
  //   console.log(res.datas.cmsDatas[0].datas, "材料题");
  //   if (!res.datas) return false;
  //   res.datas.cmsDatas[0].datas.forEach((item, index) => {
  //     let oldObj = {};
  //     if (oldList) {
  //       oldObj = oldList.find((item) => item.id == qitem);
  //     }
  //     const questionObj = {
  //       number: index + 1, // 题号
  //       id: item.id,
  //       stem:
  //         item.Embedded_QuestionBank_QuestionType == "completion"
  //           ? JSON.parse(item.Embedded_QuestionBank_Stem)
  //               .stemTxt.replaceAll("<vacancy>", ",input,")
  //               .split(",")
  //           : JSON.parse(item.Embedded_QuestionBank_Stem), // 题干
  //       answer: item.Embedded_QuestionBank_Answer, // 答案
  //       option: item.Embedded_QuestionBank_Option
  //         ? JSON.parse(item.Embedded_QuestionBank_Option)
  //         : "", // 选择题选项
  //       analysisCon: item.Embedded_QuestionBank_AnalysisCon, // 解析
  //       questionType: item.Embedded_QuestionBank_QuestionType, // 题型
  //       optionStyle: item.Embedded_QuestionBank_OptionStyle, // 选项显示类型
  //       stemStyle: item.Embedded_QuestionBank_StemStyle, // 题干显示类型
  //       difficulty: item.Embedded_QuestionBank_Difficulty
  //         ? 4 - item.Embedded_QuestionBank_Difficulty
  //         : 0, // 难度等级
  //       userAnswer: oldObj
  //         ? oldObj.userAnswer
  //         : item.Embedded_QuestionBank_QuestionType == "completion" ||
  //           item.Embedded_QuestionBank_QuestionType == "multipleChoice"
  //         ? []
  //         : "",
  //       isSubmit: false, // 查看解析
  //       isRight: null, // 是否正确
  //       isComplete: false,
  //       isCollect: collectList.indexOf(qitem) > -1 ? true : false,
  //       isUnfold: "",
  //     };
 
  //     console.log(item, "材料题");
 
  //     // 多选和填空答案肯为数组,要转换JSON格式
  //     if (
  //       questionObj.questionType == "completion" ||
  //       questionObj.questionType == "multipleChoice"
  //     ) {
  //       try {
  //         questionObj.answer = JSON.parse(questionObj.answer);
  //       } catch (error) {
  //         questionObj.answer = item.Embedded_QuestionBank_Answer;
  //       }
  //     }
  //     // 填空题改造
  //     if (questionObj.questionType == "completion") {
  //       let index = 0;
  //       for (let i = 0; i < questionObj.stem.length; i++) {
  //         const item = questionObj.stem[i];
  //         if (item == "input") {
  //           questionObj.stem[i] = {
  //             num: index,
  //             data: "input",
  //           };
  //           questionObj.userAnswer[index] = "";
  //           index++;
  //         }
  //       }
  //     }
  //     // 获取图片
  //     if (
  //       questionObj.stemStyle == "Image" ||
  //       questionObj.stemStyle == "TxtAndImage"
  //     ) {
  //       questionObj.stem.stemImage = getPublicImage(
  //         questionObj.stem.stemImage,
  //         150
  //       );
  //     }
  //     if (
  //       questionObj.optionStyle == "Image" ||
  //       questionObj.optionStyle == "TxtAndImage"
  //     ) {
  //       questionObj.option.forEach((optionItem) => {
  //         if (optionItem.img)
  //           optionItem.img = getPublicImage(optionItem.img, 150);
  //       });
  //     }
  //     // 题干富文本处理
  //     if (questionObj.stemStyle == "RichText") {
  //       // questionObj.option.txt = ''
  //       questionObj.stem.stemTxt = questionObj.stem.stemTxt
  //         .replace(
  //           /\<img/gi,
  //           '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
  //         )
  //         .replace(/\<p/gi, '<p class="stem-rich-p"')
  //         .replace("../file", process.env.VUE_APP_API_URL + "/file");
  //     }
  //     // 选项富文本处理
  //     if (
  //       questionObj.optionStyle == "RichText" &&
  //       (questionObj.questionType == "singleChoice" ||
  //         questionObj.questionType == "judge" ||
  //         questionObj.questionType == "multipleChoice")
  //     ) {
  //       questionObj.option.forEach((item) => {
  //         if (item.txt)
  //           item.txt = item.txt
  //             .replace(/\<img/gi, '<img class="option-rich-img"')
  //             .replace(/\<p/gi, '<p class="stem-rich-p"')
  //             .replace("../file", process.env.VUE_APP_API_URL + "/file");
  //       });
  //     }
  //     // 解析富文本处理
  //     if (
  //       questionObj.analysisCon &&
  //       typeof questionObj.analysisCon == "string"
  //     ) {
  //       questionObj.analysisCon = questionObj.analysisCon.replace(
  //         /\<img/gi,
  //         '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
  //       );
  //     }
  //     // 听力题修改
  //     // if (questionObj.questionType == 'singleChoice') {
  //     //   const src = this.extractSourceSrc(questionObj.stem.stemTxt)
  //     //   if (src) {
  //     //     questionObj.src = src
  //     //     questionObj.stem.stemTxt = this.removeVideoAndAudioTags(questionObj.stem.stemTxt)
  //     //   }
  //     // }
 
  //     if (item.Embedded_QuestionBank_QuestionType == "judge") {
  //       questionObj.type = "判断题";
  //       judgeArr.push(questionObj);
  //     } else if (item.Embedded_QuestionBank_QuestionType == "singleChoice") {
  //       questionObj.type = "单选题";
  //       singleChoiceArr.push(questionObj);
  //     } else if (item.Embedded_QuestionBank_QuestionType == "multipleChoice") {
  //       questionObj.type = "多选题";
  //       multipleChoiceArr.push(questionObj);
  //     } else if (item.Embedded_QuestionBank_QuestionType == "completion") {
  //       questionObj.type = "填空题";
  //       completionArr.push(questionObj);
  //     } else if (item.Embedded_QuestionBank_QuestionType == "shortAnswer") {
  //       questionObj.type = "简答题";
  //       shortArr.push(questionObj);
  //     } else if (item.Embedded_QuestionBank_QuestionType == "material") {
  //       questionObj.type = "材料题";
  //       materialArr.push(questionObj);
  //     }
  //   });
  // }
  // 22
  cardList[0].infoList = singleChoiceArr;
  cardList[1].infoList = judgeArr;
  cardList[2].infoList = multipleChoiceArr;
  cardList[3].infoList = completionArr;
  cardList[4].infoList = shortArr;
  cardList[5].infoList = materialArr;
  for (let index = 0; index < cardList.length; index++) {
    const item = cardList[index];
    for (let cindex = 0; cindex < item.infoList.length; cindex++) {
      const citem = item.infoList[cindex];
      citem.number = cindex + 1;
    }
  }
  return cardList.filter((item) => item.infoList.length > 0);
};
 
// 处理题目逻辑
const handleQuestion = (item, index, oldList, collectList) => {
  // 注意:这里的 qitem 应该是 item.id,我假设你原来的代码里 qitem 就是当前 item 的 id
  const qitem = item.id;
  let oldObj = {};
  if (oldList) {
    oldObj = oldList.find((oldItem) => oldItem.id == qitem);
  }
  // 1. 构建 questionObj 的基础结构
  const questionObj = {
    number: index + 1, // 题号
    id: item.id,
    stem:
      item.Embedded_QuestionBank_QuestionType == "completion"
        ? JSON.parse(item.Embedded_QuestionBank_Stem)
          .stemTxt.replaceAll("<vacancy>", ",input,")
          .split(",")
        : JSON.parse(item.Embedded_QuestionBank_Stem), // 题干
    answer: item.Embedded_QuestionBank_Answer, // 答案
    option: item.Embedded_QuestionBank_Option
      ? JSON.parse(item.Embedded_QuestionBank_Option)
      : "", // 选择题选项
    analysisCon: item.Embedded_QuestionBank_AnalysisCon, // 解析
    questionType: item.Embedded_QuestionBank_QuestionType, // 题型
    optionStyle: item.Embedded_QuestionBank_OptionStyle, // 选项显示类型
    stemStyle: item.Embedded_QuestionBank_StemStyle, // 题干显示类型
    difficulty: item.Embedded_QuestionBank_Difficulty
      ? 4 - item.Embedded_QuestionBank_Difficulty
      : 0, // 难度等级
    userAnswer: oldObj
      ? oldObj.userAnswer
      : item.Embedded_QuestionBank_QuestionType == "completion" ||
        item.Embedded_QuestionBank_QuestionType == "multipleChoice"
        ? []
        : "",
    isSubmit: false, // 查看解析
    isRight: null, // 是否正确
    isComplete: false,
    isCollect: collectList.indexOf(qitem) > -1 ? true : false,
    isUnfold: "",
  };
 
 
 
  // 2. 处理多选和填空题的答案(JSON格式转换)
  if (
    questionObj.questionType == "completion" ||
    questionObj.questionType == "multipleChoice"
  ) {
    try {
      questionObj.answer = JSON.parse(questionObj.answer);
    } catch (error) {
      console.error("解析答案失败:", error, item);
      questionObj.answer = item.Embedded_QuestionBank_Answer; // 解析失败则保留原样
    }
  }
 
  // 3. 填空题改造
  if (questionObj.questionType == "completion") {
    let inputIndex = 0; // 使用更具描述性的变量名
    for (let i = 0; i < questionObj.stem.length; i++) {
      const stemPart = questionObj.stem[i]; // 使用更具描述性的变量名
      if (stemPart == "input") {
        questionObj.stem[i] = {
          num: inputIndex,
          data: "input",
        };
        // 确保 userAnswer 是数组且长度足够
        if (!Array.isArray(questionObj.userAnswer)) {
          questionObj.userAnswer = [];
        }
        questionObj.userAnswer[inputIndex] = "";
        inputIndex++;
      }
    }
  }
 
  // 4. 材料题处理 (核心递归点)
  if (questionObj.questionType == "material" && Array.isArray(item.childList)) {
 
    // 遍历子题目列表,并对每一个子项递归调用 processQuestionItem
    questionObj.childList = item.childList.map((childItem, childIndex) => {
      // 子题目通常不需要 oldList 和 collectList,但为了逻辑统一,可以传入
      // 如果子题目也需要独立的作答状态和收藏状态,则需要调整 oldList 和 collectList 的结构
      // 这里假设它们不需要,或者由父级材料题统一管理
      return handleQuestion(childItem, childIndex + 1, null, []);
    });
  }
 
  // 5. 获取图片
  if (
    questionObj.stemStyle == "Image" ||
    questionObj.stemStyle == "TxtAndImage"
  ) {
    // 确保 stem 是一个对象且有 stemImage 属性
    if (questionObj.stem && typeof questionObj.stem === 'object' && questionObj.stem.stemImage) {
      questionObj.stem.stemImage = getPublicImage(
        questionObj.stem.stemImage,
        150
      );
    }
  }
  if (
    questionObj.optionStyle == "Image" ||
    questionObj.optionStyle == "TxtAndImage"
  ) {
    questionObj.option.forEach((optionItem) => {
      if (optionItem.img)
        optionItem.img = getPublicImage(optionItem.img, 150);
    });
  }
 
  // 6. 题干富文本处理
  if (questionObj.stemStyle == "RichText") {
    // 确保 stem 是一个对象且有 stemTxt 属性
    if (questionObj.stem && typeof questionObj.stem === 'object' && questionObj.stem.stemTxt) {
      questionObj.stem.stemTxt = questionObj.stem.stemTxt
        .replace(
          /\<img/gi,
          '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
        )
        .replace(/\<p/gi, '<p class="stem-rich-p"')
        .replace("../file", process.env.VUE_APP_API_URL + "/file");
    }
  }
 
  // 7. 选项富文本处理
  if (
    questionObj.optionStyle == "RichText" &&
    (questionObj.questionType == "singleChoice" ||
      questionObj.questionType == "judge" ||
      questionObj.questionType == "multipleChoice")
  ) {
    questionObj.option.forEach((optItem) => { // 使用更具描述性的变量名
      if (optItem.txt)
        optItem.txt = optItem.txt
          .replace(/\<img/gi, '<img class="option-rich-img"')
          .replace(/\<p/gi, '<p class="stem-rich-p"')
          .replace("../file", process.env.VUE_APP_API_URL + "/file");
    });
  }
 
  // 8. 解析富文本处理
  if (
    questionObj.analysisCon &&
    typeof questionObj.analysisCon == "string"
  ) {
    questionObj.analysisCon = questionObj.analysisCon.replace(
      /\<img/gi,
      '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
    );
  }
 
  return questionObj;
};
 
// 获取收藏列表
const getCollectList = async (activeBook) => {
  const allCollect = [
    {
      type: "bits",
      collectList: [],
    },
    {
      type: "json",
      collectList: [],
    },
  ];
  const res = await MG.identity.getUserKey({
    domain: "collectData",
    keys: [activeBook.bookId],
  });
  try {
    const collect = JSON.parse(res[0].value);
    if (collect.length) {
      allCollect[0].collectList = collect.find(
        (citem) => citem.type == "bits"
      ).collectList;
      allCollect[1].collectList = collect.find(
        (citem) => citem.type == "json"
      ).collectList;
    }
  } catch (error) {
    console.log("暂无数据");
  }
  return allCollect.find((item) => item.type == "bits").collectList;
};
const getQuestionData = async (chapter, chapterData, activeBook) => {
  const data = { ...chapterData };
  const oldAnswerData = localStorage.getItem("oldAnswerData");
  const oldData = oldAnswerData ? JSON.parse(oldAnswerData) : {};
  let oldChapterData;
  if (oldData) {
    oldChapterData = oldData[chapter];
  }
  for (let key in chapterData) {
    let oldList = [];
    if (oldChapterData) {
      oldList = oldChapterData[key];
    }
    data[key] = await getQuestionList(oldList, chapterData[key], activeBook);
  }
  return data;
};
 
export default getQuestionList;