5c3f3bca63
- 真实后端(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 等未渲染问题
309 lines
13 KiB
Vue
309 lines
13 KiB
Vue
<template>
|
|
<main class="page-container min-h-screen pt-32 pb-20 px-4 md:px-8 mx-auto max-w-[1200px]">
|
|
<!-- 未登录 -->
|
|
<div
|
|
v-if="!userStore.isAuthenticated"
|
|
class="max-w-md mx-auto mt-16 p-10 rounded-3xl bg-white border border-gray-100 shadow-xl text-center"
|
|
>
|
|
<div class="w-16 h-16 mx-auto rounded-full bg-gray-100 flex items-center justify-center mb-6">
|
|
<Icon name="ph:user-duotone" class="w-8 h-8 text-gray-400" />
|
|
</div>
|
|
<h1 class="text-2xl font-bold text-black mb-3">{{ t('account.title') }}</h1>
|
|
<p class="text-gray-500 mb-8">{{ t('auth.loginRequired') }}</p>
|
|
<button
|
|
@click="authModal.openLogin()"
|
|
class="px-8 py-3 rounded-full bg-black text-white font-semibold hover:bg-gray-800 transition-colors"
|
|
>
|
|
{{ t('auth.login') }}
|
|
</button>
|
|
</div>
|
|
|
|
<!-- 已登录 -->
|
|
<div v-else>
|
|
<div class="mb-10 text-center">
|
|
<img :src="userStore.avatar" :alt="userStore.user?.username" class="w-20 h-20 rounded-full object-cover mx-auto mb-4" />
|
|
<h1 class="text-3xl md:text-4xl font-bold text-black mb-2">{{ userStore.user?.username }}</h1>
|
|
<p class="text-gray-500">{{ userStore.user?.email }}</p>
|
|
</div>
|
|
|
|
<!-- 页签 -->
|
|
<div class="flex justify-center gap-2 md:gap-3 mb-10 flex-wrap">
|
|
<button
|
|
v-for="tab in tabs"
|
|
:key="tab.key"
|
|
@click="switchTab(tab.key)"
|
|
class="px-5 py-2 rounded-full text-sm md:text-base font-semibold transition-all duration-300"
|
|
:class="activeTab === tab.key ? 'bg-black text-white shadow-lg' : 'bg-gray-100 text-gray-700 hover:bg-gray-200'"
|
|
>
|
|
{{ tab.label }}
|
|
</button>
|
|
</div>
|
|
|
|
<!-- 个人资料 -->
|
|
<section v-if="activeTab === 'profile'" class="max-w-lg mx-auto space-y-5">
|
|
<div class="p-6 rounded-2xl bg-white border border-gray-100 shadow-sm space-y-5">
|
|
<div>
|
|
<label class="block text-sm font-semibold text-gray-800 mb-2">{{ t('account.username') }}</label>
|
|
<input v-model="profileForm.username" type="text" 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" />
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-semibold text-gray-800 mb-2">{{ t('account.phone') }}</label>
|
|
<input v-model="profileForm.phone" type="tel" 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" />
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-semibold text-gray-800 mb-2">{{ t('account.email') }}</label>
|
|
<input :value="userStore.user?.email" type="email" disabled class="w-full px-4 py-3 rounded-xl border border-gray-200 bg-gray-50 text-gray-500 cursor-not-allowed" />
|
|
</div>
|
|
<div class="grid grid-cols-2 gap-4 text-sm text-gray-600">
|
|
<div>
|
|
<p class="text-gray-400 mb-1">{{ t('account.joinedAt') }}</p>
|
|
<p class="font-medium text-black">{{ formatDate(userStore.user?.createdAt) }}</p>
|
|
</div>
|
|
<div>
|
|
<p class="text-gray-400 mb-1">{{ t('account.role') }}</p>
|
|
<p class="font-medium text-black">
|
|
{{ userStore.user?.role === 'admin' ? t('account.roleAdmin') : t('account.roleUser') }}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<p v-if="saveMessage" class="text-sm" :class="saveFailed ? 'text-red-600' : 'text-green-600'">{{ saveMessage }}</p>
|
|
<button
|
|
@click="saveProfile"
|
|
:disabled="saving"
|
|
class="w-full py-3 rounded-xl bg-black text-white font-semibold flex items-center justify-center gap-2 transition-colors disabled:opacity-50"
|
|
>
|
|
<Icon v-if="saving" name="ph:circle-notch" class="w-5 h-5 animate-spin" />
|
|
{{ saving ? t('account.saving') : t('account.save') }}
|
|
</button>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 我的收藏 -->
|
|
<section v-if="activeTab === 'favorites'" class="max-w-3xl mx-auto">
|
|
<p v-if="!favorites.length" class="text-center text-gray-500 py-12">{{ t('account.emptyFavorites') }}</p>
|
|
<div v-else class="grid grid-cols-1 sm:grid-cols-2 gap-5">
|
|
<div
|
|
v-for="slug in favorites"
|
|
:key="slug"
|
|
class="group rounded-2xl overflow-hidden bg-white border border-gray-100 shadow-sm hover:shadow-xl transition-all duration-300"
|
|
>
|
|
<NuxtLink :to="`/work/${slug}`" class="block relative aspect-[4/3] overflow-hidden">
|
|
<img :src="workBySlug[slug]?.image" :alt="t(`work.items.${slug}.title`)" class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105" />
|
|
</NuxtLink>
|
|
<div class="p-5 flex items-center justify-between">
|
|
<NuxtLink :to="`/work/${slug}`" class="font-bold text-black group-hover:text-gray-600 transition-colors">
|
|
{{ t(`work.items.${slug}.title`) }}
|
|
</NuxtLink>
|
|
<button
|
|
@click="removeFavorite(slug)"
|
|
class="w-9 h-9 rounded-full bg-gray-50 text-gray-500 hover:bg-red-50 hover:text-red-600 flex items-center justify-center transition-colors"
|
|
aria-label="unfavorite"
|
|
>
|
|
<Icon name="ph:bookmark-simple-fill" class="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 我的评论 -->
|
|
<section v-if="activeTab === 'comments'" class="max-w-2xl mx-auto space-y-4">
|
|
<p v-if="!myComments.length" class="text-center text-gray-500 py-12">{{ t('account.emptyComments') }}</p>
|
|
<div
|
|
v-for="comment in myComments"
|
|
:key="comment.id"
|
|
class="p-5 rounded-2xl bg-white border border-gray-100 shadow-sm"
|
|
>
|
|
<div class="flex items-center justify-between mb-2">
|
|
<NuxtLink :to="`/news/${comment.newsSlug}`" class="text-sm font-semibold text-black hover:text-gray-600">
|
|
{{ t(`news.items.${comment.newsSlug}.title`) }}
|
|
</NuxtLink>
|
|
<span class="text-xs text-gray-400">{{ formatDate(comment.createdAt) }}</span>
|
|
</div>
|
|
<p class="text-gray-700 text-sm break-words">{{ comment.content }}</p>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- 管理面板 -->
|
|
<section v-if="activeTab === 'admin'" class="max-w-4xl mx-auto space-y-12">
|
|
<div>
|
|
<h3 class="text-xl font-bold text-black mb-5">{{ t('account.admin.contacts') }}</h3>
|
|
<p v-if="!adminData.contacts.length" class="text-gray-500 text-center py-8">{{ t('account.admin.emptyContacts') }}</p>
|
|
<div v-else class="space-y-4">
|
|
<div v-for="c in adminData.contacts" :key="c.id" class="p-5 rounded-2xl bg-white border border-gray-100 shadow-sm">
|
|
<div class="flex items-center justify-between mb-2">
|
|
<div class="flex items-center gap-3">
|
|
<span class="font-semibold text-black">{{ c.name }}</span>
|
|
<span class="text-sm text-gray-500">{{ c.email }}</span>
|
|
</div>
|
|
<span class="text-xs px-2 py-1 rounded-full" :class="c.delivered ? 'bg-green-50 text-green-700' : 'bg-amber-50 text-amber-700'">
|
|
{{ c.delivered ? t('account.admin.delivered') : t('account.admin.notDelivered') }}
|
|
</span>
|
|
</div>
|
|
<p class="text-sm font-semibold text-gray-700 mb-1">{{ c.subject }}</p>
|
|
<p class="text-sm text-gray-600 whitespace-pre-line break-words mb-2">{{ c.message }}</p>
|
|
<p class="text-xs text-gray-400">{{ formatDate(c.createdAt) }}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<h3 class="text-xl font-bold text-black mb-5">{{ t('account.admin.subscribers') }}</h3>
|
|
<p v-if="!adminData.subscribers.length" class="text-gray-500 text-center py-8">{{ t('account.admin.emptySubscribers') }}</p>
|
|
<div v-else class="rounded-2xl overflow-hidden border border-gray-100 shadow-sm bg-white">
|
|
<div v-for="s in adminData.subscribers" :key="s.id" class="flex items-center justify-between px-5 py-3 border-b border-gray-100 last:border-b-0">
|
|
<span class="text-sm font-medium text-gray-700">{{ s.email }}</span>
|
|
<span class="text-xs text-gray-400">{{ formatDate(s.createdAt) }}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
</main>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { nextTick, onBeforeUnmount, onMounted, reactive, ref, computed } from 'vue'
|
|
import { useI18n } from 'vue-i18n'
|
|
import { useSeoMeta } from '#imports'
|
|
import gsap from 'gsap'
|
|
import { useUserStore } from '~/stores/user'
|
|
import { useAuthModalStore } from '~/stores/authModal'
|
|
import { workBySlug } from '~/data/work'
|
|
import { useApiError } from '~/composables/useApiError'
|
|
|
|
const { t, locale } = useI18n()
|
|
const userStore = useUserStore()
|
|
const authModal = useAuthModalStore()
|
|
const apiError = useApiError()
|
|
|
|
useSeoMeta({
|
|
title: () => t('account.title'),
|
|
})
|
|
|
|
const tabs = computed(() => {
|
|
const list: Array<{ key: string; label: string }> = [
|
|
{ key: 'profile', label: t('account.profile') },
|
|
{ key: 'favorites', label: t('account.favorites') },
|
|
{ key: 'comments', label: t('account.comments') },
|
|
]
|
|
if (userStore.user?.role === 'admin') {
|
|
list.push({ key: 'admin', label: t('account.adminTitle') })
|
|
}
|
|
return list
|
|
})
|
|
|
|
const activeTab = ref('profile')
|
|
const switchTab = (key: string) => {
|
|
activeTab.value = key
|
|
if (key === 'favorites') loadFavorites()
|
|
if (key === 'comments') loadComments()
|
|
if (key === 'admin') loadAdmin()
|
|
}
|
|
|
|
const formatDate = (raw?: string) => {
|
|
if (!raw) return ''
|
|
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',
|
|
}).format(d)
|
|
}
|
|
|
|
// ---- 个人资料 ----
|
|
const profileForm = reactive({ username: '', phone: '' })
|
|
const saving = ref(false)
|
|
const saveMessage = ref('')
|
|
const saveFailed = ref(false)
|
|
|
|
const initProfileForm = () => {
|
|
profileForm.username = userStore.user?.username ?? ''
|
|
profileForm.phone = userStore.user?.phone ?? ''
|
|
}
|
|
|
|
const saveProfile = async () => {
|
|
saving.value = true
|
|
saveMessage.value = ''
|
|
saveFailed.value = false
|
|
const result = await userStore.updateProfile({
|
|
username: profileForm.username,
|
|
phone: profileForm.phone,
|
|
})
|
|
if (result.success) {
|
|
saveMessage.value = t('account.saved')
|
|
} else {
|
|
saveFailed.value = true
|
|
saveMessage.value = apiError({ data: { code: result.code } })
|
|
}
|
|
saving.value = false
|
|
}
|
|
|
|
// ---- 收藏 ----
|
|
const favorites = ref<string[]>([])
|
|
const loadFavorites = async () => {
|
|
try {
|
|
const data = await $fetch<{ slugs: string[] }>('/api/favorites')
|
|
favorites.value = data.slugs
|
|
} catch (err) {
|
|
console.error('load favorites error:', err)
|
|
}
|
|
}
|
|
const removeFavorite = async (slug: string) => {
|
|
try {
|
|
await $fetch('/api/favorites', { method: 'DELETE', body: { workSlug: slug } })
|
|
favorites.value = favorites.value.filter((s) => s !== slug)
|
|
} catch (err) {
|
|
console.error('remove favorite error:', err)
|
|
}
|
|
}
|
|
|
|
// ---- 我的评论 ----
|
|
const myComments = ref<Array<{ id: number; newsSlug: string; content: string; createdAt: string }>>([])
|
|
const loadComments = async () => {
|
|
try {
|
|
const data = await $fetch<{ comments: Array<{ id: number; newsSlug: string; content: string; createdAt: string }> }>('/api/account/comments')
|
|
myComments.value = data.comments
|
|
} catch (err) {
|
|
console.error('load comments error:', err)
|
|
}
|
|
}
|
|
|
|
// ---- 管理面板 ----
|
|
const adminData = reactive<{
|
|
contacts: Array<{ id: number; name: string; email: string; subject: string; message: string; delivered: number; createdAt: string }>
|
|
subscribers: Array<{ id: number; email: string; createdAt: string }>
|
|
}>({ contacts: [], subscribers: [] })
|
|
|
|
const loadAdmin = async () => {
|
|
try {
|
|
const data = await $fetch<typeof adminData>('/api/account/admin')
|
|
adminData.contacts = data.contacts
|
|
adminData.subscribers = data.subscribers
|
|
} catch (err) {
|
|
console.error('load admin error:', err)
|
|
}
|
|
}
|
|
|
|
// ---- 页面进场动画 ----
|
|
const mainRef = ref<HTMLElement | null>(null)
|
|
let ctx: gsap.Context | null = null
|
|
|
|
onMounted(async () => {
|
|
await nextTick()
|
|
initProfileForm()
|
|
ctx = gsap.context(() => {
|
|
const targets = gsap.utils.toArray<HTMLElement>('[data-animate]', mainRef.value!)
|
|
if (targets.length) {
|
|
gsap.set(targets, { autoAlpha: 0, y: 24 })
|
|
gsap.to(targets, { autoAlpha: 1, y: 0, duration: 0.6, ease: 'power3.out', stagger: 0.08 })
|
|
}
|
|
}, mainRef.value)
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
ctx?.revert()
|
|
})
|
|
</script>
|