const app = require('../../utils/util.js'); const tools = require('../../utils/tools.js'); Page({ data: { index: 0, // 题目序列 chooseValue: [], // 选择的答案序列 questionList: [], totalScore: 0, id: '', //考试主键id(可取list接口中id) paperId: '', //试卷id(可取list接口中paperId) answersId: '', // 提交答案 sessionKey: '', }, /* * 单选事件 */ radioChange: function (e) { let json = {}; json.currentAnswer = e.detail.value; json.id = e.currentTarget.dataset.idx; json.questType = e.currentTarget.dataset.questtypes; this.data.chooseValue[this.data.index] = json; const items = this.data.questionList[this.data.index].QuestChoice for (let i = 0, len = items.length; i < len; ++i) { items[i].checked = items[i].value === e.detail.value } this.setData({ questionList: this.data.questionList }) wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList); wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index); wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue); }, /* * 多选事件 */ checkboxChange: function (e) { let json = {}; let aa = e.detail.value.join("||") json.currentAnswer = aa; json.questType = e.currentTarget.dataset.questtypes; json.id = e.currentTarget.dataset.idx; this.data.chooseValue[this.data.index] = json; const items = this.data.questionList[this.data.index].QuestChoice const values = e.detail.value for (let i = 0, lenI = items.length; i < lenI; ++i) { items[i].checked = false for (let j = 0, lenJ = values.length; j < lenJ; ++j) { if (items[i].value === values[j]) { items[i].checked = true break } } } this.setData({ questionList: this.data.questionList }) wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList); wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index); wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue); }, /* * 解答事件 */ bindTextAreaBlur(e) { let json = {}; json.currentAnswer = e.detail.value; json.id = e.currentTarget.dataset.idx; json.questType = e.currentTarget.dataset.questtypes; this.data.chooseValue[this.data.index] = json; this.data.questionList[this.data.index].currentAnswer = e.detail.value; this.setData({ questionList: this.data.questionList }) wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList); wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index); wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue); }, /* * 下一题/提交 按钮 */ nextSubmit: function () { // 判断是不是最后一题 if (this.data.index < this.data.questionList.length - 1) { // 渲染下一题 this.setData({ index: this.data.index + 1 }) wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index); } else { if (wx.getStorageSync('chooseValue' + this.data.answersId)) { this.setData({ chooseValue: wx.getStorageSync('chooseValue' + this.data.answersId) }) } console.log(this.data.chooseValue, "000000") this.submitAll(this.data.chooseValue); } }, upSubmit() { this.setData({ index: this.data.index - 1 }) wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index); }, async onLoad(options) { wx.showLoading({ title: '加载中', task: true }) const sessionKey = await tools.checkSessionAndLogin(); wx.setStorageSync('answersId', options.answersId); this.setData({ sessionKey: sessionKey, paperId: options.paperId, answersId: options.answersId, id: options.id, }) if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) { const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId')); const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId')); const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId')); console.log(wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) wx.hideLoading(); this.setData({ questionList: questionList, answersId: wx.getStorageSync('answersId'), chooseValue: chooseValue, index: titleINDEX }) } else { this.allShi(options.id, options.paperId, sessionKey); } }, allShi(id, paperId, sessionKey) { wx.request({ url: app.globalData.publicUrl + '/wx/paper/detail', method: "post", data: { sessionKey: sessionKey, id: id, paperId: paperId }, success: (res) => { console.log(res) let jsonArr = JSON.parse(res.data.paperContent).map(item => { let json = {}; json.QuestType = item.QuestType; json.id = item.Id; json.QuestAnswer = item.QuestAnswer; json.QuestStem = item.QuestStem; if (item.QuestChoice) { let arr = item.QuestChoice.split('||'); json.QuestChoice = arr; } if (item.QuestType == '1') { let arr = item.QuestChoice.split('||'); let arrJson = arr.map(item => { let json1 = {}; json1.name = item; json1.value = item; json1.checked = false; return json1 }); json.QuestChoice = arrJson; } if (item.QuestType == '0') { let arr = item.QuestChoice.split('||'); let arrJson = arr.map(item => { let json1 = {}; json1.name = item; json1.value = item; json1.checked = false; return json1 }); json.QuestChoice = arrJson; } if (item.QuestType == '2') { let arr = item.QuestChoice.split('||'); let arrJson = arr.map(item => { let json1 = {}; json1.name = item; json1.value = item; json1.checked = false; return json1 }); json.QuestChoice = arrJson; } return json; }) wx.hideLoading(); this.setData({ questionList: jsonArr }) console.log(this.data.questionList) wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0); wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []), wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList); }, fail: () => { wx.hideLoading() } }) }, submitAll(currentAnswerTemp) { // if(currentAnswerTemp.length==0){ // currentAnswerTemp // } wx.showLoading({ mask: true, title: '加载中', }) if (currentAnswerTemp.length == 0) { let arr = this.data.questionList.map(item => { let json = {}; json.currentAnswer = ''; json.questType = item.QuestType; json.id = item.id; return json }); wx.request({ url: app.globalData.publicUrl + '/wx/paper/commit', method: "post", data: { sessionKey: this.data.sessionKey, currentAnswerTemp: arr, answersId: wx.getStorageSync('answersId') }, success: (res) => { wx.hideLoading(); if (res.data.code == 0) { wx.showModal({ showCancel: false, content: '恭喜您,提交答案成功,请耐心等待老师判卷', success() { wx.navigateBack({ delta: 1 }) } }) } else { wx.showModal({ showCancel: false, content: res.data.msg, success() { wx.navigateBack({ delta: 1 }) } }) } }, fail: () => { wx.hideLoading() } }) } else { wx.request({ url: app.globalData.publicUrl + '/wx/paper/commit', method: "post", data: { sessionKey: this.data.sessionKey, currentAnswerTemp: currentAnswerTemp, answersId: wx.getStorageSync('answersId') }, success: (res) => { wx.hideLoading(); if (res.data.code == 0) { wx.showModal({ showCancel: false, content: '恭喜您,提交答案成功,请耐心等待老师判卷', success() { wx.navigateBack({ delta: 1 }) } }) } else { wx.showModal({ showCancel: false, content: res.data.msg, success() { wx.navigateBack({ delta: 1 }) } }) } }, fail: () => { wx.hideLoading() } }) } } })