index.js
10.4 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
Component({
properties: {
show: {
type: Boolean,
observer: function (n) {
if (n) {
this.initDate()
}
}
},
title: {
type: String
},
defaultTime: {
// 新增:传入默认时间
type: String,
value: ''
},
isStartTime: {
// 新增:标识是否是开始时间选择器
type: Boolean,
value: true
},
endTime: {
// 新增:结束时间(用于开始时间选择器验证)
type: String,
value: ''
},
startTime: {
// 新增:开始时间(用于结束时间选择器验证)
type: String,
value: ''
}
},
data: {
hours: [],
days: [],
minutes: [],
seconds: [],
dateValues: [],
hourValues: [],
minuteValues: [],
secondValues: [],
loading: true,
errorMsg: '' // 新增:错误提示信息
},
methods: {
noScroll() {},
hide(e) {
let { name } = e.currentTarget.dataset
let thisPage = wx.$router.getPageInstance()
thisPage.setData({
[name]: false
})
this.triggerEvent('close')
},
// 新增方法:解析默认时间
parseDefaultTime() {
const { defaultTime } = this.properties
if (!defaultTime) {
return this.getCurrentTimeData()
}
try {
const date = new Date(defaultTime.replace(/-/g, '/'))
if (isNaN(date.getTime())) {
return this.getCurrentTimeData()
}
return {
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds(),
dateStr: date.format('yyyy-MM-dd')?.replace(/\//g, '-')
}
} catch (error) {
return this.getCurrentTimeData()
}
},
// 新增方法:获取当前时间数据
getCurrentTimeData() {
const today = new Date()
return {
year: today.getFullYear(),
month: today.getMonth() + 1,
day: today.getDate(),
hour: today.getHours(),
minute: today.getMinutes(),
second: today.getSeconds(),
dateStr: today.format('yyyy-MM-dd')?.replace(/\//g, '-')
}
},
getDateList(starDay, endDay) {
const arr = []
const dates = []
const db = new Date(starDay)
const de = new Date(endDay)
const s = db.getTime() - 24 * 60 * 60 * 1000
const d = de.getTime() - 24 * 60 * 60 * 1000
for (let i = s; i <= d; ) {
i = i + 24 * 60 * 60 * 1000
arr.push(parseInt(String(i)))
}
for (const j in arr) {
const time = new Date(arr[j])
const year = time.getFullYear()
const mouth =
time.getMonth() + 1 >= 10
? time.getMonth() + 1
: '0' + (time.getMonth() + 1)
const day = time.getDate() >= 10 ? time.getDate() : '0' + time.getDate()
const YYMMDD = year + '-' + mouth + '-' + day
dates.push(YYMMDD)
}
this.getHourList(dates)
},
getHourList(days) {
const defaultTime = this.parseDefaultTime()
const today = new Date()
const hours = []
// 根据是否是当前日期决定小时范围
const isToday =
defaultTime.dateStr === today.format('yyyy-MM-dd')?.replace(/\//g, '-')
const hourLimit = isToday ? today.getHours() + 1 : 24
for (let hour = 0; hour < hourLimit; hour++) {
hours.push(hour.toString().padStart(2, '0'))
}
this.getMinuteList(days, hours, defaultTime)
},
getMinuteList(days, hours, defaultTime) {
const today = new Date()
const minutes = []
const isToday =
defaultTime.dateStr === today.format('yyyy-MM-dd')?.replace(/\//g, '-')
const isCurrentHour = defaultTime.hour === today.getHours()
const minuteLimit = isToday && isCurrentHour ? today.getMinutes() + 1 : 60
for (let minute = 0; minute < minuteLimit; minute++) {
minutes.push(minute.toString().padStart(2, '0'))
}
const seconds = ['00']
this.setDefaultValues(days, hours, minutes, seconds, defaultTime)
},
// 新增方法:设置默认选中值
setDefaultValues(days, hours, minutes, seconds, defaultTime) {
// 查找日期索引
const dateIndex = days.findIndex(day => day === defaultTime.dateStr)
const hourIndex = hours.findIndex(
hour => parseInt(hour) === defaultTime.hour
)
const minuteIndex = minutes.findIndex(
minute => parseInt(minute) === defaultTime.minute
)
const secondIndex = seconds.findIndex(
second => parseInt(second) === defaultTime.second
)
this.setData(
{
hours,
days,
minutes,
seconds,
dateValues: [dateIndex >= 0 ? dateIndex : days.length - 1],
hourValues: [hourIndex >= 0 ? hourIndex : hours.length - 1],
minuteValues: [minuteIndex >= 0 ? minuteIndex : minutes.length - 1],
secondValues: [secondIndex >= 0 ? secondIndex : seconds.length - 1],
loading: false
},
() => {
wx.nextTick(() => {})
setTimeout(() => {
// 设置当前选中时间
this.currentDate =
days[dateIndex >= 0 ? dateIndex : days.length - 1]
this.currentHour =
hours[hourIndex >= 0 ? hourIndex : hours.length - 1]
this.currentMinute =
minutes[minuteIndex >= 0 ? minuteIndex : minutes.length - 1]
this.currentSecond =
seconds[secondIndex >= 0 ? secondIndex : seconds.length - 1]
this.setData({
minuteValues: [
minuteIndex >= 0 ? minuteIndex : minutes.length - 1
],
secondValues: [
secondIndex >= 0 ? secondIndex : seconds.length - 1
],
loading: false
})
}, 0)
}
)
},
changeDate(e) {
const { days } = this.data
const { value } = e.detail
const index = value[0]
this.currentDate = days[index]
const today = new Date().format('yyyy-MM-dd')?.replace(/\//g, '-')
const timeType = this.currentDate != today ? 1 : 0
this.getAllTimeOrPartTime(timeType)
},
changeHour(e) {
const { hours } = this.data
const { value } = e.detail
const index = value[0]
this.currentHour = hours[index]
const today = new Date().format('yyyy-MM-dd')?.replace(/\//g, '-')
const timeType = this.currentDate != today ? 1 : 0
this.getAllTimeOrPartTimeMinute(timeType)
},
changeMinutes(e) {
const { minutes } = this.data
const { value } = e.detail
const index = value[0]
this.currentMinute = minutes[index]
},
changeSeconds(e) {
const { seconds } = this.data
const { value } = e.detail
const index = value[0]
this.currentSecond = seconds[index]
},
// 新增方法:验证时间有效性
validateTime() {
const { isStartTime, endTime, startTime } = this.properties
const selectedTimeStr = `${this.currentDate} ${this.currentHour}:${this.currentMinute}:00`
const selectedTime = new Date(
selectedTimeStr.replace(/-/g, '/')
).getTime()
let errorMsg = ''
if (isStartTime && endTime) {
// 开始时间不能晚于结束时间
const endTimeStamp = new Date(endTime.replace(/-/g, '/')).getTime()
if (selectedTime > endTimeStamp) {
errorMsg = '开始时间不能晚于结束时间'
}
} else if (!isStartTime && startTime) {
// 结束时间不能早于开始时间
const startTimeStamp = new Date(startTime.replace(/-/g, '/')).getTime()
if (selectedTime < startTimeStamp) {
errorMsg = '结束时间不能早于开始时间'
}
}
this.setData({ errorMsg })
return !errorMsg
},
confirm() {
// 最终确认前再次验证
if (!this.validateTime()) {
wx.showToast({
title: this.data.errorMsg,
icon: 'none'
})
return
}
const str = `${this.currentDate} ${this.currentHour}:${this.currentMinute}:00`
const result = new Date(str.replace(/-/g, '/'))
.format('yyyy-MM-dd hh:mm:ss')
?.replace(/\//g, '-')
this.triggerEvent('confirm', result)
this.setData({ errorMsg: '' })
},
cancel() {
this.triggerEvent('cancel')
},
initDate() {
this.setData({
dateValues: [],
hourValues: [],
minuteValues: [],
secondValues: [],
errorMsg: ''
})
const today = new Date()
const defaultTime = this.parseDefaultTime()
const todayEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate())
let endDay = todayEnd
if (defaultTime?.year && defaultTime?.month && defaultTime?.day) {
const defaultEnd = new Date(defaultTime.year, defaultTime.month - 1, defaultTime.day)
if (!isNaN(defaultEnd.getTime()) && defaultEnd.getTime() > todayEnd.getTime()) {
endDay = defaultEnd
}
}
this.getDateList(
'2019/01/01',
endDay
)
},
getAllTimeOrPartTime(type) {
const today = new Date()
const hours = [],
minutes = []
const hour_len = type == 1 ? 24 : today.getHours() + 1
const minute_len = type == 1 ? 60 : today.getMinutes() + 1
const second_len = 0
for (let hour = 0; hour < hour_len; hour++) {
hours.push(hour.toString().padStart(2, '0'))
}
for (let minute = 0; minute < minute_len; minute++) {
minutes.push(minute.toString().padStart(2, '0'))
}
this.setData({ hours, minutes, seconds: ['00'], secondValues: [0] })
},
getAllTimeOrPartTimeMinute(type) {
const today = new Date()
const currentHour = today.getHours()
const timeType = this.currentHour != currentHour ? 1 : 0
const minutes = []
const minute_len =
type == 1 || timeType == 1 ? 60 : today.getMinutes() + 1
const second_len = 0
for (let minute = 0; minute < minute_len; minute++) {
minutes.push(minute.toString().padStart(2, '0'))
}
this.setData({ minutes, seconds: ['00'], secondValues: [0] })
},
updateSecondsByMinute() {
this.setData({ seconds: ['00'], secondValues: [0] })
}
},
lifetimes: {
attached: function () {
this.initDate()
}
}
})