question.js 3.53 KB
import {
  getBindtapData
} from '../../utils/util';
import Date from '../../utils/date';
let app = getApp();
Page({
  data: {
    indexInfo: {},
    hadAnswer: false,
    questionList: [], // 题目列表
    questionResult: {},
    // 答题结果
    questionNumList: ["A", "B", "C", "D"],
    myAnswer: [], //我的答案
  },
  onShareAppMessage() {},
  onShow() {},
  onLoad(options) {
    this.initData();
  },
  initData() {
    app.queryIndex({
      auth: false
    }).then((result) => {
      this.setData({
        indexInfo: app.globalData.indexInfo
      })
      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) {
      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({
          hadAnswer: true,
          questionResult: questionResult
        })
        console.log("answerSubmit:", _this.data.questionResult);
      })
    } else {
      // wx.showModal({
      //   content: "答题未完成",
      //   confirmText: "继续答题",
      //   showCancel: false,
      //   success(res) {}
      // })
      wx.showToast({
        title: "答题未完成",
        icon: "none"
      })
    }
  },
  /**
   * 请求问题列表
   */
  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();
      })
    });
  },
  /**
   * 复制公众号到粘贴板
   */
  onGzhClipboardHandler() {
    wx.setClipboardData({
      data: 'MARUBI_WX',
      success(res) {
        // wx.getClipboardData({
        //   success(res) {
        //     console.log(res.data) // data
        //   }
        // })
      }
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      case "_evt_hide":
        break;

      default:
        break;
    }
  },
})