// nova-tourism/pages/homestay/homestay-order2/index.js let Parse = getApp().Parse; const company = getApp().globalData.company import dataSource from '../../../service/data'; import dateServ from '../../../service/date'; const rechText = require('../../../../utils/rech-text') Page({ /** * 页面的初始数据 */ data: { //屏幕高度 statusBarHeight: 0, // 状态栏高度 screenHeight: 0, // 屏幕高度 customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮) bottomNavHeight: 0, // 底部导航栏高度 contentHeight: 0, // 可用内容高度 roomList: [], startTime: null, // 入住开始时间 endTime: null, // 入住结束时间 roomId: null, count: 1, // 入住几晚 date: '' //入住区间 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const systemInfo = wx.getSystemInfoSync(); const statusBarHeight = systemInfo.statusBarHeight || 0; const screenHeight = systemInfo.screenHeight || 0; const custom = wx.getMenuButtonBoundingClientRect(); const customHeight = custom.height + 10 + 2 || 0; const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0; const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth; this.setData({ contentHeight }); const { objectId, date_start, date_end, } = options; const date_start1 = decodeURIComponent(date_start); const date_end1 = decodeURIComponent(date_end); this.setData({ startTime: date_start1, endTime: date_end1, roomId: objectId }) console.log('123', date_start1, date_end1); this.getroom() this.getdate() }, //计算用户选择的入住和离店日期,并生成相应的日期范围和价格信息。 getdate() { // let { // startTime, // endTime // } = this.formatTimeArea(this.data.start, this.data.end) // console.log('开始时间', this.data.start, '结束时间', this.data.end); // console.log('开始时间', startTime, '结束时间', endTime); var dataAll = dateServ.getDayAll(this.data.startTime, this.data.endTime); dataAll.pop(); // 最后一天为退房,不算在内 console.log('dataAll',dataAll); // console.log(this.formatDate(startTime), this.formatDate(endTime)) let dateStr = this.formatDate(this.data.startTime) + '-' + this.formatDate(this.data.endTime) console.log('dateStr',dateStr) this.setData({ // showDate: false, date: `${this.formatDate(this.data.startTime)} - ${this.formatDate(this.data.endTime)}`, // priceInfoArr: dataAll, count: dataAll.length }) console.log('天数',this.data.count); }, //修改时间格式 formatDate(date) { date = new Date(date); // 获取当前日期 const today = new Date(); // 格式化日期为 "月/日" const month = date.getMonth() + 1; // 月份从0开始,所以要加1 const day = date.getDate(); // 获取星期几 const weekDays = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; const weekDay = weekDays[date.getDay()]; // 获取星期几的中文 // 判断是今天还是明天 let dateDescription = ''; if (date.toDateString() === today.toDateString()) { dateDescription = '今天'; } else if (date.toDateString() === new Date(today.getTime() + 86400000).toDateString()) { dateDescription = '明天'; } else { dateDescription = weekDay; // 其他情况返回星期几 } // 返回格式化的字符串 return `${month}月${day}日 ${dateDescription}`; }, //获取房间信息 async getroom() { let room = new Parse.Query('ShopRoom'); room.equalTo('company', company); room.equalTo('objectId', this.data.roomId); room.equalTo('isEnabled', 'true'); room.include('shop'); room.notEqualTo('isDeleted', 'true'); let room2 = await room.find(); let roomList = room2.map(item => item.toJSON()); this.setData({ roomList }) console.log('房间', this.data.roomList); }, //计算选择日期的房间数量 async checkOrderCount() { // start1 end1 现在选择 // start2 end2 已有订单时间 // start1 < start2 end1 < end2 // start1 > start2 end1 > end2 // start1 = start2 end1 = end2 let now = dateServ.changeDateTime(new Date(), '14:00:00') let Order = new Parse.Query("RoomOrder") Order.equalTo("room", this.data.roomId) Order.equalTo("company", company) Order.exists("status") Order.notEqualTo("status", 100) Order.notEqualTo("status", 400) Order.notEqualTo("status", 601) Order.notEqualTo("status", 700) Order.greaterThanOrEqualTo("startTime", this.data.endTime) //20 23 21 22 Order.lessThanOrEqualTo("endTime", this.data.startTime) Order.select("startTime", "endTime") let count = await Order.count() console.log(count); return count }, //点击支付 async submit() { if (!this.data.startTime || !this.data.endTime) { wx.showToast({ title: '请选择入住时间', icon: 'none', }) return } if (this.data.customers.length == 0) { wx.showToast({ title: '请选择入住人', icon: 'none', }) return } let count = await this.checkOrderCount() if (count >= this.data.room.total) { console.log(count); wx.showToast({ title: '当前日期,已无剩余房间,请重新选择', icon: 'none', }) return } let tradeNo = this.getTradeNo() let order = await this.setOrder(tradeNo) if (order) { this.setData({ activeOrder: order, tradeNo, showPayment: true, }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })