From 52ae87e4f40318246288b93ebf0091277415efdc Mon Sep 17 00:00:00 2001
From: Tim <135014430+nagisa77@users.noreply.github.com>
Date: Wed, 9 Jul 2025 18:48:06 +0800
Subject: [PATCH] feat(frontend): add time manager and unify date formats
---
open-isle-cli/src/components/CommentItem.vue | 5 +--
open-isle-cli/src/utils/time.js | 32 ++++++++++++++++++++
open-isle-cli/src/views/HomePageView.vue | 3 +-
open-isle-cli/src/views/MessagePageView.vue | 5 +--
open-isle-cli/src/views/PostPageView.vue | 5 +--
open-isle-cli/src/views/ProfileView.vue | 3 +-
6 files changed, 45 insertions(+), 8 deletions(-)
create mode 100644 open-isle-cli/src/utils/time.js
diff --git a/open-isle-cli/src/components/CommentItem.vue b/open-isle-cli/src/components/CommentItem.vue
index 17ae9e420..9674f4291 100644
--- a/open-isle-cli/src/components/CommentItem.vue
+++ b/open-isle-cli/src/components/CommentItem.vue
@@ -66,6 +66,7 @@ import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import CommentEditor from './CommentEditor.vue'
import { renderMarkdown } from '../utils/markdown'
+import TimeManager from '../utils/time'
import BaseTimeline from './BaseTimeline.vue'
import { API_BASE_URL, toast } from '../main'
import { getToken } from '../utils/auth'
@@ -124,14 +125,14 @@ const CommentItem = {
replyList.push({
id: data.id,
userName: data.author.username,
- time: new Date(data.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }),
+ time: TimeManager.format(data.createdAt),
avatar: data.author.avatar,
text: data.content,
reactions: [],
reply: (data.replies || []).map(r => ({
id: r.id,
userName: r.author.username,
- time: new Date(r.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }),
+ time: TimeManager.format(r.createdAt),
avatar: r.author.avatar,
text: r.content,
reactions: r.reactions || [],
diff --git a/open-isle-cli/src/utils/time.js b/open-isle-cli/src/utils/time.js
new file mode 100644
index 000000000..4f722f742
--- /dev/null
+++ b/open-isle-cli/src/utils/time.js
@@ -0,0 +1,32 @@
+export default class TimeManager {
+ static format(input) {
+ const date = new Date(input)
+ if (Number.isNaN(date.getTime())) return ''
+
+ const now = new Date()
+ const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate())
+ const startOfDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
+ const diffDays = Math.floor((startOfToday - startOfDate) / 86400000)
+
+ const hh = date.getHours().toString().padStart(2, '0')
+ const mm = date.getMinutes().toString().padStart(2, '0')
+ const timePart = `${hh}:${mm}`
+
+ if (diffDays === 0) return `今天 ${timePart}`
+ if (diffDays === 1) return `昨天 ${timePart}`
+ if (diffDays === 2) return `两天前 ${timePart}`
+
+ const month = date.getMonth() + 1
+ const day = date.getDate()
+
+ if (date.getFullYear() === now.getFullYear()) {
+ return `${month}.${day} ${timePart}`
+ }
+
+ if (date.getFullYear() === now.getFullYear() - 1) {
+ return `去年 ${month}.${day} ${timePart}`
+ }
+
+ return `${date.getFullYear()}.${month}.${day} ${timePart}`
+ }
+}
diff --git a/open-isle-cli/src/views/HomePageView.vue b/open-isle-cli/src/views/HomePageView.vue
index ced03100b..5d1c6e5a9 100644
--- a/open-isle-cli/src/views/HomePageView.vue
+++ b/open-isle-cli/src/views/HomePageView.vue
@@ -102,6 +102,7 @@
import { ref, onMounted } from 'vue'
import { stripMarkdown } from '../utils/markdown'
import { API_BASE_URL } from '../main'
+import TimeManager from '../utils/time'
import CategorySelect from '../components/CategorySelect.vue'
import TagSelect from '../components/TagSelect.vue'
import ArticleTags from '../components/ArticleTags.vue'
@@ -152,7 +153,7 @@ export default {
members: [],
comments: (p.comments || []).length,
views: p.views,
- time: new Date(p.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' })
+ time: TimeManager.format(p.createdAt)
}))
)
if (data.length < pageSize) {
diff --git a/open-isle-cli/src/views/MessagePageView.vue b/open-isle-cli/src/views/MessagePageView.vue
index 8b841a4ec..5705ddb58 100644
--- a/open-isle-cli/src/views/MessagePageView.vue
+++ b/open-isle-cli/src/views/MessagePageView.vue
@@ -51,7 +51,7 @@
- {{ new Date(item.createdAt).toLocaleString() }}
+ {{ TimeManager.format(item.createdAt) }}
@@ -67,6 +67,7 @@ import { getToken } from '../utils/auth'
import { markNotificationsRead } from '../utils/notification'
import { toast } from '../main'
import { stripMarkdown } from '../utils/markdown'
+import TimeManager from '../utils/time'
import { hatch } from 'ldrs'
hatch.register()
@@ -163,7 +164,7 @@ export default {
onMounted(fetchNotifications)
- return { notifications, formatType, sanitizeDescription, isLoadingMessage, markRead }
+ return { notifications, formatType, sanitizeDescription, isLoadingMessage, markRead, TimeManager }
}
}
diff --git a/open-isle-cli/src/views/PostPageView.vue b/open-isle-cli/src/views/PostPageView.vue
index 9926e98d7..683da5662 100644
--- a/open-isle-cli/src/views/PostPageView.vue
+++ b/open-isle-cli/src/views/PostPageView.vue
@@ -82,6 +82,7 @@ import ReactionsGroup from '../components/ReactionsGroup.vue'
import { renderMarkdown } from '../utils/markdown'
import { API_BASE_URL, toast } from '../main'
import { getToken } from '../utils/auth'
+import TimeManager from '../utils/time'
import { hatch } from 'ldrs'
import { useRouter } from 'vue-router'
hatch.register()
@@ -129,7 +130,7 @@ export default {
const mapComment = c => ({
id: c.id,
userName: c.author.username,
- time: new Date(c.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }),
+ time: TimeManager.format(c.createdAt),
avatar: c.author.avatar,
text: c.content,
reactions: c.reactions || [],
@@ -181,7 +182,7 @@ export default {
tags.value = data.tags || []
postReactions.value = data.reactions || []
comments.value = (data.comments || []).map(mapComment)
- postTime.value = new Date(data.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' })
+ postTime.value = TimeManager.format(data.createdAt)
await nextTick()
gatherPostItems()
} catch (e) {
diff --git a/open-isle-cli/src/views/ProfileView.vue b/open-isle-cli/src/views/ProfileView.vue
index 631de6a88..18b2c8d02 100644
--- a/open-isle-cli/src/views/ProfileView.vue
+++ b/open-isle-cli/src/views/ProfileView.vue
@@ -181,6 +181,7 @@ import { useRoute } from 'vue-router'
import { API_BASE_URL } from '../main'
import BaseTimeline from '../components/BaseTimeline.vue'
import { stripMarkdown } from '../utils/markdown'
+import TimeManager from '../utils/time'
import { hatch } from 'ldrs'
hatch.register()
@@ -200,7 +201,7 @@ export default {
const formatDate = (d) => {
if (!d) return ''
- return new Date(d).toLocaleDateString('zh-CN', { year: 'numeric', month: 'numeric', day: 'numeric' })
+ return TimeManager.format(d)
}
const fetchData = async () => {