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
import { getInterTime } from '../../utils/utils';
const currentDate = new Date();
const weekList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const durationList = [{ value: 60, label: '1小时' }, { value: 120, label: '2小时' }, { value: 30, label: '30分钟' }];
const timeData = generateDate(365);
const timeHour = generateHour(24);
const timeMin = generateMin(60);
function padZero(val) {
return ("00" + val).slice(-2);
}
// 生成分钟 每隔五分钟
function generateMin(max) {
const time = [];
for (let i = 0; i < max; i += 5) {
time.push(padZero(i))
}
return time;
}
// 生成小时
function generateHour(max) {
const time = [];
for (let i = 0; i < max; i++) {
time.push(padZero(i))
}
return time;
}
// 生成年月日周
function generateDate(max) {
const timeData = [];
for (let i = max; i > 0; i--) {
timeData.push(GetDateStr(-i));
}
for (let i = 0; i < max; i++) {
timeData.push(GetDateStr(i));
}
return timeData
}
function GetDateStr(AddDayCount) {
let time = new Date();
time.setDate(time.getDate() + AddDayCount);
const res = getYMDWHMIN(time);
return res.y + '年' + res.m + "月" + res.d + "日 " + res.w;
}
// 获取年月日周时分秒
function getYMDWHMIN(date) {
const y = date.getFullYear();
const m = padZero(date.getMonth() + 1);
const d = padZero(date.getDate());
const w = weekList[date.getDay()];
const h = padZero(date.getHours());
const min = padZero(date.getMinutes());
const second = padZero(date.getSeconds())
return {
y, m, d, w, h, min, second
}
}
// 时间格式转化为03月01日 14:00
function getShowTime(date) {
const res = getYMDWHMIN(date);
return res.m + "月" + res.d + "日 " + res.h + ':' + res.min;
}
// 时间格式转化为2019/2/2 16:00
function getPostTimeByDate(date) {
const res = getYMDWHMIN(date);
return res.y + '/' + res.m + "/" + res.d + " " + res.h + ':' + res.min;
}
function getPickerValue(date) {
const res = getYMDWHMIN(date);
return [timeData.indexOf(res.y + "年" + res.m + "月" + res.d + "日 " + res.w), timeHour.indexOf(res.h), timeMin.indexOf(res.min)];
}
// 将周几转换为今天,明天, 昨天
function getWeekDay(date) {
let week = '今天';
//同年同月
if (currentDate.getFullYear() == date.getFullYear() &&
currentDate.getMonth() == date.getMonth()
) {
if (currentDate.getDate() == date.getDate()) {
week = '今天'
} else if (date.getDate() - currentDate.getDate() == 1) {
week = '明天'
} else if (currentDate.getDate() - date.getDate() == 1) {
week = '昨天'
} else {
week = weekList[date.getDay()]
}
} else {
week = weekList[date.getDay()]
}
return week;
}
// 根据开始时间和持续时间计算结束时间 2019/2/2 16:00
function getEndTimeByDuration(startDate, duration) {
const newDate = new Date(startDate); //新创建一个时间对象,以免改变startDate
const min = newDate.getMinutes();
newDate.setMinutes(min + duration);
const res = getYMDWHMIN(newDate);
return res.y + '/' + res.m + '/' + res.d + ' ' + res.h + ':' + res.min;
}
// 获取结束至时间的显示 16:00
function getDurationShowTime(startDate, duration) {
return getEndTimeByDuration(startDate, duration).slice(-5);
}
// 根据column值获取标准时间格式 2019/2/2 16:00
function getDateByPickValue(pickValue) {
const d = timeData[pickValue[0]];
const h = timeHour[pickValue[1]];
const m = timeMin[pickValue[2]];
const newD = d.slice(0, 10).replace('年', '/').replace('月', '/').replace('日', '/') + ' ' + h + ':' + m + ':00';
return new Date(newD)
}
// 获取持续时间 {value,lable,index}
function getDuration(startDate, endDate) {
const time = endDate.getTime() - startDate.getTime();
const min = time / 1000 / 60;
let duration = null;
durationList.forEach((it, index) => {
if (it.value == min) {
it.index = index;
duration = it
}
})
return duration
}
// 根据持续时间的显示值获取实际分钟数
function getDurationValue(durationLable) {
let value = 120;
durationList.forEach(it => {
if (it.label == durationLable) {
value = it.value
}
});
return value
}
Component({
mixins: [],
data: {
timeData: timeData,
timeHour: timeHour,
timeMin: timeMin,
durationList: durationList,
currentTab: 'start',
isEndTimeCustom: false,
pickValue: [],// 当前 pick column回显
error: '',
start: {
pickValue: [],
weekDay: '今天',
showTime: '',
},
end: {
pickValue: [],
weekDay: '今天',
showTime: '',
},
duration: {
pickValue: [2],
showTime: '',
label: '30分钟',
},
startPostTime: '',
endPostTime: '',
endDurationPostTime: ''
},
props: {
visibleItemCount: 5
},
didMount() {
this.setInitialValue();
},
didUpdate() {
},
didUnmount() { },
methods: {
//设置初始值
setInitialValue() {
let startDate = getInterTime(new Date());
//结束日期默认+ 半小时
let endDate = getInterTime(new Date());
const min = endDate.getMinutes();
endDate.setMinutes(min + 30);
// 赋值
if (this.props.startTime) {
startDate = new Date(this.props.startTime);
}
if (this.props.endTime) {
endDate = new Date(this.props.endTime);
}
console.log(this.props)
this.changeStart(startDate);
this.changeEnd(endDate);
this.setData({
pickValue: getPickerValue(startDate)
});
// 如果持续时间不在范围内,需要改变默认显示结束时间的方式
const durationData = getDuration(startDate, endDate);
if (durationData) {
this.changeDuration(startDate, durationData);
} else {
this.setData({
isEndTimeCustom: true
})
}
},
changeStart(startDate, pickValue) {
this.setData({
startPostTime: startDate,
start: {
pickValue: pickValue || getPickerValue(startDate),
showTime: getShowTime(startDate),
weekDay: getWeekDay(startDate)
}
})
},
changeEnd(endDate, pickValue) {
this.setData({
endPostTime: endDate,
end: {
pickValue: pickValue || getPickerValue(endDate),
showTime: getShowTime(endDate),
weekDay: getWeekDay(endDate)
}
})
},
changeDuration(startDate, durationData, pickValue) {
this.setData({
endDurationPostTime: getEndTimeByDuration(startDate, durationData.value),
duration: {
label: durationData.label,
showTime: getDurationShowTime(startDate, durationData.value),
pickValue: pickValue || [durationData.index]
}
})
},
onChange(e) {
const pickValue = e.detail.value;
//开始时间的分钟数必须是5的倍数才对的上
if (pickValue.includes(-1)) {
pickValue[pickValue.indexOf(-1)] = 0
}
if (this.data.currentTab === 'start') {
const startDate = getDateByPickValue(pickValue);
this.changeStart(startDate, pickValue);
// 如果是持续时间面板,改变持续时间的结束时间
const durationValue = getDurationValue(this.data.duration.label);
if (!this.data.isEndTimeCustom) {
this.setData({
'duration.showTime': getDurationShowTime(startDate, durationValue),
endDurationPostTime: getEndTimeByDuration(startDate, durationValue)
})
} else {
// 实时校验
this.checkValue(startDate, this.data.endPostTime);
}
}
// 改变结束时间,结束时间和持续时间互不影响
else if (this.data.currentTab === 'end') {
if (this.data.isEndTimeCustom) {
const endDate = getDateByPickValue(pickValue);
this.changeEnd(endDate, pickValue);
this.checkValue(this.data.startPostTime, endDate);
} else {
const durationData = durationList[pickValue[0]];
this.changeDuration(this.data.startPostTime, durationData, pickValue);
}
}
},
changeTab(e) {
const currentTab = e.currentTarget.id;
this.setData({
currentTab
})
//pick coulum 回显
if (currentTab === 'end' && !this.data.isEndTimeCustom) {
return this.setData({
pickValue: this.data.duration.pickValue
});
} else {
this.setData({
pickValue: this.data[`${currentTab}`].pickValue
});
}
},
changeWay() {
this.setData({
isEndTimeCustom: true
});
//如果在end tab 切换, 需要设置结束时间的pick column回显
if (this.data.currentTab == 'end') {
this.setData({
pickValue: this.data.end.pickValue
});
}
},
checkValue(startPostTime, endPostTime) {
if (endPostTime.getTime() <= startPostTime.getTime()) {
this.setData({
error: '结束时间小于开始时间'
})
return false;
} else {
this.setData({
error: ''
})
return true
}
},
complete() {
const { startPostTime, endPostTime, endDurationPostTime } = this.data;
if (this.data.isEndTimeCustom) {
if (this.checkValue(startPostTime, endPostTime)) {
this.props.onComplete({
startTime: getPostTimeByDate(startPostTime),
endTime: getPostTimeByDate(endPostTime)
})
}
} else {
this.props.onComplete({
startTime: getPostTimeByDate(startPostTime),
endTime: endDurationPostTime
})
}
}
},
});