Files
arcticnewone/app/components/common/CommentSection.vue
T
virtheart 5c3f3bca63 feat: 全量落地后端与功能闭环
- 真实后端(server/):SQLite 存储 + scrypt 会话鉴权 + SMTP 邮件
  - auth 注册/登录/登出/me/资料更新
  - contact 联系表单(落库+邮件)、newsletter 订阅(去重)
  - favorites 作品收藏、comments 新闻评论、account 管理收件箱
- 用户系统 UI:个人中心 /account(资料/收藏/评论/管理员面板)、
  收藏按钮、评论组件、登录/注册弹窗接真实 API(useApiError 错误映射)
- 内容单点事实源:app/data/news|work 消除三处重复,页面全部引用
- 修复断链:首页动态/作品卡可点击、分享按钮真实动作、移除 # 死链、
  语言切换器、社交链接统一 siteConfig
- SEO:动态 sitemap/robots、JSON-LD、脚本生成真实 og-image.png
- 列表增强:搜索 + 加载更多
- 部署自动化:build-and-push.sh 支持 git tag 版本化镜像;
  deploy.sh 一条命令构建推送+注入 Secret/ConfigMap+kubectl 滚动升级;
  deployment 挂 PVC 持久化;Dockerfile 支持 better-sqlite3 原生编译
- 组件注册去目录前缀,修复 AppLoader/AnimatedButton 等未渲染问题
2026-08-06 15:08:34 +08:00

155 lines
5.1 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<section class="mt-16">
<h2 class="text-2xl md:text-3xl font-bold text-black mb-8">{{ t('comments.title') }}</h2>
<!-- 登录提示 -->
<div
v-if="!userStore.isAuthenticated"
class="p-6 rounded-2xl bg-gray-50 border border-gray-200 text-center"
>
<p class="text-gray-600 mb-4">{{ t('comments.loginToComment') }}</p>
<button
@click="authModal.openLogin()"
class="px-6 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
{{ t('auth.login') }}
</button>
</div>
<!-- 评论表单(已登录) -->
<form v-else @submit.prevent="handleSubmit" class="mb-10">
<div class="flex items-start gap-4">
<img
:src="userStore.avatar"
:alt="userStore.user?.username"
class="w-10 h-10 rounded-full object-cover flex-shrink-0"
/>
<div class="flex-1">
<textarea
v-model="content"
rows="3"
maxlength="2000"
:placeholder="t('comments.placeholder')"
:disabled="isSubmitting"
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 resize-none disabled:opacity-50"
></textarea>
<div class="flex items-center justify-between mt-3">
<span v-if="errorMessage" class="text-sm text-red-600">{{ errorMessage }}</span>
<button
type="submit"
:disabled="isSubmitting || !content.trim()"
class="ml-auto px-6 py-2.5 rounded-full bg-black text-white text-sm font-semibold flex items-center gap-2 transition-all"
:class="isSubmitting || !content.trim() ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-800'"
>
<Icon v-if="isSubmitting" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ isSubmitting ? t('comments.submitting') : t('comments.submit') }}
</button>
</div>
</div>
</div>
</form>
<!-- 评论列表 -->
<div v-if="comments.length" class="space-y-6">
<div
v-for="comment in comments"
:key="comment.id"
class="flex items-start gap-4 p-5 rounded-2xl bg-gray-50 border border-gray-100"
>
<img
:src="avatarUrl(comment.author.username)"
:alt="comment.author.username"
class="w-10 h-10 rounded-full object-cover flex-shrink-0"
/>
<div class="flex-1 min-w-0">
<div class="flex items-center justify-between gap-3 mb-1">
<p class="text-sm font-semibold text-black">{{ comment.author.username }}</p>
<p class="text-xs text-gray-400 shrink-0">{{ formatDate(comment.createdAt) }}</p>
</div>
<p class="text-gray-700 text-sm md:text-base leading-relaxed break-words">{{ comment.content }}</p>
</div>
</div>
</div>
<div v-else-if="!loading && userStore.isAuthenticated" class="text-center text-gray-500 py-8">
{{ t('comments.empty') }}
</div>
</section>
</template>
<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUserStore } from '~/stores/user'
import { useAuthModalStore } from '~/stores/authModal'
const props = defineProps<{ newsSlug: string }>()
const { t, locale } = useI18n()
const userStore = useUserStore()
const authModal = useAuthModalStore()
interface CommentItem {
id: number
content: string
createdAt: string
author: { username: string }
}
const comments = ref<CommentItem[]>([])
const content = ref('')
const isSubmitting = ref(false)
const isLoading = ref(true)
const errorMessage = ref('')
const avatarUrl = (name: string) =>
`https://ui-avatars.com/api/?name=${encodeURIComponent(name)}&background=000&color=fff&size=128`
const formatDate = (raw: string) => {
const d = new Date(String(raw).replace(' ', 'T') + 'Z')
if (Number.isNaN(d.getTime())) return raw
return new Intl.DateTimeFormat(locale.value === 'zh' ? 'zh-CN' : 'en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}).format(d)
}
const fetchComments = async () => {
isLoading.value = true
try {
const data = await $fetch<{ comments: CommentItem[] }>('/api/comments', {
query: { newsSlug: props.newsSlug },
})
comments.value = data.comments
} catch (err) {
console.error('fetch comments error:', err)
} finally {
isLoading.value = false
}
}
const handleSubmit = async () => {
if (isSubmitting.value || !content.value.trim()) return
isSubmitting.value = true
errorMessage.value = ''
try {
await $fetch('/api/comments', {
method: 'POST',
body: { newsSlug: props.newsSlug, content: content.value.trim() },
})
content.value = ''
await fetchComments()
} catch (err) {
console.error('post comment error:', err)
errorMessage.value = t('comments.commentError')
} finally {
isSubmitting.value = false
}
}
onMounted(fetchComments)
watch(() => props.newsSlug, fetchComments)
</script>