123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- // pages/appointment/appointment.js
- const app = getApp()
- import { submitAppointment, getResetNumber, getAccountNmber } from '../../api/appointment'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- isBusyDay: true,
- hasAddress: false,
- accountNumber: 0,
- addressId: '',
- address: '',
- province: '',
- city: '',
- county: '',
- timeActive: '',
- appointmentSuccess: false,
- curYear: '-',
- curMonth: '-',
- choseDate: '',
- weekList: [],
- backupWeekList: [],
- currentTime: '',
- currentWeek: '',
- currentYear: '',
- appusername: '',
- appuserheadimg: '',
- durationOne: false,
- durationOneBusy: false,
- durationTwo: false,
- durationTwoBusy: false,
- durationThree: false,
- durationThreeBusy: false,
- allDayBusy: false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- this.initCalendar()
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- var headImg = wx.getStorageSync('headImg')
- var userName = wx.getStorageSync('userName')
- this.getAccountNmberFn()
- this.setData({
- appusername: userName.length > 8 ? userName.substring(0,4) : userName,
- appuserheadimg: headImg
- })
- if (app.globalData.navigateBackParams.address) {
- this.setData({
- hasAddress: true,
- address: app.globalData.navigateBackParams.address,
- contactName: app.globalData.navigateBackParams.contactName,
- contactPhone: app.globalData.navigateBackParams.contactPhone,
- addressId: app.globalData.navigateBackParams.addressId,
- province: app.globalData.navigateBackParams.province,
- city: app.globalData.navigateBackParams.city,
- county: app.globalData.navigateBackParams.county
- })
- }
- },
- // 获取当天可预约的次数
- getResetNumberFn (cDate, index) {
- var that = this
- var data = {
- province: this.data.province,
- city: this.data.city,
- county: this.data.county,
- appointmentDate: cDate
- }
- wx.showLoading({
- title: '加载中...',
- mask: true
- })
- getResetNumber(data).then(res => {
- wx.hideLoading()
- const response = res.data.carDurationConfigs
- const allBusy = response.find(item => {
- return item.isBusy == false
- })
- if (allBusy == undefined) {
- that.handleAllDayBusy(cDate)
- } else {
- that.handleAllDayAppointment(cDate)
- }
- response.forEach(item => {
- if (item.duration == 1) {
- that.setData({
- durationOne: item.isBusy,
- durationOneBusy: item.rest == 0 ? true : false
- })
- }
- if (item.duration == 2) {
- that.setData({
- durationTwo: item.isBusy,
- durationTwoBusy: item.rest == 0 ? true : false
- })
- }
- if (item.duration == 3) {
- that.setData({
- durationThree: item.isBusy,
- durationThreeBusy: item.rest == 0 ? true : false
- })
- }
- })
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e,
- confirmColor: '#333',
- showCancel: false
- })
- })
- },
- handleAllDayBusy (cDate) {
- const _weekList = this.data.weekList
- _weekList.forEach(item => {
- if (item.dateStr == cDate) {
- item.date = '约满'
- }
- })
- this.setData({
- weekList: _weekList
- })
- },
- handleAllDayAppointment (cDate) {
- console.log(cDate, '可预约')
- const _weekList = this.data.weekList
- _weekList.forEach(item => {
- if (item.dateStr == cDate && item.date == '约满') {
- console.log(item.dateStr);
- item.date = Number(item.dateStr.split('-')[2])
- }
- })
- this.setData({
- weekList: _weekList
- })
- },
- onPullDownRefresh () {
- wx.vibrateShort({
- type: 'medium'
- })
- this.getAccountNmberFn(true)
- },
- // 查询账户剩余可使用的次数
- getAccountNmberFn (pullDownRefresh = false) {
- var that = this
- wx.showLoading({
- title: '加载中...',
- mask: true
- })
- getAccountNmber({}).then(res => {
- wx.hideLoading()
- if (pullDownRefresh) {
- wx.stopPullDownRefresh()
- }
- that.setData({
- accountNumber: res.data
- })
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e,
- confirmColor: '#333',
- showCancel: false
- })
- })
- },
- // 初始化日历
- 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)
- const _month = Number(bDate.getMonth() + 1) < 10 ? '0' + Number(bDate.getMonth() + 1) : bDate.getMonth() + 1
- const _day = Number(bDate.getDate()) < 10 ? '0' + bDate.getDate() : bDate.getDate()
- _bankupWeekList.unshift({
- date: bDate.getDate(),
- dateStr: bDate.getFullYear() + '-' + _month + '-' + _day,
- disabled: true,
- status: '',
- busy: false
- })
- }
- 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 = Number(baseDate.getMonth() + 1) < 10 ? '0' + Number(baseDate.getMonth() + 1) : baseDate.getMonth() + 1
- const _year = baseDate.getFullYear()
- const _day = Number(baseDate.getDate()) < 10 ? '0' + baseDate.getDate() : baseDate.getDate()
- const _week = baseDate.getDay()
- item.week = _week
- if (_week == 0) {
- item.weekStr = '周日'
- item.busy = true
- }
- if (_week == 1) {
- item.weekStr = '周一'
- }
- if (_week == 2) {
- item.weekStr = '周二'
- }
- if (_week == 3) {
- item.weekStr = '周三'
- }
- if (_week == 4) {
- item.weekStr = '周四'
- }
- if (_week == 5) {
- item.weekStr = '周五'
- }
- if (_week == 6) {
- item.weekStr = '周六'
- item.busy = true
- }
- if (index > that.data.currentWeek) {
- if (index == that.data.currentWeek + 1) {
- item.date = '约满'
- item.busy = false
- item.disabled = true
- that.setData({
- curYear: _year,
- curMonth: _month,
- })
- } else {
- item.date = baseDate.getDate()
- }
- if (index == that.data.currentWeek + 2) {
- // item.active = true
- /* that.setData({
- choseDate: _year + '-' + _month + '-' + _day
- }) */
- }
- item.dateStr = _year + '-' + _month + '-' + _day
- } else {
- item.disabled = true
- item.busy = false
- item.dateStr = baseDate.getFullYear() + '-' + _month + '-' + _day
- if (index == that.data.currentWeek) {
- 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: '',
- busy: false
- })
- }
- this.setData({
- weekList: _list
- })
- resolve()
- })
- },
- // 选择日期
- handleSelectDate (e) {
- if (!this.data.hasAddress) {
- wx.showToast({
- title: '请选择地址',
- icon: 'error',
- mask: true
- })
- } else {
- const disabled = e.currentTarget.dataset.disabled
- if (!disabled) {
- const cDate = e.currentTarget.dataset.date
- const index = e.currentTarget.dataset.index
- const _year = cDate.split('-')[0]
- const _month = cDate.split('-')[1]
- const _weeklist = this.data.weekList
- _weeklist.map((item, i) => {
- if (index == i ) {
- item.active = true
- } else {
- item.active = false
- }
- })
- this.setData({
- curYear: _year,
- curMonth: _month,
- weekList: _weeklist,
- choseDate: cDate,
- timeActive: ''
- })
- this.getResetNumberFn(cDate,index)
- }
- }
- },
- /**
- * 选择地址
- */
- 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) {
- if (!this.data.hasAddress) {
- wx.showToast({
- title: '请选择地址',
- icon: 'error',
- mask: true
- })
- return
- }
- if (this.data.choseDate == '') {
- wx.showToast({
- title: '请选择日期',
- icon: 'error',
- mask: true
- })
- return
- }
- var timeActiveIndex = e.currentTarget.dataset.time
- var disabled = e.currentTarget.dataset.dis
- if (!disabled) {
- this.setData({
- timeActive: timeActiveIndex
- })
- }
- },
- // 去充值
- handleCharge () {},
- // 确定 先查询 区域的剩余预约次数
- handleConfirm () {
- if (this.data.accountNumber == 0) {
- wx.showModal({
- content: '您当前的检测次数为0,先充值次数',
- cancelColor: '#666',
- cancelText: '卡激活',
- confirmText: '去充值',
- confirmColor: '#333',
- success (res) {
- if (res.confirm) {
- wx.navigateTo({
- url: '/pages/buy/buy?from=appointment',
- })
- } else if (res.cancel) {
- wx.navigateTo({
- url: '/pages/exchange/exchange',
- })
- }
- }
- })
- } else if (!this.data.hasAddress) {
- wx.showToast({
- title: '请选择地址',
- icon: 'error',
- mask: true
- })
- } else {
- if (this.data.choseDate == '') {
- wx.showToast({
- title: '请选择日期',
- icon: 'error',
- mask: true
- })
- } else {
- if (this.data.timeActive == '') {
- wx.showToast({
- title: '请选择时间段',
- icon: 'error',
- mask: true
- })
- } else {
- this.submitAppointmentFn()
- }
- }
- }
- },
- // 提交预约信息
- submitAppointmentFn () {
- var data = {
- appointmentTime: this.data.choseDate,
- duration: this.data.timeActive,
- addressId: this.data.addressId
- }
- wx.showLoading({
- title: '预约中...'
- })
- var timer = null
- submitAppointment(data).then(res => {
- wx.hideLoading()
- wx.showToast({
- title: '预约成功',
- icon: 'success'
- })
- timer = setTimeout(() => {
- wx.navigateTo({
- url: '/pages/myAppointment/myAppointment?from=appointment',
- })
- clearTimeout(timer)
- }, 1000);
- /* this.setData({
- appointmentSuccess: true
- }) */
- }).catch(e => {
- wx.hideLoading()
- wx.showModal({
- content: e,
- confirmColor: '#333',
- showCancel: false
- })
- })
- },
- resetData () {
- app.globalData.navigateBackParams.address = ''
- app.globalData.navigateBackParams.contactName = ''
- app.globalData.navigateBackParams.contactPhone = ''
- app.globalData.navigateBackParams.addressId = ''
- app.globalData.navigateBackParams.province = ''
- app.globalData.navigateBackParams.city = ''
- app.globalData.navigateBackParams.county = ''
- this.setData({
- hasAddress: true,
- appointmentSuccess: false,
- timeActive: '',
- addressId: '',
- address: '',
- province: '',
- city: '',
- county: '',
- })
- },
- handleCancel () {
- this.resetData()
- wx.navigateBack()
- }
- })
|