|
|
@@ -1,36 +1,171 @@
|
|
|
+import type { DateOptionVal } from '@/types/HomeTypes'
|
|
|
+import type { DateOption } from '@/types/HomeTypes'
|
|
|
+import { batchFormatterTime } from '@/utils/common'
|
|
|
+
|
|
|
+interface ShortCutsDate {
|
|
|
+ name: string
|
|
|
+ text: string
|
|
|
+ value: Date[]
|
|
|
+ formatterVal: string[]
|
|
|
+}
|
|
|
+
|
|
|
+export type { ShortCutsDate }
|
|
|
+
|
|
|
export function useDate() {
|
|
|
/**
|
|
|
* @description: 创建一个日期范围
|
|
|
- * @param {*} day 天数
|
|
|
+ * 如-7,1表示从今天开始往前推7天,到昨天结束
|
|
|
+ * -1,-1,表示昨天,0,0表示今天,1,1,也表示昨天
|
|
|
+ * @param {number} startDay 开始的日期,可正可负,均表示距离今天的天数
|
|
|
+ * @param {number} endDay 结束日期,可正可负,也表示距离今天的天数,如不传则为开始日期,表示同一天
|
|
|
* @return {*}
|
|
|
*/
|
|
|
- const createDateRange = (day: number): Array<Date> => {
|
|
|
- const end = new Date()
|
|
|
+ const createDateRange = (startDay: number, endDay?: number) => {
|
|
|
+ if (endDay === undefined) endDay = startDay
|
|
|
+
|
|
|
+ startDay = Math.abs(startDay)
|
|
|
+ endDay = Math.abs(endDay)
|
|
|
+ if (startDay < endDay) [startDay, endDay] = [endDay, startDay]
|
|
|
const start = new Date()
|
|
|
- start.setTime(start.getTime() - 3600 * 1000 * 24 * day)
|
|
|
+ const end = new Date()
|
|
|
+ start.setTime(start.getTime() - 3600 * 1000 * 24 * startDay)
|
|
|
+ end.setTime(end.getTime() - 3600 * 1000 * 24 * endDay)
|
|
|
return [start, end]
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @description: 根据type值来获取指定count之前的日期范围
|
|
|
+ * @param {*} type 类型
|
|
|
+ * @param {number} count 往前推的周数、月数、年数
|
|
|
+ * @return {date[]} 开始日期和结束日期
|
|
|
+ */
|
|
|
+ const getRangeForPeriod = (
|
|
|
+ type: 'week' | 'month' | 'year',
|
|
|
+ count: number,
|
|
|
+ ) => {
|
|
|
+ const now = new Date()
|
|
|
+ count = Math.abs(count) // 确保count为正数,表示往前推的周数或月数
|
|
|
+
|
|
|
+ if (type === 'week') {
|
|
|
+ // 计算指定前N周的开始和结束日期
|
|
|
+ const startOfWeek = new Date(now)
|
|
|
+ const endOfWeek = new Date(now)
|
|
|
+
|
|
|
+ // 获取当前是星期几 (周日为0,设为7)
|
|
|
+ const dayOfWeek = now.getDay() || 7
|
|
|
+
|
|
|
+ // 计算目标周的开始和结束
|
|
|
+ startOfWeek.setDate(now.getDate() - dayOfWeek + 1 - count * 7) // 前N周的周一
|
|
|
+ endOfWeek.setDate(now.getDate() + (7 - dayOfWeek) - count * 7) // 前N周的周日
|
|
|
+
|
|
|
+ return [startOfWeek, endOfWeek]
|
|
|
+ } else if (type === 'month') {
|
|
|
+ // 计算指定前N个月的开始和结束日期
|
|
|
+ const startOfMonth = new Date(
|
|
|
+ now.getFullYear(),
|
|
|
+ now.getMonth() - count,
|
|
|
+ 1,
|
|
|
+ ) // 前N个月的1号
|
|
|
+ const endOfMonth = new Date(
|
|
|
+ now.getFullYear(),
|
|
|
+ now.getMonth() - count + 1,
|
|
|
+ 0,
|
|
|
+ ) // 前N个月的最后一天
|
|
|
+
|
|
|
+ return [startOfMonth, endOfMonth]
|
|
|
+ } else if (type === 'year') {
|
|
|
+ // 计算指定前N年的开始和结束日期
|
|
|
+ const startOfYear = new Date(now.getFullYear() - count, 0, 1) // 前N年的1月1号
|
|
|
+ const endOfYear = new Date(now.getFullYear() - count, 11, 31) // 前N年的12月31号
|
|
|
+ return [startOfYear, endOfYear]
|
|
|
+ } else {
|
|
|
+ return [new Date(), new Date()]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 快速选择日期
|
|
|
- const shortcuts: Array<{
|
|
|
- text: string
|
|
|
- value: () => Date[]
|
|
|
- }> = [
|
|
|
+ const shortcuts: Array<ShortCutsDate> = [
|
|
|
+ {
|
|
|
+ name: 'today',
|
|
|
+ text: '今天',
|
|
|
+ value: (() => createDateRange(0))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(createDateRange(0)))(),
|
|
|
+ },
|
|
|
{
|
|
|
- text: '上一周',
|
|
|
- value: () => createDateRange(7),
|
|
|
+ name: 'yesterday',
|
|
|
+ text: '昨天',
|
|
|
+ value: (() => createDateRange(-1, -1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(createDateRange(-1, -1)))(),
|
|
|
},
|
|
|
{
|
|
|
- text: '上个月',
|
|
|
- value: () => createDateRange(30),
|
|
|
+ name: 'last7days',
|
|
|
+ text: '过去7天(不含今天)',
|
|
|
+ value: (() => createDateRange(-7, -1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(createDateRange(-7, -1)))(),
|
|
|
},
|
|
|
{
|
|
|
- text: '近三个月',
|
|
|
- value: () => createDateRange(90),
|
|
|
+ name: 'last30days',
|
|
|
+ text: '过去30天(不含今天)',
|
|
|
+ value: (() => createDateRange(-30, -1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(createDateRange(-30, -1)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'thisweek',
|
|
|
+ text: '本周',
|
|
|
+ value: (() => getRangeForPeriod('week', 0))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('week', 0)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'lastweek',
|
|
|
+ text: '上周',
|
|
|
+ value: (() => getRangeForPeriod('week', 1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('week', 1)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'thismonth',
|
|
|
+ text: '本月',
|
|
|
+ value: (() => getRangeForPeriod('month', 0))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('month', 0)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'lastmonth',
|
|
|
+ text: '上月',
|
|
|
+ value: (() => getRangeForPeriod('month', 1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('month', 1)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'thisyear',
|
|
|
+ text: '本年',
|
|
|
+ value: (() => getRangeForPeriod('year', 0))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('year', 0)))(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'lastyear',
|
|
|
+ text: '去年',
|
|
|
+ value: (() => getRangeForPeriod('year', 1))(),
|
|
|
+ formatterVal: (() => batchFormatterTime(getRangeForPeriod('year', 1)))(),
|
|
|
},
|
|
|
]
|
|
|
|
|
|
/**
|
|
|
+ * @description: 生成日期选择器所需要的options
|
|
|
+ * @param {*} Array
|
|
|
+ * @return {*}
|
|
|
+ */
|
|
|
+ const createSelectDateOption = (): Array<DateOption> => {
|
|
|
+ return shortcuts.map(item => {
|
|
|
+ return {
|
|
|
+ label: item.text,
|
|
|
+ value: {
|
|
|
+ name: item.name,
|
|
|
+ startTime: item.formatterVal[0],
|
|
|
+ endTime: item.formatterVal[1],
|
|
|
+ },
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* @description: 禁止选取今天之后的日期
|
|
|
* @param {*} date
|
|
|
* @return {*}
|
|
|
@@ -41,6 +176,7 @@ export function useDate() {
|
|
|
|
|
|
return {
|
|
|
shortcuts,
|
|
|
+ createSelectDateOption,
|
|
|
disableDate,
|
|
|
createDateRange,
|
|
|
}
|