// pages/appointment/appointment.js const app = getApp() import { submitAppointment } from '../../api/appointment' Page({ /** * 页面的初始数据 */ data: { hasAddress: false, address: '', timeActive: '0', appointmentSuccess: false, curYear: '-', curMonth: '-', weekList: [], backupWeekList: [], currentTime: '', currentWeek: '', currentYear: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.initCalendar() }, // 初始化日历 async initCalendar () { var that = this var currentDate = new Date() this.setData({ currentTime: currentDate.getTime(), currentWeek: currentDate.getDay(), currentYear: currentDate.getFullYear(), currentDay: currentDate.getDate() }) await that.createCalendarGrild(33) var _weekList = that.data.weekList _weekList.map((item, index) => { if (index == that.data.currentWeek) { item.date = that.data.currentDay } }) that.setData({ weekList: _weekList }) // 周一开始 if (that.data.currentWeek >= 1) { that.setData({ weekList: that.setCalendarItem() }) } // 周日 if (that.data.currentWeek < 1) { let _bankupWeekList = [] for (let index = 0; index < 7; index++) { const bDate = new Date(that.data.currentTime - (index + 1) * 86400000).getDate() const _month = baseDate.getMonth() + 1 _bankupWeekList.unshift({ date: index == 0 ? '约满' : bDate.getDate(), dateStr: bDate.getFullYear() + '-' + _month, disabled: true, status: '' }) } that.setData({ backupWeekList: _bankupWeekList, weekList: that.setCalendarItem() }) } }, setCalendarItem () { var that = this var _mapWeekList = that.data.weekList _mapWeekList.map((item, index) => { const baseDate = new Date((index - that.data.currentWeek) * 86400000 + that.data.currentTime) const _month = baseDate.getMonth() + 1 const _year = baseDate.getFullYear() if (index > that.data.currentWeek) { if (index == that.data.currentWeek + 1) { item.active = true that.setData({ curYear: _year, curMonth: _month }) } item.date = baseDate.getDate() item.dateStr = baseDate.getFullYear() + '-' + _month } else { item.disabled = true item.dateStr = baseDate.getFullYear() + '-' + _month if (index == that.data.currentWeek || index == that.data.currentWeek - 1) { item.date = '约满' } else { item.date = baseDate.getDate() } } }) return _mapWeekList }, createCalendarGrild (count) { return new Promise((resolve, reject) => { var _list = [] for (let index = 0; index < count; index++) { _list.push({ date: '-', dateStr: '-', disabled: false, active: false, status: '', }) } this.setData({ weekList: _list }) resolve() }) }, // 选择日期 handleSelectDate (e) { const disabled = e.currentTarget.dataset.disabled if (!disabled) { const cDate = e.currentTarget.dataset.date const index = e.currentTarget.dataset.index console.log(index, 'cDate'); const _year = cDate.split('-')[0] const _month = cDate.split('-')[1] const _weeklist = this.data.weekList _weeklist[index].active = true _weeklist.map((item, i) => { if (index == i ) { item.active = true } else { item.active = false } }) this.setData({ curYear: _year, curMonth: _month, weekList: _weeklist }) } }, /** * 生命周期函数--监听页面显示 */ onShow() { if (app.globalData.navigateBackParams.address ) { this.setData({ hasAddress: true, address: app.globalData.navigateBackParams.address }) } }, formatterFn (day) { console.log(day); }, /** * 选择地址 */ handleChoseAddress() { wx.navigateTo({ url: '/pages/address/address?form=appointment&back=1', }) }, handleChangeAddress () { if (!this.data.appointmentSuccess) { wx.navigateTo({ url: '/pages/address/address?form=appointment&back=1', }) } }, handleTime (e) { console.log(e); var timeActiveIndex = e.currentTarget.dataset.time this.setData({ timeActive: timeActiveIndex }) }, // 激活卡片 handleActiveCard () { wx.navigateTo({ url: '/pages/scan/scan?form=appointment', }) }, // 去充值 handleCharge () {}, // 确定 handleConfirm () { if (!this.data.hasAddress) { wx.showToast({ title: '请选择地址', icon: 'error', mask: true }) } else { var itt = this wx.showLoading({ title: '预约中...' }) setTimeout(() => { wx.hideLoading({ success: (res) => { wx.showToast({ title: '预约成功', }) itt.setData({ appointmentSuccess: true }) } }) }, 1200); } } })