123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- // miniprogram/pages/answer/index.js
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- interval: undefined,
- actTime: undefined,
- answer: [],
- data: {
- countDown: "",
- list: [],
- height: [],
- data: {
- current: 0,
- total: 0
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.setNavigationBarTitle({
- title: app.globalData.selectQuestion.name
- })
- let height = wx.getSystemInfoSync().windowHeight;
- let li = app.globalData.selectQuestion.list || {};
- 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,
- 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) 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)
- },
- 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() {
- this.answer;
- let p = {
- queId: app.globalData.selectQuestion._id,
- userId: app.globalData.userInfo._id,
- startTime: this.actTime,
- endTime: new Date(),
- answer: this.answer
- };
- 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)
- })
- console.log(isAdd)
- if (v.answer.length === this.answer[i].length && isAdd) score++;
- }
- p.score = score;
- let recordList = app.globalData.userInfo.recordList || [];
- recordList ? recordList.push(p) : recordList = [p];
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- if (this.interval) clearInterval(this.interval);
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|