DatePopup.vue
4.32 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
<template>
<van-popup
v-model="show"
position="bottom"
class="date-popup"
round
@close="close"
>
<div class="date-title flex">
时间选择
<i
class="van-icon van-icon-cross van-popup__close-icon van-popup__close-icon--top-right"
@click="close"
></i>
</div>
<div class="date-box">
<div class="date-subtitle">推送日期</div>
<div class="date-list flex">
<template v-for="(i,index) in dateList">
<div
:key="index"
:class="['item flex',{'active':index == dateIndex}]"
@click="chooseDate(index)"
>
{{ i }}
</div>
</template>
</div>
<div class="date-subtitle">自定义</div>
<van-calendar
ref="calendar"
class="calendar"
:poppable="false"
:show-confirm="false"
:show-title="false"
:show-subtitle="false"
type="range"
color="#3975C6"
:min-date="minDate"
:max-date="maxDate"
:allow-same-day="true"
:lazy-render="false"
row-height="48px"
@confirm="confirmCalendar"
/>
<van-button
block
round
color="#3975C6"
class="date-btn"
:disabled="!dateValue.startTime && !dateValue.endTime"
@click="confirmDate"
>确定</van-button>
</div>
</van-popup>
</template>
<script type='text/ecmascript-6'>
import * as dayjs from 'dayjs'
export default {
name: 'DatePopup',
props: {
value: Boolean
},
data () {
return {
show: false,
dateList: ['上月', '近三月', '近一年'],
dateValue: {
startTime: '',
endTime: ''
},
dateIndex: -1,
minDate: new Date('2023/01/01'),
maxDate: new Date(),
}
},
watch: {
value (n) {
this.show = n
}
},
mounted () {
},
methods: {
chooseDate (index) {
const monthNumber = {
0: 1,
1: 3,
2: 12
}[index]
let startTime = dayjs().subtract(monthNumber, 'month'),
endTime = dayjs()
if (index == 0) {
startTime = startTime.startOf('M')
endTime = dayjs(startTime).endOf('month')
}
this.dateIndex = index
this.setDateValue([startTime, endTime])
this.$refs.calendar.reset()
},
confirmCalendar (val) {
this.dateIndex = -1
this.setDateValue(val)
},
setDateValue ([startTime, endTime]) {
this.dateValue = {
startTime: `${dayjs(startTime).format('YYYY-MM-DD')} 00:00:00`,
endTime: `${dayjs(endTime).format('YYYY-MM-DD')} 23:59:59`
}
console.log(JSON.parse(JSON.stringify(this.dateValue)))
},
close () {
this.$emit('update:value', false)
},
confirmDate () {
let { dateValue } = this
this.$emit('confirm', dateValue)
this.close()
}
}
}
</script>
<style scoped lang='scss'>
.date-popup {
max-height: 95%;
/deep/.van-popup__close-icon {
z-index: 3;
margin-left: auto;
position: static;
}
/deep/.van-calendar__day {
font-size: 14px;
}
/deep/.van-calendar__month-title {
height: 32px;
line-height: 32px;
}
/deep/.van-calendar__bottom-info {
font-size: 8px;
bottom: 2px;
}
.date-title {
height: 44px;
padding: 0 16px;
font-weight: 500;
font-size: 16px;
color: #222222;
position: sticky;
background-color: #fff;
z-index: 2;
top: 0;
}
.date-box {
padding: 16px 16px calc(var(--constantSafeAreaInsetBottom) + 76px);
padding: 16px 16px calc(var(--envSafeAreaInsetBottom) + 76px);
.date-subtitle {
font-size: 14px;
color: #333333;
margin-bottom: 8px;
}
.date-list {
margin-bottom: 16px;
gap: 8px;
.item {
width: 80px;
height: 28px;
border: 1px solid #dadde0;
border-radius: 14px;
font-size: 14px;
color: #333333;
justify-content: center;
}
.active {
color: #3975c6;
background: #3975c61a;
border-color: #3975c61a;
}
}
.calendar {
height: 320px;
}
}
.date-btn {
position: fixed;
left: 16px;
bottom: calc(var(--constantSafeAreaInsetBottom) + 16px);
bottom: calc(var(--envSafeAreaInsetBottom) + 16px);
z-index: 2;
width: calc(100% - 32px);
}
}
</style>