wordsPlan.js 6.4 KB
// pages/MemorizingWords/wordsPlan/wordsPlan.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    f:false,
    ifedit:false,//是否是修改计划
    dayNum: [
     {
      show:'15天',
      num:15,
      state:false
     },
     {
      show: '30天',
      num: 30,
      state: false
     },
     {
      show: '45天',
      num: 45,
      state: false
     },
     {
      show: '60天',
      num: 60,
      state: false
     }
    ],//天数选择数组
    days: '',
    wordsNums:[],//单词数选择数组
    wordsNum:'',//每天背的单词数
    wordspackageid:'',//词包ID
    packageDetail:[],//词包详情
    toView: '',//天数to
    toView1:'',//单词数to
    ifeidtwordpackage:false//是否是编辑词包
  },
  setdayNum(e){
    let that = this;
    let days = e.currentTarget.dataset.num;
    let index = parseInt(e.currentTarget.dataset.index);
    let dayNum = that.data.dayNum;
    let url = 'Words/handle';
    let params = {
      days: days,
      package_id: that.data.wordspackageid
    }
    app.post(url, params).then((res) => {
      if (res.data.code == 1) {
        for (let i in dayNum){
          dayNum[i].state = false
          if(i == index){
          dayNum[i].state = true
          }
        }
        that.setData({
          days: days,
          wordsNum: res.data.data.everyday_words,
          dayNum: dayNum,      
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg)
    })
  },
  setwordsNum(e){
    let that=this;
    let url ='Words/handle2';
    let index = parseInt(e.currentTarget.dataset.index);
    let wordsNum=e.currentTarget.dataset.num;
    let dayNum = that.data.dayNum;
    let wordsNums = that.data.wordsNums; 
    let params = {
      words_num: e.currentTarget.dataset.num,
      package_id: that.data.wordspackageid
    }
    app.post(url, params).then((res) => {
      if (res.data.code == 1) { 
        for (let i in wordsNums) {
          wordsNums[i].state = false
          if (i == index) {
            wordsNums[i].state = true
          }
        }
        for (let i in dayNum) {
          dayNum[i].state = false
          if (parseInt(dayNum[i].num) == parseInt(res.data.data.everyday_days)) {
            dayNum[i].state = true
          }
        }
        that.setData({
          days: res.data.data.everyday_days,
          wordsNum: wordsNum,
          dayNum: dayNum,
          wordsNums: wordsNums,
          toView: 'inToView' + res.data.data.everyday_days
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg)
    })
  },
  setplanpage(){ 
   let that=this;
   if (wx.getStorageSync('ifeidtwordpackage')){//true为修改词包
    that.setPackage();
   }else{//false为设置计划
    that.confirmPlan();
   }
  },
  //设定计划操作
  confirmPlan() {
   let that=this;
   if (that.data.days==''){
    wx.showToast({
     title: '请选择天数!',
     icon: 'none',
     duration: 2000    
    })
   }else{
    let url ='Words/setPlan';
    let params={
      package_id: that.data.wordspackageid,
      token: wx.getStorageSync('token'),
      days:that.data.days,
      everyday_words: that.data.wordsNum
    }
    app.post(url,params).then((res)=>{
     wx.switchTab({
      url: '../../MemorizingWords/MemorizingWords',
     })
    }).catch((errMsg)=>{
      console.log(errMssg)
    }) 
   }
  },
 //接口:修改词包
 setPackage() {
  let that = this;
  if (that.data.days == '') {
   wx.showToast({
    title: '请选择天数!',
    icon: 'none',
    duration: 2000
   })
  }else{
   let url = 'Words/setPackage';
   wx.setStorageSync('ifeidtwordpackage', false);
   let params = {
    package_id: that.data.wordspackageid,
    token: wx.getStorageSync('token'),
    days: that.data.days,
    everyday_words: that.data.wordsNum
   }
   app.post(url, params).then((res) => {
    if (res.data.code == 1) {
     wx.switchTab({
      url: '../../MemorizingWords/MemorizingWords',
     })
    }
   }).catch((errMsg) => {
    console.log(errMssg)
   })
  }
   
 
 },
 //修改计划操作
 seteditPlan() {
  let that = this;
  if (that.data.days == '') {
   wx.showToast({
    title: '请选择天数!',
    icon: 'none',
    duration: 2000
   })
  } else {
   let url = 'Words/editPlan';
   let params = {
    token: wx.getStorageSync('token'),
    days: that.data.days,
    everyday_words: that.data.wordsNum
   }
   app.post(url, params).then((res) => {
    if (res.data.code == 1) {
     wx.switchTab({
      url: '../../MemorizingWords/MemorizingWords',
     })
    }
   }).catch((errMsg) => {
    console.log(errMssg)
   })
  }

 },

  //获取计划页面详情
  getplan(){
    let that=this;
    let url ='Words/getPlanInfo';
    let days = parseInt(that.data.days);//选择天
    let wordsNum = parseInt(that.data.wordsNum);//每天背的单词数
    let params={
      package_id: that.data.wordspackageid
    }
    app.post(url,params).then((res)=>{
      if(res.data.code==1){
        that.setData({
          packageDetail: res.data.data,
          total_num: res.data.data.total_num,
        })
      }
    }).catch((errMsg)=>{
      console.log(errMsg)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that=this;
    if (options.wordspackageid !=undefined){//设置或修改词包
      that.setData({
        wordspackageid:options.wordspackageid,
        ifeidtwordpackage: wx.getStorageSync('ifeidtwordpackage')
      })  
    }
    if (options.days !=undefined){
     let dayNum = that.data.dayNum;
     for (let obj of dayNum){
      if (obj.num == options.days){
       obj.state=true
      }
     }
      that.setData({
        ifedit:true,
        dayNum: dayNum,
        days: options.days,//选择天(默认是第一天)
        wordsNum: options.wordnum,//每天背的单词数
      })
    }
    that.getplan(); 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})