微信小程序日历(酒店入住日期选择)
wxml代码。
<wxs src="../../pages/calendar/getTime.wxs" module="getTime" />
<view class="container">
<view class="content">
<view class="head">
<view class="week" wx:for="{{['日','一','二','三','四','五','六']}}">{{item}}</view>
</view>
<view style="margin-top:80rpx"></view>
<view class="day" wx:for="{{futureArr}}" wx:for-item="item">
<view style="flex-shrink: 0;width: 100%; text-align: left;font-weight: 600;padding: 0 20rpx;box-sizing: border-box;justify-content: flex-start;">{{item.newTimer}}</view>
<view wx:for="{{item.newWeek}}" class="old-class"></view>
<view wx:for="{{item.newMonth}}" wx:for-item="date" class="{{getTime.getTime(item.newTimer + '/' + date) < getTime.getTime(nowTimer)?'oldTimer':''}} {{time[0] <= getTime.getTime(item.newTimer + '/' + date) && getTime.getTime(item.newTimer + '/' + date)<= time[1]?'count':''}}">
<view class="{{item.newTimer + '/' + date == nowTimer?'nowTimer':''}} {{time[0] == getTime.getTime(item.newTimer + '/' + date)?'start':''}} {{time[1] == getTime.getTime(item.newTimer + '/' + date)?'end':''}} " data-getTime="{{getTime.getTime(item.newTimer + '/' + date)}}" data-timer="{{item.newTimer + '/' + date}}" bindtap="getTimer">
<view>{{date}}</view>
<view class="one" wx:if="{{item.newTimer + '/' + date == nowTimer && time[0] != getTime.getTime(item.newTimer + '/' + date)}}">今</view>
<view class="one" wx:if="{{time[0] == getTime.getTime(item.newTimer + '/' + date)?'start':''}}">入住</view>
<view class="one" wx:if="{{time[1] == getTime.getTime(item.newTimer + '/' + date)?'end':''}}">离开</view>
</view>
</view>
</view>
</view>
</view>
wxss代码
.container{
width: 100%;
}
.content{
width: 100%;
}
.head{
width: 100%;
display: flex;
flex-direction: row;
justify-content: flex-start;
flex-wrap: wrap;
position: fixed;
top: 0;
left: 0;
}
.week,.day>view{
width: calc(100% / 7);
height: 80rpx;
box-sizing: border-box;
text-align: center;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
font-size: 28rpx;
}
.week{
border-bottom: 2rpx solid #999999;
background-color: #ffffff;
}
.day{
width: 100%;
display: flex;
flex-direction: row;
justify-content: flex-start;
flex-wrap: wrap;
border-bottom: 2rpx solid #999999;
}
.day>.old-class{
border: none;
}
.oldTimer{
color: #999999;
}
.nowTimer{
color: #ffffff;
width: 80rpx;
height: 80rpx;
border-radius: 50%;
font-weight: 900;
background-color: #2e6ee9;
display: flex;
flex-direction: column;
justify-content: center;
}
.nowTimer>.one{
font-size: 24rpx;
}
.start,.end{
color: red;
background-color: rgb(2, 72, 112);
font-weight: 900;
width: 100%;
height: 100%;
border-radius: 15rpx;
}
.count{
width: 100%;
height: 100%;
color: #ffffff;
background-color: #6193ee;
}
js代码
Page({
data: {
futureArr: [],
nowTimer: '',
time: [],
},
onLoad: function (options) {
const that = this;
const date = new Date(this.nowDate());
const year = date.getFullYear();
const month = date.getMonth() + 1;
let futureArr = [];
let newMonth = month;
let newYear = year;
let newWeek = "";
let newTimer = "";
let sumDaysArr = {};
for (let i = 0; i < 6; i++) {
newWeek = new Date(newYear + '/' + newMonth + '/' + 1).getDay();
newTimer = newYear + '/' + newMonth;
sumDaysArr = {
newMonth: that.getDays(newMonth, newYear),
newWeek,
newTimer
};
futureArr.push(sumDaysArr);
newMonth++;
if (newMonth > 12) {
newMonth = 1;
newYear++;
}
};
this.setData({
futureArr: futureArr,
nowTimer: new Date().getFullYear() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getDate(),
});
console.log(futureArr)
},
nowDate() {
const nowDate = new Date()
const nowYear = nowDate.getFullYear()
const nowMonth = nowDate.getMonth() + 1
return nowYear + '/' + nowMonth + '/' + 1
},
isDays(month, year) {
const months = ['1', '3', '5', '7', '8', '10', '12']
if (months.indexOf(month + '') != -1) {
return 31
} else if (month == 2) {
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
return 29
} else {
return 28
}
} else {
return 30
}
},
getDays(month, year) {
let that = this;
let dayArray = [];
for (let i = 0; i < that.isDays(month, year); i++) {
dayArray.push(i + 1);
};
return dayArray
},
getTimer(e) {
let time = this.data.time
const getTime = e.currentTarget.dataset.gettime
if (e.currentTarget.dataset.gettime + 0.1 > new Date(new Date().getFullYear() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getDate()).getTime()) {
if (time.length == 0) {
time.push(getTime)
} else if (time.length == 1) {
if (time[0] < getTime) {
time.push(getTime)
} else if (time[0] > getTime) {
time.unshift(getTime)
} else if (time[0] = getTime) {
time.pop()
}
} else if (time.length == 2) {
if (time[0] > getTime) {
time[0] = getTime
} else if (time[0] < getTime && time[1] != getTime) {
time[1] = getTime
} else if (time[1] == getTime) {
time.pop()
} else if (getTime == time[0]) {
time.shift()
}
}
this.setData({
time: time
})
}
},
})
wxs代码,因为要在wxml中使用js方法。大概相当于Vue中的过滤器。
var getTime = function(time) {
return getDate(time).getTime()
}
module.exports = {
getTime:getTime
}
今年刚毕业参加工作,微信小程序属于自学。望大佬指正代码的错误,不喜勿喷。当然也在线求职,现在在外包公司,工作半年了,基本都是做微信小程序的开发,老板只让单纯写页面,不让写功能和调接口,想换一个公司,提高自习。
|