question.js 3.47 KB
import {
  getBindtapData
} from '../../utils/util';
import Date from '../../utils/date';
let app = getApp();
Page({
  data: {
    questionList: [], // 题目列表
    // questionResult: {
    //   "status": 1,
    //   "elasticValue": 50,
    //   "coupons": [{
    //     "couponId": "20839",
    //     "couponCode": "RLLL1IHG",
    //     "rule": "RLLL1IHG",
    //     "startDate": 1564479217000,
    //     "endDate": 1567094399000,
    //     "type": 4,
    //     "name": "眼部护理体验券-7月24日",
    //     "state": 0
    //   }]
    // },
    questionResult:{},
    // 答题结果
    questionNumList: ["A", "B", "C", "D"],
    myAnswer: [], //我的答案
  },
  onShareAppMessage() {},
  onLoad(options) {
    this.initData();
  },
  initData() {
    this.queryQuestionList();
  },
  /**
   * 跳转至创建心愿单
   */
  onCreateWishHandler() {
    app.router.push({
      openType: "redirectTo",
      path: "createWish"
    })
  },
  /**
   * 点击答案
   * 设置选择的按钮
   */
  onSelectAnswerHandler(evt) {
    let curIndex = getBindtapData(evt, "index");
    let curPIndex = getBindtapData(evt, "pindex");
    let questionList = this.data.questionList;
    // 重置本题的选中状态
    questionList[curPIndex].answers.forEach(element => {
      element.selected = false;
    });
    questionList[curPIndex].answers[curIndex].selected = true;
    questionList[curPIndex].selected = true; // 题目已被选择
    let myAnswer = this.data.myAnswer;
    let curAnswer = {
      questionCode: questionList[curPIndex].questionCode,
      answerNo: questionList[curPIndex].answers[curIndex].answerNo
    }
    myAnswer[curPIndex] = curAnswer;
    this.setData({
      questionList,
      myAnswer
    })
  },
  /**
   * 提交答案
   */
  onSubmitQuestionResultHandler() {
    let _this = this;
    // 检查是否有提没提交 完整判断应该判断题目数要大于0,这里就算了
    let questionList = this.data.questionList;
    let isComplete = questionList.every((item) => {
      return item.selected == true;
    })
    let myAnswer = this.data.myAnswer;

    if (isComplete) {
      wx.showModal({
        content: "确认提交?",
        confirmText: "确认",
        success(res) {
          app.post({
            url: app.api.answerSubmit,
            data: myAnswer
          }).then((result) => {
            let questionResult = result;
            questionResult.coupons.forEach(element => {
              element.endDateStr = new Date(element.endDate).toString("yy-MM-dd");
            });
            _this.setData({
              questionResult
            })
            console.log("answerSubmit:", _this.data.questionResult);
          })
        }
      })
    } else {
      wx.showModal({
        content: "答题未完成",
        confirmText: "继续答题",
        showCancel: false,
        success(res) {

        }
      })
    }
  },
  /**
   * 请求问题列表
   */
  queryQuestionList() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.questionList,
        data: {}
      }).then((result) => {
        let {
          questionList
        } = result;
        console.log("questionList", questionList);
        this.setData({
          questionList: questionList
        })
        resolve();
      })
    });
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      case "_evt_hide":
        break;

      default:
        break;
    }
  },
})