calendar.js
6.48 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import util from '../../../utils/util';
Component({
properties:{
date:{
type:String,
value:util.unixtoTime(new Date()/1000,'YYYY-MM-DD'),
},
marklist:{
type:Array,
value:[]
}
},
data: {
showprev:false
},
observers: {
'date': function(newdate,newmarklist) {
let showYearMonth=this.getshowmonth();
let obj={
...showYearMonth,
action:'defalut'
}
if(newdate!=''){
if(this.data.showYearMonth){ //周历点击
if(showYearMonth.showyear==this.data.showYearMonth.showyear && showYearMonth.showmonth==this.data.showYearMonth.showmonth){
this.changedatestyle(newdate);
}else{
this.setData({
showYearMonth:showYearMonth,
showprev:this.isShowPrev(showYearMonth)
})
this.getdayslist(showYearMonth.showyear,showYearMonth.showmonth);
this.triggerEvent('tellmonth',obj);
}
}else{
//初始化---默认
console.log('默认')
this.setData({
showYearMonth:showYearMonth,
showprev:this.isShowPrev(showYearMonth)
})
this.getdayslist(showYearMonth.showyear,showYearMonth.showmonth);
this.triggerEvent('tellmonth',obj);
}
}
},
'marklist'(newmarklist){
if(this.data.daylist){
this.markdaylist(newmarklist)
}
}
},
attached(){
this.getweeklist();
},
ready(){
},
detached(){
console.log('在组件实例被从页面节点树移除时执行')
},
methods:{
//获取当前选中日期
getshowmonth(){
let datearr=this.data.date.split('-');
return{
showyear:Number(datearr[0]),
showmonth:Number(datearr[1]),
}
},
//获得周列表
getweeklist(){
let week=['日','一','二','三','四','五','六'];
let weeklist=[];
for(let i=0;i<week.length;i++){
weeklist.push({
weekfont:week[i],
weeknum:i
})
}
this.setData({
weeklist:weeklist,
})
},
//获取显示月份天数
getMonthdays(year,month){
var d = new Date(year,month,0);
return d.getDate();
},
//获取某一天是星期几
getdayweek:function(year,month,day){
var tmpdate=new Date(year,month-1,day);
return tmpdate.getDay()
},
//获取当月日期
getdayslist(year,month){
this.today={
curdate:util.unixtoTime(new Date()/1000,'YYYY-MM-DD'),
curtimestamp:new Date()/1000
}
//当前显示月份天数
let monthdays=this.getMonthdays(year,month);
//当前月1号星期几
let dayweek=this.getdayweek(year,month,1);
let daylist=[];
//多少行
let singlelen=Math.ceil((monthdays+dayweek)/7);
for(let i=0;i<singlelen;i++){
daylist.push({
list:[]
})
}
//本月日期
for(let i=0;i<singlelen*7;i++){
let thisdayobj={};
if(i>=dayweek && i<monthdays+dayweek){
let thisday=i-dayweek+1;
let thisdate=year+'-'+(month<10 ? '0'+month : month)+'-'+(thisday<10 ? '0'+thisday : thisday);
let thistimestamp=Number(util.timetouUnix(thisdate+' 00:00:00'));
thisdayobj={
year:year,
month:month,
day:thisday,
week:i%7,
date:thisdate,
istoday:false,
overdate:thistimestamp<=(new Date()/1000)-86400 ? true : false, //日期是否过期,
}
if(thisdate==this.today.curdate){
thisdayobj.automark=true;
thisdayobj.istoday=true;
thisdayobj.mark='今'
}
if(util.unixtoTime(this.today.curtimestamp+86400,'YYYY-MM-DD')==thisdate){
thisdayobj.automark=true;
thisdayobj.mark='明'
}
thisdayobj.isselected=this.data.date==thisdate ? true : false;
}
daylist[Math.floor(i/7)].list.push(thisdayobj)
}
this.setData({
daylist:daylist
})
console.log(daylist)
},
//选择日期
selectdate(e){
let _target=e.currentTarget.dataset;
this.changedatestyle(_target.date);
this.triggerEvent('tellselectdate',{date:_target.date,action:'click'});
},
//修改日期列表
changedatestyle(selecteddate){
let daylist=this.data.daylist;
let prevsingle,previndex,cursingle,curindex;
daylist.forEach((v,i)=>{
v.list.forEach((r,n)=>{
if(r.isselected){
prevsingle=i;
previndex=n;
let prevkey='daylist['+prevsingle+'].list['+previndex+'].isselected';
this.setData({
[prevkey]:false,
})
}
if(r.date==selecteddate){
cursingle=i;
curindex=n;
}
})
})
let currentkey='daylist['+cursingle+'].list['+curindex+'].isselected';
this.setData({
[currentkey]:true
})
},
//计算是否显示prev
isShowPrev(showYearMonth){
let curyear=new Date().getFullYear();
let curmonth=new Date().getMonth() + 1;
let curtitle=''+curyear+(curmonth<10 ? '0'+curmonth : curmonth);
let showtitle=''+showYearMonth.showyear+(showYearMonth.showmonth< 10 ? '0'+showYearMonth.showmonth : showYearMonth.showmonth)
return Number(showtitle)>Number(curtitle);
},
//回退月份
prevmonth(){
var showyear=this.data.showYearMonth.showyear;
var showmonth=this.data.showYearMonth.showmonth;
if(showmonth==1){
showmonth=12;
showyear=showyear-1;
}else{
showmonth-=1;
}
let showYearMonth={
showyear:showyear,
showmonth:showmonth
}
let obj={
...showYearMonth,
action:'click'
}
this.setData({
showYearMonth:showYearMonth,
showprev:this.isShowPrev(showYearMonth)
})
this.getdayslist(showYearMonth.showyear,showYearMonth.showmonth);
this.triggerEvent('tellmonth',obj);
},
//推前月份
nextmonth(){
var showyear=this.data.showYearMonth.showyear;
var showmonth=this.data.showYearMonth.showmonth;
if(showmonth==12){
showmonth=1;
showyear=showyear+1;
}else{
showmonth+=1;
}
let showYearMonth={
showyear:showyear,
showmonth:showmonth
}
this.setData({
showYearMonth:showYearMonth,
showprev:this.isShowPrev(showYearMonth)
})
this.getdayslist(showYearMonth.showyear,showYearMonth.showmonth);
let obj={
...showYearMonth,
action:'click'
}
this.triggerEvent('tellmonth',obj);
},
//标记满房
markdaylist(marklist){
console.log('xxxxxx')
let daylist=this.data.daylist;
daylist.forEach((v,i)=>{
v.list.forEach((r,n)=>{
if(r.mark){
if(r.automark){
if(r.mark!='今' || r.mark!='明'){
if(r.istoday){
r.mark='今'
}else{
r.mark='明'
}
}
}else{
r.mark=null
}
}
marklist.forEach((r1,n1)=>{
if(r.date && r1.date==r.date){
r.mark=r1.mark
}
})
})
})
this.setData({
daylist:daylist
})
},
}
})