zhaojs
2023-07-31 8dcfae4fff98fbbb19ff46bd9750f25bc56801a9
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<template>
    <view>
        <uni-popup ref="popup">
            <view class="popup-box">
                <view class="title">
                    <view class="tabs-box" v-if="mode=='all'">
                        <view v-for="(item,index) in tabs" :key="index" class="tabs-item" @click="tabChange(index)"
                            :class="{'tabs-check':index==tabIndex}">
                            <text class="item-text">{{item}}</text>
                        </view>
                    </view>
                    <view class="title-text" v-else>
                        {{mode=='multiple'?'日期筛选':'月份选择'}}
                    </view>
                    <uni-icons type="closeempty" class="close" size="16" @click="close"></uni-icons>
                </view>
 
 
                <view class="options-box" v-if="tabIndex">
                    <view class="flex row-between">
                        <view v-for="(item,index) in options" :key="index" class="option" @click="optionSelect(index)"
                            :class="{'option-check':opIndex==index}">
                            <text class="option-text">{{item}}</text>
                        </view>
                    </view>
                    <view class="flex row-between" style="margin-top: 40rpx;">
                        <view class="option" :class="{'option-check':op1Index==0}"
                            style="width: 320rpx;padding-left: 0;padding-right: 0;" @click="optionChange(0)">
                            <text class="option-text">{{timeFormat(getTimestamp(start_time),fmt)}}</text>
                        </view>
                        <view style="width: 18rpx;height: 2rpx;background: #7B8686;"></view>
                        <view class="option" :class="{'option-check':op1Index==1}"
                            style="width: 320rpx;padding-left: 0;padding-right: 0;" @click="optionChange(1)">
                            <text class="option-text">{{timeFormat(getTimestamp(end_time),fmt)}}</text>
                        </view>
                    </view>
                </view>
                <view class="picker-body">
                    <picker-view class="picker-view"
                        indicator-style="height: 76rpx;background: rgba(0,0,0,.03);border:none;" :value="valueArr"
                        @change="change" @touchend="touchend">
                        <picker-view-column v-if="params.year">
                            <view class="column-item" v-for="(item,index) in years" :key="index">
                                <text class="column-item-text">{{item}}年</text>
                            </view>
                        </picker-view-column>
                        <picker-view-column v-if="params.month">
                            <view class="column-item" v-for="(item,index) in months" :key="index">
                                <text class="column-item-text">{{formatNumber(item)}}月</text>
                            </view>
                        </picker-view-column>
                        <picker-view-column v-if="params.day">
                            <view class="column-item" v-for="(item,index) in days" :key="index">
                                <text class="column-item-text">{{formatNumber(item)}}日</text>
                            </view>
                        </picker-view-column>
                        <picker-view-column v-if="params.hour">
                            <view class="column-item" v-for="(item,index) in hours" :key="index">
                                <text class="column-item-text">{{formatNumber(item)}}时</text>
                            </view>
                        </picker-view-column>
                        <picker-view-column v-if="params.minute">
                            <view class="column-item" v-for="(item,index) in minutes" :key="index">
                                <text class="column-item-text">{{formatNumber(item)}}分</text>
                            </view>
                        </picker-view-column>
                        <picker-view-column v-if="params.second">
                            <view class="column-item" v-for="(item,index) in seconds" :key="index">
                                <text class="column-item-text">{{formatNumber(item)}}秒</text>
                            </view>
                        </picker-view-column>
                    </picker-view>
                </view>
                <view class="bottom">
                    <view class="h-btn h-btn-line" @click="reset">
                        <text style="color: #333;">重置</text>
                    </view>
                    <view class="h-btn" @click="getResult">
                        <text style="color: #fff;">确定</text>
                    </view>
                </view>
            </view>
        </uni-popup>
    </view>
</template>
 
<script>
    import timeFormat from "../../js/timeFormat.js"
    export default {
        props: {
            // picker中需要显示的参数
            params: {
                type: Object,
                default () {
                    return {
                        year: true,
                        month: true,
                        day: true,
                        hour: true,
                        minute: true,
                        second: false,
                        timestamp: true,
                    };
                }
            },
            // 默认显示的时间,2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02
            defaultTime: {
                type: String,
                default: ''
            },
            // 年份开始时间
            startYear: {
                type: [String, Number],
                default: 1950
            },
            // 年份结束时间
            endYear: {
                type: [String, Number],
                default: 2050
            },
            // 月份结束时间
            endMonth: {
                type: [String, Number],
                default: 12
            },
            // 自定义时间选项模式month:月 day:天
            optionMode: {
                type: String,
                default: 'month'
            },
            //选择器模式 single:单月 multiple:多月 all:都包括
            mode: {
                type: String,
                default: 'all'
            },
            startTime: {
                type: String,
                default: ''
            },
            endTime: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                timeFormat,
                years: [],
                months: [],
                days: [],
                hours: [],
                minutes: [],
                seconds: [],
                valueArr: [],
                yearKey: 0,
                monthKey: 0,
                dayKey: 0,
                hourKey: 0,
                minuteKey: 0,
                secondy: 0,
                time: {},
                start_time: {},
                end_time: {},
                tabs: ['月份选择', '自定义时间'],
                tabIndex: 0,
                opIndex: -1,
                op1Index: 0,
                options: ['近三月', '近半年', '近一年'],
                itemIndex: 0,
                fmt: 'yyyy-mm-dd',
                isClick: false
            }
        },
        mounted() {
            this.init();
        },
        watch: {
            'time.year'(val) {
                if(this.params.day){
                    this.setDays(this.time)
                }
            },
            'time.month'(val) {
                if(this.params.day){
                    this.setDays(this.time)
                }
            },
            'start_time.year'(val) {
                this.setDays(this.start_time)
            },
            'start_time.month'(val) {
                this.setDays(this.start_time)
            },
            'end_time.year'(val) {
                this.setDays(this.end_time)
            },
            'end_time.month'(val) {
                this.setDays(this.end_time)
            },
        },
        methods: {
            open() {
                this.$refs.popup.open('bottom')
            },
            close() {
                this.$refs.popup.close()
            },
            //点击重置
            reset() {
                this.close();
                this.$emit('reset');
            },
            // 点击确定
            getResult() {
                let result = {};
                if (this.tabIndex) {
                    let start_time = {}
                    let end_time = {}
                    if (this.params.year) {
                        start_time.year = this.formatNumber(this.start_time.year || 0);
                        end_time.year = this.formatNumber(this.end_time.year || 0);
                    }
                    if (this.params.month) {
                        start_time.month = this.formatNumber(this.start_time.month || 0);
                        end_time.month = this.formatNumber(this.end_time.month || 0);
                    }
                    if (this.params.day) {
                        start_time.day = this.formatNumber(this.start_time.day || 0);
                        end_time.day = this.formatNumber(this.end_time.day || 0);
                    }
                    if (this.params.hour) {
                        start_time.hour = this.formatNumber(this.start_time.hour || 0);
                        end_time.hour = this.formatNumber(this.end_time.hour || 0);
                    }
                    if (this.params.minute) {
                        start_time.minute = this.formatNumber(this.start_time.minute || 0);
                        end_time.minute = this.formatNumber(this.end_time.minute || 0);
                    }
                    if (this.params.second) {
                        start_time.second = this.formatNumber(this.start_time.second || 0);
                        end_time.second = this.formatNumber(this.end_time.second || 0);
                    }
                    if (this.params.timestamp) {
                        start_time.timestamp = this.getTimestamp(this.start_time);
                        end_time.timestamp = this.getTimestamp(this.end_time);
                    }
                    start_time.time = this.timeFormat(this.getTimestamp(this.start_time), this.fmt);
                    end_time.time = this.timeFormat(this.getTimestamp(this.end_time), this.fmt);
                    result = {
                        start_time,
                        end_time
                    }
                } else {
                    if (this.params.year) result.year = this.formatNumber(this.time.year || 0);
                    if (this.params.month) result.month = this.formatNumber(this.time.month || 0);
                    if (this.params.day) result.day = this.formatNumber(this.time.day || 0);
                    if (this.params.hour) result.hour = this.formatNumber(this.time.hour || 0);
                    if (this.params.minute) result.minute = this.formatNumber(this.time.minute || 0);
                    if (this.params.second) result.second = this.formatNumber(this.time.second || 0);
                    if (this.params.timestamp) result.timestamp = this.getTimestamp(this.time);
                    result.time = this.timeFormat(this.getTimestamp(this.time), this.fmt);
                }
                this.$emit('confirm', result);
                this.close();
            },
            init() {
                this.valueArr = [];
                this.tabIndex = this.mode == 'multiple' ? 1 : 0
                //初始化时间
                if (this.startTime) {
                    this.start_time = this.time = this.initTimeValue(this.startTime);
                } else {
                    this.start_time = this.time = this.initTimeValue(this.defaultTime);
                }
                if (this.endTime) {
                    this.end_time = this.initTimeValue(this.endTime);
                } else {
                    this.end_time = this.initTimeValue(this.defaultTime);
                }
                let fmt = ''
                if (this.optionMode == 'day') {
                    this.options = ['近3天', '近7天', '近15天']
                }
                if (this.params.year) {
                    this.valueArr.push(0);
                    this.yearKey = this.valueArr.length - 1
                    this.setYears(this.time);
                    fmt = 'yyyy'
                }
                if (this.params.month) {
                    this.valueArr.push(0);
                    this.monthKey = this.valueArr.length - 1
                    this.setMonths(this.time);
                    fmt += '-mm'
                }
                if (this.params.day) {
                    this.valueArr.push(0);
                    this.dayKey = this.valueArr.length - 1
                    this.setDays(this.time);
                    fmt += '-dd'
                }
                if (this.params.hour) {
                    this.valueArr.push(0);
                    this.hourKey = this.valueArr.length - 1
                    this.setHours(this.time);
                    fmt += ' hh'
                }
                if (this.params.minute) {
                    this.valueArr.push(0);
                    this.minuteKey = this.valueArr.length - 1
                    this.setMinutes(this.time);
                    fmt += ':MM'
                }
                if (this.params.second) {
                    this.valueArr.push(0);
                    this.secondKey = this.valueArr.length - 1
                    this.setSeconds(this.time);
                    fmt += ':ss'
                }
                this.fmt = fmt
                this.$forceUpdate();
            },
            // 设置picker的某一列值
            setYears(date) {
                // 获取年份集合
                this.years = this.generateArray(this.startYear, this.endYear);
                // 设置this.valueArr某一项的值,是为了让picker预选中某一个值
                this.valueArr.splice(this.yearKey, 1, this.getIndex(this.years, date.year));
            },
            setMonths(date) {
                if (this.endYear == this.year) {
                    this.months = this.generateArray(1, this.endMonth);
                } else {
                    this.months = this.generateArray(1, 12);
                }
                this.valueArr.splice(this.monthKey, 1, this.getIndex(this.months, date.month));
            },
            setDays(date) {
                let totalDays = new Date(date.year, date.month, 0).getDate();
                this.days = this.generateArray(1, totalDays);
                // let index = 0;
                // 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
                // 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误
                // if (this.params.year && this.params.month) index = 2;
                // else if (this.params.month) index = 1;
                // else if (this.params.year) index = 1;
                // else index = 0;
                // 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值
                // 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始)
                if (this.day > this.days.length) this.day = this.days.length;
                this.valueArr.splice(this.dayKey, 1, this.getIndex(this.days, date.day));
            },
            setHours() {
                this.hours = this.generateArray(0, 23);
                this.valueArr.splice(this.hourKey, 1, this.getIndex(this.hours, this.time.hour));
            },
            setMinutes() {
                this.minutes = this.generateArray(0, 59);
                this.valueArr.splice(this.minuteKey, 1, this.getIndex(this.minutes, this.time.minute));
            },
            setSeconds() {
                this.seconds = this.generateArray(0, 59);
                this.valueArr.splice(this.secondKey, 1, this.getIndex(this.seconds, this.time.second));
            },
            // 生成递进的数组
            generateArray: function(start, end) {
                // 转为数值格式,否则用户给end-year等传递字符串值时,下面的end+1会导致字符串拼接,而不是相加
                start = Number(start);
                end = Number(end);
                end = end > start ? end : start;
                // 生成数组,获取其中的索引,并剪出来
                return [...Array(end + 1).keys()].slice(start);
            },
            getIndex: function(arr, val) {
                let index = arr.indexOf(val);
                // 如果index为-1(即找不到index值),~(-1)=-(-1)-1=0,导致条件不成立
                return ~index ? index : 0;
            },
            //日期时间处理
            initTimeValue(defaultTime) {
                // 格式化时间,在IE浏览器(uni不存在此情况),无法识别日期间的"-"间隔符号
                let fdate = defaultTime.replace(/\-/g, '/');
                fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate;
                let time = null;
                if (fdate) time = new Date(fdate);
                else time = new Date();
                // 获取年日月时分秒
                return {
                    year: time.getFullYear(),
                    month: Number(time.getMonth()) + 1,
                    day: time.getDate(),
                    hour: time.getHours(),
                    minute: time.getMinutes(),
                    second: time.getSeconds(),
                }
            },
            // 小于10前面补0,用于月份,日期,时分秒等
            formatNumber(num) {
                return +num < 10 ? '0' + num : String(num);
            },
            // 获取时间戳
            getTimestamp(date) {
                // yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容
                let time = date.year + '/' + date.month + '/' + date.day + ' ' + date.hour + ':' + date.minute + ':' + date
                    .second;
                return new Date(time).getTime();
            },
            touchend(e) {
                this.opIndex = -1
            },
            // 用户更改picker的列选项
            change(e) {
                this.itemIndex = e.detail.value[0]
                this.valueArr = e.detail.value;
                let i = 0;
                let time = {
                    year: 0,
                    month: 0,
                    day: 0,
                    hour: 0,
                    minute: 0,
                    second: 0,
                }
                if (this.params.year) time.year = this.years[this.valueArr[i++]];
                if (this.params.month) time.month = this.months[this.valueArr[i++]];
                if (this.params.day) time.day = this.days[this.valueArr[i++]];
                if (this.params.hour) time.hour = this.hours[this.valueArr[i++]];
                if (this.params.minute) time.minute = this.minutes[this.valueArr[i++]];
                if (this.params.second) time.second = this.seconds[this.valueArr[i++]];
                //自定义时间段
                if (this.tabIndex) {
                    if (this.op1Index) {
                        this.end_time = time
                    } else {
                        this.start_time = time
                    }
                } else { //月份选择
                    this.time = time
                }
            },
            tabChange(i) {
                this.tabIndex = i
                if (!i) {
                    this.setYears(this.time)
                    this.setMonths(this.time)
                    this.setDays(this.time)
                } else {
                    this.optionChange(this.op1Index)
                }
            },
            //选择快捷时间段
            optionSelect(i) {
                this.opIndex = i;
                let end_time = new Date()
                let date;
                switch (i) {
                    case 0: //近三月,近三天
                        if (this.optionMode == 'day') {
                            date = end_time.setDate(new Date(end_time).getDate() - 3)
                        } else {
                            date = end_time.setMonth(new Date(end_time).getMonth() - 3)
                        }
                        break;
                    case 1: //近半年,近七天
                        if (this.optionMode == 'day') {
                            date = end_time.setDate(new Date(end_time).getDate() - 7)
                        } else {
                            date = end_time.setMonth(new Date(end_time).getMonth() - 6)
                        }
                        break;
                    case 2: //近一年,近十五天
                        if (this.optionMode == 'day') {
                            date = end_time.setDate(new Date(end_time).getDate() - 15)
                        } else {
                            date = end_time.setMonth(new Date(end_time).getMonth() - 12)
                        }
                        break;
                }
                this.start_time = this.initTimeValue(this.timeFormat(date, this.fmt))
                this.end_time = this.initTimeValue(this.timeFormat(new Date(), this.fmt))
                if (this.op1Index) {
                    this.setYears(this.end_time)
                    this.setMonths(this.end_time)
                    this.setDays(this.end_time)
                } else {
                    this.setYears(this.start_time)
                    this.setMonths(this.start_time)
                    this.setDays(this.start_time)
                }
 
            },
            //起始时间、截至时间切换
            optionChange(i) {
                this.op1Index = i
                let date = i ? this.end_time : this.start_time
                this.setYears(date)
                this.setMonths(date)
                this.setDays(date)
            }
        }
    }
</script>
 
<style lang="scss">
    .flex {
        display: flex;
        flex-direction: row;
        align-items: center;
    }
 
    .row-between {
        justify-content: space-between;
    }
 
    .popup-box {
        background-color: #fff;
        border-radius: 24rpx 24rpx 0 0;
 
    }
 
    .picker-body {
        overflow: hidden;
        padding: 32rpx 40rpx;
    }
 
    .picker-view {
        height: 380rpx;
        width: 670rpx;
    }
 
    .column-item {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 32rpx;
        color: #888;
        height: 76rpx;
 
    }
 
    .column-item-text {
        // line-height: 100rpx;
        font-size: 32rpx;
    }
 
    .title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-direction: row;
        position: relative;
 
        .close {
            position: absolute;
            right: 50rpx;
        }
 
        .title-text {
            width: 100%;
            text-align: center;
            padding: 24rpx 0;
            font-size: 32rpx;
            font-weight: 600;
        }
    }
 
    .tabs-box {
        width: 100%;
        display: flex;
        flex-direction: row;
        padding: 0 50rpx 0 32rpx;
        align-items: center;
        border-bottom: 2rpx solid rgba(225, 225, 225, 0.5);
 
        .tabs-item {
            padding: 24rpx 0 22rpx 0;
            margin-right: 84rpx;
            border-bottom: 4rpx solid #fff;
        }
    }
 
    .tabs-check {
        border-bottom: 4rpx solid #1DAA9F !important;
 
        .item-text {
            color: #1DAA9F;
        }
    }
 
    .bottom {
        padding: 0 32rpx 32rpx;
        display: flex;
        align-items: center;
        flex-direction: row;
 
        .h-btn {
            flex: 1;
            height: 80rpx;
            background: #0BC7B9;
            border-radius: 16rpx;
            display: flex;
            align-items: center;
            border: 2rpx solid #0BC7B9;
            justify-content: center;
        }
 
        .h-btn-line {
            background: #fff;
            border: 2rpx solid #E1E7EC;
            margin-right: 42rpx;
        }
    }
 
    .option {
        padding: 14rpx 58rpx;
        font-size: 28rpx;
        border-radius: 16rpx;
        background: #F4F7F8;
        border: 3rpx solid #F4F7F8;
        display: flex;
        justify-content: center;
        flex-direction: row;
 
        .option-text {
            font-size: 28rpx;
            font-weight: 500;
            color: #333333;
            line-height: 40rpx;
 
        }
    }
 
    .option-check {
        background: #F5FFFF;
        border-radius: 16rpx;
        border: 3rpx solid #1DAA9F;
 
        .option-text {
            color: #1DAA9F;
        }
    }
 
    .options-box {
        padding: 32rpx;
        transition-property: height;
        transition-duration: .4s;
    }
</style>