import { getPublicImage } from "../../../../assets/js/middleGround/tool"; const app = getApp(); Page({ /** * 页面的初始数据 */ data: { barHeight: "", navBarHeight: "", loading: false, answerTitle: "", // 导航栏标题 countdownInterval: null, // 计时器 isCountdownRunning: true, // 是否倒计时 countdownTime: 0, // 倒计时时间 bookId: "", productLinkPath: "", rootCmsItemId: "", idPathList: [], // 题目列表 answerType: "", // 答题模式 submitStatus: false, // 提交状态 currentIndex: 0, // 当前显示的题号 collectList: [], // 收藏题目列表 errorList: [], // 错题列表 subjectiveTotal: 0, // 客观题总数 subjectiveNum: 0, // 客观题得分 subjectiveGrade: 0, // 客观题总分 correctNum: 0, // 正确题目数量 total: 0, // 题目总数 cardList: [], // 提交项, questionDataList: [], // 显示题目列表 mockid: 0, uuid: 0, mockData: { // 组卷信息 state: "0", score: 0, // 总分 sumTime: 0, time: 0, // 答题剩余时间 datas: [], // 所有模考记录数据(题目列表,用户答题,考试报告) answer: [], }, saveTime: 20, isNight: false, sliderValue: 0, // 字体滑块 startTime: "", //进入页面当前时间 pauseTime: 0, //暂停时间 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度 this.setData({ barHeight: systInfo.statusBarHeight, navBarHeight: navBarHeight, answerTitle: options.answerTitle, bookId: options.bookId, productLinkPath: options.productLinkPath ? options.productLinkPath : "", rootCmsItemId: options.rootCmsItemId, idPathList: options.idPathList ? JSON.parse(options.idPathList) : [], answerType: options.answerType, }); if (this.data.answerType == "mock") { this.setData({ uuid: options.uuid, mockid: options.mockid, }); } this.init(); console.log("传参", options); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { if (this.data.answerType != "mock") { if (wx.timer) { clearInterval(wx.timer); } } if (this.data.answerType == "mock") { if (wx.timer) { clearInterval(wx.timer); } wx.timer = setInterval(() => { this.setData({ saveTime: this.data.saveTime - 1, }); if (this.data.saveTime == 0) { this.saveMockData(); } }, 1000); } this.setData({ startTime: Date.now(), }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { if (wx.timer) { clearInterval(wx.timer); } this.setData({ pauseTime: Date.now(), }); if (wx.getStorageSync(app.config.tokenKey)) { let duration = this.data.pauseTime - this.data.startTime; this.count(duration); } }, /** * 生命周期函数--监听页面卸载 */ onUnload(e) { if (wx.timer) { clearInterval(wx.timer); } if (this.data.countdownInterval !== null) { clearInterval(this.data.countdownInterval); } this.setData({ pauseTime: Date.now(), }); if (wx.getStorageSync(app.config.tokenKey)) { let duration = this.data.pauseTime - this.data.startTime; this.count(duration); } }, count(timeStr) { const data = { appRefCode: app.config.appRefCode, type: "LearningTime", //统计类型--阅读时长 data: timeStr + "", //统计内容--时长毫秒 event: "LearningTime", sysType: "App", }; //阅读商品的id if (this.data.bookId) { data.productId = this.data.bookId; } //阅读资源的id // if (product.cmsItemId) { // data.cmsItemId = product.cmsItemId // } app.MG.job.newJobWithApiNewEvent(data).then((res) => {}); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, // 监听watch watch(context, variableName, callback) { let value = context.data[variableName]; // 获取被监听属性的当前值 // 使用 Object.defineProperty 方法在数据对象上定义属性的 getter 和 setter Object.defineProperty(context.data, variableName, { configurable: true, // 可配置 enumerable: true, // 可枚举 get: function () { return value; // 返回属性的当前值 }, set: function (newVal) { const oldVal = value; // 记录属性的旧值 value = newVal; // 更新属性的值 callback.call(context, newVal, oldVal); // 调用回调函数,传递新值和旧值 }, }); }, // 改变loading状态 changeLoadingState() { this.setData({ loading: true }) }, // 返回拦截 beforeleave() { wx.showModal({ title: "提示", content: "未提交,是否退出答题", confirmColor: "#ff6c00", cancelColor: "#949494", complete: (res) => { if (res.cancel) {} if (res.confirm) { this.setData({ submitStatus: true, }); wx.navigateBack(); } }, }); }, onChangeSlider(e) { this.setData({ sliderValue: e.detail.value, }); console.log(e.detail.value); }, // 返回 goBack() { console.log(this.data.submitStatus); wx.navigateBack(); }, //设置背景色 changeBGColor(e) { this.setData({ isNight: e.detail.value, }); }, // 获取保存的倒计时时间 getSavedTime() { const savedTime = wx.getStorageSync("countdownTime"); return savedTime ? parseInt(savedTime) : null; }, // 保存倒计时时间到本地存储 saveTime() { wx.setStorageSync("countdownTime", this.data.countdownTime.toString()); }, clearTime() { this.setData({ countdownTime: 2 * 60 * 60 * 1000, }); }, // 暂停或继续倒计时 toggleCountdown() { if (this.data.countdownInterval) { clearInterval(this.data.countdownInterval); this.setData({ countdownInterval: null, isCountdownRunning: false, }); } else { this.startCountdown(); this.setData({ isCountdownRunning: true, }); } }, // 开始倒计时 startCountdown() { // 如果计时器已经存在,先清除之前的计时器 if (this.data.countdownInterval) { clearInterval(this.data.countdownInterval); this.setData({ countdownInterval: null, }); } this.setData({ countdownInterval: setInterval(() => { this.setData({ countdownTime: this.data.countdownTime - 1000, }); if (this.data.countdownTime <= 0) { clearInterval(this.data.countdownInterval); this.setData({ countdownTime: 0, isCountdownRunning: false, }); } this.saveTime(); }, 1000), }); }, // 切换题目 changeSwiper(e) { this.setData({ currentIndex: e.detail.index, }); let index = e.detail.index - 1 >= 0 ? e.detail.index - 1 : 0; const item = this.data.questionDataList[index]; if ( (this.data.answerType == "collectQuestion" || this.data.answerType == "errorQuestion") && !item.isComplete ) { let flag = this.isHaveAnswer(item.userAnswer); if (flag) this.handleQuestion(index); } }, // 我的错题,收藏查看解析按钮 viewParsing() { if ( this.data.answerType == "collectQuestion" || this.data.answerType == "errorQuestion" ) { const item = this.data.questionDataList[this.data.currentIndex]; if (!item.isComplete) { this.handleQuestion(this.data.currentIndex); } } }, // 点击答题卡跳转题目 goQuestion(e) { const id = e.detail.id; this.data.questionDataList.forEach((item, index) => { if (item.id == id) { this.setData({ currentIndex: index, }); } }); }, // 单选 多选 触发 onChangeRadio(e) { const radioData = e.detail.value.currentTarget.dataset.value; const id = e.detail.value.currentTarget.dataset.id; const radioChecked = e.detail.value.detail.value; const questionList = this.data.questionDataList; // 我的错题和我的收藏模式下,单选题选择了直接批改 for (let index = 0; index < questionList.length; index++) { const item = questionList[index]; if (item.id == id && !item.isComplete) { item.userAnswer = radioChecked; item.isUserAnswer = this.isHaveAnswer(radioChecked); if ( (item.questionType == "singleChoice" || item.questionType == "judge") && (this.data.answerType == "collectQuestion" || this.data.answerType == "errorQuestion") && !item.isComplete ) { this.handleQuestion(this.data.currentIndex); } } } // questionList.forEach(item => { // if (item.id == id && !item.isComplete) { // item.userAnswer = radioChecked // item.isUserAnswer = this.isHaveAnswer(radioChecked) // if ((item.questionType == 'singleChoice' || item.questionType == 'judge') && (this.data.answerType == 'collectQuestion' || this.data.answerType == 'errorQuestion') && !item.isComplete) { // this.handleQuestion(this.data.currentIndex) // } // } // }) const cardListUpdata = this.data.cardList; for (let index = 0; index < cardListUpdata.length; index++) { const item = cardListUpdata[index]; for (let cindex = 0; cindex < item.infoList.length; cindex++) { const citem = item.infoList[cindex]; if (citem.id == id && !citem.isComplete) { citem.userAnswer = radioChecked; citem.isUserAnswer = this.isHaveAnswer(radioChecked); } } } // cardListUpdata.forEach(item => { // item.infoList.forEach(citem => { // if (citem.id == id && !citem.isComplete) { // citem.userAnswer = radioChecked // citem.isUserAnswer = this.isHaveAnswer(radioChecked) // } // }) // }) this.setData({ questionDataList: questionList, cardList: cardListUpdata, }); if (this.data.answerType == "mock") { this.setData({ saveTime: 20, }); } }, // 输入框触发 onChangeInput(e) { const inputData = e.detail.value.detail.value; const id = e.detail.value.currentTarget.dataset.id; const index = Number(e.detail.value.currentTarget.dataset.index); console.log(index); const questionList = this.data.questionDataList; questionList.forEach((item) => { if (item.id == id) { item.userAnswer[index] = inputData; return (item.isUserAnswer = this.isHaveAnswer(inputData)); } }); const cardListUpdata = this.data.cardList; cardListUpdata.forEach((item) => { item.infoList.forEach((citem) => { if (citem.id == id) { citem.userAnswer[index] = inputData; return (citem.isUserAnswer = this.isHaveAnswer(inputData)); } }); }); this.setData({ questionDataList: questionList, cardList: cardListUpdata, }); if (this.data.answerType == "mock") { this.setData({ saveTime: 20, }); } }, // 数组转为字符串方法 arrayToString(data) { // 检查是否为数组 if (Array.isArray(data)) { // 使用 join 方法将数组转换为字符串,默认使用逗号分隔 return data.join(",").replace(/<[^>]*>/g, ""); } else { // 如果不是数组,直接返回原始值 return data.replace(/<[^>]*>/g, ""); } }, // 判断是否有用户答案 isHaveAnswer(data) { if (typeof data == "string") { data = data .replace(/<[^>]*>/g, "") .replace(/ /g, "") .trim(); if (data.length) { return true; } else { return false; } } else if (typeof data == "object") { const answer = data.find((item) => item.length > 0); if (answer) { return true; } else { return false; } } else { return false; } }, // 提交逻辑 submitPaper() { this.setData({ submitStatus: true, loading: true, }); const child = this.selectComponent("#question-options"); debugger if ( this.data.answerType == "option" || this.data.answerType == "errorQuestion" || this.data.answerType == "mock" ) { // 先遍历所有题目,将未批改的题目批改 const qustionList = this.data.questionDataList; for (let index = 0; index < qustionList.length; index++) { const item = qustionList[index]; if (!item.isComplete) this.handleQuestion(index); } } if (this.data.answerType == "option") { this.toggleCountdown(); this.recordAnswerData(); child.openTestReportDialog(); } else if ( this.data.answerType == "collectQuestion" || this.data.answerType == "errorQuestion" ) { this.goBack(); } else if (this.data.answerType == "mock") { this.toggleCountdown(); // 设置模考状态为已完成 const mockData = this.data.mockData; mockData.datas.forEach((item) => { if (item.id == this.data.uuid) { item.state = "3"; item.report = { userScore: this.data.subjectiveNum, }; } }); this.setData({ mockData: mockData, submitStatus: true, }); this.setMockInfo(mockData.datas); // 记录用户答题数据 let saveData = []; for (let index = 0; index < this.data.questionDataList.length; index++) { const item = this.data.questionDataList[index]; saveData.push({ id: item.id, answer: item.userAnswer, isRight: item.isRight, }); } app.MG.identity.setUserKey({ setKeyRequests: [{ domain: "mockAnswerData", key: this.data.uuid, value: JSON.stringify({ time: this.data.countdownTime, answerData: saveData, }), }, ], }); child.openTestReportDialog(); } this.setData({ loading: false, }); }, // 初始化函数 async init() { this.setData({ loading: true, subjectiveTotal: 0, subjectiveNum: 0, subjectiveGrade: 0, }); if (this.data.answerType == "option") { if (this.data) // this.startCountdown() this.setData({ countdownTime: 2 * 60 * 60 * 1000, }); // 测试答题 await this.getCollectIdList(); // 获取收藏id列表 await this.getErrorList(); // 获取错题id列表 } else if (this.data.answerType == "collectQuestion") { this.setData({ submitStatus: true, }); // 我的收藏 await this.getcollectId(); // 获取收藏题目 } else if (this.data.answerType == "errorQuestion") { this.setData({ submitStatus: true, }); // 我的错题 // loadings.value = true await this.getErrorIdList(); await this.getCollectIdList(); // 获取收藏id列表 } else if (this.data.answerType == "mock") { this.getErrorList(); this.getCollectIdList(); this.getMockInfo(async (res) => { let mocklist; try { mocklist = JSON.parse(res[0].value); } catch (error) { mocklist = []; } if (mocklist.length && this.data.uuid) { const oldMockData = mocklist.find( (mockItem) => mockItem.id == this.data.uuid ); this.setData({ "mockData.state": oldMockData.state, "mockData.score": oldMockData.score, "mockData.datas": mocklist, }); if (oldMockData.state == "0") { // 组卷购买了,但未进入页面 await this.getEduQuizConfig(); } else if ( oldMockData.state == "1" || oldMockData.state == "2" || oldMockData.state == "3" ) { // 组卷后进入过页面,但是未答过题 let oldQuestionList = []; oldQuestionList = await this.getMockQuestionList(); const userAnswerList = await this.getMockAnswer(); this.setData({ "mockData.sumTime": oldMockData.time, cardList: oldQuestionList, }); await this.getMockDataList(oldQuestionList, userAnswerList); } if ( oldMockData.state == "0" || oldMockData.state == "1" || oldMockData.state == "2" ) { this.startCountdown(); } if (oldMockData.state == "3") { this.setData({ submitStatus: true, subjectiveNum: oldMockData.report.userScore, }); } } }); } }, async restart() { const countDownRef = this.selectComponent("#countDownRef"); this.setData({ loading: true, total: 0, subjectiveGrade: 0, subjectiveTotal: 0, subjectiveNum: 0, currentIndex: 0, submitStatus: false, }); if (this.data.answerType == "option") { this.setData({ countdownTime: 2 * 60 * 60 * 1000, }); this.delAnswerInfo(() => { this.getQuestionList(); this.clearTime(); }); if (!this.data.submitStatus) { this.startCountdown(); } } else if (this.data.answerType == "mock") { // 组卷模式 // 清空答题记录 await app.MG.identity.setUserKey({ setKeyRequests: [{ domain: "mockAnswerData", key: this.data.uuid, value: JSON.stringify({ time: this.data.countdownTime, answerData: [], }), }, ], }); this.init(); } else { this.init(); this.clearTime(); if (submitStatus.value) { this.startCountdown(); } } }, // 获取收藏题目列表id getCollectIdList() { app.MG.identity .getUserKey({ domain: "collectData", keys: [this.data.rootCmsItemId], }) .then((res) => { try { this.setData({ collectList: JSON.parse(res[0].value), }); } catch (error) {} if (this.data.answerType == "option") { // 先获取用户答题记录 this.getAnswerInfo(async (res) => { if (res.length) { // 有记录,不能答题,状态设为已提交 this.setData({ submitStatus: true, }); let value = JSON.parse(res[0].value); // 有答题记录,得分赋值 if (value) { this.setData({ submitStatus: true, }); value.dataList.forEach((item) => { if ( item.name == "客观题得分" && item.path == this.data.productLinkPath ) this.setData({ subjectiveNum: item.score, }); }); } this.setData({ currentIndex: value.currentIndex, }); console.log(this.data.submitStatus); // 携带答题记录 获取题目 await this.getQuestionList(value.dataList); } else { await this.getQuestionList(); // 获取题库题目 } }); } }); }, // 获取错题id列表 getErrorList() { app.MG.identity .getUserKey({ domain: "errorData", keys: [this.data.rootCmsItemId], }) .then((res) => { try { this.setData({ errorList: JSON.parse(res[0].value), }); } catch (error) {} }); }, // 获取题库题目 async getQuestionList(oldData) { // 清空正确题数记录 this.setData({ cardList: [], correctNum: 0, }); let flag = 0; for (let index = 0; index < this.data.idPathList.length; index++) { const pathitem = this.data.idPathList[index]; const pathList = this.data.cardList; pathList.push({ path: pathitem.productLinkPath, catalogName: pathitem.name, infoList: [], }); this.setData({ cardList: pathList, }); // 获取题目 let query = { path: "*", queryType: "*", productId: this.data.bookId, cmsPath: pathitem.productLinkPath, itemFields: { // SysType: 'CmsFolder', 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: [], }, pading: { start: 0, size: 999, }, }; await app.MG.store.getProductDetail(query).then((res) => { this.setData({ total: res.datas.cmsDatas[0].datas.length, }); // total.value += res.datas.cmsDatas[0].datas.length let oldList; if (oldData) { // 提交过,存在答题记录 oldList = oldData.find( (item) => item.path == pathitem.productLinkPath ).infoList; this.setData({ submitStatus: true, }); } res.datas.cmsDatas[0].datas.forEach((item, index) => { let oldObj = ""; if (oldList) oldObj = oldList.find((oldItem) => oldItem.id == item.id); let questionObj = { // num: index, // 题号 id: item.id, type: pathitem.name, stem: item.Embedded_QuestionBank_QuestionType == "completion" ? JSON.parse(item.Embedded_QuestionBank_Stem) .stemTxt.replaceAll("", ",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" ? [] : "", isUserAnswer: oldObj ? this.isHaveAnswer(oldObj.userAnswer) : false, isRight: oldObj ? oldObj.isRight : null, isComplete: oldObj ? oldObj.isComplete : false, isCollect: this.data.collectList.indexOf(item.id) > -1 ? true : false, isUnfold: "", // 控制解析的折叠面板是否展开 }; // 多选和填空答案肯为数组,要转换JSON格式 if ( questionObj.questionType == "completion" || questionObj.questionType == "multipleChoice" ) { try { questionObj.answer = JSON.parse(questionObj.answer).toString(); } 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", }; if (!oldObj) questionObj.userAnswer[index] = ""; index++; } } } // 题干富文本处理 if (questionObj.stemStyle == "RichText") { // questionObj.option.txt = '' questionObj.stem.stemTxt = questionObj.stem.stemTxt .replace( /\ { if (item.txt) item.txt = item.txt .replace(/\ { if (optionItem.img) optionItem.img = getPublicImage(optionItem.img, 150); }); } // 客观题总分赋值 if (questionObj.questionType !== "shortAnswer") { this.setData({ subjectiveGrade: this.data.subjectiveGrade + 2, }); } // if (questionObj.optionStyle == 'RichText') { // questionObj.option.forEach(optionItem => { // optionItem.txt.replace(//g, "") // }) // } // 旧数据里 题目正确 记录正确数量 if (questionObj.isRight) { this.setData({ correctNum: this.data.correctNum + 1, }); } if ( pathitem.name == "判断题" || pathitem.name == "填空题" || pathitem.name == "多选题" || pathitem.name == "单选题" || pathitem.name == "听力题" ) { if (oldObj) { this.setData({ subjectiveTotal: this.data.subjectiveTotal + 1, }); } } // cardList赋值 let cardIndex = this.data.cardList.findIndex( (item) => item.path == pathitem.productLinkPath ); let infoList = this.data.cardList[cardIndex].infoList; infoList.push(questionObj); this.setData({ [`cardList[${cardIndex}].infoList`]: infoList, }); flag++; let questionList = []; let grade = 0; const cardUpdatedList = this.data.cardList; // if (flag == this.data.idPathList.length) { cardUpdatedList.forEach((aitem) => { aitem.infoList.forEach((bitem, bindex) => { questionList.push(bitem); bitem.number = bindex + 1; bitem.score = 2; }); }); this.setData({ questionDataList: questionList, cardList: cardUpdatedList, }); }); }); } // 有题目再开始倒计时 if (this.data.questionDataList.length) this.startCountdown(); this.setData({ loading: false, }); }, // 批改题目 (练习,我的错题,我的收藏,,组卷) handleQuestion(index) { const questionList = this.data.questionDataList; if (questionList[index].isComplete) { // 题目已完成,跳过 return true; } questionList[index].isComplete = true; const item = questionList[index]; // 批改题目 if (item.questionType == "multipleChoice") { // 多选题 if (item.answer.length == item.userAnswer.length) { const sortedArr1 = item.answer.slice().sort(); const sortedArr2 = item.userAnswer.slice().sort(); questionList[index].isRight = sortedArr1.every( (value, valueIndex) => value === sortedArr2[valueIndex] ); } else { questionList[index].isRight = false; } } else if ( item.questionType == "singleChoice" || item.questionType == "judge" ) { // 单选 判断 questionList[index].isRight = item.answer == item.userAnswer; } else if (item.questionType == "shortAnswer") { // 简答 翻译 questionList[index].isRight = null; } else if (item.questionType == "completion") { if (item.answer == "string") { item.isRight = item.answer == item.userAnswer[0]; } else { if (item.answer.length != item.userAnswer.length) { item.isRight = false; } else { item.isRight = item.answer.every( (value, index) => value === item.userAnswer[index] ); } } if (typeof item.answer == "string") { questionList[index].isRight = item.answer == item.userAnswer[0]; } else { if (item.answer.length != item.userAnswer.length) { questionList[index].isRight = false; } else { questionList[index].isRight = item.answer.every( (value, valueIndex) => value === item.userAnswer[valueIndex] ); } } } if (item.questionType != "shortAnswer") { this.setData({ subjectiveTotal: this.data.subjectiveTotal + 1, }); } if (questionList[index].isRight && item.questionType != "shortAnswer") { // 客观题回答正确 this.setData({ subjectiveNum: this.data.subjectiveNum + item.score, correctNum: this.data.correctNum + 1, }); } if (!questionList[index].isRight && item.questionType != "shortAnswer") { // 客观题回答错误 记录错题 if ( this.data.errorList.findIndex((errorItem) => errorItem == item.id) == -1 ) { this.data.errorList.push(item.id); } } else { if ( this.data.answerType == "errorQuestion" || this.data.answerType == "option" ) { // 从错题集中移除 let errorIndex = this.data.errorList.findIndex( (erroritem) => erroritem == item.id ); if (errorIndex > -1) { this.data.errorList.splice(errorIndex, 1); } } } if (this.data.answerType != "collectQuestion") { // 记录错题 app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "errorData", key: this.data.rootCmsItemId, value: JSON.stringify(this.data.errorList), }, ], }) .then((res) => { console.log(res); }); } this.setData({ questionDataList: questionList, }); const cardUpdatedList = this.data.cardList; cardUpdatedList.forEach((item) => { item.infoList.forEach((citem) => { if (citem.id == questionList[index].id) { citem = questionList[index]; } }); }); this.setData({ cardList: cardUpdatedList, }); }, // 题目收藏按钮,收藏和取消同一接口,取消数组减去该项id setCollect() { const citem = this.data.questionDataList[this.data.currentIndex]; const questionList = this.data.questionDataList; for (let index = 0; index < questionList.length; index++) { const item = questionList[index]; if (item.id == citem.id) { item.isCollect = !item.isCollect; } } this.setData({ questionDataList: questionList, }); if (this.data.collectList.length == 0) { this.setData({ collectList: [citem.id], }); } else { const collectItme = this.data.collectList.filter( (item) => item == citem.id ); if (collectItme.length) { const arr = this.data.collectList.filter((item) => item != citem.id); this.setData({ collectList: arr, }); } else { const collectArr = this.data.collectList; collectArr.push(citem.id); this.setData({ collectList: collectArr, }); } } app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "collectData", key: this.data.rootCmsItemId, value: JSON.stringify(this.data.collectList), }, ], }) .then((res) => {}); }, // 处理答题数据 recordAnswerData() { this.data.cardList.push({ name: "客观题得分", score: this.data.subjectiveNum, path: this.data.productLinkPath, // infoList: [], // catalogName: '' }); let setInfoData = { currentIndex: this.data.currentIndex, dataList: JSON.parse(JSON.stringify(this.data.cardList)), }; for (let i = 0; i < setInfoData.dataList.length; i++) { const item = setInfoData.dataList[i]; if (!item.name && !item.name == "客观题得分") { for (let j = 0; j < item.infoList.length; j++) { let obj = { id: item.infoList[j].id, userAnswer: item.infoList[j].userAnswer, isComplete: item.infoList[j].isComplete, isRight: item.infoList[j].isRight, isCollect: item.infoList[j].isCollect, }; item.infoList[j] = obj; } } } this.setAnswerInfo(setInfoData); }, // 提交答题数据 setAnswerInfo(data) { app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "answerData", key: this.data.productLinkPath, value: JSON.stringify(data), }, ], }) .then((res) => {}); }, // 获取旧答题数据 getAnswerInfo(callback) { app.MG.identity .getUserKey({ domain: "answerData", keys: [this.data.productLinkPath], }) .then((res) => { if (callback) callback(res); }); }, // 删除旧答题数据 delAnswerInfo(callback) { app.MG.identity .delUserKey({ domain: "answerData", keys: [this.data.productLinkPath], }) .then((res) => { if (callback) callback(); }); }, // 我的收藏模式下获取收藏题目id async getcollectId() { app.MG.identity .getUserKey({ domain: "collectData", keys: [this.data.rootCmsItemId], }) .then(async (res) => { try { this.setData({ collectList: JSON.parse(res[0].value), }); } catch (error) {} if (this.data.collectList && this.data.collectList.length) { await this.getCollectDataList(); } else { this.setData({ loading: false, }); wx.showModal({ title: "提示", content: "收藏夹暂无数据", //editable如果为true,这就是输入框的内容 confirmColor: "#ff6c00", cancelColor: "#949494", editable: false, //是否显示输入框 showCancel: false, success: (res) => { if (res.confirm) { this.setData({ submitStatus: true, }); this.goBack(); } }, }); } }); }, // 获取收藏夹 async getCollectDataList() { let questionArr = []; this.setData({ cardList: [{ catalogName: "单选题", infoList: [], }, { catalogName: "判断题", infoList: [], }, { catalogName: "多选题", infoList: [], }, { catalogName: "填空题", infoList: [], }, { catalogName: "简答题", infoList: [], }, ], }); let singleChoiceArr = []; // 单选 let judgeArr = []; // 判断 let shortArr = []; // 简答 let multipleChoiceArr = []; // 多选 let completionArr = []; // 填空 for (let qindex = 0; qindex < this.data.collectList.length; qindex++) { const qitem = this.data.collectList[qindex]; let query = { path: "*", cmsPath: this.data.rootCmsItemId, cmsType: "*", productId: this.data.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: [], }, }; await app.MG.store.getProductDetail(query).then((res) => { let questionArr = []; res.datas.cmsDatas[0].datas.forEach((item, index) => { const questionObj = { number: index + 1, // 题号 id: item.id, stem: item.Embedded_QuestionBank_QuestionType == "completion" ? JSON.parse(item.Embedded_QuestionBank_Stem) .stemTxt.replaceAll("", ",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: item.Embedded_QuestionBank_QuestionType == "completion" || item.Embedded_QuestionBank_QuestionType == "multipleChoice" ? [] : "", isSubmit: false, // 查看解析 isRight: null, // 是否正确 isComplete: false, isCollect: true, }; // 多选和填空答案肯为数组,要转换JSON格式 if ( questionObj.questionType == "completion" || questionObj.questionType == "multipleChoice" ) { try { questionObj.answer = JSON.parse(questionObj.answer).toString(); } 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( /\ { if (item.txt) item.txt = item.txt .replace(/\ { try { this.setData({ errorList: JSON.parse(res[0].value), }); } catch (error) {} if (this.data.errorList && this.data.errorList.length) { this.getErrorDataList(); } else { this.setData({ loading: true, }); wx.showModal({ title: "提示", content: "错题集暂无数据", //editable如果为true,这就是输入框的内容 confirmColor: "#ff6c00", cancelColor: "#949494", editable: false, //是否显示输入框 showCancel: false, success: (res) => { if (res.confirm) { this.setData({ submitStatus: true, }); this.goBack(); } }, }); } }); }, // 获取错题集 async getErrorDataList() { this.setData({ cardList: [{ catalogName: "单选题", infoList: [], }, { catalogName: "判断题", infoList: [], }, { catalogName: "多选题", infoList: [], }, { catalogName: "填空题", infoList: [], }, { catalogName: "简答题", infoList: [], }, ], }); let singleChoiceArr = []; // 单选 let judgeArr = []; // 判断 let shortArr = []; // 简答 let multipleChoiceArr = []; // 多选 let completionArr = []; // 填空 let query = { path: "*", cmsPath: this.data.rootCmsItemId, cmsType: "*", productId: this.data.bookId, queryType: "*", itemIds: this.data.errorList.map((item) => item + ""), 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: [], }, }; await app.MG.store.getProductDetail(query).then((res) => { let questionArr = []; res.datas.cmsDatas[0].datas.forEach((item, index) => { const questionObj = { number: index + 1, // 题号 id: item.id, stem: item.Embedded_QuestionBank_QuestionType == "completion" ? JSON.parse(item.Embedded_QuestionBank_Stem) .stemTxt.replaceAll("", ",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: item.Embedded_QuestionBank_QuestionType == "completion" || item.Embedded_QuestionBank_QuestionType == "multipleChoice" ? [] : "", isSubmit: false, // 查看解析 isRight: null, // 是否正确 isComplete: false, isCollect: this.data.collectList.some( (collectItem) => collectItem == item.id ), }; // 多选和填空答案肯为数组,要转换JSON格式 if ( questionObj.questionType == "completion" || questionObj.questionType == "multipleChoice" ) { try { questionObj.answer = JSON.parse(questionObj.answer).toString(); } 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( /\ { if (item.txt) item.txt = item.txt .replace(/\ { // 获取 分数 时间 this.setData({ "mockData.score": configRes.score, }); try { this.setData({ "mockData.time": JSON.parse(configRes.config).time * 1000, countdownTime: JSON.parse(configRes.config).time * 1000, }); } catch (error) { this.setData({ "mockData.time": 3600 * 1000, countdownTime: 3600 * 1000, }); } this.setData({ "mockData.sumTime": this.data.mockData.time, }); let itemIds = []; let numberIndex = 1; // 获取抽题结果 const cardList = configRes.eduQuizConfigStageResultList.map((item) => { let questionIds = []; // 获取必含题目 let list = item.cmsItemList.map((idItem) => { itemIds.push(idItem.id + ""); questionIds.push({ id: idItem.id + "", score: idItem.score, }); numberIndex++; return { number: numberIndex, id: idItem.id, score: idItem.score, }; }); // 获取抽题题目 let extractList = []; for (let i = 0; i < item.generatorResultList.length; i++) { const extractItem = item.generatorResultList[i]; let extractItemList = extractItem.cmsItemList.map( (extractCmsItem) => { itemIds.push(extractCmsItem.id + ""); questionIds.push({ id: extractCmsItem.id + "", score: extractCmsItem.score, }); numberIndex++; return { number: numberIndex, id: extractCmsItem.id, score: extractCmsItem.score, }; } ); extractList = extractList.concat(extractItemList); } return { path: item.id, catalogName: item.name, infoList: list.concat(extractList), }; }); this.setData({ cardList: cardList, }); // 更新该条组卷的数据状态,包括记录改试卷总分,更改组卷状态state为1,表示已经抽题完成,并要将抽屉数据记录下来 if (this.data.mockData.datas && this.data.mockData.datas.length) { const mockData = this.data.mockData; mockData.datas.forEach((item, index) => { if (item.id == this.data.uuid) { item.state = "1"; item.time = mockData.time; item.score = configRes.score; mockData.state = "1"; } }); this.setData({ mockData: mockData, }); } this.setMockInfo(this.data.mockData.datas); // 记录试卷总分 时长,修改state为 1 // 更新本地mockData this.saveMockQuestionList(this.data.cardList); // 记录题目列表 await this.getMockDataList(this.data.cardList); // 请求题目数据 }); }, // 获取组卷题目列表 async getMockDataList(questionList, oldList) { const questionDataList = this.data.questionDataList; for (let pathindex = 0; pathindex < questionList.length; pathindex++) { const pathitem = questionList[pathindex]; let itemIds = []; pathitem.infoList.forEach((item) => { itemIds.push(item.id + ""); }); let query = { path: "*", cmsPath: this.data.rootCmsItemId, cmsType: "*", productId: this.data.bookId, queryType: "*", itemIds, 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: [], }, }; await app.MG.store.getProductDetail(query).then((res) => { res.datas.cmsDatas[0].datas.forEach((item, index) => { // 循环questionList,给每题赋值分数 let oldObj; if (oldList) oldObj = oldList.find((oldItem) => oldItem.id == item.id); let questionObj = { // number: pathitem.infoList.find(infoItem => infoItem.itemId == item.id).number, id: item.id, score: pathitem.infoList.find((infoItem) => infoItem.id == item.id) .score, stem: item.Embedded_QuestionBank_QuestionType == "completion" ? JSON.parse(item.Embedded_QuestionBank_Stem) .stemTxt.replaceAll("", ",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.answer : item.Embedded_QuestionBank_QuestionType == "completion" || item.Embedded_QuestionBank_QuestionType == "multipleChoice" ? [] : "", isUserAnswer: oldObj ? this.isHaveAnswer(oldObj.answer) : false, isRight: oldObj ? oldObj.isRight : null, // isComplete: oldObj ? oldObj.isComplete : false, isComplete: this.data.mockData.state == "3" ? true : false, isCollect: this.data.collectList.indexOf(item.id) > -1 ? true : false, isUnfold: "", // 控制解析的折叠面板是否展开 }; // 多选和填空答案肯为数组,要转换JSON格式 if ( questionObj.questionType == "completion" || questionObj.questionType == "multipleChoice" ) { try { questionObj.answer = JSON.parse(questionObj.answer).toString(); } 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", }; if (!oldObj) questionObj.userAnswer[index] = ""; index++; } } } // 题干富文本处理 if (questionObj.stemStyle == "RichText") { // questionObj.option.txt = '' questionObj.stem.stemTxt = questionObj.stem.stemTxt.replace( /\ { if (item.txt) item.txt = item.txt.replace( /\ { if (optionItem.img) optionItem.img = getPublicImage(optionItem.img, 150); }); } // 客观题总分赋值 if (questionObj.questionType !== "shortAnswer") { this.setData({ subjectiveGrade: this.data.subjectiveGrade + questionObj.score, }); } if (oldObj && oldObj.isRight) { this.setData({ correctNum: this.data.correctNum + 1, }); } if (item.questionType != "shortAnswer" && item.isComplete) { this.setData({ subjectiveTotal: this.data.subjectiveTotal + 1, }); } // if (oldObj && isHaveAnswer(oldObj.answer)) countDownRef.value.changeAlready() // cardList赋值 let cardIndex = this.data.cardList.findIndex( (item) => item.catalogName == pathitem.catalogName ); let infoIndex = this.data.cardList[cardIndex].infoList.findIndex( (infoItem) => infoItem.id == item.id ); this.setData({ [`cardList[${cardIndex}].infoList[${infoIndex}]`]: questionObj, }); let questionList = []; const cardUpdatedList = this.data.cardList; cardUpdatedList.forEach((aitem) => { aitem.infoList.forEach((bitem, bindex) => { questionList.push(bitem); bitem.number = bindex + 1; }); }); this.setData({ questionDataList: questionList, cardList: cardUpdatedList, }); }); }); } this.setData({ loading: false, }); }, // 获取组卷数据 getMockInfo(callback) { app.MG.identity .getUserKey({ domain: "mockData", keys: [this.data.bookId], }) .then((res) => { if (callback) callback(res); }); }, // 记录购买组卷时间 setMockInfo(data) { app.MG.identity.setUserKey({ setKeyRequests: [{ domain: "mockData", key: this.data.bookId + "", value: JSON.stringify(data), }, ], }); }, // 记录组卷题目列表 saveMockQuestionList(data) { app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "mockQuestionData", key: this.data.uuid, value: JSON.stringify(data), }, ], }) .then((res) => { console.log("模考题目列表已记录"); }); }, // 获取记录组卷题目列表 async getMockQuestionList() { let list = []; await app.MG.identity .getUserKey({ domain: "mockQuestionData", keys: [this.data.uuid], }) .then((res) => { list = JSON.parse(res[0].value); }); return list; }, // 获取模考用户答题数据 async getMockAnswer() { let data; await app.MG.identity .getUserKey({ domain: "mockAnswerData", keys: [this.data.uuid], }) .then((res) => { if (res[0]) { this.setData({ currentIndex: JSON.parse(res[0].value).currentIndex, "mockData.time": JSON.parse(res[0].value).time, countdownTime: JSON.parse(res[0].value).time ? JSON.parse(res[0].value).time : this.data.mockData.sumTime, "mockData.answer": JSON.parse(res[0].value).answerData, }); console.log(JSON.parse(res[0].value)); data = JSON.parse(res[0].value).answerData; } }); return data; }, // 记录模考答题数据 saveMockData() { const data = this.data.mockData.datas.find( (itme) => itme.id == this.data.uuid ); if (data.state == "3") return true; const saveData = this.data.mockData.answer; for (let i = 0; i < this.data.questionDataList.length; i++) { const item = this.data.questionDataList[i]; if (this.isHaveAnswer(item.userAnswer)) { const index = saveData.findIndex((saveitem) => saveitem.id == item.id); if (index + "" != "-1") { saveData[index].answer = item.userAnswer; } else { saveData.push({ id: item.id, answer: item.userAnswer, }); } } } // if (this.data.mockData.state == "1") { const datas = this.data.mockData.datas; datas.forEach((item) => { if (item.id == this.data.uuid) { item.state = "2"; } }); this.setMockInfo(datas); } // app.MG.identity .setUserKey({ setKeyRequests: [{ domain: "mockAnswerData", key: this.data.uuid, value: JSON.stringify({ currentIndex: this.data.currentIndex, time: this.data.countdownTime, answerData: saveData, }), }, ], }) .then((res) => { this.setData({ saveTime: 20, }); console.log("模考答题数据已记录"); }); }, // 富文本处理 formatRichText(html) { console.log(html); let newContent = html.replace(/]*>/gi, function (match, capture) { match = match .replace(/style="[^"]+"/gi, "") .replace(/style='[^']+'/gi, ""); match = match .replace(/width="[^"]+"/gi, "") .replace(/width='[^']+'/gi, ""); match = match .replace(/height="[^"]+"/gi, "") .replace(/height='[^']+'/gi, ""); return match; }); newContent = newContent.replace( /style="[^"]+"/gi, function (match, capture) { match = match .replace(/

/gi, '

') .replace(/width:[^;]+;/gi, "max-width:100%;") .replace(/width:[^;]+;/gi, "max-width:100%;"); return match; } ); newContent = newContent.replace(/]*\/>/gi, ""); newContent = newContent.replace(//gi, '/gi, '