answer.js 4.6 KB
const app = getApp();
Page({


    data: {
        id: '',
        list: '',
        answers: [],
        finaAnswer: [],
        showAnswer: false
    },

    onLoad: function (options) {
        console.log(options)
        this.setData({
            id: options.id
        })
    },
    onReady: function () {

    },
    onShow: function () {
        let that = this;
        that.getlist();

    },
    //问题列表
    getlist() {

        let that = this;
        let url = '/index/second/getClassQuestion';
        let head = {
            'XX-Token': wx.getStorageSync('token')
        }
        let params = {
            class_id: that.data.id
        }
        app.post(url, params, head).then((res) => {
            console.log(res)
            that.setData({
                list: res,
                tempList: JSON.parse(JSON.stringify(res))
            })
        }).catch((err) => {

        })

    },
    radioChange(e) {
        let tempAnswer = e.detail.value.split(',');
        console.log(tempAnswer)
        this.data.answers[tempAnswer[1]] = tempAnswer[0];
        this.data.finaAnswer[tempAnswer[1]] = {
            key: tempAnswer[0],
            answer: tempAnswer[1]
        }
        this.data.list[tempAnswer[1]].choose = tempAnswer[2]
        this.setData({
            answers: this.data.answers,
            finaAnswer: this.data.finaAnswer,
            list: this.data.list
        })


        console.log(this.data.list)



    },

    // 确认
    getcomfirm() {

        let that = this;
        let url = '/index/second/commitAnswer';
        let head = {
            'XX-Token': wx.getStorageSync('token')
        }

     
        var num=that.data.list.length;
        var answer=new Object;
        for(var i=0;i<num;i++){
            answer[that.data.list[i].id]=that.data.list[i].choose;
        }
        var answer_str=JSON.stringify(answer);
        console.log(answer);
        console.log(answer_str);
        console.log(params)

        let params = {
            class_id: that.data.id,
            answer_info:answer_str
        }
        app.post(url, params, head).then((res) => {

            if (that.data.answers == '') {
                wx.showModal({
                    title: '提示',
                    content: '请完成答题后再提交',
                    showCancel: false,
                })

            } else {

                if (res.code == '40000') {
                    let answer = res
                    wx.showModal({
                        title: '提示',
                        content: '很遗憾你没有获得学时',
                        confirmText: '重新答题',
                        cancelText: '查看答案',
                        success(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '/pages/getStudycontent/answer/answer?id=' + that.data.id,
                                })
                                // let tempList = JSON.stringify(that.data.tempList)
                                // that.setData({
                                //     showAnswer: false,
                                //     // list: JSON.parse(tempList)
                                // })
                                console.log('用户点击确定')
                            } else if (res.cancel) {
                                that.setData({
                                    showAnswer: true
                                })

                                console.log('用户点击取消')
                            }
                        }
                    })


                } else {
                    wx.showModal({
                        title: '提示',
                        content: '恭喜您全部答对了',
                        showCancel: false,
                        success: function (res) {

                        }
                    })
                    setTimeout(function () {
                        wx.navigateTo({
                            url: '/pages/Online_regulations/Online_regulations',
                          })
                      }, 1000)
                   


                }
            }

        }).catch((err) => {
            console.log(err)
        })


    },


    onHide: function () {

    },


    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})