picker_datetime.js
8.5 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
var pickerDatetime = function(obj) {
//初始化参数
var _this = this;
this.page = obj.page;
this.height = obj.height != undefined ? obj.height : 600;
this.success = obj.success != undefined ? obj.success : function(){};
var pickerToolHeight = 100;
this.page.setData({pickerViewHeight:this.height - pickerToolHeight});
//是否有动画效果,设置属性,按钮
this.pickerTimeout = null;
if (obj.animation == 'slide' || obj.animation == 'fade') {
this.animationType = 'slide';
this.duration = obj.duration != undefined ? obj.duration : 500;
this.TimingFunction = obj.timingFunction != undefined ? obj.timingFunction : 'linear';
this.thisDelay = obj.delay != undefined ? obj.delay : 0;
this.animation = obj.animation;
this.page.pickerClear = function(){
var newObj = {};
newObj[_this.pickerName] = '';
newObj['pickerAnimation'] = _this.pickerHideAnimation.export();
clearTimeout(_this.pickerTimeout);
_this.pickerTimeout = setTimeout(function() {
_this.page.setData({pickerViewShow:false});
},_this.duration);
this.setData(newObj);
};
this.page.pickerOk = function(){
var newObj = {};
newObj['pickerAnimation'] = _this.pickerHideAnimation.export();
this.setData(newObj);
clearTimeout(_this.pickerTimeout);
_this.pickerTimeout = setTimeout(function() {
_this.page.setData({pickerViewShow:false});
},_this.duration);
_this.success();
};
} else {
this.animation = '';
this.page.pickerClear = function(){
var newObj = {};
newObj[_this.pickerName] = '';
newObj['pickerViewShow'] = false;
this.setData(newObj);
};
this.page.pickerOk = function(){
this.setData({pickerViewShow:false});
_this.success();
};
}
};
//启用时间选择器的方法
pickerDatetime.prototype.setPicker = function(pickerName) {
var _this = this;
var pickerPreFlag = false;
if (this.page.data[pickerName] == undefined || this.page.data[pickerName] == '') {
//未有原先值,不处理
var pickerPreDate = new Date();
var pickerPreYear = pickerPreDate.getFullYear();
var pickerPreMonth = pickerPreDate.getMonth()+1;
var pickerPreDay = pickerPreDate.getDate();
var pickerPreHour = pickerPreDate.getHours();
var pickerPreMinute = pickerPreDate.getMinutes();
var pickerPreSecond = pickerPreDate.getSeconds();
var obj = new Object();
obj[pickerName] = pickerPreYear + '-' + pickerPreMonth + '-' + pickerPreDay + ' ' + addDatetimeZero(pickerPreHour) + ':' + addDatetimeZero(pickerPreMinute) + ':' + addDatetimeZero(pickerPreSecond);
this.page.setData(obj);
} else {
var pickerPreDate = new Date(Date.parse(this.page.data[pickerName].replace('-','/').replace('-','/').replace(' ','/')));
var pickerPreYear = pickerPreDate.getFullYear();
var pickerPreMonth = pickerPreDate.getMonth()+1;
var pickerPreDay = pickerPreDate.getDate();
var pickerPreHour = pickerPreDate.getHours();
var pickerPreMinute = pickerPreDate.getMinutes();
var pickerPreSecond = pickerPreDate.getSeconds();
}
this.pickerName = pickerName;
this.pickerDateTextArr = [];
this.pickerDateValueArr = [];
this.pickerHourTextArr = [];
this.pickerHourValueArr = [];
this.pickerMinuteTextArr = [];
this.pickerMinuteValueArr = [];
this.pickerSecondTextArr = [];
this.pickerSecondValueArr = [];
var pickerNowDate = new Date();
var pickerNowYear = pickerNowDate.getFullYear();
var pickerNowHour = pickerNowDate.getHours();
var pickerNowMinute = pickerNowDate.getMinutes();
var pickerNowSecond = pickerNowDate.getSeconds();
var pickerOldYearDate = new Date (pickerNowYear - 1,2,0);
var pickerOldYearDayNum = 365;
if (pickerOldYearDate.getDate() == 29 ) pickerOldYearDayNum=366;
var pickerNowMonth = pickerNowDate.getMonth() + 1;
var pickerNowDay = pickerNowDate.getDate();
// var pickerWeekArr = ["日","一","二","三","四","五","六"];
var pickerDateIndex=0;
var pickerHourIndex=0;
var pickerMinuteIndex=0;
var pickerSecondIndex = 0;
//获取年月日选择数组
for (let y = pickerNowYear - 1; y <= pickerNowYear + 1; y++) {
for (let m = 1; m <= 12; m++) {
let newDate = new Date(y, m, 0);
let m_days = newDate.getDate();
for (let d = 1; d <= m_days; d++) {
let newDate = new Date(y, m - 1, d);
// let w = pickerWeekArr[newDate.getDay()];
if (d == pickerPreDay && m == pickerPreMonth && y == pickerPreYear) {
pickerDateIndex = parseInt((new Date(y + '/' + m + '/' + d) - new Date((pickerNowYear - 1) + '/1/1')) / 1000 / 60 / 60 / 24);
}
if (d == pickerNowDay && m == pickerNowMonth && y == pickerNowYear) {
this.pickerDateTextArr.push('今日');
} else {
// this.pickerDateTextArr.push(m+'月'+d+'日 星期' + w);
this.pickerDateTextArr.push(y + '-' + m + '-' + d);
}
// this.pickerDateValueArr.push(y+'年'+m+'月'+d+'日');
this.pickerDateValueArr.push(y + '-' + m + '-' + d);
}
}
}
//获取小时和分钟的数组,设置默认值
if (pickerPreFlag) {
for (let i=pickerNowHour;i < 24; i++ ) {
this.pickerHourValueArr.push(i);
this.pickerHourTextArr.push(addDatetimeZero(i));
if (pickerPreHour == i) pickerHourIndex=i;
}
for (let i = pickerNowMinute;i < 60; i++ ) {
this.pickerMinuteValueArr.push(i);
this.pickerMinuteTextArr.push(addDatetimeZero(i));
if (pickerPreMinute == i) pickerMinuteIndex = i;
}
for (let i = pickerNowSecond; i < 60; i++) {
this.pickerSecondValueArr.push(i);
this.pickerSecondTextArr.push(addDatetimeZero(i));
if (pickerPreSecond == i) pickerSecondIndex = i;
}
} else {
for (let i=0;i < 24; i++ ) {
this.pickerHourValueArr.push(i);
this.pickerHourTextArr.push(addDatetimeZero(i));
if (pickerNowHour == i) pickerHourIndex=i;
}
for (let i=0;i < 60; i++ ) {
this.pickerMinuteValueArr.push(i);
this.pickerMinuteTextArr.push(addDatetimeZero(i));
if (pickerNowMinute == i) pickerMinuteIndex = i;
}
for (let i = 0; i < 60; i++) {
this.pickerSecondValueArr.push(i);
this.pickerSecondTextArr.push(addDatetimeZero(i));
if (pickerPreSecond == i) pickerSecondIndex = i;
}
}
//setData调用页面的选择器值、默认值
var newObj = {};
newObj['pickerDateTextArr'] = this.pickerDateTextArr;
newObj['pickerHourTextArr'] = this.pickerHourTextArr;
newObj['pickerMinuteTextArr'] = this.pickerMinuteTextArr;
newObj['pickerSecondTextArr'] = this.pickerSecondTextArr;
newObj['pickDatetimeValue'] = [pickerDateIndex, pickerHourIndex, pickerMinuteIndex, pickerSecondIndex];
this.page.setData(newObj);
console.log(newObj['pickerDateTextArr'])
if (this.animation == '') {
//无动画
this.page.setData({pickerViewShow:true});
} else {
//创建动画(由于只创建一个调用测试有问题,这里创建两个各自使用)
var pickerShowAnimationType = wx.createAnimation({
duration: _this.duration,
timingFunction: _this.thisTimingFunction,
delay: _this.thisDelay,
transformOrigin: '50% 50% 0',
success: function(res) {
}
});
var pickerHideAnimationType = wx.createAnimation({
duration: _this.duration,
timingFunction: _this.TimingFunction,
delay: _this.Delay,
transformOrigin: '50% 50% 0',
success: function(res) {
}
});
clearTimeout(this.pickerTimeout);
if (this.animation == 'slide') {
this.pickerShowAnimation = pickerShowAnimationType.height(this.height + 'rpx').step();
this.pickerHideAnimation = pickerHideAnimationType.height(0).step();
this.page.setData({pickerViewShow:true,pickerViewStyle:'height:0;'});
} else {
this.pickerShowAnimation = pickerShowAnimationType.opacity(1).step();
this.pickerHideAnimation = pickerHideAnimationType.opacity(0).step();
this.page.setData({pickerViewShow:true,pickerViewStyle:'opacity:0;'});
}
this.page.setData({
pickerAnimation:_this.pickerShowAnimation.export()
})
}
//设置bindChange事件
this.page.bindChange = function(e) {
var val = e.detail.value
var newObj = {};
newObj[pickerName] = _this.pickerDateValueArr[val[0]] + ' ' + _this.pickerHourTextArr[val[1]] + ':' + _this.pickerMinuteTextArr[val[2]] + ':' + _this.pickerSecondTextArr[val[3]];
this.setData(newObj);
};
};
module.exports = {
pickerDatetime : pickerDatetime,
}
function addDatetimeZero(num){
return new Array(2 - num.toString().length + 1).join("0") + num;
}