fix(查询组件): 时间范围选择动态时间为一月前的预览错误

This commit is contained in:
dataeaseShu
2025-03-31 17:39:37 +08:00
committed by dataeaseShu
parent 85d8053e7e
commit 4ca26e1afc
5 changed files with 29 additions and 75 deletions

View File

@@ -1,4 +1,5 @@
<script lang="ts" setup>
import type { ManipulateType } from 'dayjs'
import { toRefs, PropType, ref, onBeforeMount, watch, computed } from 'vue'
import { Calendar } from '@element-plus/icons-vue'
import { type DatePickType } from 'element-plus-secondary'
@@ -22,7 +23,7 @@ interface SelectConfig {
defaultValueCheck: boolean
id: string
timeNum: number
relativeToCurrentType: string
relativeToCurrentType: ManipulateType
around: string
arbitraryTime: Date
timeGranularity: DatePickType

View File

@@ -1,4 +1,5 @@
<script lang="ts" setup>
import type { ManipulateType } from 'dayjs'
import { toRefs, PropType, ref, onBeforeMount, watch, computed } from 'vue'
import { type DatePickType } from 'element-plus-secondary'
import {
@@ -15,7 +16,7 @@ import {
interface SelectConfig {
relativeToCurrent: string
timeNum: number
relativeToCurrentType: string
relativeToCurrentType: ManipulateType
around: string
arbitraryTime: Date
timeGranularity: DatePickType

View File

@@ -1,4 +1,5 @@
<script lang="ts" setup>
import type { ManipulateType } from 'dayjs'
import { toRefs, PropType, ref, onBeforeMount, watch, computed } from 'vue'
import { Calendar } from '@element-plus/icons-vue'
import { type DatePickType } from 'element-plus-secondary'
@@ -13,12 +14,12 @@ interface SelectConfig {
id: string
relativeToCurrentRange: string
timeNum: number
relativeToCurrentType: string
relativeToCurrentType: ManipulateType
around: string
arbitraryTime: Date
timeGranularity: DatePickType
timeNumRange: number
relativeToCurrentTypeRange: string
relativeToCurrentTypeRange: ManipulateType
aroundRange: string
arbitraryTimeRange: Date
}

View File

@@ -54,95 +54,46 @@ function getYearBeginning() {
return new Date(`${date.getFullYear()}/1/1`)
}
function getYearMonthRange(result, flag, sort) {
function getYearMonthRange(result, sort) {
const [direction, scene] = (sort || '').split('-')
const [dateTimeType] = (flag || '').split('range')
if (direction === 'start') {
return result
return new Date(result.startOf('day').format('YYYY/MM/DD HH:mm:ss'))
} else if (direction === 'end') {
if (scene === 'config') {
return result
return new Date(result.format('YYYY/MM/DD HH:mm:ss'))
} else if (scene === 'panel') {
return new Date(
+getCustomTime(1, dateTimeType, dateTimeType, 'b', null, flag, 'start-config', result) -
1000
)
return new Date(dayjs(result).endOf('day').format('YYYY/MM/DD HH:mm:ss'))
}
}
}
function getCustomTime(
timeNum: number,
timeType: string,
timeType: ManipulateType | 'date',
timeGranularity: string,
around: string,
arbitraryTime?: Date,
timeGranularityMultiple?: string,
sort?: string,
withDate?: Date
sort?: string
) {
const date = withDate ? new Date(withDate) : new Date()
const num = around === 'f' ? -timeNum : timeNum
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const type = around === 'f' ? 'subtract' : 'add'
let resultYear = timeType === 'year' ? year + num : year
let resultMonth = timeType === 'month' ? month + num : month
if (resultMonth > 12) {
resultYear += parseInt(`${resultMonth / 12}`)
resultMonth = resultMonth % 12
} else if (resultMonth < 0) {
resultYear += parseInt(`${resultMonth / 12}`) - 1
resultMonth = (resultMonth % 12) + 12
} else if (resultMonth === 0) {
resultYear += parseInt(`${resultMonth / 12}`) - 1
resultMonth = 12
}
const resultDate =
timeType === 'date' ? new Date(date.getTime() + 24 * 60 * 60 * 1000 * num).getDate() : day
if (timeType === 'date') {
resultMonth = new Date(date.getTime() + 24 * 60 * 60 * 1000 * num).getMonth() + 1
resultYear = new Date(date.getTime() + 24 * 60 * 60 * 1000 * num).getFullYear()
}
const result = dayjs()[type](timeNum, timeType === 'date' ? 'day' : timeType)
switch (timeGranularityMultiple) {
case 'monthrange':
return getYearMonthRange(new Date(`${resultYear}/${resultMonth}/1`), 'monthrange', sort)
case 'yearrange':
return getYearMonthRange(new Date(`${resultYear}/1`), 'yearrange', sort)
case 'daterange':
return getYearMonthRange(
new Date(`${resultYear}/${resultMonth}/${resultDate}`),
'daterange',
sort
)
default:
break
if (['monthrange', 'yearrange', 'daterange'].includes(timeGranularityMultiple)) {
return getYearMonthRange(result, sort)
}
if (!!arbitraryTime) {
const time = new Date(arbitraryTime)
time.setFullYear(resultYear)
time.setMonth(resultMonth - 1)
time.setDate(resultDate)
return time
const time = dayjs(arbitraryTime).format('YYYY/MM/DD HH:mm:ss')
const [_, q] = time.split(' ')
const [s] = result.format('YYYY/MM/DD HH:mm:ss').split(' ')
return new Date(`${s} ${q}`)
}
switch (timeGranularity) {
case 'year':
return new Date(`${resultYear}/1`)
case 'month':
return new Date(`${resultYear}/${resultMonth}/1`)
case 'date':
return new Date(`${resultYear}/${resultMonth}/${resultDate}`)
case 'monthrange':
return new Date(`${resultYear}/${resultMonth}/1`)
case 'yearrange':
return new Date(`${resultYear}/1`)
default:
break
}
const [k] = timeGranularity.split('range')
return new Date(result.startOf(k as ManipulateType).format('YYYY/MM/DD HH:mm:ss'))
}
function getDynamicRange({

View File

@@ -1,4 +1,5 @@
import { dvMainStoreWithOut } from '@/store/modules/data-visualization/dvMain'
import type { ManipulateType } from 'dayjs'
import { storeToRefs } from 'pinia'
import dayjs from 'dayjs'
import { getDynamicRange, getCustomTime } from '@/custom-component/v-query/time-format'
@@ -9,23 +10,22 @@ const { componentData, canvasStyleData } = storeToRefs(dvMainStore)
const getDynamicRangeTime = (type: number, selectValue: any, timeGranularityMultiple: string) => {
const timeType = (timeGranularityMultiple || '').split('range')[0]
if (['datetimerange', 'yearrange'].includes(timeGranularityMultiple) || type === 1 || !timeType) {
if (timeGranularityMultiple.includes('range') || type === 1 || !timeType) {
return selectValue.map(ele => +new Date(ele))
}
const [start, end] = selectValue
const [start] = selectValue
return [
+new Date(start),
+getCustomTime(
1,
timeType,
timeType as ManipulateType,
timeType,
'b',
null,
timeGranularityMultiple,
'start-config',
new Date(end)
'start-config'
) - 1000
]
}