Skip to content

Commit

Permalink
feat: 引入 dayjs 统一日期格式
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhana committed Nov 23, 2024
1 parent 5376d12 commit 364fb66
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion components/main/Header/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ onUnmounted(() => {
</HanaDropdown>
<HanaDropdown v-if="userStore.loggedIn" animation="slide" offset="end" :show-arrow="false" @command="handleUserCommand">
<NuxtImg v-if="userStore.userInfo!.avatar" :src="userStore.userInfo!.avatar" class="size-8 cursor-pointer rounded-full" />
<div v-else class="size-8 cursor-pointer rounded-full bg-hana-blue text-center text-2xl text-white">
<div v-else class="flex size-8 cursor-pointer items-center justify-center rounded-full bg-hana-blue text-xl text-white">
<span>{{ userStore.userInfo!.username[0] }}</span>
</div>
<template #dropdown>
Expand Down
3 changes: 2 additions & 1 deletion components/thoughts/Input.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import { useStore } from '~/store'
import type { MessageItem } from '~/types/message'
Expand All @@ -19,7 +20,7 @@ function handlePublish() {
site: '/about',
avatar: '/images/avatar.webp',
},
publishedAt: new Date().toLocaleString(),
publishedAt: dayjs().format('YYYY-MM-DD HH:mm:ss'),
editedAt: '',
isMe: true,
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@vueuse/nuxt": "^11.1.0",
"bcrypt": "^5.1.1",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
"dotenv": "^16.4.5",
"eslint": "^9.12.0",
"eslint-plugin-tailwindcss": "^3.17.4",
Expand Down
5 changes: 3 additions & 2 deletions pages/articles/[article].vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import dayjs from 'dayjs'
import type { ArticleHeader } from '~/types/content'
definePageMeta({
Expand All @@ -17,8 +18,8 @@ const articleHeader = computed<ArticleHeader>(() => ({
ogImage: article.value?.ogImage || '/images/not-found.webp',
tags: article.value?.tags || [],
category: article.value?.category || '未分类',
publishedAt: new Date(article.value?.publishedAt || '').toLocaleDateString(),
editedAt: new Date(article.value?.editedAt || '').toLocaleDateString(),
publishedAt: dayjs(article.value?.publishedAt).format('YYYY-MM-DD'),
editedAt: dayjs(article.value?.editedAt).format('YYYY-MM-DD'),
published: article.value?.published || false,
wordCount: article.value?.wordCount || 0,
}))
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion scripts/newArticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ rl.on('close', () => {
console.log(`New article created at ${newArticlePath}`)
})

// Date -> YYYY-MM-DD HH:MM:SS
function formatDate() {
const now = new Date()

const year = now.getFullYear()
const month = String(now.getMonth() + 1).padStart(2, '0') // 月份从0开始
const month = String(now.getMonth() + 1).padStart(2, '0')
const day = String(now.getDate()).padStart(2, '0')
const hours = String(now.getHours()).padStart(2, '0')
const minutes = String(now.getMinutes()).padStart(2, '0')
Expand Down
5 changes: 3 additions & 2 deletions server/api/articles/count.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as p from '@prisma/client'
import dayjs from 'dayjs'
import prisma from '~/lib/prisma'
import type { ArticleCountQuery } from '~/server/types/articles'
import { formattedEventHandler } from '~/server/utils/formattedEventHandler'
Expand All @@ -8,8 +9,8 @@ type Options = p.Prisma.ArticleWhereInput & { publishedAtMonth?: string }
async function selectArticleCount(options: Options) {
const { publishedAtMonth, ...rest } = options
if (publishedAtMonth) {
const curMonth = new Date(publishedAtMonth)
const nextMonth = new Date(curMonth.getFullYear(), curMonth.getMonth() + 1)
const curMonth = dayjs(publishedAtMonth).toDate()
const nextMonth = dayjs(publishedAtMonth).add(1, 'month').toDate()
rest.publishedAt = { gte: curMonth, lt: nextMonth }
}
return await prisma.article.count({ where: rest })
Expand Down
9 changes: 5 additions & 4 deletions server/api/articles/list.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as p from '@prisma/client'
import dayjs from 'dayjs'
import prisma from '~/lib/prisma'
import type { ArticleListQuery } from '~/server/types/articles'
import { formattedEventHandler } from '~/server/utils/formattedEventHandler'
Expand All @@ -13,8 +14,8 @@ type Options = p.Prisma.ArticleWhereInput
async function selectArticleList(options: Options) {
const { page, pageSize, publishedAtMonth, ...rest } = options
if (publishedAtMonth) {
const curMonth = new Date(publishedAtMonth)
const nextMonth = new Date(curMonth.getFullYear(), curMonth.getMonth() + 1)
const curMonth = dayjs(publishedAtMonth).toDate()
const nextMonth = dayjs(publishedAtMonth).add(1, 'month').toDate()
rest.publishedAt = { gte: curMonth, lt: nextMonth }
}
const retrievedRes = await prisma.article.findMany({
Expand All @@ -27,8 +28,8 @@ async function selectArticleList(options: Options) {
const result = retrievedRes.map(article => ({
...article,
tags: article.tags.map(tag => tag.name),
publishedAt: article.publishedAt.toISOString(),
editedAt: article.editedAt.toISOString(),
publishedAt: dayjs(article.publishedAt).format('YYYY-MM-DD'),
editedAt: dayjs(article.editedAt).format('YYYY-MM-DD'),
}))
return result
}
Expand Down

0 comments on commit 364fb66

Please sign in to comment.