制作一个微信小程序预约单/入住时间/退房时间/总计天数/房型选择/房型价格/总价随着选择房型变动计算 不设最大日期 随着选择房型点击时间 代码
时间: 2023-07-20 14:43:51 浏览: 123
微信小程序 报名预约 家政预约 (源代码+截图)
5星 · 资源好评率100%
好的,我会为您提供一个微信小程序预约单的代码示例。请注意,为了简化代码,我将使用一个静态数据源来存储房型和价格信息,您可以根据自己的需求修改代码。
**wxml代码:**
```html
<view class="container">
<view class="form-group">
<view class="form-label">入住时间</view>
<picker mode="date" value="{{checkInDate}}" start="{{today}}" end="{{endDate}}" bindchange="onCheckInDateChange">
<view class="picker">{{checkInDate}}</view>
</picker>
</view>
<view class="form-group">
<view class="form-label">退房时间</view>
<picker mode="date" value="{{checkOutDate}}" start="{{checkInDate}}" end="{{endDate}}" bindchange="onCheckOutDateChange">
<view class="picker">{{checkOutDate}}</view>
</picker>
</view>
<view class="form-group">
<view class="form-label">房型选择</view>
<picker mode="selector" range="{{roomTypes}}" value="{{roomTypeIndex}}" bindchange="onRoomTypeChange">
<view class="picker">{{roomTypes[roomTypeIndex]}}</view>
</picker>
</view>
<view class="form-group">
<view class="form-label">总计天数</view>
<view class="form-value">{{totalDays}}</view>
</view>
<view class="form-group">
<view class="form-label">房型价格</view>
<view class="form-value">{{roomTypePrice}}元/天</view>
</view>
<view class="form-group">
<view class="form-label">总价</view>
<view class="form-value">{{totalPrice}}元</view>
</view>
</view>
```
**js代码:**
```javascript
Page({
data: {
today: '', // 当前日期
endDate: '', // 结束日期
checkInDate: '', // 入住日期
checkOutDate: '', // 退房日期
roomTypes: ['标准间', '豪华间', '套房'], // 房型列表
roomTypeIndex: 0, // 当前房型索引
roomTypePrices: [200, 300, 500], // 房型价格列表
totalDays: 0, // 总计天数
roomTypePrice: 0, // 房型价格
totalPrice: 0 // 总价
},
onLoad: function () {
// 初始化日期
let today = new Date().toLocaleDateString().replace(/\//g, '-');
this.setData({
today: today,
endDate: '2099-12-31'
});
},
// 入住时间改变事件
onCheckInDateChange: function (e) {
let checkInDate = e.detail.value;
let checkOutDate = this.data.checkOutDate;
let totalDays = this.calculateTotalDays(checkInDate, checkOutDate);
let roomTypePrice = this.data.roomTypePrices[this.data.roomTypeIndex];
let totalPrice = totalDays * roomTypePrice;
this.setData({
checkInDate: checkInDate,
totalDays: totalDays,
roomTypePrice: roomTypePrice,
totalPrice: totalPrice
});
},
// 退房时间改变事件
onCheckOutDateChange: function (e) {
let checkOutDate = e.detail.value;
let checkInDate = this.data.checkInDate;
let totalDays = this.calculateTotalDays(checkInDate, checkOutDate);
let roomTypePrice = this.data.roomTypePrices[this.data.roomTypeIndex];
let totalPrice = totalDays * roomTypePrice;
this.setData({
checkOutDate: checkOutDate,
totalDays: totalDays,
roomTypePrice: roomTypePrice,
totalPrice: totalPrice
});
},
// 房型改变事件
onRoomTypeChange: function (e) {
let roomTypeIndex = e.detail.value;
let roomTypePrice = this.data.roomTypePrices[roomTypeIndex];
let totalDays = this.data.totalDays;
let totalPrice = totalDays * roomTypePrice;
this.setData({
roomTypeIndex: roomTypeIndex,
roomTypePrice: roomTypePrice,
totalPrice: totalPrice
});
},
// 计算总计天数
calculateTotalDays: function (checkInDate, checkOutDate) {
let checkInTimeStamp = new Date(checkInDate).getTime();
let checkOutTimeStamp = new Date(checkOutDate).getTime();
let totalDays = (checkOutTimeStamp - checkInTimeStamp) / (1000 * 60 * 60 * 24);
return totalDays;
}
})
```
这段代码实现了根据用户选择的入住时间、退房时间和房型来计算总计天数、房型价格和总价的功能。当用户选择入住时间或退房时间时,会计算总计天数,并根据当前房型价格计算总价;当用户选择房型时,会根据当前总计天数和新的房型价格来计算总价。
阅读全文