-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdateSelect.js
More file actions
185 lines (169 loc) · 5.45 KB
/
dateSelect.js
File metadata and controls
185 lines (169 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// pages/dateSelect/dateSelect.js
var Moment = require("utils/Moment.js");
var DATE_LIST = [];
var DATE_YEAR = new Date().getFullYear()
var DATE_MONTH = new Date().getMonth()+1
var DATE_DAY = new Date().getDate()
Page({
data:{
maxMonth:3, //最多渲染月数
dateList:[],
systemInfo:{},
weekStr:['日','一','二','三','四','五','六'],
checkInDate:Moment(new Date()).format('YYYY-MM-DD'),
checkOutDate:Moment(new Date()).add(1,'day').format('YYYY-MM-DD'),
markcheckInDate:false, //标记开始时间是否已经选择
markcheckOutDate:false //标记结束时间是否已经选择
},
onLoad:function(options){
// 页面初始化 options为页面跳转所带来的参数
this.createDateListData();
var _this = this;
// 页面初始化 options为页面跳转所带来的参数
var checkInDate = options.checkInDate?options.checkInDate:Moment(new Date()).format('YYYY-MM-DD');
var checkOutDate = options.checkOutDate?options.checkOutDate:Moment(new Date()).add(1,'day').format('YYYY-MM-DD');
wx.getSystemInfo({
success: function(res) {
_this.setData({systemInfo:res,checkInDate:checkInDate,checkOutDate:checkOutDate});
}
})
},
onReady:function(){
// 页面渲染完成
},
onShow:function(){
// 页面显示
},
onHide:function(){
// 页面隐藏
},
onUnload:function(){
// 页面关闭
},
createDateListData:function(){
var dateList = [];
var now = new Date();
/*
设置日期为 年-月-01,否则可能会出现跨月的问题
比如:2017-01-31为now ,月份直接+1(now.setMonth(now.getMonth()+1)),则会直接跳到跳到2017-03-03月份.
原因是由于2月份没有31号,顺推下去变成了了03-03
*/
now = new Date(now.getFullYear(),now.getMonth(),1);
for(var i=0;i<this.data.maxMonth;i++){
var momentDate = Moment(now).add(this.data.maxMonth-(this.data.maxMonth-i),'month').date;
var year = momentDate.getFullYear();
var month = momentDate.getMonth()+1;
var days = [];
var totalDay = this.getTotalDayByMonth(year,month);
var week = this.getWeek(year,month,1);
//-week是为了使当月第一天的日期可以正确的显示到对应的周几位置上,比如星期三(week = 2),
//则当月的1号是从列的第三个位置开始渲染的,前面会占用-2,-1,0的位置,从1开正常渲染
for(var j = -week+1;j<=totalDay;j++){
var tempWeek = -1;
if(j>0)
tempWeek = this.getWeek(year,month,j);
var clazz = '';
if(tempWeek == 0 || tempWeek == 6)
clazz = 'week'
if(j<DATE_DAY && year == DATE_YEAR && month == DATE_MONTH)
//当天之前的日期不可用
clazz = 'unavailable '+clazz;
else
clazz = 'nostate '+clazz
days.push({day:j,class:clazz})
}
var dateItem = {
id:year+'-'+month,
year:year,
month:month,
days:days
}
dateList.push(dateItem);
}
this.setData({
dateList:dateList
});
DATE_LIST = dateList;
},
/*
* 获取月的总天数
*/
getTotalDayByMonth:function(year,month){
month=parseInt(month,10);
var d=new Date(year,month,0);
return d.getDate();
},
/*
* 获取月的第一天是星期几
*/
getWeek:function(year,month,day){
var d=new Date(year,month-1,day);
return d.getDay();
},
/**
* 点击日期事件
*/
onPressDate:function(e){
var {year,month,day} = e.target.dataset;
//当前选择的日期为同一个月并小于今天,或者点击了空白处(即day<0),不执行
if((day<DATE_DAY && month == DATE_MONTH) || day<=0) return;
var tempMonth = month;
var tempDay = day;
if(month<10) tempMonth='0'+month
if(day <10) tempDay= '0'+day
var date = year+'-'+tempMonth+'-'+tempDay;
//如果点击选择的日期A小于入住时间,则重新渲染入住时间为A
if((this.data.markcheckInDate && Moment(date).before(this.data.checkInDate)||this.data.checkInDate === date)){
this.setData({
markcheckInDate:false,
markcheckOutDate:false,
dateList:DATE_LIST.concat()
});
};
if(!this.data.markcheckInDate){
this.setData({
checkInDate:date,
markcheckInDate:true,
});
}else if(!this.data.markcheckOutDate){
this.setData({
checkOutDate:date,
markcheckOutDate:true,
});
//设缓存,返回页面时,可在onShow时获取缓存起来的日期
wx.setStorage({
key:'ROOM_SOURCE_DATE',
data:{
checkInDate:this.data.checkInDate,
checkOutDate:this.data.checkOutDate
}
});
wx.navigateBack({
delta: 1, // 回退前 delta(默认为1) 页面
});
}
this.renderPressStyle(year,month,day);
},
renderPressStyle:function(year,month,day){
var dateList = this.data.dateList;
//渲染点击样式
for(var i=0;i<dateList.length;i++){
var dateItem = dateList[i];
var id = dateItem.id;
if(id === year+'-'+month){
var days = dateItem.days;
for(var j=0;j<days.length;j++){
var tempDay = days[j].day;
if(tempDay == day){
days[j].class = days[j].class+' active';
break;
}
}
break;
}
}
this.setData({
dateList:dateList
});
}
})