food/src/components/DatePickerOrder.vue

112 lines
3.7 KiB
Vue
Raw Normal View History

<template>
2023-11-10 16:52:17 +08:00
<el-date-picker v-model="value" type="daterange" align="right" unlink-panels range-separator=""
start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" format="yyyy-MM-dd">
</el-date-picker>
</template>
<script>
export default {
data () {
2023-11-10 16:52:17 +08:00
const orderSearch = this.$store.getters['app/getOrderSerch']
const startTimestamp = orderSearch.start_time
const endTimestamp = orderSearch.end_time
return {
pickerOptions: {
shortcuts: [
{
text: '今天',
onClick (picker) {
2023-11-09 15:20:28 +08:00
const start = new Date(new Date().setHours(0, 0, 0, 0))
const end = new Date(new Date().setHours(23, 59, 59, 999))
picker.$emit('pick', [start, end])
}
},
{
text: '这月',
onClick (picker) {
const end = new Date()
2023-11-09 15:20:28 +08:00
end.setHours(23, 59, 59, 999)
const start = new Date(new Date().getFullYear(), new Date().getMonth(), 1, 0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
},
{
2023-11-09 15:20:28 +08:00
text: '今年',
onClick (picker) {
const end = new Date()
2023-11-09 15:20:28 +08:00
end.setHours(23, 59, 59, 999)
const start = new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
},
{
text: '最近一周',
onClick (picker) {
const end = new Date()
2023-11-09 15:20:28 +08:00
end.setHours(23, 59, 59, 999)
const start = new Date()
2023-11-09 15:20:28 +08:00
start.setDate(end.getDate() - 6)
start.setHours(0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
2023-11-09 15:20:28 +08:00
},
{
text: '最近一个月',
onClick (picker) {
const end = new Date()
2023-11-09 15:20:28 +08:00
end.setHours(23, 59, 59, 999)
const start = new Date()
2023-11-09 15:20:28 +08:00
start.setMonth(end.getMonth() - 1)
start.setHours(0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
2023-11-09 15:20:28 +08:00
},
{
text: '最近三个月',
onClick (picker) {
const end = new Date()
2023-11-09 15:20:28 +08:00
end.setHours(23, 59, 59, 999)
const start = new Date()
2023-11-09 15:20:28 +08:00
start.setMonth(end.getMonth() - 3)
start.setHours(0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
},
{
text: '上月',
onClick (picker) {
const end = new Date(new Date().getFullYear(), new Date().getMonth(), 0, 23, 59, 59, 999)
const start = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1, 0, 0, 0, 0)
picker.$emit('pick', [start, end])
}
}
]
},
2023-11-10 16:52:17 +08:00
value: [startTimestamp, endTimestamp]
}
2023-11-09 15:20:28 +08:00
},
watch: {
2023-11-10 16:52:17 +08:00
value (newVal) {
2023-11-09 15:20:28 +08:00
if (newVal && newVal.length === 2) {
const end = newVal[1]
end.setHours(23, 59, 59, 999) // 设置结束日期为当天的23:59:59
const startTimestamp = Math.floor(newVal[0].getTime() / 1000) // 转换毫秒为秒
const endTimestamp = Math.floor(end.getTime() / 1000) // 转换毫秒为秒
2023-11-10 16:52:17 +08:00
this.$store.commit('app/setOrderSerch', { start_time: startTimestamp })
this.$store.commit('app/setOrderSerch', { end_time: endTimestamp })
2023-11-09 15:20:28 +08:00
}
if (newVal === null) {
2023-11-10 16:52:17 +08:00
this.$store.commit('app/setOrderSerch', { start_time: '' })
this.$store.commit('app/setOrderSerch', { end_time: '' })
2023-11-09 15:20:28 +08:00
}
}
}
}
</script>
<style lang="scss" scoped>
.el-date-editor .el-range__close-icon {
display: block !important;
}
</style>