685c7b51d1
- 账号体系:修改密码(/account + PATCH /api/auth/password)、自助删除账号 (DELETE /api/auth/me)、订阅退订(token + /unsubscribe 页) - 前台/SEO:RSS 订阅 /feed.xml、作品详情分享、canonical URL、 文章 Article JSON-LD、品牌化错误页(i18n) - 后台增强:图片管理 /admin/uploads、操作审计日志 /admin/audit(含清空)、 联系消息已读标记、评论列表分页 - 工程:vitest 测试(密码哈希/内容校验器/种子完整性,12 例)+ pnpm test - DB 迁移:contacts.read、subscribers.token、audit_logs(老库 ALTER 兼容) - 密码逻辑抽为 server/utils/password.ts 供测试
403 lines
16 KiB
Vue
403 lines
16 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 v-if="userStore.user?.role === 'admin'" class="flex justify-center mb-6">
|
||
<NuxtLink
|
||
to="/admin"
|
||
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
|
||
>
|
||
<Icon name="ph:gear-six" class="w-4 h-4" />
|
||
{{ t('account.adminEntry') }}
|
||
</NuxtLink>
|
||
</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>
|
||
|
||
<!-- 修改密码 -->
|
||
<form class="p-6 rounded-2xl bg-white border border-gray-100 shadow-sm space-y-5" @submit.prevent="changePassword">
|
||
<h3 class="text-lg font-bold text-black">{{ t('account.changePassword.title') }}</h3>
|
||
<div>
|
||
<label class="block text-sm font-semibold text-gray-800 mb-2">{{ t('account.changePassword.currentPassword') }}</label>
|
||
<input
|
||
v-model="pwForm.currentPassword"
|
||
type="password"
|
||
autocomplete="current-password"
|
||
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.changePassword.newPassword') }}</label>
|
||
<input
|
||
v-model="pwForm.newPassword"
|
||
type="password"
|
||
autocomplete="new-password"
|
||
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.changePassword.confirmPassword') }}</label>
|
||
<input
|
||
v-model="pwForm.confirmPassword"
|
||
type="password"
|
||
autocomplete="new-password"
|
||
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>
|
||
<p v-if="pwMessage" class="text-sm" :class="pwFailed ? 'text-red-600' : 'text-green-600'">{{ pwMessage }}</p>
|
||
<button
|
||
type="submit"
|
||
:disabled="pwSaving"
|
||
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="pwSaving" name="ph:circle-notch" class="w-5 h-5 animate-spin" />
|
||
{{ pwSaving ? t('account.changePassword.saving') : t('account.changePassword.submit') }}
|
||
</button>
|
||
</form>
|
||
|
||
<!-- 删除账号(危险区) -->
|
||
<div class="p-6 rounded-2xl bg-white border border-red-200 shadow-sm space-y-5">
|
||
<h3 class="text-lg font-bold text-red-600">{{ t('account.deleteAccount.title') }}</h3>
|
||
<p class="text-sm text-gray-500">{{ t('account.deleteAccount.hint') }}</p>
|
||
<div>
|
||
<input
|
||
v-model="deletePassword"
|
||
type="password"
|
||
autocomplete="current-password"
|
||
:placeholder="t('account.deleteAccount.passwordPlaceholder')"
|
||
@keydown.enter.prevent
|
||
class="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-red-500 focus:ring-2 focus:ring-red-500/5 outline-none transition-all"
|
||
/>
|
||
</div>
|
||
<p v-if="deleteMessage" class="text-sm" :class="deleteFailed ? 'text-red-600' : 'text-green-600'">{{ deleteMessage }}</p>
|
||
<button
|
||
@click="confirmDeleteAccount"
|
||
:disabled="deleting"
|
||
class="w-full py-3 rounded-xl bg-red-600 text-white font-semibold flex items-center justify-center gap-2 transition-colors hover:bg-red-700 disabled:opacity-50"
|
||
>
|
||
<Icon v-if="deleting" name="ph:circle-notch" class="w-5 h-5 animate-spin" />
|
||
{{ t('account.deleteAccount.confirmButton') }}
|
||
</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="fav in favorites"
|
||
:key="fav.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/${fav.slug}`" class="block relative aspect-[4/3] overflow-hidden">
|
||
<img :src="fav.image" :alt="favTitle(fav)" 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/${fav.slug}`" class="font-bold text-black group-hover:text-gray-600 transition-colors">
|
||
{{ favTitle(fav) }}
|
||
</NuxtLink>
|
||
<button
|
||
@click="removeFavorite(fav.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">
|
||
#{{ comment.newsSlug }}
|
||
</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>
|
||
</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 { 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') },
|
||
]
|
||
return list
|
||
})
|
||
|
||
const activeTab = ref('profile')
|
||
const switchTab = (key: string) => {
|
||
activeTab.value = key
|
||
if (key === 'favorites') loadFavorites()
|
||
if (key === 'comments') loadComments()
|
||
}
|
||
|
||
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 pwForm = reactive({ currentPassword: '', newPassword: '', confirmPassword: '' })
|
||
const pwSaving = ref(false)
|
||
const pwMessage = ref('')
|
||
const pwFailed = ref(false)
|
||
|
||
const changePassword = async () => {
|
||
pwMessage.value = ''
|
||
pwFailed.value = false
|
||
|
||
if (pwForm.newPassword.length < 8) {
|
||
pwFailed.value = true
|
||
pwMessage.value = t('account.changePassword.tooShort')
|
||
return
|
||
}
|
||
if (pwForm.newPassword !== pwForm.confirmPassword) {
|
||
pwFailed.value = true
|
||
pwMessage.value = t('account.changePassword.mismatch')
|
||
return
|
||
}
|
||
|
||
pwSaving.value = true
|
||
try {
|
||
await $fetch('/api/auth/password', {
|
||
method: 'PATCH',
|
||
body: { currentPassword: pwForm.currentPassword, newPassword: pwForm.newPassword },
|
||
})
|
||
pwMessage.value = t('account.changePassword.success')
|
||
pwForm.currentPassword = ''
|
||
pwForm.newPassword = ''
|
||
pwForm.confirmPassword = ''
|
||
} catch (err) {
|
||
pwFailed.value = true
|
||
pwMessage.value =
|
||
(err as any)?.data?.code === 'WRONG_PASSWORD'
|
||
? t('account.changePassword.wrongPassword')
|
||
: apiError(err)
|
||
}
|
||
pwSaving.value = false
|
||
}
|
||
|
||
// ---- 删除账号 ----
|
||
const deletePassword = ref('')
|
||
const deleting = ref(false)
|
||
const deleteMessage = ref('')
|
||
const deleteFailed = ref(false)
|
||
|
||
const confirmDeleteAccount = async () => {
|
||
if (!window.confirm(t('account.deleteAccount.confirmText'))) return
|
||
deleteMessage.value = ''
|
||
deleteFailed.value = false
|
||
deleting.value = true
|
||
try {
|
||
await $fetch('/api/auth/me', { method: 'DELETE', body: { password: deletePassword.value } })
|
||
await userStore.logout()
|
||
await navigateTo('/')
|
||
} catch (err) {
|
||
deleteFailed.value = true
|
||
deleteMessage.value =
|
||
(err as any)?.data?.code === 'WRONG_PASSWORD'
|
||
? t('account.deleteAccount.wrongPassword')
|
||
: apiError(err)
|
||
}
|
||
deleting.value = false
|
||
}
|
||
|
||
// ---- 收藏 ----
|
||
interface FavedWork { slug: string; titleZh: string; titleEn: string; image: string | null }
|
||
const favorites = ref<FavedWork[]>([])
|
||
const favTitle = (w: FavedWork) => (locale.value === 'zh' ? w.titleZh : w.titleEn)
|
||
|
||
const loadFavorites = async () => {
|
||
try {
|
||
const [favRes, workRes] = await Promise.all([
|
||
$fetch<{ slugs: string[] }>('/api/favorites'),
|
||
$fetch<{ works: FavedWork[] }>('/api/work'),
|
||
])
|
||
const bySlug = new Map(workRes.works.map((w) => [w.slug, w]))
|
||
favorites.value = favRes.slugs.map((s) => bySlug.get(s)).filter(Boolean) as FavedWork[]
|
||
} 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((f) => f.slug !== 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 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>
|