datePicker.js
7.63 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// components/datePicker/datePicker.js
const util = require('../../utils/util.js')
Component({
/**
* 组件的属性列表
*/
properties: {
dateList: {
type: Array,
value: [],
observer: function(newVal, oldVal) {
if (newVal) {
this.setData({
time: this.pushDay(this.data.today.getMonth() + 1)
})
}
}
},
show: {
type: Boolean,
value: false,
observer: function(newVal, oldVal) {
if (newVal) {
this.open()
}
}
},
count: {
type: Number,
value: 3
},
date: {
type: String,
value: new Date().format('yyyy-MM-dd'),
observer: function(newVal, oldVal) {
let today = this.data.date.split('-');
if(today[0] == ""){
today=(new Date()).format('yyyy-MM-dd').split("-")
}
this.setData({
today: new Date(today[0], today[1] - 1, today[2]),
day: `${today[0]}年\n${today[1]}月`
})
wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
time: this.pushDay(this.data.today.getMonth() + 1)
})) : setTimeout(() => this.setData({
time: this.pushDay(this.data.today.getMonth() + 1)
}), 100)
}
}
},
/**
* 组件的初始数据
*/
data: {
up: false,
height: 0,
day: '',
time: [],
today: null,
cookie: null,
showRight: false,
stop: ''
},
/**
* 组件的方法列表
*/
methods: {
/**
* 禁止滑动
*/
noScroll() {},
/**
* 显示
*/
open() {
const query = wx.createSelectorQuery().in(this)
wx.canIUse('nextTick') ? wx.nextTick(() => this.setHeight(query)) : setTimeout(() => this.setHeight(query), 100)
if (this.data.today.format('yyyy-MM') != new Date().format('yyyy-MM')) this.setData({showRight: true})
let time = new Date()
time.setMonth(time.getMonth() - (this.data.count - 1))
this.setData({stop: time.format('yyyy-MM')})
},
/**
* 设置日期高度
*/
setHeight(el) {
el.select('.day').boundingClientRect(res => {
this.setData({
up: true,
height: res.width * 2 * 0.84
})
}).exec()
},
/**
* 关闭
*/
close() {
this.triggerEvent('close', false)
this.setData({up: false})
},
/**
* 选中日期
*/
chooseDay(e) {
let dataset = e.currentTarget.dataset
let index = dataset.index
let day = dataset.date.split('-')
let time = this.data.time
let cookie = new Date(day[0], day[1] - 1, day[2])
time.forEach(e => e.active = false)
time[index].active = true
this.setData({
time: time,
cookie: cookie
})
this.triggerEvent('choose',cookie.format('yyyy-MM-dd'))
this.close()
},
/**
* 上个月
*/
left() {
if (this.data.today.format('yyyy-MM') == this.data.stop) {
wx.showModal({
content: `只支持查看近${ this.data.count}个月的记录`,
showCancel:false,
confirmText: '知道了'
})
} else {
this.setData({
day: this.getLastMonth(this.data.today)
})
if (this.data.today.format('yyyy-MM') < new Date().format('yyyy-MM') && this.data.showRight == false) {
this.setData({
showRight: true
})
}
}
},
/**
* 下个月
*/
right() {
let today = this.data.today
let time = new Date()
if (today.format('yyyy-MM') < time.format('yyyy-MM')) {
this.setData({
day: this.getNextMonth(this.data.today),
})
if (this.data.today.format('yyyy-MM') == time.format('yyyy-MM')) this.setData({
showRight: false
})
}
},
/**
* 获取上个月
*/
getLastMonth(date) {
let year = date.getFullYear()
let month = date.getMonth()
if (month == 0) {
year -= 1
month = 12
}
this.setData({
today: new Date(year, month - 1),
})
wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
time: this.pushDay(month)
})) : setTimeout(() => this.setData({
time: this.pushDay(month)
}), 100)
let m = month
if (m < 10) m = '0' + m
return `${year}年\t${m}月`
},
/**
* 获取下个月
*/
getNextMonth(date) {
let year = date.getFullYear()
let month = date.getMonth() + 1
if (month == 12) {
year += 1
month = 0
}
this.setData({
today: new Date(year, month),
})
wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
time: this.pushDay(month + 1)
})) : setTimeout(() => this.setData({
time: this.pushDay(month + 1)
}), 100)
let m = month + 1
if (m < 10) m = '0' + m
return `${year}年\t${m}月`
},
/**
* 获取月份天数
*/
getDayNumber(year, time) {
let day = 0
if (time == 2) {
day = year % 4 == 0 ? 29 : 28
} else if (time == 4 || time == 6 || time == 9 || time == 11) {
day = 30
} else {
day = 31
}
return day
},
/**
* 填充数据
*/
pushDay(month, arr = [], day = new Date()) {
let last = null
let next = null
let today = this.data.today
let week = new Date(today.format('yyyy'), today.format('MM') - 1, 1).getDay()
last = today.format('MM') == 1 ? new Date(today.getFullYear() - 1, 11, 1) : new Date(today.getFullYear(), today.getMonth() - 1, 1)
next = today.format('MM') == 12 ? new Date(today.getFullYear() + 1, 0, 1) : new Date(today.getFullYear(), today.format('MM'), 1)
let lastNumber = this.getDayNumber(last, parseInt(last.format('MM')))
for (let i = 1; i <= this.getDayNumber(today, month); i++) {
let d = i
if (d < 10) d = '0' + d
arr.push({
value: i,
date: `${today.format('yyyy')}-${today.format('MM')}-${d}`,
active: false
})
}
if (today.format('yyyy-MM') != this.data.stop) {
for (let i = lastNumber; i > lastNumber - week; i--) {
let item = [{
value: i,
date: `${last.format('yyyy')}-${last.format('MM')}-${i}`,
active: false,
opacity: true
}]
arr = item.concat(arr)
}
}
let length = arr.length
let sum = 42
if (length < 35) sum = 35
for (let i = 1; i <= sum - length; i++) {
let d = i
if (d < 10) d = '0' + d
arr.push({
value: i,
date: `${next.format('yyyy')}-${next.format('MM')}-${d}`,
active: false,
opacity: true
})
}
if (this.data.cookie !== null) {
let cookie = this.data.cookie
arr.forEach((e, index) => {
if (e.date == cookie.format('yyyy-MM-dd')) {
arr[index].active = true
}
})
}
if (this.data.cookie === null) {
arr.forEach((e, index) => {
if (e.date == this.data.date) {
arr[index].active = true
}
})
}
const res = new Map()
let dateList = this.data.dateList
dateList = dateList.filter(a => !res.has(a) && res.set(a, 1))
arr.forEach((e, index) => {
dateList.forEach(i => {
if (e.date == i) arr[index].have = true
})
})
if (today.format('yyyy-MM') == new Date().format('yyyy-MM')) {
arr.forEach((e, index) => {
if (e.date > new Date().format('yyyy-MM-dd')) arr.splice(index, arr.length)
})
}
return arr
}
}
})