123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- // miniprogram/pages/answer/index.js
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- interval: undefined,
- actTime: undefined,
- answer: [],
- data: {
- imgLi:[],
- countDown: "",
- list: [],
- height: [],
- data: {
- current: 0,
- total: 0
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- let reset = options.reset
- wx.setNavigationBarTitle({
- title: app.globalData.selectQuestion.name
- })
- let height = wx.getSystemInfoSync().windowHeight;
- let li = app.globalData.selectQuestion.list || {};
- if(reset != 1){
- for (let i = 0; i < li.length; i++) {
- const item = li[i];
- let l = []
- for (let key in item.options) {
- l.push({
- name: key,
- option: item.options[key],
- checked: false
- })
- }
- li[i].options = l;
- }
- }
- this.answer = new Array(li.length).fill([]);
- this.setData({
- list: li,
- imgLi: new Array(li.length).fill({}),
- height: [
- height * 0.07,
- height * 0.86,
- ],
- data: {
- current: 1,
- total: app.globalData.selectQuestion.list.length
- }
- })
- this.actTime = new Date();
- this.interval = setInterval(() => {
- let d = new Date();
- let c = d - this.actTime;
- let Q = app.globalData.selectQuestion;
- if (c >= Q.duration) {
- wx.showModal({
- title: "提示",
- content: '时间到请点击确认后交卷',
- showCancel: false,
- success: this.isSubmit
- })
- return clearInterval(this.interval);
- }
- let longTime = Q.duration - c;
- let h = Math.floor(longTime / 3600000);
- let m = Math.floor(longTime % 3600000 / 60000);
- let s = Math.floor(longTime % 3600000 % 60000 / 1000);
- let time = "";
- if (h) {
- time += h > 9 ? h : '0' + h;
- time += ":"
- }
- time += m > 9 ? m : '0' + m;
- time += ":"
- time += s > 9 ? s : '0' + s;
- this.setData({
- countDown: time
- })
- }, 500)
- },
- loadimage(e){
- const li = this.data.imgLi || [];
- const sys = wx.getSystemInfoSync();
- li[e.currentTarget.dataset.index] = {
- width: sys.windowWidth,
- height: e.detail.height/e.detail.width * sys.windowWidth
- }
- this.setData({
- imgLi: li
- })
- },
- prov() {
- let data = this.data.data;
- data.current--;
- this.setData({
- data: data
- })
- },
- next() {
- let data = this.data.data;
- data.current++;
- this.setData({
- data: data
- })
- },
- changeSwiper(r) {
- let data = this.data.data;
- data.current = r.detail.current + 1;
- this.setData({
- data: data
- })
- },
- changeGroup(e) {
- let answer = this.answer;
- if (typeof e.detail.value === 'string') {
- // 单选
- answer[this.data.data.current - 1] = [e.detail.value]
- } else {
- // 复选
- answer[this.data.data.current - 1] = e.detail.value
- }
- this.answer = answer
- },
- submit() {
- console.log(this.answer);
- let queNum = [];
- for (let i = 0; i < this.answer.length; i++) {
- const v = this.answer[i];
- if (!v.length) queNum.push(i + 1)
- }
- const modal = {
- title: "提示",
- content: '确认提交',
- success: this.isSubmit
- }
- if (queNum.length) modal.content = '第' + queNum.join(",") + '题未作答,是否确认提交'
- wx.showModal(modal)
- },
- isSubmit() {
- let p = {
- queId: app.globalData.selectQuestion._id,
- userId: app.globalData.userInfo._openid,
- startTime: this.actTime,
- istrue: [],
- endTime: new Date(),
- answer: this.answer
- };
- p.duration = p.endTime - p.startTime;
- let score = 0;
- for (let i = 0; i < app.globalData.selectQuestion.list.length; i++) {
- let an = this.answer[i].join(",")
- const v = app.globalData.selectQuestion.list[i];
- let isAdd = true;
- v.answer.map(r => {
- isAdd = (new RegExp(r.select)).test(an)
- })
- p.istrue.push(isAdd);
- if (v.answer.length === this.answer[i].length && isAdd) score++;
- }
- p.score = score;
- wx.showLoading();
- wx.cloud.callFunction({
- name: "quickstartFunctions",
- data: {
- type: "createQuestionRecord",
- data: p
- }
- }).then(res => {
- wx.hideLoading()
- if(res.result.code !== 0) return wx.showToast({
- title: '网络繁忙'
- })
- wx.reLaunch({
- url: '/pages/result/index?data=' + JSON.stringify(res.result.data || {}),
- })
- }).catch(err => {
- console.log(err)
- wx.hideLoading()
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- if (this.interval) clearInterval(this.interval);
- console.log()
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|