123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- 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
- })
- })
- }
- });
|