Skip to content

Commit

Permalink
新增帖子url配置方式
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry committed Jun 7, 2024
1 parent b0f1299 commit 82ada38
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 43 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ngClass",
"ui"
],
"editor.fontSize": 16,
"editor.fontSize": 14,
"editor.fontFamily": "'Cascadia Code','LXGW WenKai','Courier New', monospace",
// Enable the ESlint flat config support
// (remove this if your ESLint extension above v3.0.5)
Expand Down Expand Up @@ -68,5 +68,6 @@
"scss",
"pcss",
"postcss"
]
],
"terminal.integrated.fontSize": 14
}
6 changes: 5 additions & 1 deletion assets/css/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@

.dark .md-editor-preview img{
border-color: rgb(51 65 85) !important;
}
}

.comment:target,.comment:target .md-editor-preview{
background-color: rgb(241,245,249);
}
2 changes: 1 addition & 1 deletion components/XComment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function openModal(message: string) {
</script>

<template>
<div class="px-4 flex space-x-2 items-start py-2 ">
<div :id="`${props.floor}`" class="px-4 flex space-x-2 items-start py-2 comment">
<NuxtLink :to="`/member/${author.username}`">
<UAvatar v-if="author && author.avatarUrl" :src="getAvatarUrl(author.avatarUrl, author.headImg)" size="lg" alt="Avatar" />
</NuxtLink>
Expand Down
16 changes: 7 additions & 9 deletions components/XTagList.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
<script lang="ts" setup>
import type { TagDTO } from '~/types'
const route = useRoute()
const props = defineProps<{
selected?: string
}>()
const tagRes = await useFetch('/api/go/list?hot=true', {
method: 'POST',
key: 'tagLists',
})
console.log('selected', props.selected)
const tagList = computed(() => {
return tagRes.data.value?.tags as any as TagDTO[]
})
const selectedTag = ref('')
watch(() => route.fullPath, () => {
selectedTag.value = route.params.tag as string
}, { immediate: true })
</script>

<template>
<div class="flex text-sm">
<div class="flex space-x-2 flex-1 overflow-auto">
<UBadge
v-for="tag in tagList" :key="tag.name" :color="selectedTag === tag.name ? 'primary' : 'gray'" variant="solid" size="md"
v-for="tag in tagList" :key="tag.name" :color="selected === tag.enName ? 'primary' : 'gray'" variant="solid" size="md"
class="shrink-0 cursor-pointer "
>
<NuxtLink :to="`/go/${tag.enName}`">
Expand All @@ -34,7 +32,7 @@ watch(() => route.fullPath, () => {
暂无标签,请去后台添加
</div>
</div>
<UBadge :color="selectedTag === 'all' ? 'primary' : 'gray'" variant="solid" size="md" class="cursor-pointer w-[78px]">
<UBadge :color="selected === 'all' ? 'primary' : 'gray'" variant="solid" size="md" class="cursor-pointer w-[78px]">
<NuxtLink to="/tags">
全部节点
</NuxtLink>
Expand Down
10 changes: 9 additions & 1 deletion components/XUserCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ async function signIn() {
<template #header>
<div class="flex gap-4 items-center">
<NuxtLink :to="`/member/${userinfo.username}`">
<UAvatar :src="getAvatarUrl(userinfo.avatarUrl!, userinfo.headImg)" size="lg" alt="Avatar" />
<UAvatar
v-if="userinfo.unRead > 0"
chip-color="rose"
:chip-text="userinfo.unRead"
chip-position="top-right" :src="getAvatarUrl(userinfo.avatarUrl!, userinfo.headImg)" size="lg" alt="Avatar"
/>
<UAvatar
v-else :src="getAvatarUrl(userinfo.avatarUrl!, userinfo.headImg)" size="lg" alt="Avatar"
/>
</NuxtLink>
<div class="flex flex-col text-sm gap-1">
<div class="flex justify-between">
Expand Down
37 changes: 18 additions & 19 deletions layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ watch(token, async () => {
}
})
watch(() => route.fullPath, async (n) => {
if (n.startsWith('/post/') || n === '/') {
userCardChanged.emit()
}
})
await loadProfile()
if (sysconfig.css) {
Expand Down Expand Up @@ -108,7 +102,11 @@ useHead({
})
const tag = ref<TagDTO>()
watch(() => route.fullPath, async (n) => {
if (n.startsWith('/post/') || n === '/') {
userCardChanged.emit()
}
})
watch(() => route.fullPath, async () => {
if (route.fullPath.startsWith('/go/')) {
const name = route.fullPath.replaceAll('/go/', '')
Expand All @@ -126,30 +124,31 @@ watch(() => route.fullPath, async () => {
<div class="p-4 flex-1 space-y-4 bg-slate-700">
<UIcon name="i-carbon-close-large" class="size-5 text-white" @click="sliderOpen = false" />
<XUserCard v-if="userinfo && userinfo.username" />
<UCard
v-if="route.fullPath.startsWith('/go/') && tag" class="w-full mt-2"
:ui="{ header: { padding: 'px-0 py-0 sm:px-0' } }"
>

<UCard v-if="sysconfig" class="w-full mt-2" :ui="{ header: { padding: 'px-0 py-0 sm:px-0' } }">
<template #header>
<div class="px-4 py-1 rounded-t sm:px-6 text-primary bg-gray-100 dark:bg-slate-500">
{{ tag.name }}
关于本站
</div>
</template>
<div class="text-sm">
{{ tag.desc }}
<MdPreview
:model-value="sysconfig.websiteAnnouncement" editor-id="websiteAnnouncement" no-mermaid no-katex
no-highlight
/>
</div>
</UCard>
<UCard v-if="sysconfig" class="w-full mt-2" :ui="{ header: { padding: 'px-0 py-0 sm:px-0' } }">
<UCard
v-if="route.fullPath.startsWith('/go/') && tag" class="w-full mt-2"
:ui="{ header: { padding: 'px-0 py-0 sm:px-0' } }"
>
<template #header>
<div class="px-4 py-1 rounded-t sm:px-6 text-primary bg-gray-100 dark:bg-slate-500">
关于本站
{{ tag.name }}
</div>
</template>
<div class="text-sm">
<MdPreview
:model-value="sysconfig.websiteAnnouncement" editor-id="websiteAnnouncement" no-mermaid no-katex
no-highlight
/>
{{ tag.desc }}
</div>
</UCard>
<XHotUser />
Expand Down
2 changes: 1 addition & 1 deletion pages/go/[tag].vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ useHead({
<template>
<UCard class="w-full mt-2" style="min-height: 300px;" :ui="{ body: { padding: 'px-0 sm:p-0' }, header: { padding: ' py-2 sm:px-4 px-2' } }">
<template #header>
<XTagList />
<XTagList :selected="state.tag" />
</template>
<div class="flex flex-col divide-y divide-gray-100">
<XPost v-for="post in postList" :key="post.pid" :show-avatar="true" v-bind="post" />
Expand Down
24 changes: 24 additions & 0 deletions pages/manage/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const state = reactive({
websiteAnnouncement: ``,
css: '',
js: '',
postUrlFormat: {
type: 'UUID',
minNumber: 10000,
dateFormat: 'YYYYMMDDHHmmssSSS',
},
})
Object.assign(state, JSON.parse(configData.value?.config as string))
Expand All @@ -59,6 +64,8 @@ async function saveSettings() {
await refresh()
toast.success('保存成功')
}
const postUrlFormatOptions = [{ value: 'UUID', label: 'UUID' }, { value: 'Number', label: '数字' }, { value: 'Date', label: '日期' }]
</script>

<template>
Expand Down Expand Up @@ -88,6 +95,23 @@ async function saveSettings() {
</UFormGroup>
</div>

<div class="flex flex-row space-x-2">
<UFormGroup label="帖子链接定义" name="type">
<USelectMenu v-model="state.postUrlFormat.type" :options="postUrlFormatOptions" value-attribute="value" option-attribute="label" />
</UFormGroup>
<UFormGroup v-if="state.postUrlFormat.type === 'Number'" label="起始数字" name="minNumber">
<UInput v-model="state.postUrlFormat.minNumber" />
</UFormGroup>
<UFormGroup v-if="state.postUrlFormat.type === 'Date'" label="日期格式" name="dateFormat">
<template #hint>
<ULink class="text-green-600 underline" to="https://day.js.org/docs/zh-CN/display/format" target="_blank">
支持的格式
</ULink>
</template>
<UInput v-model="state.postUrlFormat.dateFormat" />
</UFormGroup>
</div>

<div class="flex flex-row space-x-2">
<UFormGroup label="每次发帖获得积分" name="pointPerPost">
<UInput v-model.number="state.pointPerPost" autocomplete="off" />
Expand Down
4 changes: 2 additions & 2 deletions pages/member/[username].vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ watch(() => route.fullPath, () => {

<NuxtLink class="flex flex-row gap-1 items-center" :to="`/member/${userinfo.username}/point`">
<UBadge size="lg" :color="selectedTab === 'point' ? 'primary' : 'white'" variant="solid" class="space-x-1">
<UIcon name="i-carbon-favorite" />
<UIcon name="i-carbon-model" />
<span>积分({{ userinfo.point }})</span>
</UBadge>
</NuxtLink>

<NuxtLink v-if="currentUser.username === userinfo.username && token" class="flex flex-row gap-1 items-center" :to="`/member/${userinfo.username}/message`">
<UBadge size="lg" :color="selectedTab === 'message' ? 'primary' : 'white'" variant="solid" class="space-x-1">
<UIcon name="i-carbon-favorite" />
<UIcon name="i-carbon-notification" />
<span>消息({{ userinfo._count.ReceiveMessage }})</span>
</UBadge>
</NuxtLink>
Expand Down
4 changes: 2 additions & 2 deletions pages/post/[pid].vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ useHead({
<XPost :show-avatar="true" v-bind="post" @support="doSupport" />
</div>
<div class="px-4 pt-2 leading-5 border-t space-y-2 dark:border-slate-700">
<MdPreview v-model="post.content" :editor-id="post.pid" no-mermaid no-katex />
<MdPreview v-model="post.content" :editor-id="`pv-${post.pid}`" no-mermaid no-katex />
</div>

<div class="px-4 flex justify-end pb-2 items-center space-x-2 my-2">
Expand All @@ -116,7 +116,7 @@ useHead({
</div>

<div class=" gap-2 divide-y divide-gray-300 dark:divide-gray-700 border-t dark:border-gray-700">
<XComment v-for="(comment, index) in post.comments" :key="comment.cid" v-bind="comment" :index="index" />
<XComment v-for="(comment, index) in post.comments" :id="comment.floor" :key="comment.cid" v-bind="comment" :index="index" />
<UPagination
v-if="totalComments > state.size" v-model="state.page" size="sm" class="p-4" :to="(page: number) => ({
query: { page },
Expand Down
2 changes: 1 addition & 1 deletion server/api/go/list.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
where.hot = true
}
if (name) {
where.name = name
where.enName = name
}
const tags = await prisma.tag.findMany({
where,
Expand Down
11 changes: 10 additions & 1 deletion server/api/post/[pid].ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ export default defineEventHandler(async (event) => {
},
tag: true,
comments: {
include: {
select: {
id: false,
createdAt: true,
floor: true,
content: true,
pid: true,
uid: true,
cid: true,
updatedAt: true,
mentioned: true,
post: {
select: {
pid: true,
Expand Down
21 changes: 18 additions & 3 deletions server/api/post/new.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,24 @@ export default defineEventHandler(async (event) => {
if (user.point <= 0) {
throw createError('用户积分小于或等于0分,不能发帖')
}
const pid = `p${randomId()}`
let pid = `p${randomId()}`
const sysConfig = await prisma.sysConfig.findFirst()
const sysConfigDTO = JSON.parse(sysConfig?.content as string) as unknown as SysConfigDTO

if (sysConfigDTO.postUrlFormat) {
const { type, minNumber, dateFormat } = sysConfigDTO.postUrlFormat
if (type === 'Number') {
const res = await prisma.post.aggregate({
_max: {
id: true,
},
})
pid = `${(res._max.id ?? 0) + 1 + (minNumber ?? 0)}`
}
else if (sysConfigDTO.postUrlFormat?.type === 'Date') {
pid = `${dayjs().format(dateFormat)}`
}
}

try {
await prisma.post.upsert({
Expand Down Expand Up @@ -73,8 +90,6 @@ export default defineEventHandler(async (event) => {
},
})

const sysConfig = await prisma.sysConfig.findFirst()
const sysConfigDTO = JSON.parse(sysConfig?.content as string) as unknown as SysConfigDTO
let {
_sum: { point: totalToday },
} = await prisma.pointHistory.aggregate({
Expand Down
5 changes: 5 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ export interface SysConfigDTO {
websiteAnnouncement: string
css: string
js: string
postUrlFormat: {
type: 'UUID' | 'Date' | 'Number'
minNumber: number
dateFormat: string
}
}

export interface MessageDTO {
Expand Down

0 comments on commit 82ada38

Please sign in to comment.