|
@@ -0,0 +1,387 @@
|
|
1
|
+<template>
|
|
2
|
+ <view class="rpickerBox">
|
|
3
|
+ <view :class="{'pickerMask':showPicker}" @click="maskClick" @touchmove.stop.prevent="returnHandle">
|
|
4
|
+ <view class="r-dtpicker" :class="{'r-dtpicker-show':showPicker}">
|
|
5
|
+ <view class="rdtBtn" @touchmove.stop.prevent="returnHandle" @tap.stop="returnHandle">
|
|
6
|
+ <view @click="pickerCancel">取消</view>
|
|
7
|
+ <view :style="{color:themeColor}" @click="pickerConfirm">确定</view>
|
|
8
|
+ </view>
|
|
9
|
+ <view class="rangeBox" @touchmove.stop.prevent="returnHandle" @tap.stop="returnHandle">
|
|
10
|
+ <view :class="['inputView',{'inputEmpty':!startDate}]" :style="{color:themeColor,'border-color':themeColor,opacity:dateType=='startDate'?1:.5}" @tap="changeDateType('startDate')">{{startDate||'开始时间'}}</view>
|
|
11
|
+ 至
|
|
12
|
+ <view :class="['inputView',{'inputEmpty':!endDate}]" :style="{color:themeColor,'border-color':themeColor,opacity:dateType=='endDate'?1:.5}" @tap="changeDateType('endDate')">{{endDate||'结束时间'}}</view>
|
|
13
|
+ </view>
|
|
14
|
+ <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChangeMul">
|
|
15
|
+ <picker-view-column>
|
|
16
|
+ <view class="picker-item" v-for="(item,index) in yearArr" :key="index">{{item}}</view>
|
|
17
|
+ </picker-view-column>
|
|
18
|
+ <picker-view-column v-if="fields!='year'">
|
|
19
|
+ <view class="picker-item" v-for="(item,index) in monthArr" :key="index">{{item}}</view>
|
|
20
|
+ </picker-view-column>
|
|
21
|
+ <picker-view-column v-if="fields=='day'">
|
|
22
|
+ <view class="picker-item" v-for="(item,index) in dayArr" :key="index">{{item}}</view>
|
|
23
|
+ </picker-view-column>
|
|
24
|
+ </picker-view>
|
|
25
|
+ </view>
|
|
26
|
+ </view>
|
|
27
|
+ </view>
|
|
28
|
+</template>
|
|
29
|
+
|
|
30
|
+<script>
|
|
31
|
+ export default {
|
|
32
|
+ name: 'range-dtpicker',
|
|
33
|
+ props: {
|
|
34
|
+ //粒度
|
|
35
|
+ fields:{
|
|
36
|
+ type: String,
|
|
37
|
+ default: 'day'
|
|
38
|
+ },
|
|
39
|
+ /**
|
|
40
|
+ * picker允许选中的最小值
|
|
41
|
+ */
|
|
42
|
+ start: {
|
|
43
|
+ type: String,
|
|
44
|
+ default: '1900-01-01'
|
|
45
|
+ },
|
|
46
|
+ /**
|
|
47
|
+ * picker允许选中的最大值
|
|
48
|
+ */
|
|
49
|
+ end: {
|
|
50
|
+ type: String,
|
|
51
|
+ default: '2200-12-01'
|
|
52
|
+ },
|
|
53
|
+ /**
|
|
54
|
+ * picker默认展示的值
|
|
55
|
+ */
|
|
56
|
+ value: {
|
|
57
|
+ type: Array,
|
|
58
|
+ default(){
|
|
59
|
+ return [0,0]
|
|
60
|
+ }
|
|
61
|
+ },
|
|
62
|
+ //是否显示
|
|
63
|
+ show: {
|
|
64
|
+ type: Boolean,
|
|
65
|
+ default: false
|
|
66
|
+ },
|
|
67
|
+ /**
|
|
68
|
+ 主题色
|
|
69
|
+ */
|
|
70
|
+ themeColor:{
|
|
71
|
+ type: String,
|
|
72
|
+ default: '#4C83D6'
|
|
73
|
+ }
|
|
74
|
+ },
|
|
75
|
+ created() {
|
|
76
|
+ this.init()
|
|
77
|
+ },
|
|
78
|
+ data() {
|
|
79
|
+ return {
|
|
80
|
+ showPicker: this.show,
|
|
81
|
+ dayArr:[],
|
|
82
|
+ pickerValue:this.fields=='year'?[0]:this.fields=='month'?[0,0]:[0,0,0],
|
|
83
|
+ dateType:"startDate",
|
|
84
|
+ startDate:'',
|
|
85
|
+ endDate:""
|
|
86
|
+ };
|
|
87
|
+ },
|
|
88
|
+ watch: {
|
|
89
|
+ value(value){
|
|
90
|
+ this.value=value;
|
|
91
|
+ this.init()
|
|
92
|
+ },
|
|
93
|
+ show(isShow) {
|
|
94
|
+ this.showPicker = isShow;
|
|
95
|
+ },
|
|
96
|
+ start(){
|
|
97
|
+ this.init()
|
|
98
|
+ },
|
|
99
|
+ end(){
|
|
100
|
+ this.init()
|
|
101
|
+ }
|
|
102
|
+ },
|
|
103
|
+ computed: {
|
|
104
|
+ yearArr(){
|
|
105
|
+ var arr = [],start = parseInt(this.start.slice(0,4)),end=parseInt(this.end.slice(0,4));
|
|
106
|
+ for(var i=0;i<=end-start;i++){
|
|
107
|
+ arr.push(start+i)
|
|
108
|
+ }
|
|
109
|
+ return arr;
|
|
110
|
+ },
|
|
111
|
+ monthArr(){
|
|
112
|
+ var arr = [];
|
|
113
|
+ for(var i=1;i<=12;i++){
|
|
114
|
+ var v = i;
|
|
115
|
+ if(v<10)v="0"+v;
|
|
116
|
+ if(this.start.length>4&&this.end.length>4&&(this.start.slice(0,4)==this.end.slice(0,4))){
|
|
117
|
+ if(parseInt(v)>=this.start.slice(5,7)&&parseInt(v)<=this.end.slice(5,7)){
|
|
118
|
+ arr.push(v.toString())
|
|
119
|
+ }
|
|
120
|
+ }else{
|
|
121
|
+ arr.push(v.toString())
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ return arr;
|
|
125
|
+ }
|
|
126
|
+ },
|
|
127
|
+ methods:{
|
|
128
|
+ returnHandle(){},
|
|
129
|
+ init(){
|
|
130
|
+ var that = this,pickerValue="";
|
|
131
|
+ if((this.fields=='year'&&this.start.length!=4)||(this.fields=='month'&&this.start.length!=7)||(this.fields=='day'&&this.start.length!=10)){
|
|
132
|
+ console.error("最小值格式与粒度格式不符");return;
|
|
133
|
+ }else if((this.fields=='year'&&this.end.length!=4)||(this.fields=='month'&&this.end.length!=7)||(this.fields=='day'&&this.end.length!=10)){
|
|
134
|
+ console.error("最大值格式与粒度格式不符");return;
|
|
135
|
+ }
|
|
136
|
+ var start=this.fields=='year'?this.start.slice(0,4):this.fields=='month'?this.start.slice(0,7):this.start,
|
|
137
|
+ end=this.fields=='year'?this.end.slice(0,4):this.fields=='month'?this.end.slice(0,7):this.end;
|
|
138
|
+ if(!start||!end){
|
|
139
|
+ console.error("时间不能为空");return;
|
|
140
|
+ }else if(start>end){
|
|
141
|
+ console.error("结束时间必须大等于开始时间");return;
|
|
142
|
+ }
|
|
143
|
+ if(this.value[0]){
|
|
144
|
+ if((this.fields=='year'&&this.value[0].length!=4)||(this.fields=='month'&&this.value[0].length!=7)||(this.fields=='day'&&this.value[0].length!=10)){
|
|
145
|
+ console.error("默认值格式与粒度格式不符");return;
|
|
146
|
+ }
|
|
147
|
+ this.startDate=this.value[0];
|
|
148
|
+ if(this.value[1]){
|
|
149
|
+ if((this.fields=='year'&&this.value[1].length!=4)||(this.fields=='month'&&this.value[1].length!=7)||(this.fields=='day'&&this.value[1].length!=10)){
|
|
150
|
+ console.error("默认值格式与粒度格式不符");return;
|
|
151
|
+ }
|
|
152
|
+ this.endDate=this.value[1];
|
|
153
|
+ this.dateType="endDate";
|
|
154
|
+ if(this.fields=='day')this.dayArr=this.getMonthDay(this.value[1].slice(0,4),this.value[1].slice(5,7));
|
|
155
|
+ pickerValue=this.getIndex(this.value[1]);
|
|
156
|
+ }else{
|
|
157
|
+ this.dateType="startDate";
|
|
158
|
+ if(this.fields=='day')this.dayArr=this.getMonthDay(this.value[0].slice(0,4),this.value[0].slice(5,7));
|
|
159
|
+ pickerValue=this.getIndex(this.value[0]);
|
|
160
|
+ }
|
|
161
|
+ }else{
|
|
162
|
+ this.startDate=start;
|
|
163
|
+ pickerValue=this.getIndex(start);
|
|
164
|
+ if(this.fields=='day')this.dayArr=this.getMonthDay(start.slice(0,4),start.slice(5,7));
|
|
165
|
+ }
|
|
166
|
+ if(pickerValue)setTimeout(function(){that.pickerValue=pickerValue},20)
|
|
167
|
+ },
|
|
168
|
+ maskClick(){
|
|
169
|
+ this.$emit("showchange",false);
|
|
170
|
+ },
|
|
171
|
+ pickerConfirm(){
|
|
172
|
+ if(this.endDate<this.startDate){
|
|
173
|
+ uni.showToast({
|
|
174
|
+ title:"结束时间不得小于开始时间",
|
|
175
|
+ icon:"none",
|
|
176
|
+ mask:true
|
|
177
|
+ })
|
|
178
|
+ return;
|
|
179
|
+ }
|
|
180
|
+ this.$emit("change",[this.startDate,this.endDate]);
|
|
181
|
+ this.$emit("showchange",false);
|
|
182
|
+ },
|
|
183
|
+ pickerCancel(){
|
|
184
|
+ this.$emit("cancel");
|
|
185
|
+ this.$emit("showchange",false);
|
|
186
|
+ },
|
|
187
|
+ changeDateType(dateType){
|
|
188
|
+ var that = this;
|
|
189
|
+ this.dateType=dateType;
|
|
190
|
+ if(this[dateType]){
|
|
191
|
+ this.pickerValue=this.getIndex(this[dateType])
|
|
192
|
+ }else{
|
|
193
|
+ var dateTxt=this.fields=='year'?this.yearArr[this.pickerValue[0]]:this.fields=='month'?this.yearArr[this.pickerValue[0]]+'-'+this.monthArr[this.pickerValue[1]]:this.yearArr[this.pickerValue[0]]+'-'+this.monthArr[this.pickerValue[1]]+'-'+this.dayArr[this.pickerValue[2]];
|
|
194
|
+ this[dateType]=dateTxt;
|
|
195
|
+ this.pickerValue=this.fields=='year'?[this.pickerValue[0]]:this.fields=='month'?[this.pickerValue[0],this.pickerValue[1]]:[this.pickerValue[0],this.pickerValue[1],this.pickerValue[2]];
|
|
196
|
+ }
|
|
197
|
+ },
|
|
198
|
+ pickerChangeMul(e){
|
|
199
|
+ var that=this,val = e.detail.value,dateTxt="";
|
|
200
|
+ if(this.fields=='day'&&(val[0]!=this.pickerValue[0]||val[1]!=this.pickerValue[1])){
|
|
201
|
+ this.dayArr=this.getMonthDay(this.yearArr[val[0]],this.monthArr[val[1]])
|
|
202
|
+ function returnMax(){
|
|
203
|
+ if(!that.dayArr[val[2]]){
|
|
204
|
+ val[2]=(val[2]-1)
|
|
205
|
+ returnMax()
|
|
206
|
+ }
|
|
207
|
+ }
|
|
208
|
+ returnMax()
|
|
209
|
+ }
|
|
210
|
+ dateTxt=this.yearArr[val[0]]+'-'+this.monthArr[val[1]]+'-'+this.dayArr[val[2]];
|
|
211
|
+ this[this.dateType]=this.fields=='year'?dateTxt.slice(0,4):this.fields=='month'?dateTxt.slice(0,7):dateTxt;
|
|
212
|
+ this.pickerValue=this.getIndex(this[this.dateType]);
|
|
213
|
+ },
|
|
214
|
+ getIndex(value){
|
|
215
|
+ var year = value.slice(0,4),month=value.slice(5,7),day=value.slice(8,10),y=0,m=0,d=0;
|
|
216
|
+ for(var i in this.yearArr){
|
|
217
|
+ if(year==this.yearArr[i]){
|
|
218
|
+ y=i;break;
|
|
219
|
+ }
|
|
220
|
+ }
|
|
221
|
+ for(var i in this.monthArr){
|
|
222
|
+ if(month==this.monthArr[i]){
|
|
223
|
+ m=i;break;
|
|
224
|
+ }
|
|
225
|
+ }
|
|
226
|
+ for(var i in this.dayArr){
|
|
227
|
+ if(day==this.dayArr[i]){
|
|
228
|
+ d=i;break;
|
|
229
|
+ }
|
|
230
|
+ }
|
|
231
|
+ var value = [];
|
|
232
|
+ switch (this.fields){
|
|
233
|
+ case 'year':value = [Number(y)]
|
|
234
|
+ break;
|
|
235
|
+ case 'month':value = [Number(y),Number(m)]
|
|
236
|
+ break;
|
|
237
|
+ default:value = [Number(y),Number(m),Number(d)]
|
|
238
|
+ break;
|
|
239
|
+ }
|
|
240
|
+ return value;
|
|
241
|
+ },
|
|
242
|
+ withData: (num) => {
|
|
243
|
+ let param = parseInt(num);
|
|
244
|
+ return param < 10 ? '0' + param : '' + param;
|
|
245
|
+ },
|
|
246
|
+ getLoopArray(start, end,specialDay){
|
|
247
|
+ var start = start || 0;
|
|
248
|
+ var end = end || 1;
|
|
249
|
+ var array = [];
|
|
250
|
+ if(specialDay){
|
|
251
|
+ array=specialDay;
|
|
252
|
+ return array;
|
|
253
|
+ }
|
|
254
|
+ for (var i = start; i <= end; i++) {
|
|
255
|
+ array.push(this.withData(i));
|
|
256
|
+ }
|
|
257
|
+ return array;
|
|
258
|
+ },
|
|
259
|
+ getMonthDay(year, month,specialDay){
|
|
260
|
+ var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;
|
|
261
|
+
|
|
262
|
+ switch (month) {
|
|
263
|
+ case '01':
|
|
264
|
+ case '03':
|
|
265
|
+ case '05':
|
|
266
|
+ case '07':
|
|
267
|
+ case '08':
|
|
268
|
+ case '10':
|
|
269
|
+ case '12':
|
|
270
|
+ array = this.getLoopArray(1, 31,specialDay)
|
|
271
|
+ break;
|
|
272
|
+ case '04':
|
|
273
|
+ case '06':
|
|
274
|
+ case '09':
|
|
275
|
+ case '11':
|
|
276
|
+ array = this.getLoopArray(1, 30,specialDay)
|
|
277
|
+ break;
|
|
278
|
+ case '02':
|
|
279
|
+ array = flag ? this.getLoopArray(1, 29,specialDay) : this.getLoopArray(1, 28,specialDay)
|
|
280
|
+ break;
|
|
281
|
+ default:
|
|
282
|
+ array = '月份格式不正确,请重新输入!'
|
|
283
|
+ }
|
|
284
|
+ return array;
|
|
285
|
+ }
|
|
286
|
+ }
|
|
287
|
+ }
|
|
288
|
+</script>
|
|
289
|
+
|
|
290
|
+<style>
|
|
291
|
+ .pickerMask {
|
|
292
|
+ position: fixed;
|
|
293
|
+ z-index: 998;
|
|
294
|
+ top: 0;
|
|
295
|
+ right: 0;
|
|
296
|
+ left: 0;
|
|
297
|
+ bottom: 0;
|
|
298
|
+ background: rgba(0, 0, 0, 0.6);
|
|
299
|
+ }
|
|
300
|
+
|
|
301
|
+ .r-dtpicker {
|
|
302
|
+ position: fixed;
|
|
303
|
+ bottom: 0;
|
|
304
|
+ left: 0;
|
|
305
|
+ width: 100%;
|
|
306
|
+ transition: all 0.3s ease;
|
|
307
|
+ transform: translateY(100%);
|
|
308
|
+ z-index: 998;
|
|
309
|
+ }
|
|
310
|
+
|
|
311
|
+ .r-dtpicker-show {
|
|
312
|
+ transform: translateY(0);
|
|
313
|
+ }
|
|
314
|
+
|
|
315
|
+ .rdtBtn {
|
|
316
|
+ display: flex;
|
|
317
|
+ padding: 9px 15px;
|
|
318
|
+ background-color: #fff;
|
|
319
|
+ position: relative;
|
|
320
|
+ text-align: center;
|
|
321
|
+ font-size: 17px;
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+ .rdtBtn:after {
|
|
325
|
+ content: ' ';
|
|
326
|
+ position: absolute;
|
|
327
|
+ left: 0;
|
|
328
|
+ bottom: 0;
|
|
329
|
+ right: 0;
|
|
330
|
+ height: 1px;
|
|
331
|
+ border-bottom: 1px solid #e5e5e5;
|
|
332
|
+ color: #e5e5e5;
|
|
333
|
+ transform-origin: 0 100%;
|
|
334
|
+ transform: scaleY(0.5);
|
|
335
|
+ }
|
|
336
|
+
|
|
337
|
+ .rdtBtn view{
|
|
338
|
+ display: block;
|
|
339
|
+ flex: 1;
|
|
340
|
+ color: #1aad19;
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+ .rdtBtn view:first-child {
|
|
344
|
+ text-align: left;
|
|
345
|
+ color: #888;
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ .rdtBtn view:last-child {
|
|
349
|
+ text-align: right;
|
|
350
|
+ }
|
|
351
|
+
|
|
352
|
+ .picker-item {
|
|
353
|
+ text-align: center;
|
|
354
|
+ line-height: 40px;
|
|
355
|
+ font-size: 16px;
|
|
356
|
+ }
|
|
357
|
+
|
|
358
|
+ .mpvue-picker-view {
|
|
359
|
+ position: relative;
|
|
360
|
+ bottom: 0;
|
|
361
|
+ left: 0;
|
|
362
|
+ width: 100%;
|
|
363
|
+ height: 238px;
|
|
364
|
+ background-color: rgba(255, 255, 255, 1);
|
|
365
|
+ }
|
|
366
|
+ .rangeBox{
|
|
367
|
+ background: #fff;
|
|
368
|
+ display: flex;
|
|
369
|
+ justify-content: center;
|
|
370
|
+ padding: 15px 0;
|
|
371
|
+ font-size: 16px;
|
|
372
|
+ align-items: center;
|
|
373
|
+ }
|
|
374
|
+ .rangeBox .inputView{
|
|
375
|
+ white-space: nowrap;
|
|
376
|
+ min-width: 196upx;
|
|
377
|
+ padding: 8rpx;
|
|
378
|
+ margin: 0 10px;
|
|
379
|
+ text-align: center;
|
|
380
|
+ align-items: center;
|
|
381
|
+ min-height: auto;
|
|
382
|
+ border-bottom: 1px solid #000;
|
|
383
|
+ }
|
|
384
|
+ .rangeBox .inputView.inputEmpty{
|
|
385
|
+ color: #999!important;
|
|
386
|
+ }
|
|
387
|
+</style>
|