// pages/set_time/set_time.js import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js' import { VacationSetSave, VacationList } from '../../utils/api.js' const { navigateTo, redirectTo, navigateBack } = routers() const { globalData, saveUserInfo, getWxloginCode, checkStatus } = getApp() const { baseImgUrl, thumbnail } = globalData const app = getApp() Page({ /** * 页面的初始数据 */ data: { isSelectTime:true, startDate:'', endDate:'', btnIndex:0, btnList:['今年','明年'], list:[ // { // libId: "xxxxx", // id: "sssss", // type: 1, // endDate: "2020--01-24", // startDate: "2020--02-24", // setState: 1, // limitMsg:'5555' // }, // { // libId: "xxxxx", // id: "sssss", // type: 2, // endDate: "2020--01-24", // startDate: "2020--02-24", // setState: 2, // limitMsg: '5555' // }, // { // libId: "xxxxx", // id: "sssss", // type: 3, // endDate: "2020--01-24", // startDate: "2020--02-24", // setState: 1, // limitMsg: '5555' // }, // { // libId: "xxxxx", // id: "sssss", // type: 4, // endDate: "2020--01-24", // startDate: "2020--02-24", // setState: 2, // limitMsg: '5555' // }, ] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData(options) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { const obj = { navigateTo, redirectTo, navigateBack, viewImage } for (const i in obj) { this[i] = obj[i] } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.sureVacationList() }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, /** * 选择年份 */ selectYear:function(e){ const { idx } = e.currentTarget.dataset; this.setData({btnIndex:idx}) this.sureVacationList() }, /** * 获取假期 */ sureVacationList: function (cb) { const continuousFn = { fn: this.sureVacationList, param: { ...arguments } } const { libId,btnIndex} = this.data; console.log(btnIndex) VacationList({ data: { libId,type:btnIndex}, continuousFn }).then(res => { const {list}=res.data; this.setData({list}) if (isFn(cb)) cb() }).catch(res => { if (isFn(cb)) cb() }) }, /** * 设置节假日时间 */ sureVacationSetSave: function (cb) { const continuousFn = { fn: this.sureVacationSetSave, param: { ...arguments } } const { startDate, endDate, setState,id,type } = this.data; // 判断不能超出七天 const startTimestamp = new Date(startDate).getTime(); const endTimestamp = new Date(endDate).getTime(); if (type==2||type==4){ if ((endTimestamp - startTimestamp) > 7 * 24 * 60 * 60 * 1000) { wx.showToast({ title: '查询范围不能超过一周', icon: 'none' }) return; } } if ((endTimestamp - startTimestamp) <0){ wx.showToast({ title: '结束时间不能早于开始时间', icon: 'none' }) return; } console.log(startDate, endDate,setState,id) VacationSetSave({ data: { startDate, endDate, setState:Number(setState), id }, continuousFn }).then(res => { wx.showToast({ title: '保存成功', icon: 'none' }) this.sureVacationList() // this.setData({ isSelectTime:true}) if (isFn(cb)) cb() }).catch(res => { if (isFn(cb)) cb() }) }, /** * 设置开关 */ selectDay:function(e){ const { startDate, endDate,list,btnIndex}=this.data; console.log(e) const { id, type, istype,time,index } = e.currentTarget.dataset.paras; var temp = {}; temp[`startDate`] = list[index].startDate; temp[`endDate`] = list[index].endDate; this.setData(temp) const today = new Date().getTime(); if(time){ var start = new Date(time).getTime(); }else{ var start = new Date().getTime(); } var timestamp = Date.parse(new Date()); var date = new Date(timestamp); if(btnIndex==1){ var year = date.getFullYear()+1; }else{ var year = date.getFullYear(); } if (istype == 1 && (start - today) < 0 || (start - today)==0&&!time){ if(type==1){ this.setData({ star: year + '-' + '01' + '-' +'01', end: year + '-' + '02' + '-' + 28, }) } else if (type == 2){ this.setData({ star: year + '-' + '04' + '-' + '01', end: year + '-' + '05' + '-' + 31, }) } else if (type == 3) { this.setData({ star: year + '-' + '07' + '-' + '01', end: year + '-' + '08' + '-' + 31, }) } else if (type == 4) { this.setData({ star: year + '-' + '09' + '-' + '01', end: year + '-' + 10 + '-' + 31, }) } this.setData({setState: istype == 1 ? '2' : '1',id, type,}) }else{ this.setData({setState: istype == 1 ? '2' : '1',id, type,}) this.sureVacationSetSave() } }, /** * 选择开始时间 */ selectStartDate: function (e) { const { endDate} = this.data; const end = new Date(endDate).getTime(); const today = new Date().getTime(); const start = new Date(e.detail.value).getTime(); if ((start - today) < 0) { wx.showToast({ title: '开始时间不能早于今天', icon: 'none' }) return; } if ((start-end) >0) { wx.showToast({ title: '开始时间不能大于结束时间', icon: 'none' }) return; } this.setData({ startDate: e.detail.value }) }, /** * 选择结束时间 */ selectEndDate: function (e) { const { startDate} = this.data; const start = new Date(startDate).getTime(); const end = new Date(e.detail.value).getTime(); if ((end - start) < 0) { wx.showToast({ title: '结束时间不能早于开始时间', icon: 'none' }) return; } this.setData({ endDate: e.detail.value }) }, })