Novecane
2025-02-15 a12e5f63a90446525b2f322fb2bbd21d5285cd6d
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
<template>
    <view class="uni-calendar">
        <view v-if="!insert&&show" class="uni-calendar__mask" :class="{'uni-calendar--mask-show':aniMaskShow}" @click="clean"></view>
        <view v-if="insert || show" class="uni-calendar__content" :class="{'uni-calendar--fixed':!insert,'uni-calendar--ani-show':aniMaskShow}">
            <view v-if="!insert" class="uni-calendar__header uni-calendar--fixed-top">
                <view class="uni-calendar__header-btn-box" @click="close">
                    <text class="uni-calendar__header-text uni-calendar--fixed-width">{{cancelText}}</text>
                </view>
                <view class="uni-calendar__header-btn-box" @click="confirm">
                    <text class="uni-calendar__header-text uni-calendar--fixed-width">{{okText}}</text>
                </view>
            </view>
            <view class="uni-calendar__header">
                <view class="uni-calendar__header-btn-box" @click.stop="pre">
                    <view class="uni-calendar__header-btn uni-calendar--left"></view>
                </view>
                <picker mode="date" :value="date" fields="month" @change="bindDateChange">
                    <text class="uni-calendar__header-text">{{ (nowDate.year||'') +' / '+( nowDate.month||'')}}</text>
                </picker>
                <view class="uni-calendar__header-btn-box" @click.stop="next">
                    <view class="uni-calendar__header-btn uni-calendar--right"></view>
                </view>
                <text class="uni-calendar__backtoday" @click="backToday">{{todayText}}</text>
 
            </view>
            <view class="uni-calendar__box">
                <view v-if="showMonth" class="uni-calendar__box-bg">
                    <text class="uni-calendar__box-bg-text">{{nowDate.month}}</text>
                </view>
                <view class="uni-calendar__weeks">
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{SUNText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{monText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{TUEText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{WEDText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{THUText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{FRIText}}</text>
                    </view>
                    <view class="uni-calendar__weeks-day">
                        <text class="uni-calendar__weeks-day-text">{{SATText}}</text>
                    </view>
                </view>
                <view class="uni-calendar__weeks" v-for="(item,weekIndex) in weeks" :key="weekIndex">
                    <view class="uni-calendar__weeks-item" v-for="(weeks,weeksIndex) in item" :key="weeksIndex">
                        <calendar-item class="uni-calendar-item--hook" :weeks="weeks" :calendar="calendar" :selected="selected" :lunar="lunar" @change="choiceDate"></calendar-item>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    import Calendar from './util.js';
    import CalendarItem from './uni-calendar-item.vue'
 
    import { initVueI18n } from '@dcloudio/uni-i18n'
    import i18nMessages from './i18n/index.js'
    const {    t    } = initVueI18n(i18nMessages)
 
    /**
     * Calendar 日历
     * @description 日历组件可以查看日期,选择任意范围内的日期,打点操作。常用场景如:酒店日期预订、火车机票选择购买日期、上下班打卡等
     * @tutorial https://ext.dcloud.net.cn/plugin?id=56
     * @property {String} date 自定义当前时间,默认为今天
     * @property {Boolean} lunar 显示农历
     * @property {String} startDate 日期选择范围-开始日期
     * @property {String} endDate 日期选择范围-结束日期
     * @property {Boolean} range 范围选择
     * @property {Boolean} insert = [true|false] 插入模式,默认为false
     *     @value true 弹窗模式
     *     @value false 插入模式
     * @property {Boolean} clearDate = [true|false] 弹窗模式是否清空上次选择内容
     * @property {Array} selected 打点,期待格式[{date: '2019-06-27', info: '签到', data: { custom: '自定义信息', name: '自定义消息头',xxx:xxx... }}]
     * @property {Boolean} showMonth 是否选择月份为背景
     * @event {Function} change 日期改变,`insert :ture` 时生效
     * @event {Function} confirm 确认选择`insert :false` 时生效
     * @event {Function} monthSwitch 切换月份时触发
     * @example <uni-calendar :insert="true":lunar="true" :start-date="'2019-3-2'":end-date="'2019-5-20'"@change="change" />
     */
    export default {
        components: {
            CalendarItem
        },
        emits:['close','confirm','change','monthSwitch'],
        props: {
            date: {
                type: String,
                default: ''
            },
            selected: {
                type: Array,
                default () {
                    return []
                }
            },
            lunar: {
                type: Boolean,
                default: false
            },
            startDate: {
                type: String,
                default: ''
            },
            endDate: {
                type: String,
                default: ''
            },
            range: {
                type: Boolean,
                default: false
            },
            insert: {
                type: Boolean,
                default: true
            },
            showMonth: {
                type: Boolean,
                default: true
            },
            clearDate: {
                type: Boolean,
                default: true
            }
        },
        data() {
            return {
                show: false,
                weeks: [],
                calendar: {},
                nowDate: '',
                aniMaskShow: false
            }
        },
        computed:{
            /**
             * for i18n
             */
 
            okText() {
                return t("uni-calender.ok")
            },
            cancelText() {
                return t("uni-calender.cancel")
            },
            todayText() {
                return t("uni-calender.today")
            },
            monText() {
                return t("uni-calender.MON")
            },
            TUEText() {
                return t("uni-calender.TUE")
            },
            WEDText() {
                return t("uni-calender.WED")
            },
            THUText() {
                return t("uni-calender.THU")
            },
            FRIText() {
                return t("uni-calender.FRI")
            },
            SATText() {
                return t("uni-calender.SAT")
            },
            SUNText() {
                return t("uni-calender.SUN")
            },
        },
        watch: {
            date(newVal) {
                // this.cale.setDate(newVal)
                this.init(newVal)
            },
            startDate(val){
                this.cale.resetSatrtDate(val)
                this.cale.setDate(this.nowDate.fullDate)
                this.weeks = this.cale.weeks
            },
            endDate(val){
                this.cale.resetEndDate(val)
                this.cale.setDate(this.nowDate.fullDate)
                this.weeks = this.cale.weeks
            },
            selected(newVal) {
                this.cale.setSelectInfo(this.nowDate.fullDate, newVal)
                this.weeks = this.cale.weeks
            }
        },
        created() {
            this.cale = new Calendar({
                selected: this.selected,
                startDate: this.startDate,
                endDate: this.endDate,
                range: this.range,
            })
            this.init(this.date)
        },
        methods: {
            // 取消穿透
            clean() {},
            bindDateChange(e) {
                const value = e.detail.value + '-1'
                this.setDate(value)
 
                const { year,month } = this.cale.getDate(value)
        this.$emit('monthSwitch', {
            year,
            month
        })
            },
            /**
             * 初始化日期显示
             * @param {Object} date
             */
            init(date) {
                this.cale.setDate(date)
                this.weeks = this.cale.weeks
                this.nowDate = this.calendar = this.cale.getInfo(date)
            },
            /**
             * 打开日历弹窗
             */
            open() {
                // 弹窗模式并且清理数据
                if (this.clearDate && !this.insert) {
                    this.cale.cleanMultipleStatus()
                    // this.cale.setDate(this.date)
                    this.init(this.date)
                }
                this.show = true
                this.$nextTick(() => {
                    setTimeout(() => {
                        this.aniMaskShow = true
                    }, 50)
                })
            },
            /**
             * 关闭日历弹窗
             */
            close() {
                this.aniMaskShow = false
                this.$nextTick(() => {
                    setTimeout(() => {
                        this.show = false
                        this.$emit('close')
                    }, 300)
                })
            },
            /**
             * 确认按钮
             */
            confirm() {
                this.setEmit('confirm')
                this.close()
            },
            /**
             * 变化触发
             */
            change() {
                if (!this.insert) return
                this.setEmit('change')
            },
            /**
             * 选择月份触发
             */
            monthSwitch() {
                let {
                    year,
                    month
                } = this.nowDate
                this.$emit('monthSwitch', {
                    year,
                    month: Number(month)
                })
            },
            /**
             * 派发事件
             * @param {Object} name
             */
            setEmit(name) {
                let {
                    year,
                    month,
                    date,
                    fullDate,
                    lunar,
                    extraInfo
                } = this.calendar
                this.$emit(name, {
                    range: this.cale.multipleStatus,
                    year,
                    month,
                    date,
                    fulldate: fullDate,
                    lunar,
                    extraInfo: extraInfo || {}
                })
            },
            /**
             * 选择天触发
             * @param {Object} weeks
             */
            choiceDate(weeks) {
                if (weeks.disable) return
                this.calendar = weeks
                // 设置多选
                this.cale.setMultiple(this.calendar.fullDate)
                this.weeks = this.cale.weeks
                this.change()
            },
            /**
             * 回到今天
             */
            backToday() {
                const nowYearMonth = `${this.nowDate.year}-${this.nowDate.month}`
                const date = this.cale.getDate(new Date())
        const todayYearMonth = `${date.year}-${date.month}`
 
                this.init(date.fullDate)
 
        if(nowYearMonth !== todayYearMonth) {
          this.monthSwitch()
        }
 
                this.change()
            },
            /**
             * 上个月
             */
            pre() {
                const preDate = this.cale.getDate(this.nowDate.fullDate, -1, 'month').fullDate
                this.setDate(preDate)
                this.monthSwitch()
 
            },
            /**
             * 下个月
             */
            next() {
                const nextDate = this.cale.getDate(this.nowDate.fullDate, +1, 'month').fullDate
                this.setDate(nextDate)
                this.monthSwitch()
            },
            /**
             * 设置日期
             * @param {Object} date
             */
            setDate(date) {
                this.cale.setDate(date)
                this.weeks = this.cale.weeks
                this.nowDate = this.cale.getInfo(date)
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    $uni-bg-color-mask: rgba($color: #000000, $alpha: 0.4);
    $uni-border-color: #EDEDED;
    $uni-text-color: #333;
    $uni-bg-color-hover:#f1f1f1;
    $uni-font-size-base:14px;
    $uni-text-color-placeholder: #808080;
    $uni-color-subtitle: #555555;
    $uni-text-color-grey:#999;
    .uni-calendar {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: column;
    }
 
    .uni-calendar__mask {
        position: fixed;
        bottom: 0;
        top: 0;
        left: 0;
        right: 0;
        background-color: $uni-bg-color-mask;
        transition-property: opacity;
        transition-duration: 0.3s;
        opacity: 0;
        /* #ifndef APP-NVUE */
        z-index: 99;
        /* #endif */
    }
 
    .uni-calendar--mask-show {
        opacity: 1
    }
 
    .uni-calendar--fixed {
        position: fixed;
        /* #ifdef APP-NVUE */
        bottom: 0;
        /* #endif */
        left: 0;
        right: 0;
        transition-property: transform;
        transition-duration: 0.3s;
        transform: translateY(460px);
        /* #ifndef APP-NVUE */
        bottom: calc(var(--window-bottom));
        z-index: 99;
        /* #endif */
    }
 
    .uni-calendar--ani-show {
        transform: translateY(0);
    }
 
    .uni-calendar__content {
        background-color: #fff;
    }
 
    .uni-calendar__header {
        position: relative;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: center;
        align-items: center;
        height: 50px;
        border-bottom-color: $uni-border-color;
        border-bottom-style: solid;
        border-bottom-width: 1px;
    }
 
    .uni-calendar--fixed-top {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: space-between;
        border-top-color: $uni-border-color;
        border-top-style: solid;
        border-top-width: 1px;
    }
 
    .uni-calendar--fixed-width {
        width: 50px;
    }
 
    .uni-calendar__backtoday {
        position: absolute;
        right: 0;
        top: 25rpx;
        padding: 0 5px;
        padding-left: 10px;
        height: 25px;
        line-height: 25px;
        font-size: 12px;
        border-top-left-radius: 25px;
        border-bottom-left-radius: 25px;
        color: $uni-text-color;
        background-color: $uni-bg-color-hover;
    }
 
    .uni-calendar__header-text {
        text-align: center;
        width: 100px;
        font-size: $uni-font-size-base;
        color: $uni-text-color;
    }
 
    .uni-calendar__header-btn-box {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: center;
        width: 50px;
        height: 50px;
    }
 
    .uni-calendar__header-btn {
        width: 10px;
        height: 10px;
        border-left-color: $uni-text-color-placeholder;
        border-left-style: solid;
        border-left-width: 2px;
        border-top-color: $uni-color-subtitle;
        border-top-style: solid;
        border-top-width: 2px;
    }
 
    .uni-calendar--left {
        transform: rotate(-45deg);
    }
 
    .uni-calendar--right {
        transform: rotate(135deg);
    }
 
 
    .uni-calendar__weeks {
        position: relative;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
    }
 
    .uni-calendar__weeks-item {
        flex: 1;
    }
 
    .uni-calendar__weeks-day {
        flex: 1;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 45px;
        border-bottom-color: #F5F5F5;
        border-bottom-style: solid;
        border-bottom-width: 1px;
    }
 
    .uni-calendar__weeks-day-text {
        font-size: 14px;
    }
 
    .uni-calendar__box {
        position: relative;
    }
 
    .uni-calendar__box-bg {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        justify-content: center;
        align-items: center;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
 
    .uni-calendar__box-bg-text {
        font-size: 200px;
        font-weight: bold;
        color: $uni-text-color-grey;
        opacity: 0.1;
        text-align: center;
        /* #ifndef APP-NVUE */
        line-height: 1;
        /* #endif */
    }
</style>