timepicker.js
3.0 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
// component/time-picker/index.js
let utils=require("../../utils/util.js")
const app = getApp()
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
deliveryTime: "",
selfData: [], //自提日期
showDelivery: false,
deliveryTimeList: [{
day: "",
timeList: ""
}],
nowDay: 0,
selectTime: '',
selectTimeIndex: '',
selectDay: 0
},
pageLifetimes: {
show: function() {
this.getDliveryTime()
}
},
/**
* 组件的方法列表
*/
methods: {
getnowDay(e) {
this.setData({
nowDay: e.currentTarget.dataset.index
})
},
clickTime(e) {
let selfData = this.data.selfData[this.data.nowDay]
let index = e.currentTarget.dataset.index
let selectTime = this.data.deliveryTimeList[this.data.nowDay].timeList[index]
let deliveryTime = selfData.year + '-' + selfData.month + '-' + selfData.day + ' ' + selectTime
let nowDay = this.data.nowDay
this.setData({
selectTimeIndex: index,
selectTime: selectTime,
deliveryTime: deliveryTime,
selectDay: nowDay
})
this.triggerEvent('selectTime', {
deliveryTime: this.data.deliveryTime
})
},
//计算配送时间
getDliveryTime() {
let deliveryTimeList = [];
let date = new Date();
this.setData({
selfData: utils.getDates(5)
// selfData: utils.getDates(5)
})
//获取时间段
let timeDate = new Date(date.getTime());
let todayList = getTimeList(timeDate.getHours(), 1);
deliveryTimeList.push({
timeList: todayList
});
let nextDayList = getTimeList(9, 0);
for (let i = 1; i < 5; i++) {
deliveryTimeList.push({
timeList: nextDayList
});
}
this.triggerEvent('selectTime', {
deliveryTime: this.data.deliveryTime
})
this.setData({
deliveryTimeList: []
})
this.setData({
deliveryTimeList: deliveryTimeList
})
// today 1 是今天 0 不是今天
function getTimeList(hour, today) {
let timeList = [];
let startTime = hour <= 9 ? 9 : hour;
for (let i = 0; i < 8; i++) {
if (((hour <= 9) && (startTime + 2 * i >21)) || ((hour > 9) && (startTime + 2 * i >= 21))) {
break;
} else {
if (hour <= 9) {
timeList.push(
startTime + 2 * i + ":00" + "-" + (startTime + 2 * (i + 1)) + ":00"
);
} else {
if (startTime % 2 === 0) {
timeList.push(
startTime + 2 * i + 1 + ":00" + "-" + (startTime + 2 * i + 3) + ":00"
);
} else {
console.log(i,'i')
timeList.push(
startTime + 2 * i +2 + ":00" + "-" + (startTime + 2 * i + 4) + ":00"
);
}
}
}
}
return timeList;
}
}
}
})