-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bagolo6911
committed
Jan 10, 2024
1 parent
60328ce
commit c8c9574
Showing
13 changed files
with
126 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import '../global' | ||
import { boot } from 'quasar/wrappers' | ||
|
||
let app = null | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import dayjs, { Dayjs } from 'dayjs' | ||
import { type MaybeRefOrGetter, toValue } from 'vue' | ||
import { parseTime, toNow } from 'src/utils/time' | ||
|
||
/** | ||
* 返回一个定时刷新的 'xx天前' 文案 | ||
* | ||
* @example | ||
* ```ts | ||
* const book = ref<{ LastUpdateTime: Date }>({ LastUpdateTime: new Date() }) | ||
* onMounted(async () => { | ||
* Promise.resolve().then(() => { | ||
* book.value.LastUpdateTime = new Date() | ||
* }) | ||
* }) | ||
* | ||
* const lastUpdateTimeSourceRef = computed(() => book.value.LastUpdateTime) | ||
* const lastUpdateTime = useToNow(lastUpdateTimeSourceRef) | ||
* | ||
* // 这样合起来写也行↓ | ||
* // const lastUpdateTime = useToNow(() => book.value.LastUpdateTime) | ||
* | ||
* return { lastUpdateTime } | ||
* ``` | ||
*/ | ||
export function useToNowRef(dateGetter: MaybeRefOrGetter<Date | Dayjs | undefined | null>): ComputedRef<string> { | ||
const dateRef = computed<Dayjs | null>(() => { | ||
const dateVal = toValue(dateGetter) | ||
if (!dateVal) { | ||
return null | ||
} | ||
|
||
return parseTime(dateVal) | ||
}) | ||
|
||
const nowRef = shallowRef(dayjs()) | ||
// 刷新nowRef | ||
watchEffect((onClean) => { | ||
const date = unref(dateRef) | ||
const now = unref(nowRef) | ||
|
||
// 不是同一天的就不更新了 | ||
if (!date || !date.isSame(now, 'day')) { | ||
return | ||
} | ||
|
||
// 秒级别的差异,每半分钟刷新一次 | ||
// => "x秒前" | ||
if (date.diff(now, 'second') < 60) { | ||
const timeout = setTimeout(() => { | ||
nowRef.value = dayjs() | ||
}, 1_000 * 1) | ||
|
||
onClean(() => clearTimeout(timeout)) | ||
return | ||
} | ||
|
||
// 分钟级别的差异,每分钟刷新一次 | ||
// => "x分钟前" | ||
if (date.diff(now, 'minute') < 60) { | ||
const timeout = setTimeout(() => { | ||
nowRef.value = dayjs() | ||
}, 1_000 * 60) | ||
onClean(() => clearTimeout(timeout)) | ||
return | ||
} | ||
|
||
// 小时级别的差异,每半小时刷新一次 | ||
// => "x小时前" | ||
const timeout = setTimeout(() => { | ||
nowRef.value = dayjs() | ||
}, 1_000 * 60 * 60) | ||
onClean(() => clearTimeout(timeout)) | ||
return | ||
}) | ||
|
||
return computed(() => { | ||
// console.log('re-calc toNow', dateRef.value.format(), nowRef.value.format()) | ||
return dateRef.value ? toNow(dateRef.value, { now: nowRef.value }) : null | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 全局初始化内容 | ||
import dayjs from 'dayjs' | ||
import zh from 'dayjs/locale/zh' | ||
|
||
import relativeTime from 'dayjs/plugin/relativeTime' | ||
import updateLocale from 'dayjs/plugin/updateLocale' | ||
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter' | ||
|
||
dayjs.extend(relativeTime) | ||
dayjs.extend(isSameOrAfter) | ||
dayjs.extend(updateLocale) | ||
|
||
dayjs.locale(zh) | ||
dayjs.updateLocale(zh.name, { relativeTime: { ...zh.relativeTime, s: '%d秒' } }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,39 @@ | ||
import dayjs, { Dayjs } from 'dayjs' | ||
import relativeTime from 'dayjs/plugin/relativeTime' | ||
dayjs.extend(relativeTime) | ||
|
||
/** | ||
* 解析时间 | ||
* | ||
* @param date 接受js时间对象、ISO字符串、luxon对象 | ||
* @param date 接受js时间对象、ISO字符串、dayjs对象 | ||
*/ | ||
export function parseTime(date: Date | Dayjs | string): Dayjs { | ||
// 字符串格式的时间戳会parse成错误的时间,但目前没有这种场景,先注释,省点 | ||
// if (typeof date === 'string' && date === (+date).toString()) { | ||
// date = +date | ||
// } | ||
|
||
return dayjs(date) | ||
} | ||
|
||
/** 获取时间相对目前的文案描述 */ | ||
/** | ||
* 获取时间相对目前的文案描述 | ||
* | ||
* @url https://day.js.org/docs/en/display/from-now#list-of-breakdown-range | ||
*/ | ||
export function toNow( | ||
date: Date | Dayjs, | ||
config: { | ||
base?: Dayjs | ||
locale?: string | ||
now?: Dayjs | ||
notNegative?: boolean | ||
} = { | ||
notNegative: true | ||
} | ||
): string { | ||
const { base, locale, notNegative } = config | ||
if (notNegative && parseTime(date).isAfter(dayjs())) { | ||
const { now = dayjs(), notNegative } = config | ||
const dateObj = parseTime(date) | ||
|
||
if (notNegative && dateObj.isSameOrAfter(now, 'second')) { | ||
return '刚刚' | ||
} | ||
|
||
return parseTime(date).to(base) | ||
return now.to(dateObj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters