Skip to content

Commit

Permalink
fix: 修复搜索结果 undefined 的 BUG, 修复超星章节测试填空题BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 13, 2022
1 parent be8c3ff commit 97500c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
41 changes: 24 additions & 17 deletions packages/core/src/core/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,33 @@ export class OCSWorker<E extends RawElements = RawElements> {
}

/** 查找题目 */
const searchResults = await this.doAnswer(elements, type);

if (
!searchResults ||
searchResults.length === 0 ||
searchResults.map((res) => res.answers.map((ans) => ans.answer)).flat().length === 0
) {
if (!searchResults) {
throw new Error("答案获取失败, 请重新运行, 或者忽略此题。");
} else {
const ansLength = searchResults.map((res) => res.answers.map((ans) => ans.answer)).flat();
if (ansLength.length === 0) {
throw new Error("搜索不到答案, 请重新运行, 或者忽略此题。");
}
let searchResults = await this.doAnswer(elements, type);

if (!searchResults) {
throw new Error("答案获取失败, 请重新运行, 或者忽略此题。");
} else {
/** 筛选出有效的答案 */
const validResults = searchResults
.map((res) => res.answers.map((ans) => ans.answer))
.flat()
.filter((ans) => ans);

// 答案为 undefined 的情况, 需要赋值给一个空字符串
searchResults.forEach((res) => {
res.answers = res.answers.map((ans) => {
ans.answer = ans.answer ? ans.answer : "";
return ans;
});
});

/** 改变上下文 */
this.currentContext = { searchResults, root: el, elements };

if (searchResults.length === 0 || validResults.length === 0) {
throw new Error("搜索不到答案, 请重新运行, 或者忽略此题。");
}
}

/** 改变上下文 */
this.currentContext = { searchResults, root: el, elements };

/** 开始处理 */
if (typeof this.opts.work === "object") {
if (elements.options) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/script/cx/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async function chapterTestTask(setting: ScriptSettings["cx"]["work"], frame: HTM
* ul li label:not(.after) 判断题
* ul li textarea 填空题
*/
options: "ul li .after,ul li textarea,ul li label:not(.before)",
options: "ul li .after,ul li textarea,ul textarea,ul li label:not(.before)",
type: 'input[id^="answertype"]',
},
/** 默认搜题方法构造器 */
Expand Down

0 comments on commit 97500c5

Please sign in to comment.