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

  /**
   * 页面的初始数据
   */
  data: {

    questionlist:[],
    idx:'',
    parid:'',
    arr:[]
  },
  /**
   * 生命周期函数--监听页面加载
   * 
   */
  onLoad: function (options) {

    this.getquestion()

  },

  getquestion(){
    let that = this;
    var url = '/home/Questionnaire/index';
    var params = {
      
    }
    var header={
      'XX-Token': wx.getStorageSync('token'),
      'XX-Device-Type':'wxapp'
    }
    app.post(url, params,header).then((res) => {
      console.log(res);

      for(var obj of res){
        obj.choice=[]
      }
      
      this.setData({
        examList: res
      })
      for(var obj of res){
        let arr=[]
        for(var i=0;i<obj.option.length;i++){
          
          arr[i] = {
            name: obj.option[i],
            choose: false
          }
        }

        obj.option = arr
       }
      this.setData({
        questionlist:res,
        
      })
      console.log(that.data.questionlist)
    }).catch((err) => {

    })
  },

  // selectoption(e){
  //     console.log(e)
  //     let parid = e.currentTarget.dataset.paridx;
  //     this.setData({
  //       parid: parid
  //     })
  //     // console.log(this.data.parid)
     
  // },

  //选择选项
  select(e){
    let that=this;
    let newarr =that.data.arr;
    let idx = e.currentTarget.dataset.idx;
    let paridx = e.currentTarget.dataset.paridx;
    let newquestionlist = that.data.questionlist;

    newquestionlist[paridx].option[idx].choose = !newquestionlist[paridx].option[idx].choose
    if(newquestionlist[paridx].option[idx].choose==true){
      newarr.push(newquestionlist[paridx].option[idx].name);
     
      that.setData({
        arr:newarr
      })
    } else if (newquestionlist[paridx].option[idx].choose==false){
     
      for(var i=0;i<newarr.length;i++){
        if (newquestionlist[paridx].option[idx].name==newarr[i]){
            newarr.splice(i,1);
          }
      }
    }

    that.setData({
      arr:newarr
    })
    
    console.log(newarr);
    let newexamList = that.data.examList;
    for (var obj of newexamList) {
        obj.choice=[]
      
      }

      that.setData({
        examList: newexamList
      })

     
    

   
    for (var obj of newexamList){
        for(var i=0;i<obj.option.length;i++){
            for(var j=0;j<newarr.length;j++){
              if (obj.option[i]==newarr[j]){
                obj.choice.push(newarr[j])

                var temp = []; //一个新的临时数组
                for (var k = 0; k < obj.choice.length; k++) {
                  if (temp.indexOf(obj.choice[k]) ==-1) {
                    temp.push(obj.choice[k]);
                  }
                }

                obj.choice=temp;
              }
            }
        }

    }
    console.log(newexamList)
    that.setData({
      questionlist: newquestionlist,
      examList: newexamList
    })

    console.log(that.data.examList)

  },

  //提交问卷
  subquestion(){
    let that = this;

    let newarr = that.data.arr.join(",");
    if (that.data.arr.length==0){
        wx.showToast({
          title: '请填写问卷',
          icon:'none'
        })
       
    }else{
      console.log(that.data.examList)
      var url = '/home/Questionnaire/submit';
      var params = {
        tag_name: that.data.arr.join(","),
        data: JSON.stringify(that.data.examList)
      }
      var header = {
        'XX-Token': wx.getStorageSync('token'),
        'XX-Device-Type': 'wxapp'
      }
      app.post(url, params, header).then((res) => {
        console.log(res);
        if(res==""){
          wx.showToast({
            title: '提交成功',
            icon:'none'
          })

          setTimeout(function(){
            wx.switchTab({
              url: '../pocketlesson/pocketlesson',
            })
          },1500)
        }
      }).catch((err) => {

      })
    }
   

  },
  
  //跳过
  jump(){
    console.log(666)
    wx.switchTab({
      url: '../pocketlesson/pocketlesson',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

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

  },

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

  }
})