提交 | 用户 | 时间
|
820397
|
1 |
import { useTimeAgo as useTimeAgoCore, UseTimeAgoMessages } from '@vueuse/core' |
H |
2 |
import { useLocaleStoreWithOut } from '@/store/modules/locale' |
|
3 |
|
|
4 |
const TIME_AGO_MESSAGE_MAP: { |
|
5 |
'zh-CN': UseTimeAgoMessages |
|
6 |
en: UseTimeAgoMessages |
|
7 |
} = { |
|
8 |
// @ts-ignore |
|
9 |
'zh-CN': { |
|
10 |
justNow: '刚刚', |
|
11 |
past: (n) => (n.match(/\d/) ? `${n}前` : n), |
|
12 |
future: (n) => (n.match(/\d/) ? `${n}后` : n), |
|
13 |
month: (n, past) => (n === 1 ? (past ? '上个月' : '下个月') : `${n} 个月`), |
|
14 |
year: (n, past) => (n === 1 ? (past ? '去年' : '明年') : `${n} 年`), |
|
15 |
day: (n, past) => (n === 1 ? (past ? '昨天' : '明天') : `${n} 天`), |
|
16 |
week: (n, past) => (n === 1 ? (past ? '上周' : '下周') : `${n} 周`), |
|
17 |
hour: (n) => `${n} 小时`, |
|
18 |
minute: (n) => `${n} 分钟`, |
|
19 |
second: (n) => `${n} 秒` |
|
20 |
}, |
|
21 |
// @ts-ignore |
|
22 |
en: { |
|
23 |
justNow: 'just now', |
|
24 |
past: (n) => (n.match(/\d/) ? `${n} ago` : n), |
|
25 |
future: (n) => (n.match(/\d/) ? `in ${n}` : n), |
|
26 |
month: (n, past) => |
|
27 |
n === 1 ? (past ? 'last month' : 'next month') : `${n} month${n > 1 ? 's' : ''}`, |
|
28 |
year: (n, past) => |
|
29 |
n === 1 ? (past ? 'last year' : 'next year') : `${n} year${n > 1 ? 's' : ''}`, |
|
30 |
day: (n, past) => (n === 1 ? (past ? 'yesterday' : 'tomorrow') : `${n} day${n > 1 ? 's' : ''}`), |
|
31 |
week: (n, past) => |
|
32 |
n === 1 ? (past ? 'last week' : 'next week') : `${n} week${n > 1 ? 's' : ''}`, |
|
33 |
hour: (n) => `${n} hour${n > 1 ? 's' : ''}`, |
|
34 |
minute: (n) => `${n} minute${n > 1 ? 's' : ''}`, |
|
35 |
second: (n) => `${n} second${n > 1 ? 's' : ''}` |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
export const useTimeAgo = (time: Date | number | string) => { |
|
40 |
const localeStore = useLocaleStoreWithOut() |
|
41 |
|
|
42 |
const currentLocale = computed(() => localeStore.getCurrentLocale) |
|
43 |
|
|
44 |
const timeAgo = useTimeAgoCore(time, { |
|
45 |
messages: TIME_AGO_MESSAGE_MAP[unref(currentLocale).lang] |
|
46 |
}) |
|
47 |
|
|
48 |
return timeAgo |
|
49 |
} |