// 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, // 可用内容高度 room: {}, startTime: null, // 入住开始时间 endTime: null, // 入住结束时间 roomId: null, count: 1, // 入住几晚 date: '', //入住区间 date2: '', name: '', mobile: '', activeOrder: null, //创建的订单 tradeNo: null, //订单号 showPayment: false, //显示支付组件 //显示日历选择 show:false, }, /** * 生命周期函数--监听页面加载 */ 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 -5- 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)}`, date2: `${this.formatDate2(this.data.startTime,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}`; }, formatDate2(date1, date2) { date1 = new Date(date1); date2 = new Date(date2); // 获取年份、月份和日期 const year1 = date1.getFullYear(); const month1 = date1.getMonth() + 1; // 月份从0开始,所以要加1 const day1 = date1.getDate(); const year2 = date2.getFullYear(); const month2 = date2.getMonth() + 1; // 月份从0开始,所以要加1 const day2 = date2.getDate(); // 返回格式化的字符串 return `${year1}年${month1}月${day1}日入住 - ${year2}年${month2}月${day2}日离店`; }, //获取房间信息 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({ room: roomList[0] }) console.log('房间', this.data.room); }, //计算选择日期的房间数量 async checkOrderCount() { // start1 end1 现在选择 // start2 end2 已有订单时间 // start1 < start2 end1 < end2 // start1 > start2 end1 > end2 // start1 = start2 end1 = end2 const startTime = new Date(this.data.startTime); const endTime = new Date(this.data.endTime); 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.notContainedIn('status', [100, 400, 601, 700]) Order.greaterThanOrEqualTo("startTime", endTime) //20 23 21 22 Order.lessThanOrEqualTo("endTime", startTime) Order.select("startTime", "endTime") let count = await Order.count() console.log('count', count); return count }, //生成订单号 getTradeNo() { let now = new Date() let tradeNo = "C" + String(now.getFullYear()) + (now.getMonth() + 1) + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds() + Math.random().toString().slice(-6); //生成六位随机数 return tradeNo }, // 创建待支付订单 async setOrder(tradeNo) { const startTime = new Date(this.data.startTime); const endTime = new Date(this.data.endTime); let currentUser = Parse.User.current() let Order = Parse.Object.extend("RoomOrder"); let order = new Order() try { // order.set("details", this.data.room.details); order.set("user", { __type: 'Pointer', className: '_User', objectId: currentUser.id }); order.set("orderNum", tradeNo); order.set("room", { __type: 'Pointer', className: 'ShopRoom', objectId: this.data.roomId }); console.log(this.data.startTime, endTime); order.set("startTime", startTime); order.set("endTime", endTime); order.set("merber", 1); order.set("price", this.data.count * this.data.room.price); order.set("shopStore", { __type: 'Pointer', className: 'ShopStore', objectId: this.data.room.shop.objectId }); order.set("company", { __type: 'Pointer', className: 'Company', objectId: company }) order.set("name", this.data.name); order.set("mobile", this.data.mobile); // order.set("merber", this.data.peopleNum); order = await order.save() } catch (error) { console.log(error); wx.showToast({ title: error.errMsg, icon: 'none' }) } return order }, //获取住客姓名 getname(e) { this.setData({ name: e.detail.value }) console.log(this.data.name); }, //获取住客电话 getmobile(e) { const mobileNumber = e.detail.value; // 假设手机号是通过 e.detail.value 传递的 console.log(mobileNumber); // 正则表达式:匹配中国大陆手机号 const mobilePattern = /^1[3-9]\d{9}$/; // 验证手机号格式 if (mobilePattern.test(mobileNumber)) { this.setData({ mobile: mobileNumber }) console.log("手机号格式正确"); // 这里可以执行其他操作,比如保存手机号或进行下一步操作 } else { wx.showToast({ title: '手机号格式不正确', icon: 'none', }) console.log("手机号格式不正确"); // 这里可以提示用户手机号格式不正确 } }, //点击支付 async submit() { // if (!this.data.startTime || !this.data.endTime) { // wx.showToast({ // title: '请选择入住时间', // icon: 'none', // }) // return // } if (this.data.name.length == 0 || this.data.mobile.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, //展示支付组件 }) } }, // 返回首页 getback() { wx.navigateTo({ url: '../../../pages/index/index', }); }, //支付反馈 async acceptResult(e) { let { activeOrder } = this.data let that = this let { params, no } = e.detail; that.setData({ showPayment: false }) try { if (params == "ok") { activeOrder.set("status", 100) activeOrder.set("isPay", true) await activeOrder.save() wx.showToast({ title: '支付成功', icon: 'none', duration: 1500, mask: false, }); wx.navigateBack({ delta: 1, }) } else { wx.showToast({ title: '支付失败,取消订单', icon: 'none', duration: 1500, mask: false, }); } } catch (error) { console.log(error) wx.showToast({ title: "支付失败", icon: "error", duration: 1500, }); wx.hideLoading() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, //开日历 onDisplay() { this.setData({ show: true }); }, //关日历 onClose() { this.setData({ show: false }); }, //选好日期点击完成后 onConfirm(event) { const [start, end] = event.detail; this.setData({ show: false, startTime:start, endTime:end, }); this.getdate() }, })