feat: 收官补齐全部未实现功能

- 账号体系:修改密码(/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 供测试
This commit is contained in:
2026-08-06 17:26:26 +08:00
parent d67849a311
commit 685c7b51d1
47 changed files with 1600 additions and 72 deletions
+132
View File
@@ -87,6 +87,72 @@
{{ 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>
<!-- 我的收藏 -->
@@ -211,6 +277,72 @@ const saveProfile = async () => {
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[]>([])
+227
View File
@@ -0,0 +1,227 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<div class="flex items-center gap-3">
<div class="w-11 h-11 rounded-2xl bg-black text-white flex items-center justify-center flex-shrink-0">
<Icon name="ph:list-checks" class="w-6 h-6" />
</div>
<div>
<h1 class="text-2xl font-bold text-black leading-tight">操作日志</h1>
<p class="text-sm text-gray-500 mt-1">后台管理员的关键操作审计记录。</p>
</div>
</div>
<button
@click="clearLogs"
:disabled="!logs.length || clearing"
class="inline-flex items-center gap-2 px-5 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 disabled:opacity-40 disabled:cursor-not-allowed transition-colors flex-shrink-0"
title="清空全部操作日志"
>
<Icon v-if="clearing" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
<Icon v-else name="ph:trash" class="w-4 h-4" />
清空日志
</button>
</header>
<!-- 操作反馈 -->
<div
v-if="flash"
class="mb-6 px-4 py-3 rounded-xl text-sm font-medium flex items-center gap-2"
:class="flashKind === 'ok' ? 'bg-green-50 text-green-700 border border-green-200' : 'bg-red-50 text-red-700 border border-red-200'"
>
<Icon :name="flashKind === 'ok' ? 'ph:check-circle' : 'ph:warning-circle'" class="w-4 h-4" />
{{ flash }}
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ total }} 条日志</span>
</div>
<!-- 加载中 -->
<div v-if="loading" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:circle-notch" class="w-8 h-8 animate-spin" />
<p class="text-sm">加载中…</p>
</div>
<!-- 加载失败 -->
<div v-else-if="loadError" class="py-24 text-center">
<div class="inline-flex flex-col items-center gap-3 text-gray-500">
<Icon name="ph:warning-circle" class="w-8 h-8 text-red-500" />
<p class="text-sm">{{ loadError }}</p>
<button @click="loadLogs" class="px-5 py-2 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors">
重试
</button>
</div>
</div>
<!-- 空态 -->
<div v-else-if="!logs.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:list-checks" class="w-10 h-10" />
<p class="text-sm">暂无操作日志</p>
</div>
<!-- 日志表格 -->
<div v-else class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="text-left text-gray-500 border-b border-gray-100 bg-gray-50">
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">时间</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">操作人</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">操作</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider">对象</th>
<th class="px-6 py-4 font-semibold text-xs uppercase tracking-wider">详情</th>
</tr>
</thead>
<tbody>
<tr
v-for="log in logs"
:key="log.id"
class="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/70 transition-colors"
>
<td class="px-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(log.createdAt) }}</td>
<td class="px-6 py-4 text-gray-700 whitespace-nowrap">{{ log.actor_name || 'system' }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span
class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-semibold"
:class="actionMeta(log.action).cls"
>
{{ actionMeta(log.action).label }}
</span>
</td>
<td class="px-6 py-4">
<code class="text-xs text-gray-600 bg-gray-50 border border-gray-100 rounded px-2 py-1">{{ log.target }}</code>
</td>
<td class="px-6 py-4 text-gray-600 max-w-[320px] md:max-w-md truncate" :title="log.detail || undefined">
{{ log.detail || '—' }}
</td>
</tr>
</tbody>
</table>
</div>
<!-- 加载更多 -->
<div v-if="logs.length < total" class="p-4 border-t border-gray-100 flex justify-center">
<button
@click="loadMore"
:disabled="loadingMore"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 disabled:opacity-60 transition-colors"
>
<Icon v-if="loadingMore" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ loadingMore ? '加载中…' : '加载更多' }}
</button>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '操作日志' })
interface AuditLog {
id: number
actor_name: string | null
action: string
target: string
detail: string | null
createdAt: string
}
const pageSize = 20
const page = ref(0)
const total = ref(0)
const logs = ref<AuditLog[]>([])
const loading = ref(true)
const loadingMore = ref(false)
const loadError = ref('')
const ACTION_META: Record<string, { label: string; cls: string }> = {
create: { label: '新增', cls: 'bg-green-100 text-green-700' },
update: { label: '更新', cls: 'bg-amber-100 text-amber-700' },
delete: { label: '删除', cls: 'bg-red-100 text-red-700' },
upload: { label: '上传', cls: 'bg-blue-100 text-blue-700' },
clear: { label: '清空', cls: 'bg-gray-200 text-gray-600' },
}
const actionMeta = (action: string) => ACTION_META[action] ?? { label: action, cls: 'bg-gray-100 text-gray-700' }
const flash = ref('')
const flashKind = ref<'ok' | 'err'>('ok')
let flashTimer: ReturnType<typeof setTimeout> | null = null
const showFlash = (msg: string, kind: 'ok' | 'err' = 'ok') => {
flash.value = msg
flashKind.value = kind
if (flashTimer) clearTimeout(flashTimer)
flashTimer = setTimeout(() => {
flash.value = ''
}, 3000)
}
const loadLogs = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ logs: AuditLog[]; total: number; page: number }>('/api/admin/audit', {
query: { page: 1, pageSize },
})
logs.value = data.logs
total.value = data.total
page.value = data.page
} catch (err) {
console.error('load audit error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const loadMore = async () => {
if (loadingMore.value || logs.value.length >= total.value) return
loadingMore.value = true
try {
const next = page.value + 1
const data = await $fetch<{ logs: AuditLog[]; total: number; page: number }>('/api/admin/audit', {
query: { page: next, pageSize },
})
logs.value.push(...data.logs)
page.value = data.page
} catch (err) {
console.error('load more audit error:', err)
showFlash('加载更多失败,请重试', 'err')
} finally {
loadingMore.value = false
}
}
const clearing = ref(false)
const clearLogs = async () => {
if (!window.confirm('确定清空全部操作日志吗?此操作不可恢复。')) return
clearing.value = true
try {
await $fetch('/api/admin/audit', { method: 'DELETE' })
showFlash('日志已清空')
await loadLogs()
} catch (err) {
console.error('clear audit error:', err)
showFlash((err as any)?.data?.message || '清空失败,请重试', 'err')
} finally {
clearing.value = false
}
}
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('zh-CN', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}).format(d)
}
onMounted(loadLogs)
</script>
+41 -2
View File
@@ -41,7 +41,7 @@
</button>
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ comments.length }} 条评论</span>
<span class="text-sm text-gray-500">共 {{ total }} 条评论</span>
<template v-if="keyword">
<span class="text-sm text-gray-400">· 匹配 {{ filteredComments.length }} 条</span>
</template>
@@ -122,6 +122,18 @@
</tbody>
</table>
</div>
<!-- 加载更多 -->
<div v-if="comments.length < total" class="p-4 border-t border-gray-100 flex justify-center">
<button
@click="loadMore"
:disabled="loadingMore"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 disabled:opacity-60 transition-colors"
>
<Icon v-if="loadingMore" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ loadingMore ? '加载中…' : '加载更多' }}
</button>
</div>
</div>
</section>
</template>
@@ -140,9 +152,13 @@ interface AdminComment {
username: string
}
const pageSize = 20
const page = ref(0)
const total = ref(0)
const comments = ref<AdminComment[]>([])
const keyword = ref('')
const loading = ref(true)
const loadingMore = ref(false)
const loadError = ref('')
const filteredComments = computed(() => {
@@ -169,8 +185,12 @@ const loadComments = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ comments: AdminComment[] }>('/api/admin/comments')
const data = await $fetch<{ comments: AdminComment[]; total: number; page: number }>('/api/admin/comments', {
query: { page: 1, pageSize },
})
comments.value = data.comments
total.value = data.total
page.value = data.page
} catch (err) {
console.error('load comments error:', err)
loadError.value = '加载失败,请重试'
@@ -179,11 +199,30 @@ const loadComments = async () => {
}
}
const loadMore = async () => {
if (loadingMore.value || comments.value.length >= total.value) return
loadingMore.value = true
try {
const next = page.value + 1
const data = await $fetch<{ comments: AdminComment[]; total: number; page: number }>('/api/admin/comments', {
query: { page: next, pageSize },
})
comments.value.push(...data.comments)
page.value = data.page
} catch (err) {
console.error('load more comments error:', err)
showFlash('加载更多失败,请重试', 'err')
} finally {
loadingMore.value = false
}
}
const removeComment = async (c: AdminComment) => {
if (!window.confirm('确定删除这条评论吗?此操作不可恢复。')) return
try {
await $fetch(`/api/admin/comments/${c.id}`, { method: 'DELETE' })
comments.value = comments.value.filter((x) => x.id !== c.id)
total.value = Math.max(0, total.value - 1)
showFlash('评论已删除')
} catch (err) {
console.error('delete comment error:', err)
+36
View File
@@ -25,6 +25,7 @@
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ contacts.length }} 条消息</span>
<span class="text-sm text-gray-400">· 已送达 {{ deliveredCount }} 条</span>
<span class="text-sm text-gray-400">· 未读 {{ unreadCount }} 条</span>
</div>
<!-- 加载中 -->
@@ -80,6 +81,23 @@
<Icon :name="Number(c.delivered) === 1 ? 'ph:check' : 'ph:clock'" class="w-3.5 h-3.5" />
{{ Number(c.delivered) === 1 ? '已送达' : '未送达' }}
</span>
<span
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs"
:class="Number(c.read) === 1 ? 'bg-gray-100 text-gray-500' : 'bg-black text-white font-bold'"
>
<Icon :name="Number(c.read) === 1 ? 'ph:envelope-open' : 'ph:envelope-simple'" class="w-3.5 h-3.5" />
{{ Number(c.read) === 1 ? '已读' : '未读' }}
</span>
<button
@click="toggleRead(c)"
:disabled="togglingId === c.id"
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full border text-xs font-semibold transition-colors disabled:opacity-50"
:class="Number(c.read) === 1 ? 'border-gray-200 text-gray-500 hover:bg-gray-100' : 'border-black text-black hover:bg-black hover:text-white'"
>
<Icon v-if="togglingId === c.id" name="ph:circle-notch" class="w-3.5 h-3.5 animate-spin" />
<Icon v-else :name="Number(c.read) === 1 ? 'ph:envelope-simple-open' : 'ph:check-circle'" class="w-3.5 h-3.5" />
{{ Number(c.read) === 1 ? '标为未读' : '标为已读' }}
</button>
<button
@click="removeContact(c)"
class="w-8 h-8 rounded-full flex items-center justify-center text-gray-400 hover:bg-red-50 hover:text-red-600 transition-colors"
@@ -111,6 +129,7 @@ interface AdminContact {
subject: string
message: string
delivered: number
read: number
createdAt: string
}
@@ -119,6 +138,7 @@ const loading = ref(true)
const loadError = ref('')
const deliveredCount = computed(() => contacts.value.filter((c) => Number(c.delivered) === 1).length)
const unreadCount = computed(() => contacts.value.filter((c) => Number(c.read) !== 1).length)
const flash = ref('')
const flashKind = ref<'ok' | 'err'>('ok')
@@ -146,6 +166,22 @@ const loadContacts = async () => {
}
}
const togglingId = ref<number | null>(null)
const toggleRead = async (c: AdminContact) => {
const next = Number(c.read) === 1 ? 0 : 1
togglingId.value = c.id
try {
await $fetch(`/api/admin/contacts/${c.id}`, { method: 'PATCH', body: { read: next } })
c.read = next
showFlash(next === 1 ? '已标记为已读' : '已标记为未读')
} catch (err) {
console.error('toggle contact read error:', err)
showFlash((err as any)?.data?.message || '操作失败,请重试', 'err')
} finally {
togglingId.value = null
}
}
const removeContact = async (c: AdminContact) => {
if (!window.confirm(`确定删除「${c.name}」的这条消息吗?此操作不可恢复。`)) return
try {
+165
View File
@@ -0,0 +1,165 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
<div class="flex items-center gap-3">
<div class="w-11 h-11 rounded-2xl bg-black text-white flex items-center justify-center flex-shrink-0">
<Icon name="ph:image" class="w-6 h-6" />
</div>
<div>
<h1 class="text-2xl font-bold text-black leading-tight">图片管理</h1>
<p class="text-sm text-gray-500 mt-1">站点上传的图片文件,删除前请确认未被动态/作品引用。</p>
</div>
</div>
<button
@click="loadFiles"
class="inline-flex items-center gap-2 px-4 py-2 rounded-full border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors flex-shrink-0"
>
<Icon name="ph:arrows-clockwise" class="w-4 h-4" />
刷新
</button>
</header>
<!-- 操作反馈 -->
<div
v-if="flash"
class="mb-6 px-4 py-3 rounded-xl text-sm font-medium flex items-center gap-2"
:class="flashKind === 'ok' ? 'bg-green-50 text-green-700 border border-green-200' : 'bg-red-50 text-red-700 border border-red-200'"
>
<Icon :name="flashKind === 'ok' ? 'ph:check-circle' : 'ph:warning-circle'" class="w-4 h-4" />
{{ flash }}
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ files.length }} 张图片</span>
</div>
<!-- 加载中 -->
<div v-if="loading" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:circle-notch" class="w-8 h-8 animate-spin" />
<p class="text-sm">加载中…</p>
</div>
<!-- 加载失败 -->
<div v-else-if="loadError" class="py-24 text-center">
<div class="inline-flex flex-col items-center gap-3 text-gray-500">
<Icon name="ph:warning-circle" class="w-8 h-8 text-red-500" />
<p class="text-sm">{{ loadError }}</p>
<button @click="loadFiles" class="px-5 py-2 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors">
重试
</button>
</div>
</div>
<!-- 空态 -->
<div v-else-if="!files.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:image" class="w-10 h-10" />
<p class="text-sm">暂无图片</p>
</div>
<!-- 图片网格 -->
<div v-else class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
<div
v-for="f in files"
:key="f.name"
class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden group"
>
<div class="relative aspect-square bg-gray-50">
<img :src="f.url" :alt="f.name" loading="lazy" class="w-full h-full object-cover" />
<button
@click="removeFile(f)"
class="absolute top-2 right-2 w-8 h-8 rounded-full bg-black/60 text-white flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity hover:bg-red-600"
title="删除图片"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
<div class="p-3">
<p class="text-xs font-semibold text-gray-800 truncate" :title="f.name">{{ f.name }}</p>
<div class="mt-1 flex items-center justify-between gap-2 text-[11px] text-gray-400">
<span class="flex-shrink-0">{{ formatSize(f.size) }}</span>
<span class="truncate">{{ formatDate(f.modified) }}</span>
</div>
</div>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '图片管理' })
interface UploadFile {
name: string
size: number
modified: string
url: string
}
const files = ref<UploadFile[]>([])
const loading = ref(true)
const loadError = ref('')
const flash = ref('')
const flashKind = ref<'ok' | 'err'>('ok')
let flashTimer: ReturnType<typeof setTimeout> | null = null
const showFlash = (msg: string, kind: 'ok' | 'err' = 'ok') => {
flash.value = msg
flashKind.value = kind
if (flashTimer) clearTimeout(flashTimer)
flashTimer = setTimeout(() => {
flash.value = ''
}, 3000)
}
const loadFiles = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ files: UploadFile[] }>('/api/admin/uploads')
files.value = data.files
} catch (err) {
console.error('load uploads error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const removeFile = async (f: UploadFile) => {
if (!window.confirm(`确定删除图片「${f.name}」吗?此操作不可恢复。`)) return
try {
await $fetch(`/api/admin/uploads/${encodeURIComponent(f.name)}`, { method: 'DELETE' })
files.value = files.value.filter((x) => x.name !== f.name)
showFlash('已删除;如被内容引用请同步更新')
} catch (err) {
console.error('delete upload error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
const formatSize = (bytes: number) => {
if (!bytes && bytes !== 0) return ''
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
return `${Math.max(1, Math.round(bytes / 1024))} KB`
}
const formatDate = (raw?: string) => {
if (!raw) return ''
// 兼容 "YYYY-MM-DD HH:MM:SS"(UTC)与 ISO 两种格式
const iso = String(raw).includes('T') ? String(raw) : String(raw).replace(' ', 'T') + 'Z'
const d = new Date(iso)
if (Number.isNaN(d.getTime())) return raw
return new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}).format(d)
}
onMounted(loadFiles)
</script>
+25 -1
View File
@@ -151,7 +151,7 @@
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useSeoMeta, createError } from '#imports'
import { useSeoMeta, useHead, useRuntimeConfig, createError } from '#imports'
const { t, locale } = useI18n()
const route = useRoute()
@@ -224,6 +224,30 @@ useSeoMeta({
description: () => locText(article.value.excerptZh, article.value.excerptEn),
})
// 文章 JSON-LD(Article schema,利于搜索引擎富摘要)
const { public: publicCfg } = useRuntimeConfig()
const siteBase = String(publicCfg.siteUrl || '').replace(/\/$/, '')
useHead({
script: [
{
type: 'application/ld+json',
innerHTML: JSON.stringify({
'@context': 'https://schema.org',
'@type': 'Article',
headline: title.value,
description: locText(article.value.excerptZh, article.value.excerptEn),
image: article.value.image || undefined,
datePublished: article.value.date || undefined,
author: { '@type': 'Organization', name: 'ArcticNewOne' },
mainEntityOfPage: {
'@type': 'WebPage',
'@id': siteBase ? `${siteBase}/news/${slug}` : undefined,
},
}),
},
],
})
// ---- 分享(真实动作) ----
const copyFeedback = ref('')
+82
View File
@@ -0,0 +1,82 @@
<template>
<main class="min-h-screen flex items-center justify-center px-4 py-32">
<div ref="cardRef" class="w-full max-w-md mx-auto 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:envelope-simple-duotone" class="w-8 h-8 text-gray-400" />
</div>
<h1 class="text-2xl font-bold text-black mb-3">{{ t('unsubscribe.title') }}</h1>
<p class="text-gray-500 mb-8">{{ t('unsubscribe.description') }}</p>
<!-- 处理中 -->
<p v-if="status === 'processing'" class="text-gray-500 flex items-center justify-center gap-2">
<Icon name="ph:circle-notch" class="w-5 h-5 animate-spin" />
{{ t('unsubscribe.unsubscribe') }}
</p>
<!-- 结果 -->
<div v-else class="space-y-8">
<p
class="text-lg font-semibold"
:class="status === 'unsubscribed' ? 'text-green-600' : 'text-gray-700'"
>
{{ resultText }}
</p>
<NuxtLink
to="/"
class="inline-block px-8 py-3 rounded-full bg-black text-white font-semibold hover:bg-gray-800 transition-colors"
>
{{ t('unsubscribe.backHome') }}
</NuxtLink>
</div>
</div>
</main>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useSeoMeta } from '#imports'
import gsap from 'gsap'
const { t } = useI18n()
const route = useRoute()
useSeoMeta({
title: () => t('unsubscribe.title'),
})
const status = ref<'processing' | 'unsubscribed' | 'already' | 'invalid'>('processing')
const resultText = computed(() => {
if (status.value === 'unsubscribed') return t('unsubscribe.unsubscribed')
if (status.value === 'already') return t('unsubscribe.already')
return t('unsubscribe.invalid')
})
const cardRef = ref<HTMLElement | null>(null)
onMounted(async () => {
// 入场淡入(与其他公开页一致的轻量 GSAP 动画)
if (cardRef.value) {
gsap.fromTo(cardRef.value, { autoAlpha: 0, y: 24 }, { autoAlpha: 1, y: 0, duration: 0.6, ease: 'power3.out' })
}
const token = route.query.token
if (!token || Array.isArray(token)) {
status.value = 'invalid'
return
}
try {
const data = await $fetch<{ ok: boolean; unsubscribed: boolean }>('/api/newsletter/unsubscribe', {
method: 'POST',
body: { token },
})
status.value = data.unsubscribed ? 'unsubscribed' : 'already'
} catch (err) {
console.error('unsubscribe error:', err)
status.value = 'invalid'
}
})
</script>
+60 -1
View File
@@ -129,6 +129,38 @@
<!-- 收藏 -->
<FavoriteButton :work-slug="slug" />
</div>
<!-- 分享 -->
<div class="mt-8 pt-6 border-t border-gray-200">
<p class="text-sm text-gray-500 mb-3">{{ t('share.article') }}</p>
<div class="flex gap-3">
<button
@click="shareToX"
class="w-10 h-10 rounded-full bg-white border border-gray-200 hover:bg-black hover:text-white hover:border-black flex items-center justify-center transition-all duration-300"
:title="'X'"
aria-label="Share on X"
>
<Icon name="ph:x-logo" class="w-5 h-5" />
</button>
<button
@click="shareToLinkedIn"
class="w-10 h-10 rounded-full bg-white border border-gray-200 hover:bg-black hover:text-white hover:border-black flex items-center justify-center transition-all duration-300"
:title="'LinkedIn'"
aria-label="Share on LinkedIn"
>
<Icon name="ph:linkedin-logo" class="w-5 h-5" />
</button>
<button
@click="copyLink"
class="w-10 h-10 rounded-full bg-white border border-gray-200 hover:bg-black hover:text-white hover:border-black flex items-center justify-center transition-all duration-300"
:title="t('share.copy')"
aria-label="Copy link"
>
<Icon name="ph:link" class="w-5 h-5" />
</button>
</div>
<p v-if="copyFeedback" class="text-sm mt-3 text-green-600">{{ copyFeedback }}</p>
</div>
</div>
</div>
</div>
@@ -160,7 +192,7 @@
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useSeoMeta, createError } from '#imports'
@@ -218,4 +250,31 @@ useSeoMeta({
title: () => `${title.value} - ${t('work.title')}`,
description: () => description.value,
})
// ---- 分享(真实动作) ----
const copyFeedback = ref('')
const shareUrl = () => (typeof window !== 'undefined' ? window.location.href : '')
const shareText = () => title.value
const shareToX = () => {
const url = `https://twitter.com/intent/tweet?url=${encodeURIComponent(shareUrl())}&text=${encodeURIComponent(shareText())}`
window.open(url, '_blank', 'noopener,noreferrer,width=600,height=480')
}
const shareToLinkedIn = () => {
const url = `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(shareUrl())}`
window.open(url, '_blank', 'noopener,noreferrer,width=600,height=480')
}
const copyLink = async () => {
try {
await navigator.clipboard.writeText(shareUrl())
copyFeedback.value = t('share.copied')
} catch {
// 剪贴板不可用时回退到 prompt 复制
window.prompt(t('share.copyFailed'), shareUrl())
}
setTimeout(() => (copyFeedback.value = ''), 3000)
}
</script>