130 lines
4.4 KiB
JavaScript
130 lines
4.4 KiB
JavaScript
import getlogicStatus from "./logical";
|
|
|
|
// 更新code
|
|
function updateCode(action, logic, pages, page) {
|
|
const { question_index, skip_question_index } = logic;
|
|
const startIndex = pages.findIndex(
|
|
(page) => page.findIndex((questionIndex) => questionIndex === question_index) !== -1
|
|
);
|
|
if (startIndex < page) {
|
|
if (skip_question_index === -1) {
|
|
action.code = 20011;
|
|
action.msg = "成功结束页";
|
|
} else if (skip_question_index === -2) {
|
|
action.code = 20004;
|
|
action.msg = "甄别结束页";
|
|
} else if (skip_question_index === -3) {
|
|
action.code = 20016;
|
|
action.msg = "配额超限页";
|
|
}
|
|
}
|
|
}
|
|
// 更新分页pages(题后跳转逻辑)
|
|
function updatePagesAfter(pages, logic, jumpTo, page) {
|
|
const { question_index, skip_question_index } = logic;
|
|
const startIndex = pages.findIndex(
|
|
(page) => page.findIndex((questionIndex) => questionIndex === question_index) !== -1
|
|
);
|
|
const endIndex = pages.findIndex(
|
|
(page) => page.findIndex((questionIndex) => questionIndex === skip_question_index) !== -1
|
|
);
|
|
if (endIndex !== -1) {
|
|
const endQuestionIndex = pages[endIndex].findIndex(
|
|
(questionIndex) => questionIndex === skip_question_index
|
|
);
|
|
pages[endIndex].splice(0, endQuestionIndex);
|
|
// 跳转到某页
|
|
if (startIndex > endIndex && startIndex < page) {
|
|
jumpTo.question_index = question_index;
|
|
return (jumpTo.question_page = endIndex + 1);
|
|
}
|
|
pages.splice(startIndex + 1, endIndex - startIndex - 1);
|
|
}
|
|
}
|
|
|
|
// 更新分页pages(题前设置逻辑)
|
|
function updatePagesBefore(pages, hideQuestionIndex) {
|
|
const pagesIndex = pages.findIndex(
|
|
(page) => page.findIndex((questionIndex) => questionIndex === hideQuestionIndex) !== -1
|
|
);
|
|
if (pagesIndex === -1) return;
|
|
if (pages[pagesIndex].length === 1) {
|
|
pages.splice(pagesIndex, 1);
|
|
} else {
|
|
const pageIndex = pages[pagesIndex].findIndex(
|
|
(questionIndex) => questionIndex === hideQuestionIndex
|
|
);
|
|
pages[pagesIndex].splice(pageIndex, 1);
|
|
}
|
|
}
|
|
|
|
// 自动填写
|
|
function autoFill(answerAutoFill, logic) {
|
|
answerAutoFill.push({
|
|
answer: logic.autofill.answer_insert,
|
|
question_index: logic.question_index,
|
|
question_type: logic.autofill.question_type,
|
|
});
|
|
}
|
|
|
|
// 选项隐藏逻辑
|
|
function updateOptionHidden(hide_options, logic) {
|
|
const { question_index, hide_option_index } = logic;
|
|
hide_options.question_index = question_index;
|
|
if (logic.logicStatus) {
|
|
hide_options.option_key.push(...hide_option_index.map((opt) => opt.option_key));
|
|
}
|
|
}
|
|
|
|
// 模拟答题接口
|
|
export default function answerMock(questionsData, page) {
|
|
const data = {
|
|
action: { code: 20010, msg: "答案已记录" },
|
|
jump_to: {},
|
|
hide_options: {
|
|
option_key: [],
|
|
},
|
|
answer_info_autofill: [],
|
|
pages: JSON.parse(JSON.stringify(questionsData.answer.pages_init)),
|
|
};
|
|
const logics = getlogicStatus(questionsData);
|
|
logics.forEach((logic) => {
|
|
if (logic.logicStatus && logic.skip_type === 0) {
|
|
// 题后跳转逻辑
|
|
if (logic.skip_question_index < 0) {
|
|
return updateCode(data.action, logic, data.pages, page);
|
|
}
|
|
updatePagesAfter(data.pages, logic, data.jump_to, page);
|
|
} else if (logic.logicStatus && logic.skip_type === 1) {
|
|
// 题前设置逻辑
|
|
updatePagesBefore(data.pages, logic.question_index);
|
|
} else if (logic.logicStatus && logic.skip_type === 3) {
|
|
// 自动填写逻辑
|
|
autoFill(data.answer_info_autofill, logic);
|
|
} else if (logic.skip_type === 4) {
|
|
// 只计算跳转后所在页面的隐藏逻辑,否则会出现只返回最后一道隐藏选项题目的情况,导致失效
|
|
const toPage = page + 1
|
|
const hasHiddenLogicQuizPage = data.pages.findIndex((page) => page.includes(logic.question_index)) + 1
|
|
if (hasHiddenLogicQuizPage === toPage) {
|
|
// 选项隐藏逻辑
|
|
updateOptionHidden(data.hide_options, logic);
|
|
}
|
|
}
|
|
});
|
|
// 更新问卷状态
|
|
if (page === data.pages.length) {
|
|
data.action.code = 20011;
|
|
data.action.msg = "成功结束页";
|
|
}
|
|
// 拒绝知情同意书
|
|
// const refuseIndex = questionsData.questions.findIndex(
|
|
// (question) => question.question_type === 23 && question.answer?.value === "2"
|
|
// );
|
|
// if (refuseIndex !== -1) {
|
|
// data.action.code = 20013;
|
|
// data.action.msg = "不同意继续参与,已结束作答";
|
|
// }
|
|
// 返回数据
|
|
return data;
|
|
}
|