import commonConfig from "../../common/common" let app = getApp() Page({ data: { data: [],//all题列表 record: [],//提交给后台数据组 id: 0,//当前swiper-item的索引值 result_id: '',//考试结果id duration: ''//倒计时 }, onLoad(options) { let that = this if (options.result_status == 1) { dd.getStorage({ key: 'record', success: function (res) { let record = res.data if (record && record.length != 0) { dd.getStorage({ key: 'data', success: function (r) { console.log(r.data) if (r.data) { that.setData({ data: r.data }) that.setData({ record:record }) } }, fail: function (res) { dd.alert({ content: res.errorMessage }); } }) that.setData({ id: record.indexOf(null) }) } else { that.getExamQuestions(options.exam_id) that.setData({ id: 0 }) } }, fail: function (res) { dd.alert({ content: res.errorMessage }); } }); } else { that.getExamQuestions(options.exam_id) } dd.setStorage({ key: 'answerid', data: { exam_id: options.exam_id, result_id: options.result_id } }); that.setData({ result_id: options.result_id }) that.getdjs() }, onUnload() { // 页面被关闭 let record = this.data.record console.log('页面被关闭') dd.setStorage({ key: 'record', data: record }); // dd.showToast({ // type: 'none', // content: '暂时离开', // duration: 3000 // }); }, getdjs() { let params = { result_id: this.data.result_id } app.https('POST', app.globaldata.apiUrl.getRemainingTime, JSON.stringify(params), { type: false }).then(res => { this.setData({ djs: res.data.data.time }) let examSurplusTime = res.data.data.time;//获取考试剩余时间 // let examSurplusTime = 60; this.examDjsFn(examSurplusTime) }) }, examDjsFn(examSurplusTime) { let timer = null; let that = this let time = examSurplusTime * 1000 timer = setInterval(() => { let h, m, s if (time >= 0) { m = Math.floor(time / 1000 / 60 % 60); s = Math.floor(time / 1000 % 60); m = m < 10 ? ('0' + m) : m; s = s < 10 ? ('0' + s) : s; let duration = m + ':' + s; that.setData({ duration: duration }) time = time - 1000 } else { clearInterval(timer); let duration = '00:00' that.setData({ duration: duration }) dd.showToast({ type: 'fail', content: '答题超时,将为你自动提交!', duration: 3000, success: () => { that.formSubmit() }, }); } }, 1000); }, onSubmit(e) {}, radioChange(e) { console.log(e) let record = this.data.record let id = this.data.id; record[id] = { question_id: e.currentTarget.dataset.id, answer: [{ key: e.detail.value, checked: 1 }] }; this.setData({ record: record, }) let data = commonConfig.filterData(this.data.record, this.data.data) dd.setStorage({ key: 'data', data: data }) console.log(this.data.record) }, checkedChange(e) { let record = this.data.record let value = e.detail.value let id = this.data.id; let answers = [] answers = value.map((item) => { return { key: item, checked: 1 } }) record[id] = { question_id: e.currentTarget.dataset.id, answer: answers }; console.log(record) this.setData({ record: record, }) let data = commonConfig.filterData(this.data.record, this.data.data) dd.setStorage({ key: 'data', data: data }) console.log(this.data.record) }, swiperChange(e) { this.setData({ id: e.detail.current }) }, getExamQuestions(exam_id) { let that = this; let params = { exam_id: exam_id } app.https('POST', app.globaldata.apiUrl.getExamQuestions, JSON.stringify(params), { type: false }).then(res => { that.setData({ record: new Array(res.data.data.questions.length), data: res.data.data.questions }) dd.setStorage({ key: 'data', data: res.data.data.questions }) }) }, lastq() { if (this.data.id != 0) { this.setData({ id: this.data.id - 1, }) } }, nextq() { if (this.data.id < this.data.data.length) { this.setData({ id: this.data.id + 1, }) } }, formSubmit() {//提交 let params = { result_id: this.data.result_id, record: this.data.record } app.https('POST', app.globaldata.apiUrl.subExam, JSON.stringify(params), { type: false }).then(res => { console.log(res) dd.reLaunch({ url: "../result/result?scores=" + res.data.data.scores + "&qualified=" + res.data.data.qualified }) }) } });