2024-10-22 13:19:13 +08:00
|
|
|
<template>
|
2024-10-22 14:47:25 +08:00
|
|
|
<div class='time-control'>
|
2024-10-22 13:19:13 +08:00
|
|
|
<el-select
|
2024-10-22 14:47:25 +08:00
|
|
|
class='select'
|
|
|
|
|
v-model='timeUnit'
|
|
|
|
|
placeholder='选择时间单位'
|
|
|
|
|
@change='handleChange'
|
2024-10-22 13:19:13 +08:00
|
|
|
>
|
2024-10-22 14:47:25 +08:00
|
|
|
<!--采用 v-for 去动态渲染-->
|
|
|
|
|
<el-option label='按日' value='日'></el-option>
|
|
|
|
|
<el-option label='按周' value='周'></el-option>
|
|
|
|
|
<el-option label='按月' value='月'></el-option>
|
|
|
|
|
<el-option label='按季度' value='季度'></el-option>
|
|
|
|
|
<el-option label='按年' value='年'></el-option>
|
|
|
|
|
<el-option label='自定义' value='自定义'></el-option>
|
|
|
|
|
|
2024-10-22 13:19:13 +08:00
|
|
|
</el-select>
|
|
|
|
|
|
2024-10-22 14:47:25 +08:00
|
|
|
<!-- 禁用时间选择器 -->
|
|
|
|
|
<div class='date-display'>
|
2024-10-22 13:19:13 +08:00
|
|
|
<el-date-picker
|
2024-10-22 14:47:25 +08:00
|
|
|
class='date-picker'
|
|
|
|
|
v-model='startDate'
|
|
|
|
|
type='date'
|
|
|
|
|
placeholder='起始时间'
|
|
|
|
|
:readonly="timeUnit != '自定义'"
|
2024-10-22 13:19:13 +08:00
|
|
|
></el-date-picker>
|
|
|
|
|
<el-text>~</el-text>
|
|
|
|
|
<el-date-picker
|
2024-10-22 14:47:25 +08:00
|
|
|
class='date-picker'
|
|
|
|
|
v-model='endDate'
|
|
|
|
|
type='date'
|
|
|
|
|
placeholder='结束时间'
|
2024-10-22 13:19:13 +08:00
|
|
|
:readonly="timeUnit !== '自定义'"
|
|
|
|
|
></el-date-picker>
|
|
|
|
|
</div>
|
2024-10-22 14:47:25 +08:00
|
|
|
<div class='date-display' v-if="timeUnit !== '自定义'">
|
2024-10-22 13:19:13 +08:00
|
|
|
<el-button
|
2024-10-22 14:47:25 +08:00
|
|
|
style='width: 10px;'
|
|
|
|
|
class='triangle-button'
|
|
|
|
|
type='primary'
|
|
|
|
|
@click='prevPeriod'
|
2024-10-22 13:19:13 +08:00
|
|
|
>
|
2024-10-22 14:47:25 +08:00
|
|
|
<div class='left_triangle'></div>
|
2024-10-22 13:19:13 +08:00
|
|
|
</el-button>
|
2024-10-22 14:47:25 +08:00
|
|
|
<el-button class='triangle-button' type='primary' @click='goToCurrent'>
|
2024-10-22 13:19:13 +08:00
|
|
|
本{{ timeUnit }}
|
|
|
|
|
</el-button>
|
|
|
|
|
<el-button
|
2024-10-22 14:47:25 +08:00
|
|
|
style='width: 10px;'
|
|
|
|
|
class='triangle-button'
|
|
|
|
|
type='primary'
|
|
|
|
|
@click='nextPeriod'
|
|
|
|
|
:disabled='isNextDisabled'
|
2024-10-22 13:19:13 +08:00
|
|
|
>
|
2024-10-22 14:47:25 +08:00
|
|
|
<div class='right_triangle'></div>
|
2024-10-22 13:19:13 +08:00
|
|
|
</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
timeUnit: '周', // 默认选择按周
|
|
|
|
|
startDate: null, // 起始日期
|
|
|
|
|
endDate: null, // 结束日期
|
|
|
|
|
isNextDisabled: false, // 控制下一周期按钮的禁用状态
|
2024-10-22 14:47:25 +08:00
|
|
|
today: new Date(), // 当前日期
|
|
|
|
|
}
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
created() {
|
|
|
|
|
// 组件创建时更新日期范围
|
2024-10-22 14:47:25 +08:00
|
|
|
this.updateDateRange()
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleChange(value) {
|
|
|
|
|
// 根据选择的时间单位处理日期变化
|
|
|
|
|
if (value !== '自定义') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.updateDateRange()
|
2024-10-22 13:19:13 +08:00
|
|
|
} else {
|
|
|
|
|
// 自定义选项逻辑
|
2024-10-22 14:47:25 +08:00
|
|
|
this.startDate = new Date()
|
|
|
|
|
this.endDate = new Date()
|
2024-10-22 13:19:13 +08:00
|
|
|
}
|
2024-10-22 14:47:25 +08:00
|
|
|
this.updateNextButtonStatus()
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
updateDateRange() {
|
2024-10-22 14:47:25 +08:00
|
|
|
const today = this.today
|
2024-10-22 13:19:13 +08:00
|
|
|
// 根据选择的时间单位计算起始和结束日期
|
|
|
|
|
if (this.timeUnit === '日') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.startDate = today
|
|
|
|
|
this.endDate = today
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '周') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.startDate = this.getStartOfWeek(today)
|
|
|
|
|
this.endDate = this.getEndOfWeek(today)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '月') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.startDate = new Date(today.getFullYear(), today.getMonth(), 1)
|
|
|
|
|
this.endDate = new Date(today.getFullYear(), today.getMonth() + 1, 0)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '季度') {
|
2024-10-22 14:47:25 +08:00
|
|
|
const quarter = Math.floor(today.getMonth() / 3)
|
|
|
|
|
this.startDate = new Date(today.getFullYear(), quarter * 3, 1)
|
|
|
|
|
this.endDate = new Date(today.getFullYear(), quarter * 3 + 3, 0)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '年') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.startDate = new Date(today.getFullYear(), 0, 1)
|
|
|
|
|
this.endDate = new Date(today.getFullYear(), 11, 31)
|
2024-10-22 13:19:13 +08:00
|
|
|
}
|
2024-10-22 14:47:25 +08:00
|
|
|
this.updateNextButtonStatus()
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
getStartOfWeek(date) {
|
2024-10-22 14:47:25 +08:00
|
|
|
const startOfWeek = new Date(date)
|
|
|
|
|
const day = startOfWeek.getDay()
|
|
|
|
|
const diff = day === 0 ? -6 : 1 - day // 星期天的情况
|
|
|
|
|
startOfWeek.setDate(startOfWeek.getDate() + diff)
|
|
|
|
|
return startOfWeek
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
getEndOfWeek(date) {
|
2024-10-22 14:47:25 +08:00
|
|
|
const endOfWeek = new Date(date)
|
|
|
|
|
const day = endOfWeek.getDay()
|
|
|
|
|
const diff = day === 0 ? 0 : 7 - day // 星期天的情况
|
|
|
|
|
endOfWeek.setDate(endOfWeek.getDate() + diff)
|
|
|
|
|
return endOfWeek
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
prevPeriod() {
|
2024-10-22 14:47:25 +08:00
|
|
|
const prevStartDate = new Date(this.startDate)
|
|
|
|
|
const prevEndDate = new Date(this.endDate)
|
|
|
|
|
|
2024-10-22 13:19:13 +08:00
|
|
|
if (this.timeUnit === '日') {
|
2024-10-22 14:47:25 +08:00
|
|
|
prevStartDate.setDate(prevStartDate.getDate() - 1)
|
|
|
|
|
prevEndDate.setDate(prevEndDate.getDate() - 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '周') {
|
2024-10-22 14:47:25 +08:00
|
|
|
prevStartDate.setDate(prevStartDate.getDate() - 7)
|
|
|
|
|
prevEndDate.setDate(prevEndDate.getDate() - 7)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '月') {
|
2024-10-22 14:47:25 +08:00
|
|
|
prevStartDate.setMonth(prevStartDate.getMonth() - 1)
|
|
|
|
|
prevEndDate.setMonth(prevEndDate.getMonth() - 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '季度') {
|
2024-10-22 14:47:25 +08:00
|
|
|
prevStartDate.setMonth(prevStartDate.getMonth() - 3)
|
|
|
|
|
prevEndDate.setMonth(prevEndDate.getMonth() - 3)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '年') {
|
2024-10-22 14:47:25 +08:00
|
|
|
prevStartDate.setFullYear(prevStartDate.getFullYear() - 1)
|
|
|
|
|
prevEndDate.setFullYear(prevEndDate.getFullYear() - 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
}
|
2024-10-22 14:47:25 +08:00
|
|
|
|
|
|
|
|
this.startDate = prevStartDate
|
|
|
|
|
this.endDate = prevEndDate
|
|
|
|
|
this.updateNextButtonStatus()
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
goToCurrent() {
|
|
|
|
|
if (this.timeUnit !== '自定义') {
|
2024-10-22 14:47:25 +08:00
|
|
|
this.updateDateRange() // 更新为当前选择时间单位的时间范围
|
2024-10-22 13:19:13 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nextPeriod() {
|
2024-10-22 14:47:25 +08:00
|
|
|
const nextStartDate = new Date(this.startDate)
|
|
|
|
|
const nextEndDate = new Date(this.endDate)
|
|
|
|
|
|
2024-10-22 13:19:13 +08:00
|
|
|
if (this.timeUnit === '日') {
|
2024-10-22 14:47:25 +08:00
|
|
|
nextStartDate.setDate(nextStartDate.getDate() + 1)
|
|
|
|
|
nextEndDate.setDate(nextEndDate.getDate() + 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '周') {
|
2024-10-22 14:47:25 +08:00
|
|
|
nextStartDate.setDate(nextStartDate.getDate() + 7)
|
|
|
|
|
nextEndDate.setDate(nextEndDate.getDate() + 7)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '月') {
|
2024-10-22 14:47:25 +08:00
|
|
|
nextStartDate.setMonth(nextStartDate.getMonth() + 1)
|
|
|
|
|
nextEndDate.setMonth(nextEndDate.getMonth() + 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '季度') {
|
2024-10-22 14:47:25 +08:00
|
|
|
nextStartDate.setMonth(nextStartDate.getMonth() + 3)
|
|
|
|
|
nextEndDate.setMonth(nextStartDate.getMonth() + 3)
|
2024-10-22 13:19:13 +08:00
|
|
|
} else if (this.timeUnit === '年') {
|
2024-10-22 14:47:25 +08:00
|
|
|
nextStartDate.setFullYear(nextStartDate.getFullYear() + 1)
|
|
|
|
|
nextEndDate.setFullYear(nextEndDate.getFullYear() + 1)
|
2024-10-22 13:19:13 +08:00
|
|
|
}
|
2024-10-22 14:47:25 +08:00
|
|
|
|
|
|
|
|
this.startDate = nextStartDate
|
|
|
|
|
this.endDate = nextEndDate
|
|
|
|
|
this.updateNextButtonStatus()
|
2024-10-22 13:19:13 +08:00
|
|
|
},
|
|
|
|
|
updateNextButtonStatus() {
|
|
|
|
|
// 更新下一个按钮的禁用状态
|
2024-10-22 14:47:25 +08:00
|
|
|
const maxDate = new Date() // 假设最新日期为今天
|
|
|
|
|
this.isNextDisabled = this.endDate > maxDate
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// defineProps('include','exclude','default')
|
2024-10-22 13:19:13 +08:00
|
|
|
</script>
|
|
|
|
|
|
2024-10-22 14:47:25 +08:00
|
|
|
<style scoped lang='scss'>
|
2024-10-22 13:19:13 +08:00
|
|
|
@import "./index.scss";
|
|
|
|
|
</style>
|