feat: 后台管理系统(Admin Backend + 全量 CMS)

- 内容迁入数据库:articles/works 表(中英双语 + 内容块),
  server/utils/seed.ts 灌入现有 6 动态 + 6 作品,站点初始内容不变
- 前台内容 API:/api/news、/api/work(列表/详情,仅已发布),
  页面/首页组件改为 useFetch 拉取并按 locale 选 zh/en 字段
- 后台管理 /admin(admin 中间件守卫):
  - Dashboard 统计、动态/作品增删改(块编辑器、图片上传 /api/admin/upload)
  - 用户管理(角色切换/删除)、评论审核、联系/订阅管理(导出 CSV)
- 预置管理员:NUXT_ADMIN_INITIAL_PASSWORD 首次初始化创建 admin 账号
- 图片自托管:上传存 PVC /data/uploads,/uploads/[...] 路由服务
- sitemap 改查 DB;i18n 移除内容键(305 键对齐);删除 app/data 冗余模块
- 角色以数据库为准(后台可切换);admin 中间件 + 客户端会话恢复插件
This commit is contained in:
2026-08-06 16:15:14 +08:00
parent 5c3f3bca63
commit d67849a311
67 changed files with 4909 additions and 754 deletions
+30 -68
View File
@@ -26,6 +26,17 @@
<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
@@ -83,19 +94,19 @@
<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"
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/${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 :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/${slug}`" class="font-bold text-black group-hover:text-gray-600 transition-colors">
{{ t(`work.items.${slug}.title`) }}
<NuxtLink :to="`/work/${fav.slug}`" class="font-bold text-black group-hover:text-gray-600 transition-colors">
{{ favTitle(fav) }}
</NuxtLink>
<button
@click="removeFavorite(slug)"
@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"
>
@@ -116,48 +127,13 @@
>
<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`) }}
#{{ 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>
<!-- 管理面板 -->
<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>
@@ -169,7 +145,6 @@ 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()
@@ -187,9 +162,6 @@ const tabs = computed(() => {
{ 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
})
@@ -198,7 +170,6 @@ const switchTab = (key: string) => {
activeTab.value = key
if (key === 'favorites') loadFavorites()
if (key === 'comments') loadComments()
if (key === 'admin') loadAdmin()
}
const formatDate = (raw?: string) => {
@@ -241,11 +212,18 @@ const saveProfile = async () => {
}
// ---- 收藏 ----
const favorites = ref<string[]>([])
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 data = await $fetch<{ slugs: string[] }>('/api/favorites')
favorites.value = data.slugs
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)
}
@@ -253,7 +231,7 @@ const loadFavorites = async () => {
const removeFavorite = async (slug: string) => {
try {
await $fetch('/api/favorites', { method: 'DELETE', body: { workSlug: slug } })
favorites.value = favorites.value.filter((s) => s !== slug)
favorites.value = favorites.value.filter((f) => f.slug !== slug)
} catch (err) {
console.error('remove favorite error:', err)
}
@@ -270,22 +248,6 @@ const loadComments = async () => {
}
}
// ---- 管理面板 ----
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
+208
View File
@@ -0,0 +1,208 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8">
<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:chat-text" 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>
</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="relative max-w-md mb-4">
<Icon name="ph:magnifying-glass" class="w-5 h-5 text-gray-400 absolute left-4 top-1/2 -translate-y-1/2" />
<input
v-model="keyword"
type="text"
placeholder="按文章或内容关键词筛选…"
class="w-full pl-12 pr-10 py-3 rounded-full border border-gray-200 focus:border-black focus:ring-2 focus:ring-black/5 outline-none transition-all duration-300 text-sm"
/>
<button
v-if="keyword"
@click="keyword = ''"
class="w-6 h-6 rounded-full flex items-center justify-center text-gray-400 hover:text-gray-600 absolute right-3 top-1/2 -translate-y-1/2"
title="清空筛选"
>
<Icon name="ph:x" class="w-4 h-4" />
</button>
</div>
<div class="flex items-center gap-2 mb-4">
<span class="text-sm text-gray-500">共 {{ comments.length }} 条评论</span>
<template v-if="keyword">
<span class="text-sm text-gray-400">· 匹配 {{ filteredComments.length }} 条</span>
</template>
</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="loadComments" 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="!comments.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:chat-text" class="w-10 h-10" />
<p class="text-sm">暂无评论</p>
</div>
<div v-else-if="!filteredComments.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:magnifying-glass" 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">文章</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>
<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 text-right">操作</th>
</tr>
</thead>
<tbody>
<tr
v-for="c in filteredComments"
:key="c.id"
class="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/70 transition-colors"
>
<td class="px-6 py-4 whitespace-nowrap">
<NuxtLink
:to="`/news/${c.newsSlug}`"
class="inline-flex items-center gap-1.5 font-semibold text-black hover:text-gray-600 transition-colors"
>
<Icon name="ph:article" class="w-4 h-4 text-gray-400" />
#{{ c.newsSlug }}
</NuxtLink>
</td>
<td class="px-6 py-4 text-gray-600 whitespace-nowrap">{{ c.username }}</td>
<td class="px-6 py-4">
<p class="text-gray-700 max-w-[300px] md:max-w-md truncate" :title="c.content">{{ c.content }}</p>
</td>
<td class="px-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(c.createdAt) }}</td>
<td class="px-6 py-4">
<div class="flex items-center justify-end">
<button
@click="removeComment(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"
title="删除评论"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '评论管理' })
interface AdminComment {
id: number
newsSlug: string
content: string
createdAt: string
username: string
}
const comments = ref<AdminComment[]>([])
const keyword = ref('')
const loading = ref(true)
const loadError = ref('')
const filteredComments = computed(() => {
const kw = keyword.value.trim().toLowerCase()
if (!kw) return comments.value
return comments.value.filter(
(c) => c.newsSlug.toLowerCase().includes(kw) || c.content.toLowerCase().includes(kw),
)
})
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 loadComments = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ comments: AdminComment[] }>('/api/admin/comments')
comments.value = data.comments
} catch (err) {
console.error('load comments error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.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)
showFlash('评论已删除')
} catch (err) {
console.error('delete comment error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
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(loadComments)
</script>
+175
View File
@@ -0,0 +1,175 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8">
<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:envelope-simple-open" 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>
</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">共 {{ contacts.length }} 条消息</span>
<span class="text-sm text-gray-400">· 已送达 {{ deliveredCount }} 条</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="loadContacts" 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="!contacts.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:envelope-simple-open" class="w-10 h-10" />
<p class="text-sm">暂无联系消息</p>
</div>
<!-- 消息卡片列表 -->
<div v-else class="space-y-4">
<div
v-for="c in contacts"
:key="c.id"
class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 md:p-6 hover:shadow-md transition-shadow"
>
<div class="flex items-start justify-between gap-4">
<div class="flex items-center gap-3 min-w-0">
<div class="w-10 h-10 rounded-full bg-black text-white flex items-center justify-center font-bold flex-shrink-0">
{{ (c.name || '?').charAt(0) }}
</div>
<div class="min-w-0">
<div class="flex items-center gap-2 flex-wrap">
<h3 class="font-bold text-black">{{ c.name }}</h3>
<a :href="`mailto:${c.email}`" class="text-sm text-gray-500 hover:text-black transition-colors truncate">
{{ c.email }}
</a>
</div>
<p class="text-xs text-gray-400 mt-1">{{ formatDate(c.createdAt) }}</p>
</div>
</div>
<div class="flex items-center gap-2 flex-shrink-0">
<span
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-semibold"
:class="Number(c.delivered) === 1 ? 'bg-green-100 text-green-700' : 'bg-amber-100 text-amber-600'"
>
<Icon :name="Number(c.delivered) === 1 ? 'ph:check' : 'ph:clock'" class="w-3.5 h-3.5" />
{{ Number(c.delivered) === 1 ? '已送达' : '未送达' }}
</span>
<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"
title="删除消息"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
</div>
<div class="mt-4 border-t border-gray-100 pt-4">
<p class="text-sm font-semibold text-gray-800">{{ c.subject }}</p>
<p class="mt-1.5 text-sm text-gray-600 whitespace-pre-line break-words">{{ c.message }}</p>
</div>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '联系消息' })
interface AdminContact {
id: number
name: string
email: string
subject: string
message: string
delivered: number
createdAt: string
}
const contacts = ref<AdminContact[]>([])
const loading = ref(true)
const loadError = ref('')
const deliveredCount = computed(() => contacts.value.filter((c) => Number(c.delivered) === 1).length)
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 loadContacts = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ contacts: AdminContact[] }>('/api/admin/contacts')
contacts.value = data.contacts
} catch (err) {
console.error('load contacts error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const removeContact = async (c: AdminContact) => {
if (!window.confirm(`确定删除「${c.name}」的这条消息吗?此操作不可恢复。`)) return
try {
await $fetch(`/api/admin/contacts/${c.id}`, { method: 'DELETE' })
contacts.value = contacts.value.filter((x) => x.id !== c.id)
showFlash('消息已删除')
} catch (err) {
console.error('delete contact error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
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(loadContacts)
</script>
+135
View File
@@ -0,0 +1,135 @@
<script setup lang="ts">
/**
* 后台 Dashboard:渲染 /api/admin/stats 统计卡片。
*/
import { computed, onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: 'Dashboard' })
interface Stats {
articles: number
works: number
publishedArticles: number
publishedWorks: number
users: number
comments: number
contacts: number
subscribers: number
favorites: number
}
const stats = ref<Stats | null>(null)
const loading = ref(true)
const error = ref('')
const loadStats = async () => {
loading.value = true
error.value = ''
try {
stats.value = await $fetch<Stats>('/api/admin/stats')
} catch (err: any) {
error.value = err?.data?.message || '统计加载失败'
} finally {
loading.value = false
}
}
onMounted(loadStats)
const cards = computed(() => [
{
label: '动态文章',
value: stats.value?.articles,
sub: () => `已发布 ${stats.value?.publishedArticles ?? 0}`,
icon: 'ph:newspaper',
to: '/admin/news',
},
{
label: '作品',
value: stats.value?.works,
sub: () => `已发布 ${stats.value?.publishedWorks ?? 0}`,
icon: 'ph:image-square',
to: '/admin/work',
},
{ label: '注册用户', value: stats.value?.users, sub: () => '全部用户', icon: 'ph:users-three', to: '/admin/users' },
{ label: '评论', value: stats.value?.comments, sub: () => '全部评论', icon: 'ph:chat-circle-text', to: '/admin/comments' },
{ label: '联系留言', value: stats.value?.contacts, sub: () => '表单留言', icon: 'ph:envelope-simple', to: '/admin/contacts' },
{ label: '订阅邮箱', value: stats.value?.subscribers, sub: () => 'Newsletter', icon: 'ph:paper-plane-tilt', to: '/admin/subscribers' },
{ label: '收藏', value: stats.value?.favorites, sub: () => '作品收藏', icon: 'ph:bookmark-simple', to: '/admin/work' },
])
</script>
<template>
<div>
<!-- 页头 -->
<div class="flex flex-col sm:flex-row sm:items-end sm:justify-between gap-4 mb-8">
<div>
<h1 class="text-2xl md:text-3xl font-bold text-black tracking-tight">Dashboard</h1>
<p class="mt-1 text-sm text-gray-500">站点内容与数据概览</p>
</div>
<div class="flex gap-3">
<NuxtLink
to="/admin/news"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
<Icon name="ph:plus" class="w-4 h-4" /> 新建动态
</NuxtLink>
<NuxtLink
to="/admin/work"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors"
>
<Icon name="ph:plus" class="w-4 h-4" /> 新建作品
</NuxtLink>
</div>
</div>
<!-- 加载态 -->
<div v-if="loading" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5">
<div
v-for="i in 8"
:key="i"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-6 h-32 animate-pulse"
></div>
</div>
<!-- 错误态 -->
<div
v-else-if="error"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-10 text-center"
>
<Icon name="ph:warning-circle" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
<p class="text-gray-500 mb-4">{{ error }}</p>
<button
type="button"
@click="loadStats"
class="px-5 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
重新加载
</button>
</div>
<!-- 统计卡片 -->
<div v-else class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5">
<NuxtLink
v-for="card in cards"
:key="card.label"
:to="card.to"
class="group rounded-2xl bg-white border border-gray-100 shadow-sm p-6 hover:shadow-lg transition-all duration-300 hover:-translate-y-1"
>
<div class="flex items-center justify-between mb-5">
<div
class="w-11 h-11 rounded-xl bg-gray-100 text-gray-600 flex items-center justify-center group-hover:bg-black group-hover:text-white transition-colors duration-300"
>
<Icon :name="card.icon" class="w-5 h-5" />
</div>
<Icon name="ph:arrow-up-right" class="w-4 h-4 text-gray-300 group-hover:text-gray-500 transition-colors" />
</div>
<p class="text-3xl font-bold text-black">{{ card.value ?? 0 }}</p>
<p class="mt-1 text-sm font-semibold text-gray-800">{{ card.label }}</p>
<p class="mt-0.5 text-xs text-gray-400">{{ card.sub() }}</p>
</NuxtLink>
</div>
</div>
</template>
+533
View File
@@ -0,0 +1,533 @@
<script setup lang="ts">
/**
* 后台动态管理:列表 + 新建/编辑表单。
* - 列表来自 /api/admin/articles(含未发布)
* - 新建 POST /api/admin/articles;编辑 PATCH /api/admin/articles/[id];删除 DELETE /api/admin/articles/[id]
* - 正文为内容块编辑器(中英各一组)
*/
import { onMounted, reactive, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: '动态管理' })
interface Block {
type: string
text: string
}
interface Article {
id?: number
slug: string
category: 'insights' | 'projects' | 'announcements'
titleZh: string
titleEn: string
excerptZh: string
excerptEn: string
contentZh: Block[]
contentEn: Block[]
image: string | null
date: string | null
readTime: number
author: string
published: number
createdAt: string
updatedAt: string
}
const CATEGORIES = [
{ value: 'insights', label: '行业洞察' },
{ value: 'projects', label: '项目动态' },
{ value: 'announcements', label: '公告通知' },
]
const catLabel = (c: string) => CATEGORIES.find((x) => x.value === c)?.label ?? c
/* ---------- 列表 ---------- */
const articles = ref<Article[]>([])
const loading = ref(true)
const error = ref('')
const loadArticles = async () => {
loading.value = true
error.value = ''
try {
const res = await $fetch<{ articles: Article[] }>('/api/admin/articles')
articles.value = res.articles
} catch (err: any) {
error.value = err?.data?.message || '加载失败'
} finally {
loading.value = false
}
}
onMounted(loadArticles)
/* ---------- 提示 ---------- */
const message = ref('')
const messageType = ref<'success' | 'error'>('success')
let toastTimer: ReturnType<typeof setTimeout> | undefined
const toast = (text: string, type: 'success' | 'error' = 'success') => {
message.value = text
messageType.value = type
if (toastTimer) clearTimeout(toastTimer)
toastTimer = setTimeout(() => (message.value = ''), 3000)
}
/* ---------- 表单 ---------- */
const view = ref<'list' | 'form'>('list')
const editingId = ref<number | null>(null)
const saving = ref(false)
const formError = ref('')
const emptyForm = () => ({
slug: '',
category: 'insights' as Article['category'],
titleZh: '',
titleEn: '',
excerptZh: '',
excerptEn: '',
contentZh: [] as Block[],
contentEn: [] as Block[],
image: '',
date: '',
readTime: 5,
author: 'ArcticNewOne',
published: true,
})
const form = reactive(emptyForm())
const openCreate = () => {
Object.assign(form, emptyForm())
editingId.value = null
formError.value = ''
view.value = 'form'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const openEdit = (a: Article) => {
if (a.id == null) {
toast('该条目缺少 id 字段,暂时无法编辑(后端待补)', 'error')
return
}
editingId.value = a.id
Object.assign(form, {
slug: a.slug,
category: a.category,
titleZh: a.titleZh,
titleEn: a.titleEn,
excerptZh: a.excerptZh,
excerptEn: a.excerptEn,
contentZh: a.contentZh.map((b) => ({ ...b })),
contentEn: a.contentEn.map((b) => ({ ...b })),
image: a.image ?? '',
date: a.date ?? '',
readTime: a.readTime,
author: a.author,
published: !!a.published,
})
formError.value = ''
view.value = 'form'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const backToList = () => {
view.value = 'list'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const save = async () => {
formError.value = ''
if (!/^[a-z0-9-]{2,64}$/.test(form.slug)) {
formError.value = 'slug 需为 2-64 位小写字母/数字/中划线'
return
}
if (!form.titleZh.trim() || !form.titleEn.trim()) {
formError.value = '中英文标题均为必填'
return
}
saving.value = true
try {
const payload = {
slug: form.slug,
category: form.category,
titleZh: form.titleZh,
titleEn: form.titleEn,
excerptZh: form.excerptZh,
excerptEn: form.excerptEn,
contentZh: form.contentZh,
contentEn: form.contentEn,
image: form.image || null,
date: form.date || null,
readTime: Number(form.readTime) || 5,
author: form.author.trim() || 'ArcticNewOne',
published: form.published,
}
if (editingId.value != null) {
await $fetch(`/api/admin/articles/${editingId.value}`, { method: 'PATCH', body: payload })
toast('动态已更新')
} else {
await $fetch('/api/admin/articles', { method: 'POST', body: payload })
toast('动态已创建')
}
await loadArticles()
view.value = 'list'
} catch (err: any) {
formError.value = err?.data?.message || '保存失败'
} finally {
saving.value = false
}
}
/* ---------- 删除 ---------- */
const confirmDelete = ref<Article | null>(null)
const deleting = ref(false)
const askDelete = (a: Article) => {
if (a.id == null) {
toast('该条目缺少 id 字段,暂时无法删除(后端待补)', 'error')
return
}
confirmDelete.value = a
}
const doDelete = async () => {
const a = confirmDelete.value
if (!a || a.id == null) return
deleting.value = true
try {
await $fetch(`/api/admin/articles/${a.id}`, { method: 'DELETE' })
toast(`已删除「${a.titleZh}」`)
confirmDelete.value = null
await loadArticles()
} catch (err: any) {
toast(err?.data?.message || '删除失败', 'error')
} finally {
deleting.value = false
}
}
</script>
<template>
<div>
<!-- 页头 -->
<div class="flex items-center justify-between gap-4 mb-8">
<div>
<h1 class="text-2xl md:text-3xl font-bold text-black tracking-tight">动态管理</h1>
<p v-if="view === 'list'" class="mt-1 text-sm text-gray-500">共 {{ articles.length }} 篇动态</p>
</div>
<button
v-if="view === 'list'"
type="button"
@click="openCreate"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
<Icon name="ph:plus" class="w-4 h-4" /> 新建动态
</button>
<button
v-else
type="button"
@click="backToList"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors"
>
<Icon name="ph:arrow-left" class="w-4 h-4" /> 返回列表
</button>
</div>
<!-- 提示 -->
<div
v-if="message"
class="mb-6 px-4 py-3 rounded-xl text-sm font-medium border"
:class="messageType === 'success' ? 'bg-green-50 border-green-200 text-green-700' : 'bg-red-50 border-red-200 text-red-700'"
>
{{ message }}
</div>
<!-- ==================== 列表视图 ==================== -->
<template v-if="view === 'list'">
<!-- 加载态 -->
<div v-if="loading" class="space-y-3">
<div
v-for="i in 6"
:key="i"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 h-16 animate-pulse"
></div>
</div>
<!-- 错误态 -->
<div
v-else-if="error"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-10 text-center"
>
<Icon name="ph:warning-circle" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
<p class="text-gray-500 mb-4">{{ error }}</p>
<button
type="button"
@click="loadArticles"
class="px-5 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
重新加载
</button>
</div>
<!-- 空态 -->
<div
v-else-if="!articles.length"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-14 text-center"
>
<Icon name="ph:newspaper" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
<p class="text-gray-500 mb-4">还没有动态,点击右上角「新建动态」创建第一篇</p>
</div>
<!-- 列表 -->
<div v-else class="rounded-2xl bg-white 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-400 border-b border-gray-100">
<th class="px-5 py-4 font-semibold">标题</th>
<th class="px-5 py-4 font-semibold">分类</th>
<th class="px-5 py-4 font-semibold">发布日期</th>
<th class="px-5 py-4 font-semibold">时长</th>
<th class="px-5 py-4 font-semibold">状态</th>
<th class="px-5 py-4 font-semibold text-right">操作</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-50">
<tr
v-for="a in articles"
:key="a.slug"
class="hover:bg-gray-50/60 transition-colors"
>
<td class="px-5 py-4">
<p class="font-semibold text-black">{{ a.titleZh }}</p>
<p class="text-xs text-gray-400 mt-0.5 font-mono">{{ a.slug }}</p>
</td>
<td class="px-5 py-4">
<span class="px-2.5 py-1 rounded-full bg-gray-100 text-gray-700 text-xs font-medium">
{{ catLabel(a.category) }}
</span>
</td>
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ a.date || '-' }}</td>
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ a.readTime }} 分钟</td>
<td class="px-5 py-4">
<span
class="px-2.5 py-1 rounded-full text-xs font-semibold inline-flex items-center gap-1"
:class="a.published ? 'bg-green-50 text-green-700' : 'bg-amber-50 text-amber-700'"
>
<span
class="w-1.5 h-1.5 rounded-full"
:class="a.published ? 'bg-green-500' : 'bg-amber-500'"
></span>
{{ a.published ? '已发布' : '草稿' }}
</span>
</td>
<td class="px-5 py-4">
<div class="flex items-center justify-end gap-2">
<button
type="button"
@click="openEdit(a)"
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-gray-100 text-gray-700 text-xs font-semibold hover:bg-gray-200 transition-colors"
>
<Icon name="ph:pencil-simple" class="w-3.5 h-3.5" /> 编辑
</button>
<button
type="button"
@click="askDelete(a)"
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-red-50 text-red-600 text-xs font-semibold hover:bg-red-100 transition-colors"
>
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<!-- ==================== 表单视图 ==================== -->
<template v-else>
<div class="space-y-6 max-w-4xl">
<!-- 基本信息 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:info" class="w-4 h-4 text-gray-400" /> 基本信息
</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
slug<span class="text-red-500">*</span>
</label>
<input
v-model="form.slug"
type="text"
placeholder="如 design-trends-2026"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 font-mono"
/>
<p class="mt-1 text-xs text-gray-400">2-64 位小写字母 / 数字 / 中划线,用于前台链接</p>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">分类</label>
<select
v-model="form.category"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
>
<option v-for="c in CATEGORIES" :key="c.value" :value="c.value">{{ c.label }}</option>
</select>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">发布日期</label>
<input
v-model="form.date"
type="date"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
<div class="grid grid-cols-2 gap-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">阅读时长(分钟)</label>
<input
v-model.number="form.readTime"
type="number"
min="1"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">作者</label>
<input
v-model="form.author"
type="text"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
</div>
<div class="md:col-span-2">
<label class="block text-sm font-semibold text-gray-800 mb-2">发布状态</label>
<button
type="button"
class="inline-flex items-center gap-3 group"
@click="form.published = !form.published"
>
<span
class="relative w-11 h-6 rounded-full transition-colors duration-300"
:class="form.published ? 'bg-black' : 'bg-gray-300'"
>
<span
class="absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white shadow transition-transform duration-300"
:class="form.published ? 'translate-x-5' : ''"
></span>
</span>
<span class="text-sm" :class="form.published ? 'text-green-700 font-semibold' : 'text-gray-500'">
{{ form.published ? '已发布(前台可见)' : '草稿(前台隐藏)' }}
</span>
</button>
</div>
</div>
</section>
<!-- 标题与摘要 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:text-aa" class="w-4 h-4 text-gray-400" /> 标题与摘要
</h3>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
中文标题<span class="text-red-500">*</span>
</label>
<input
v-model="form.titleZh"
type="text"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文摘要</label>
<textarea
v-model="form.excerptZh"
rows="3"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"
></textarea>
</div>
</div>
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
English Title<span class="text-red-500">*</span>
</label>
<input
v-model="form.titleEn"
type="text"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Excerpt</label>
<textarea
v-model="form.excerptEn"
rows="3"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"
></textarea>
</div>
</div>
</div>
</section>
<!-- 正文内容 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-5 flex items-center gap-2">
<Icon name="ph:article" class="w-4 h-4 text-gray-400" /> 正文内容(中英各一组)
</h3>
<div class="space-y-6">
<AdminBlockEditor v-model="form.contentZh" label="中文正文" />
<AdminBlockEditor v-model="form.contentEn" label="English Content" />
</div>
</section>
<!-- 封面图 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:image" class="w-4 h-4 text-gray-400" /> 封面图
</h3>
<AdminImageUpload v-model="form.image" label="封面图" />
</section>
<!-- 提交 -->
<div class="flex items-center justify-end gap-3">
<p v-if="formError" class="mr-auto text-sm text-red-600">{{ formError }}</p>
<button
type="button"
@click="backToList"
class="px-5 py-2.5 rounded-lg border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors"
>
取消
</button>
<button
type="button"
:disabled="saving"
@click="save"
class="px-6 py-2.5 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-60 inline-flex items-center gap-2"
>
<Icon v-if="saving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ saving ? '保存中…' : editingId != null ? '保存修改' : '创建动态' }}
</button>
</div>
</div>
</template>
<!-- 删除确认 -->
<AdminConfirmDialog
:open="!!confirmDelete"
:loading="deleting"
title="删除动态"
:message="`确定要删除「${confirmDelete?.titleZh}」吗?该动态下的所有评论将一并删除,此操作不可撤销。`"
confirm-text="删除"
@confirm="doDelete"
@cancel="confirmDelete = null"
/>
</div>
</template>
+171
View File
@@ -0,0 +1,171 @@
<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:envelope" 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">订阅了站内更新的邮箱列表,可导出 CSV。</p>
</div>
</div>
<a
:href="'/api/admin/subscribers/export'"
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 transition-colors flex-shrink-0"
title="导出全部订阅邮箱"
>
<Icon name="ph:download-simple" class="w-4 h-4" />
导出 CSV
</a>
</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">共 {{ subscribers.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="loadSubscribers" 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="!subscribers.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:envelope" 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">邮箱</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 text-right">操作</th>
</tr>
</thead>
<tbody>
<tr
v-for="s in subscribers"
:key="s.id"
class="border-b border-gray-100 last:border-b-0 hover:bg-gray-50/70 transition-colors"
>
<td class="px-6 py-4">
<a :href="`mailto:${s.email}`" class="inline-flex items-center gap-2 font-semibold text-black hover:text-gray-600 transition-colors">
<Icon name="ph:envelope-simple" class="w-4 h-4 text-gray-400" />
{{ s.email }}
</a>
</td>
<td class="px-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(s.createdAt) }}</td>
<td class="px-6 py-4">
<div class="flex items-center justify-end">
<button
@click="removeSubscriber(s)"
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"
title="删除订阅"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '邮件订阅' })
interface AdminSubscriber {
id: number
email: string
createdAt: string
}
const subscribers = ref<AdminSubscriber[]>([])
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 loadSubscribers = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ subscribers: AdminSubscriber[] }>('/api/admin/subscribers')
subscribers.value = data.subscribers
} catch (err) {
console.error('load subscribers error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
const removeSubscriber = async (s: AdminSubscriber) => {
if (!window.confirm(`确定取消订阅「${s.email}」吗?此操作不可恢复。`)) return
try {
await $fetch(`/api/admin/subscribers/${s.id}`, { method: 'DELETE' })
subscribers.value = subscribers.value.filter((x) => x.id !== s.id)
showFlash(`已取消订阅「${s.email}」`)
} catch (err) {
console.error('delete subscriber error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
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(loadSubscribers)
</script>
+212
View File
@@ -0,0 +1,212 @@
<template>
<section class="p-4 md:p-8 lg:p-10">
<header class="mb-8">
<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:users-three" 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>
</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">共 {{ users.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="loadUsers" 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="!users.length" class="py-24 flex flex-col items-center gap-3 text-gray-400">
<Icon name="ph:users-three" 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">ID</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>
<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>
<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 text-right">操作</th>
</tr>
</thead>
<tbody>
<tr
v-for="u in users"
:key="u.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-400 font-mono text-xs whitespace-nowrap">#{{ u.id }}</td>
<td class="px-6 py-4 font-semibold text-black whitespace-nowrap">{{ u.username }}</td>
<td class="px-6 py-4 text-gray-600">{{ u.email || '—' }}</td>
<td class="px-6 py-4 text-gray-600 whitespace-nowrap">{{ u.phone || '—' }}</td>
<td class="px-6 py-4 whitespace-nowrap">
<span
class="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-semibold"
:class="u.role === 'admin' ? 'bg-black text-white' : 'bg-gray-100 text-gray-600'"
>
<Icon v-if="u.role === 'admin'" name="ph:crown-simple" class="w-3.5 h-3.5" />
{{ u.role === 'admin' ? '管理员' : '用户' }}
</span>
</td>
<td class="px-6 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(u.createdAt) }}</td>
<td class="px-6 py-4">
<div class="flex items-center justify-end gap-2">
<template v-if="u.id !== meId">
<button
@click="toggleRole(u)"
class="px-3 py-1.5 rounded-full text-xs font-semibold border transition-colors"
:class="u.role === 'admin'
? 'border-gray-200 bg-gray-100 text-gray-600 hover:bg-gray-200'
: 'border-black bg-black text-white hover:bg-gray-800'"
:title="u.role === 'admin' ? '设为普通用户' : '设为管理员'"
>
{{ u.role === 'admin' ? '设为用户' : '设为管理员' }}
</button>
<button
@click="removeUser(u)"
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"
title="删除用户"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</template>
<span v-else class="px-3 py-1.5 rounded-full text-xs font-semibold bg-gray-50 text-gray-400 border border-gray-100">
当前账号
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useUserStore } from '~/stores/user'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useSeoMeta({ title: '用户管理' })
interface AdminUser {
id: number
username: string
email: string | null
phone: string | null
role: 'user' | 'admin'
createdAt: string
}
const userStore = useUserStore()
const meId = computed(() => userStore.user?.id)
const users = ref<AdminUser[]>([])
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 loadUsers = async () => {
loading.value = true
loadError.value = ''
try {
const data = await $fetch<{ users: AdminUser[] }>('/api/admin/users')
users.value = data.users
} catch (err) {
console.error('load users error:', err)
loadError.value = '加载失败,请重试'
} finally {
loading.value = false
}
}
// 切换角色(user ↔ admin),后端拒绝修改自己的角色
const toggleRole = async (u: AdminUser) => {
const next: AdminUser['role'] = u.role === 'admin' ? 'user' : 'admin'
try {
await $fetch(`/api/admin/users/${u.id}`, {
method: 'PATCH',
body: { role: next },
})
u.role = next
showFlash(`已将「${u.username}」设为${next === 'admin' ? '管理员' : '普通用户'}`)
} catch (err) {
console.error('toggle role error:', err)
showFlash((err as any)?.data?.message || '操作失败,请重试', 'err')
}
}
const removeUser = async (u: AdminUser) => {
if (!window.confirm(`确定删除用户「${u.username}」吗?此操作不可恢复。`)) return
try {
await $fetch(`/api/admin/users/${u.id}`, { method: 'DELETE' })
users.value = users.value.filter((x) => x.id !== u.id)
showFlash(`已删除用户「${u.username}」`)
} catch (err) {
console.error('delete user error:', err)
showFlash((err as any)?.data?.message || '删除失败,请重试', 'err')
}
}
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(loadUsers)
</script>
+709
View File
@@ -0,0 +1,709 @@
<script setup lang="ts">
/**
* 后台作品管理:列表 + 新建/编辑表单。
* - 列表来自 /api/admin/works(含未发布)
* - 新建 POST /api/admin/works;编辑 PATCH /api/admin/works/[id];删除 DELETE /api/admin/works/[id]
* - tags 逗号分隔输入;gallery 多 URL 行(每行可上传)
*/
import { onMounted, reactive, ref } from 'vue'
definePageMeta({ middleware: 'admin', layout: 'admin' })
useHead({ title: '作品管理' })
interface Work {
id?: number
slug: string
category: 'webDesign' | 'branding' | 'development'
titleZh: string
titleEn: string
descriptionZh: string
descriptionEn: string
overviewZh: string
overviewEn: string
challengeZh: string
challengeEn: string
solutionZh: string
solutionEn: string
clientZh: string
clientEn: string
durationZh: string
durationEn: string
year: string
tags: string[]
image: string | null
gallery: string[]
url: string | null
published: number
createdAt: string
updatedAt: string
}
const CATEGORIES = [
{ value: 'webDesign', label: '网页设计' },
{ value: 'branding', label: '品牌设计' },
{ value: 'development', label: '程序开发' },
]
const catLabel = (c: string) => CATEGORIES.find((x) => x.value === c)?.label ?? c
/* ---------- 列表 ---------- */
const works = ref<Work[]>([])
const loading = ref(true)
const error = ref('')
const loadWorks = async () => {
loading.value = true
error.value = ''
try {
const res = await $fetch<{ works: Work[] }>('/api/admin/works')
works.value = res.works
} catch (err: any) {
error.value = err?.data?.message || '加载失败'
} finally {
loading.value = false
}
}
onMounted(loadWorks)
/* ---------- 提示 ---------- */
const message = ref('')
const messageType = ref<'success' | 'error'>('success')
let toastTimer: ReturnType<typeof setTimeout> | undefined
const toast = (text: string, type: 'success' | 'error' = 'success') => {
message.value = text
messageType.value = type
if (toastTimer) clearTimeout(toastTimer)
toastTimer = setTimeout(() => (message.value = ''), 3000)
}
/* ---------- 表单 ---------- */
const view = ref<'list' | 'form'>('list')
const editingId = ref<number | null>(null)
const saving = ref(false)
const formError = ref('')
const emptyForm = () => ({
slug: '',
category: 'webDesign' as Work['category'],
titleZh: '',
titleEn: '',
descriptionZh: '',
descriptionEn: '',
overviewZh: '',
overviewEn: '',
challengeZh: '',
challengeEn: '',
solutionZh: '',
solutionEn: '',
clientZh: '',
clientEn: '',
durationZh: '',
durationEn: '',
year: String(new Date().getFullYear()),
image: '',
gallery: [''] as string[],
url: '',
published: true,
})
const form = reactive(emptyForm())
const tagsText = ref('')
const openCreate = () => {
Object.assign(form, emptyForm())
tagsText.value = ''
editingId.value = null
formError.value = ''
view.value = 'form'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const openEdit = (w: Work) => {
if (w.id == null) {
toast('该条目缺少 id 字段,暂时无法编辑(后端待补)', 'error')
return
}
editingId.value = w.id
Object.assign(form, {
slug: w.slug,
category: w.category,
titleZh: w.titleZh,
titleEn: w.titleEn,
descriptionZh: w.descriptionZh,
descriptionEn: w.descriptionEn,
overviewZh: w.overviewZh,
overviewEn: w.overviewEn,
challengeZh: w.challengeZh,
challengeEn: w.challengeEn,
solutionZh: w.solutionZh,
solutionEn: w.solutionEn,
clientZh: w.clientZh,
clientEn: w.clientEn,
durationZh: w.durationZh,
durationEn: w.durationEn,
year: w.year,
image: w.image ?? '',
gallery: w.gallery.length ? [...w.gallery] : [''],
url: w.url ?? '',
published: !!w.published,
})
tagsText.value = w.tags.join(', ')
formError.value = ''
view.value = 'form'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const backToList = () => {
view.value = 'list'
window.scrollTo({ top: 0, behavior: 'smooth' })
}
const save = async () => {
formError.value = ''
if (!/^[a-z0-9-]{2,64}$/.test(form.slug)) {
formError.value = 'slug 需为 2-64 位小写字母/数字/中划线'
return
}
if (!form.titleZh.trim() || !form.titleEn.trim()) {
formError.value = '中英文标题均为必填'
return
}
saving.value = true
try {
const payload = {
slug: form.slug,
category: form.category,
titleZh: form.titleZh,
titleEn: form.titleEn,
descriptionZh: form.descriptionZh,
descriptionEn: form.descriptionEn,
overviewZh: form.overviewZh,
overviewEn: form.overviewEn,
challengeZh: form.challengeZh,
challengeEn: form.challengeEn,
solutionZh: form.solutionZh,
solutionEn: form.solutionEn,
clientZh: form.clientZh,
clientEn: form.clientEn,
durationZh: form.durationZh,
durationEn: form.durationEn,
year: form.year,
tags: tagsText.value.split(',').map((t) => t.trim()).filter(Boolean),
image: form.image || null,
gallery: form.gallery.filter((u) => u.trim()),
url: form.url.trim() || null,
published: form.published,
}
if (editingId.value != null) {
await $fetch(`/api/admin/works/${editingId.value}`, { method: 'PATCH', body: payload })
toast('作品已更新')
} else {
await $fetch('/api/admin/works', { method: 'POST', body: payload })
toast('作品已创建')
}
await loadWorks()
view.value = 'list'
} catch (err: any) {
formError.value = err?.data?.message || '保存失败'
} finally {
saving.value = false
}
}
/* ---------- 删除 ---------- */
const confirmDelete = ref<Work | null>(null)
const deleting = ref(false)
const askDelete = (w: Work) => {
if (w.id == null) {
toast('该条目缺少 id 字段,暂时无法删除(后端待补)', 'error')
return
}
confirmDelete.value = w
}
const doDelete = async () => {
const w = confirmDelete.value
if (!w || w.id == null) return
deleting.value = true
try {
await $fetch(`/api/admin/works/${w.id}`, { method: 'DELETE' })
toast(`已删除「${w.titleZh}」`)
confirmDelete.value = null
await loadWorks()
} catch (err: any) {
toast(err?.data?.message || '删除失败', 'error')
} finally {
deleting.value = false
}
}
/* ---------- 图集(gallery)上传 ---------- */
const galleryFileInput = ref<HTMLInputElement | null>(null)
const uploadingIndex = ref<number | null>(null)
const galleryUploading = ref(false)
const galleryError = ref('')
const pickGalleryFile = (i: number) => {
uploadingIndex.value = i
galleryFileInput.value?.click()
}
const onGalleryFile = async (e: Event) => {
const input = e.target as HTMLInputElement
const file = input.files?.[0]
input.value = ''
if (!file || uploadingIndex.value == null) return
galleryUploading.value = true
galleryError.value = ''
try {
const fd = new FormData()
fd.append('file', file)
const res = await $fetch<{ ok: boolean; url: string }>('/api/admin/upload', {
method: 'POST',
body: fd,
})
form.gallery[uploadingIndex.value] = res.url
} catch (err: any) {
galleryError.value = err?.data?.message || '上传失败'
} finally {
galleryUploading.value = false
}
}
const addGalleryRow = () => form.gallery.push('')
const removeGalleryRow = (i: number) => form.gallery.splice(i, 1)
</script>
<template>
<div>
<!-- 页头 -->
<div class="flex items-center justify-between gap-4 mb-8">
<div>
<h1 class="text-2xl md:text-3xl font-bold text-black tracking-tight">作品管理</h1>
<p v-if="view === 'list'" class="mt-1 text-sm text-gray-500">共 {{ works.length }} 个作品</p>
</div>
<button
v-if="view === 'list'"
type="button"
@click="openCreate"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
<Icon name="ph:plus" class="w-4 h-4" /> 新建作品
</button>
<button
v-else
type="button"
@click="backToList"
class="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors"
>
<Icon name="ph:arrow-left" class="w-4 h-4" /> 返回列表
</button>
</div>
<!-- 提示 -->
<div
v-if="message"
class="mb-6 px-4 py-3 rounded-xl text-sm font-medium border"
:class="messageType === 'success' ? 'bg-green-50 border-green-200 text-green-700' : 'bg-red-50 border-red-200 text-red-700'"
>
{{ message }}
</div>
<!-- ==================== 列表视图 ==================== -->
<template v-if="view === 'list'">
<div v-if="loading" class="space-y-3">
<div
v-for="i in 6"
:key="i"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 h-16 animate-pulse"
></div>
</div>
<div
v-else-if="error"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-10 text-center"
>
<Icon name="ph:warning-circle" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
<p class="text-gray-500 mb-4">{{ error }}</p>
<button
type="button"
@click="loadWorks"
class="px-5 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors"
>
重新加载
</button>
</div>
<div
v-else-if="!works.length"
class="rounded-2xl bg-white border border-gray-100 shadow-sm p-14 text-center"
>
<Icon name="ph:image-square" class="w-10 h-10 text-gray-300 mx-auto mb-4" />
<p class="text-gray-500 mb-4">还没有作品,点击右上角「新建作品」创建第一个</p>
</div>
<div v-else class="rounded-2xl bg-white 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-400 border-b border-gray-100">
<th class="px-5 py-4 font-semibold">标题</th>
<th class="px-5 py-4 font-semibold">分类</th>
<th class="px-5 py-4 font-semibold">年份</th>
<th class="px-5 py-4 font-semibold">标签</th>
<th class="px-5 py-4 font-semibold">状态</th>
<th class="px-5 py-4 font-semibold text-right">操作</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-50">
<tr
v-for="w in works"
:key="w.slug"
class="hover:bg-gray-50/60 transition-colors"
>
<td class="px-5 py-4">
<div class="flex items-center gap-3">
<img
v-if="w.image"
:src="w.image"
:alt="w.titleZh"
class="w-10 h-10 rounded-lg object-cover border border-gray-100 flex-shrink-0"
/>
<div v-else class="w-10 h-10 rounded-lg bg-gray-100 flex items-center justify-center flex-shrink-0">
<Icon name="ph:image" class="w-4 h-4 text-gray-400" />
</div>
<div class="min-w-0">
<p class="font-semibold text-black truncate">{{ w.titleZh }}</p>
<p class="text-xs text-gray-400 mt-0.5 font-mono truncate">{{ w.slug }}</p>
</div>
</div>
</td>
<td class="px-5 py-4">
<span class="px-2.5 py-1 rounded-full bg-gray-100 text-gray-700 text-xs font-medium">
{{ catLabel(w.category) }}
</span>
</td>
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ w.year || '-' }}</td>
<td class="px-5 py-4">
<div class="flex flex-wrap gap-1.5 max-w-[220px]">
<span
v-for="tag in w.tags.slice(0, 3)"
:key="tag"
class="px-2 py-0.5 rounded-full bg-gray-50 text-gray-500 text-xs"
>
{{ tag }}
</span>
<span v-if="w.tags.length > 3" class="text-xs text-gray-400">+{{ w.tags.length - 3 }}</span>
</div>
</td>
<td class="px-5 py-4">
<span
class="px-2.5 py-1 rounded-full text-xs font-semibold inline-flex items-center gap-1"
:class="w.published ? 'bg-green-50 text-green-700' : 'bg-amber-50 text-amber-700'"
>
<span
class="w-1.5 h-1.5 rounded-full"
:class="w.published ? 'bg-green-500' : 'bg-amber-500'"
></span>
{{ w.published ? '已发布' : '草稿' }}
</span>
</td>
<td class="px-5 py-4">
<div class="flex items-center justify-end gap-2">
<button
type="button"
@click="openEdit(w)"
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-gray-100 text-gray-700 text-xs font-semibold hover:bg-gray-200 transition-colors"
>
<Icon name="ph:pencil-simple" class="w-3.5 h-3.5" /> 编辑
</button>
<button
type="button"
@click="askDelete(w)"
class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-red-50 text-red-600 text-xs font-semibold hover:bg-red-100 transition-colors"
>
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<!-- ==================== 表单视图 ==================== -->
<template v-else>
<div class="space-y-6 max-w-4xl">
<!-- 基本信息 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:info" class="w-4 h-4 text-gray-400" /> 基本信息
</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
slug<span class="text-red-500">*</span>
</label>
<input
v-model="form.slug"
type="text"
placeholder="如 forma-digital"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 font-mono"
/>
<p class="mt-1 text-xs text-gray-400">2-64 位小写字母 / 数字 / 中划线,用于前台链接</p>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">分类</label>
<select
v-model="form.category"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
>
<option v-for="c in CATEGORIES" :key="c.value" :value="c.value">{{ c.label }}</option>
</select>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">年份</label>
<input
v-model="form.year"
type="text"
placeholder="如 2026"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
标签<span class="text-gray-400 font-normal">(逗号分隔)</span>
</label>
<input
v-model="tagsText"
type="text"
placeholder="Web Design, React, E-commerce"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">
外部链接<span class="text-gray-400 font-normal">(可选)</span>
</label>
<input
v-model="form.url"
type="text"
placeholder="https://… 留空则前台不显示访问按钮"
class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black"
/>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">发布状态</label>
<button
type="button"
class="inline-flex items-center gap-3 group mt-1"
@click="form.published = !form.published"
>
<span
class="relative w-11 h-6 rounded-full transition-colors duration-300"
:class="form.published ? 'bg-black' : 'bg-gray-300'"
>
<span
class="absolute top-0.5 left-0.5 w-5 h-5 rounded-full bg-white shadow transition-transform duration-300"
:class="form.published ? 'translate-x-5' : ''"
></span>
</span>
<span class="text-sm" :class="form.published ? 'text-green-700 font-semibold' : 'text-gray-500'">
{{ form.published ? '已发布(前台可见)' : '草稿(前台隐藏)' }}
</span>
</button>
</div>
</div>
</section>
<!-- 标题与简介 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:text-aa" class="w-4 h-4 text-gray-400" /> 标题与简介
</h3>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文标题<span class="text-red-500">*</span></label>
<input v-model="form.titleZh" type="text" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文简介</label>
<textarea v-model="form.descriptionZh" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
</div>
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Title<span class="text-red-500">*</span></label>
<input v-model="form.titleEn" type="text" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Description</label>
<textarea v-model="form.descriptionEn" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
</div>
</div>
</section>
<!-- 项目详情(中英) -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:notebook" class="w-4 h-4 text-gray-400" /> 项目详情(中英各一组)
</h3>
<div class="space-y-6">
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文概述(Overview)</label>
<textarea v-model="form.overviewZh" rows="4" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文挑战(Challenge)</label>
<textarea v-model="form.challengeZh" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文方案(Solution)</label>
<textarea v-model="form.solutionZh" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
</div>
<div class="space-y-4">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Overview</label>
<textarea v-model="form.overviewEn" rows="4" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Challenge</label>
<textarea v-model="form.challengeEn" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Solution</label>
<textarea v-model="form.solutionEn" rows="3" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5 resize-y"></textarea>
</div>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文客户(Client)</label>
<input v-model="form.clientZh" type="text" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Client</label>
<input v-model="form.clientEn" type="text" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">中文周期(Duration)</label>
<input v-model="form.durationZh" type="text" placeholder="如 3 个月" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-2">English Duration</label>
<input v-model="form.durationEn" type="text" placeholder="如 3 months" class="w-full px-3 py-2.5 rounded-lg border border-gray-200 bg-white text-sm outline-none focus:border-black" />
</div>
</div>
</div>
</section>
<!-- 封面图 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<h3 class="text-sm font-bold text-gray-900 mb-4 flex items-center gap-2">
<Icon name="ph:image" class="w-4 h-4 text-gray-400" /> 封面图
</h3>
<AdminImageUpload v-model="form.image" label="封面图" />
</section>
<!-- 作品图集 -->
<section class="rounded-2xl bg-white border border-gray-100 shadow-sm p-5 md:p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-sm font-bold text-gray-900 flex items-center gap-2">
<Icon name="ph:images" class="w-4 h-4 text-gray-400" /> 作品图集(Gallery)
</h3>
<button
type="button"
@click="addGalleryRow"
class="flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 text-xs font-semibold text-gray-700 hover:bg-gray-50 transition-colors"
>
<Icon name="ph:plus" class="w-3.5 h-3.5" /> 添加图片
</button>
</div>
<div class="space-y-3">
<div
v-for="(url, i) in form.gallery"
:key="i"
class="flex items-center gap-3 border border-gray-200 rounded-xl bg-white p-3"
>
<div class="flex-1 min-w-0">
<input
v-model="form.gallery[i]"
type="text"
placeholder="图片 URL(本地 /uploads/... 或 https://...)"
class="w-full px-3 py-2 rounded-lg border border-gray-200 bg-gray-50 text-sm outline-none transition-all focus:border-black focus:bg-white"
/>
<div v-if="url" class="mt-2">
<img :src="url" alt="图集预览" class="h-24 w-auto max-w-full rounded-lg border border-gray-100 object-cover" />
</div>
</div>
<button
type="button"
:disabled="galleryUploading && uploadingIndex === i"
@click="pickGalleryFile(i)"
class="flex items-center gap-1.5 px-3 py-2 rounded-lg bg-black text-white text-xs font-semibold hover:bg-gray-800 transition-colors disabled:opacity-60 flex-shrink-0"
>
<Icon
:name="galleryUploading && uploadingIndex === i ? 'ph:circle-notch' : 'ph:upload-simple'"
:class="galleryUploading && uploadingIndex === i ? 'w-3.5 h-3.5 animate-spin' : 'w-3.5 h-3.5'"
/>
{{ galleryUploading && uploadingIndex === i ? '上传中…' : '上传' }}
</button>
<button
type="button"
@click="removeGalleryRow(i)"
class="w-8 h-8 rounded-lg hover:bg-red-50 text-gray-400 hover:text-red-600 flex items-center justify-center transition-colors flex-shrink-0"
aria-label="删除该图"
>
<Icon name="ph:trash" class="w-4 h-4" />
</button>
</div>
<p v-if="galleryError" class="text-xs text-red-600">{{ galleryError }}</p>
</div>
<input ref="galleryFileInput" type="file" accept="image/*" class="hidden" @change="onGalleryFile" />
</section>
<!-- 提交 -->
<div class="flex items-center justify-end gap-3">
<p v-if="formError" class="mr-auto text-sm text-red-600">{{ formError }}</p>
<button
type="button"
@click="backToList"
class="px-5 py-2.5 rounded-lg border border-gray-200 bg-white text-sm font-semibold text-gray-700 hover:bg-gray-100 transition-colors"
>
取消
</button>
<button
type="button"
:disabled="saving"
@click="save"
class="px-6 py-2.5 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-60 inline-flex items-center gap-2"
>
<Icon v-if="saving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ saving ? '保存中…' : editingId != null ? '保存修改' : '创建作品' }}
</button>
</div>
</div>
</template>
<!-- 删除确认 -->
<AdminConfirmDialog
:open="!!confirmDelete"
:loading="deleting"
title="删除作品"
:message="`确定要删除「${confirmDelete?.titleZh}」吗?该作品下的所有收藏将一并删除,此操作不可撤销。`"
confirm-text="删除"
@confirm="doDelete"
@cancel="confirmDelete = null"
/>
</div>
</template>
+88 -40
View File
@@ -14,7 +14,7 @@
<!-- 文章 Hero -->
<section class="relative h-[50vh] md:h-[60vh] overflow-hidden">
<img
:src="article.image"
:src="article.image || ''"
:alt="title"
class="w-full h-full object-cover"
/>
@@ -23,7 +23,7 @@
<div class="max-w-[800px] mx-auto">
<span
class="inline-block px-3 py-1.5 text-xs md:text-sm font-semibold rounded-full mb-4"
:class="article.badge"
:class="categoryBadge[article.category]"
>
{{ t(`news.categories.${article.category}`) }}
</span>
@@ -48,41 +48,38 @@
</div>
</section>
<!-- 文章内容 -->
<!-- 文章内容(内容块:paragraph / heading / quote / image) -->
<section class="max-w-[800px] mx-auto px-6 md:px-10 py-16 md:py-24">
<article class="prose prose-lg max-w-none">
<p class="text-gray-700 text-lg leading-relaxed mb-8">
{{ t(`news.detail.items.${slug}.content`) }}
</p>
<template v-for="(block, index) in blocks" :key="index">
<p
v-if="block.type === 'paragraph'"
class="text-gray-700 text-lg leading-relaxed mb-8"
>
{{ block.text }}
</p>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6 mt-12">
{{ t(`news.detail.items.${slug}.section1Title`) }}
</h2>
<p class="text-gray-700 text-lg leading-relaxed mb-8">
{{ t(`news.detail.items.${slug}.section1Content`) }}
</p>
<h2
v-else-if="block.type === 'heading'"
class="text-2xl md:text-3xl font-bold text-black mb-6 mt-12"
>
{{ block.text }}
</h2>
<blockquote class="border-l-4 border-black pl-6 my-10 italic text-gray-600 text-lg">
"{{ t(`news.detail.items.${slug}.quote`) }}"
</blockquote>
<blockquote
v-else-if="block.type === 'quote'"
class="border-l-4 border-black pl-6 my-10 italic text-gray-600 text-lg"
>
"{{ block.text }}"
</blockquote>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6 mt-12">
{{ t(`news.detail.items.${slug}.section2Title`) }}
</h2>
<p class="text-gray-700 text-lg leading-relaxed mb-8">
{{ t(`news.detail.items.${slug}.section2Content`) }}
</p>
<div class="my-10 rounded-2xl overflow-hidden">
<img :src="article.image" :alt="title" class="w-full h-auto" />
</div>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6 mt-12">
{{ t(`news.detail.items.${slug}.section3Title`) }}
</h2>
<p class="text-gray-700 text-lg leading-relaxed mb-8">
{{ t(`news.detail.items.${slug}.section3Content`) }}
</p>
<img
v-else-if="block.type === 'image'"
:src="block.text"
:alt="title"
class="my-10 rounded-2xl w-full h-auto"
/>
</template>
</article>
<!-- 分享按钮(真实动作) -->
@@ -135,12 +132,12 @@
class="group block rounded-2xl overflow-hidden bg-white shadow-lg hover:shadow-xl transition-all duration-300 hover:-translate-y-1"
>
<div class="relative aspect-[16/10] overflow-hidden">
<img :src="related.image" :alt="t(`news.items.${related.slug}.title`)" class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105" />
<img :src="related.image || ''" :alt="locText(related.titleZh, related.titleEn)" class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105" />
</div>
<div class="p-5">
<span class="text-xs text-gray-500">{{ formatNewsDate(related.date, locale) }}</span>
<h3 class="text-lg font-bold text-black group-hover:text-gray-600 transition-colors mt-1 line-clamp-2">
{{ t(`news.items.${related.slug}.title`) }}
{{ locText(related.titleZh, related.titleEn) }}
</h3>
</div>
</NuxtLink>
@@ -155,25 +152,76 @@ import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useSeoMeta, createError } from '#imports'
import { newsBySlug, newsItems, formatNewsDate } from '~/data/news'
const { t, locale } = useI18n()
const route = useRoute()
const slug = route.params.slug as string
const article = newsBySlug[slug]
interface Block {
type: 'paragraph' | 'heading' | 'quote' | 'image'
text: string
}
if (!article) {
interface ArticleItem {
slug: string
category: 'insights' | 'projects' | 'announcements'
titleZh: string
titleEn: string
excerptZh: string
excerptEn: string
contentZh: Block[]
contentEn: Block[]
image: string | null
date: string | null
readTime: number
author: string
}
const [{ data, error }, { data: listData }] = await Promise.all([
useFetch<{ article: ArticleItem }>(`/api/news/${slug}`),
useFetch<{ articles: ArticleItem[] }>('/api/news'),
])
// 未发布 / 不存在 → 404
if (error.value || !data.value?.article) {
throw createError({ statusCode: 404, statusMessage: 'Article not found' })
}
const title = computed(() => t(`news.items.${slug}.title`))
const article = computed<ArticleItem>(() => data.value!.article)
const relatedArticles = newsItems.filter((a) => a.slug !== slug).slice(0, 3)
const locText = (zh: string, en: string) => (locale.value === 'zh' ? zh : en)
function formatNewsDate(date: string | null, loc: string): string {
if (!date) return ''
const d = new Date(`${date}T00:00:00`)
if (Number.isNaN(d.getTime())) return date
return new Intl.DateTimeFormat(loc === 'zh' ? 'zh-CN' : 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(d)
}
const categoryBadge: Record<string, string> = {
insights: 'bg-purple-100 text-purple-700',
projects: 'bg-blue-100 text-blue-700',
announcements: 'bg-green-100 text-green-700',
}
const title = computed(() => locText(article.value.titleZh, article.value.titleEn))
// 正文内容块(按当前语言选择 contentZh / contentEn)
const blocks = computed(() =>
locale.value === 'zh' ? article.value.contentZh : article.value.contentEn,
)
const relatedArticles = computed(() =>
(listData.value?.articles ?? []).filter((a) => a.slug !== slug).slice(0, 3),
)
useSeoMeta({
title: () => `${title.value} - ${t('news.title')}`,
description: () => t(`news.items.${slug}.excerpt`),
description: () => locText(article.value.excerptZh, article.value.excerptEn),
})
// ---- 分享(真实动作) ----
+47 -10
View File
@@ -58,15 +58,15 @@
>
<div class="relative aspect-[16/10] overflow-hidden bg-gray-100">
<img
:src="article.image"
:alt="t(`news.items.${article.slug}.title`)"
:src="article.image || ''"
:alt="locText(article.titleZh, article.titleEn)"
:loading="'lazy'"
class="w-full h-full object-cover transition-transform duration-700 group-hover:scale-110"
/>
<div class="absolute top-4 left-4">
<span
class="px-3 py-1.5 text-xs md:text-sm font-semibold rounded-full shadow-sm"
:class="article.badge"
:class="categoryBadge[article.category]"
>
{{ t(`news.categories.${article.category}`) }}
</span>
@@ -86,11 +86,11 @@
</div>
<h3 class="text-lg md:text-xl font-bold text-black mb-3 group-hover:text-gray-700 transition-colors duration-300 line-clamp-2">
{{ t(`news.items.${article.slug}.title`) }}
{{ locText(article.titleZh, article.titleEn) }}
</h3>
<p class="text-sm md:text-base text-gray-600 mb-4 flex-grow line-clamp-3">
{{ t(`news.items.${article.slug}.excerpt`) }}
{{ locText(article.excerptZh, article.excerptEn) }}
</p>
<div class="flex items-center justify-between pt-3 border-t border-gray-100">
@@ -127,11 +127,48 @@ import { useI18n } from 'vue-i18n'
import { useSeoMeta } from '#imports'
import gsap from 'gsap'
import { useAppStore } from '~/stores/app'
import { newsItems, formatNewsDate, getNewsCategories } from '~/data/news'
const { t, locale } = useI18n()
const appStore = useAppStore()
interface ArticleItem {
slug: string
category: 'insights' | 'projects' | 'announcements'
titleZh: string
titleEn: string
excerptZh: string
excerptEn: string
image: string | null
date: string | null
readTime: number
author: string
}
// 内容 API(仅已发布,date 降序)
const { data } = await useFetch<{ articles: ArticleItem[] }>('/api/news')
const articles = computed(() => data.value?.articles ?? [])
// 双语选择:切换语言即时生效(内容双字段已在 API 返回,无需重新请求)
const locText = (zh: string, en: string) => (locale.value === 'zh' ? zh : en)
function formatNewsDate(date: string | null, loc: string): string {
if (!date) return ''
const d = new Date(`${date}T00:00:00`)
if (Number.isNaN(d.getTime())) return date
return new Intl.DateTimeFormat(loc === 'zh' ? 'zh-CN' : 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(d)
}
// 分类徽章样式(insights=紫、projects=蓝、announcements=绿)
const categoryBadge: Record<string, string> = {
insights: 'bg-purple-100 text-purple-700',
projects: 'bg-blue-100 text-blue-700',
announcements: 'bg-green-100 text-green-700',
}
const headerWrapRef = ref<HTMLElement | null>(null)
const titleRef = ref<HTMLElement | null>(null)
const descRef = ref<HTMLElement | null>(null)
@@ -144,16 +181,16 @@ const visibleCount = ref(0)
const PAGE_SIZE = 6
const categories = ['all', ...getNewsCategories()]
const categories = ['all', 'insights', 'projects', 'announcements']
// 分类 + 搜索过滤(标题/摘要按当前语言文案匹配)
const filteredNews = computed(() => {
const kw = keyword.value.trim().toLowerCase()
return newsItems.filter((item) => {
return articles.value.filter((item) => {
if (activeCategory.value !== 'all' && item.category !== activeCategory.value) return false
if (!kw) return true
const title = t(`news.items.${item.slug}.title`).toLowerCase()
const excerpt = t(`news.items.${item.slug}.excerpt`).toLowerCase()
const title = locText(item.titleZh, item.titleEn).toLowerCase()
const excerpt = locText(item.excerptZh, item.excerptEn).toLowerCase()
return title.includes(kw) || excerpt.includes(kw)
})
})
+52 -18
View File
@@ -14,7 +14,7 @@
<!-- Hero 大图 -->
<section class="relative h-[60vh] md:h-[70vh] overflow-hidden">
<img
:src="project.image"
:src="project.image || ''"
:alt="title"
class="w-full h-full object-cover"
/>
@@ -34,7 +34,7 @@
{{ title }}
</h1>
<p class="text-lg md:text-xl text-white/80 max-w-2xl">
{{ t(`work.items.${slug}.description`) }}
{{ description }}
</p>
</div>
</div>
@@ -48,26 +48,26 @@
<div>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6">{{ t('work.detail.overview') }}</h2>
<p class="text-gray-700 text-base md:text-lg leading-relaxed">
{{ t(`work.detail.items.${slug}.overview`) }}
{{ locText(project.overviewZh, project.overviewEn) }}
</p>
</div>
<div>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6">{{ t('work.detail.challenge') }}</h2>
<p class="text-gray-700 text-base md:text-lg leading-relaxed">
{{ t(`work.detail.items.${slug}.challenge`) }}
{{ locText(project.challengeZh, project.challengeEn) }}
</p>
</div>
<div>
<h2 class="text-2xl md:text-3xl font-bold text-black mb-6">{{ t('work.detail.solution') }}</h2>
<p class="text-gray-700 text-base md:text-lg leading-relaxed">
{{ t(`work.detail.items.${slug}.solution`) }}
{{ locText(project.solutionZh, project.solutionEn) }}
</p>
</div>
<!-- 项目截图 -->
<div class="space-y-6">
<div v-if="project.gallery.length" class="space-y-6">
<h2 class="text-2xl md:text-3xl font-bold text-black">{{ t('work.detail.gallery') }}</h2>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div
@@ -89,7 +89,7 @@
<div class="space-y-5">
<div>
<p class="text-sm text-gray-500 mb-1">{{ t('work.detail.client') }}</p>
<p class="text-base font-semibold text-black">{{ t(`work.detail.items.${slug}.client`) }}</p>
<p class="text-base font-semibold text-black">{{ locText(project.clientZh, project.clientEn) }}</p>
</div>
<div>
<p class="text-sm text-gray-500 mb-1">{{ t('work.detail.year') }}</p>
@@ -97,7 +97,7 @@
</div>
<div>
<p class="text-sm text-gray-500 mb-1">{{ t('work.detail.duration') }}</p>
<p class="text-base font-semibold text-black">{{ t(`work.detail.items.${slug}.duration`) }}</p>
<p class="text-base font-semibold text-black">{{ locText(project.durationZh, project.durationEn) }}</p>
</div>
<div>
<p class="text-sm text-gray-500 mb-1">{{ t('work.detail.services') }}</p>
@@ -146,11 +146,11 @@
class="group block rounded-2xl overflow-hidden bg-white shadow-lg hover:shadow-xl transition-all duration-300 hover:-translate-y-1"
>
<div class="relative aspect-[4/3] overflow-hidden">
<img :src="related.image" :alt="t(`work.items.${related.slug}.title`)" class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105" />
<img :src="related.image || ''" :alt="locText(related.titleZh, related.titleEn)" class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-105" />
</div>
<div class="p-5">
<h3 class="text-lg font-bold text-black group-hover:text-gray-600 transition-colors">{{ t(`work.items.${related.slug}.title`) }}</h3>
<p class="text-sm text-gray-500 mt-1">{{ related.tags[0] }}</p>
<h3 class="text-lg font-bold text-black group-hover:text-gray-600 transition-colors">{{ locText(related.titleZh, related.titleEn) }}</h3>
<p class="text-sm text-gray-500 mt-1">{{ related.tags?.[0] }}</p>
</div>
</NuxtLink>
</div>
@@ -164,24 +164,58 @@ import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useSeoMeta, createError } from '#imports'
import { workBySlug, workItems } from '~/data/work'
const { t } = useI18n()
const { t, locale } = useI18n()
const route = useRoute()
const slug = route.params.slug as string
const project = workBySlug[slug]
interface WorkItem {
slug: string
category: 'webDesign' | 'branding' | 'development'
titleZh: string
titleEn: string
descriptionZh: string
descriptionEn: string
overviewZh: string
overviewEn: string
challengeZh: string
challengeEn: string
solutionZh: string
solutionEn: string
clientZh: string
clientEn: string
durationZh: string
durationEn: string
year: string
tags: string[]
image: string | null
gallery: string[]
url: string | null
}
if (!project) {
const [{ data, error }, { data: listData }] = await Promise.all([
useFetch<{ work: WorkItem }>(`/api/work/${slug}`),
useFetch<{ works: WorkItem[] }>('/api/work'),
])
// 未发布 / 不存在 → 404
if (error.value || !data.value?.work) {
throw createError({ statusCode: 404, statusMessage: 'Project not found' })
}
const title = computed(() => t(`work.items.${slug}.title`))
const project = computed<WorkItem>(() => data.value!.work)
const relatedProjects = workItems.filter((p) => p.slug !== slug).slice(0, 3)
const locText = (zh: string, en: string) => (locale.value === 'zh' ? zh : en)
const title = computed(() => locText(project.value.titleZh, project.value.titleEn))
const description = computed(() => locText(project.value.descriptionZh, project.value.descriptionEn))
const relatedProjects = computed(() =>
(listData.value?.works ?? []).filter((p) => p.slug !== slug).slice(0, 3),
)
useSeoMeta({
title: () => `${title.value} - ${t('work.title')}`,
description: () => t(`work.items.${slug}.description`),
description: () => description.value,
})
</script>
+28 -10
View File
@@ -58,8 +58,8 @@
>
<div class="relative aspect-[4/3] overflow-hidden bg-gray-100">
<img
:src="project.image"
:alt="t(`work.items.${project.slug}.title`)"
:src="project.image || ''"
:alt="locText(project.titleZh, project.titleEn)"
:loading="'lazy'"
class="w-full h-full object-cover transition-transform duration-700 group-hover:scale-110"
/>
@@ -69,13 +69,13 @@
<div class="p-6 md:p-8 bg-white">
<div class="flex items-start justify-between mb-3">
<h3 class="text-xl md:text-2xl font-bold text-black group-hover:text-gray-700 transition-colors duration-300">
{{ t(`work.items.${project.slug}.title`) }}
{{ locText(project.titleZh, project.titleEn) }}
</h3>
<div class="w-9 h-9 md:w-10 md:h-10 rounded-full flex items-center justify-center bg-gray-50 text-gray-400 group-hover:bg-black group-hover:text-white transition-all duration-300 flex-shrink-0 ml-3">
<Icon name="ph:arrow-up-right" class="w-4 h-4 md:w-5 md:h-5" />
</div>
</div>
<p class="text-sm md:text-base text-gray-600 mb-4">{{ t(`work.items.${project.slug}.description`) }}</p>
<p class="text-sm md:text-base text-gray-600 mb-4">{{ locText(project.descriptionZh, project.descriptionEn) }}</p>
<div class="flex flex-wrap gap-2">
<span
v-for="tag in project.tags"
@@ -111,11 +111,29 @@ import { useI18n } from 'vue-i18n'
import { useSeoMeta } from '#imports'
import gsap from 'gsap'
import { useAppStore } from '~/stores/app'
import { workItems, getWorkCategories } from '~/data/work'
const { t } = useI18n()
const { t, locale } = useI18n()
const appStore = useAppStore()
interface WorkItem {
slug: string
category: 'webDesign' | 'branding' | 'development'
titleZh: string
titleEn: string
descriptionZh: string
descriptionEn: string
year: string
tags: string[]
image: string | null
}
// 内容 API(仅已发布)
const { data } = await useFetch<{ works: WorkItem[] }>('/api/work')
const works = computed(() => data.value?.works ?? [])
// 双语选择:切换语言即时生效
const locText = (zh: string, en: string) => (locale.value === 'zh' ? zh : en)
const headerWrapRef = ref<HTMLElement | null>(null)
const titleRef = ref<HTMLElement | null>(null)
const descRef = ref<HTMLElement | null>(null)
@@ -128,16 +146,16 @@ const visibleCount = ref(0)
const PAGE_SIZE = 4
const categories = ['all', ...getWorkCategories()]
const categories = ['all', 'webDesign', 'branding', 'development']
// 分类 + 搜索过滤(标题/描述按当前语言文案匹配)
const filteredWork = computed(() => {
const kw = keyword.value.trim().toLowerCase()
return workItems.filter((item) => {
return works.value.filter((item) => {
if (activeCategory.value !== 'all' && item.category !== activeCategory.value) return false
if (!kw) return true
const title = t(`work.items.${item.slug}.title`).toLowerCase()
const desc = t(`work.items.${item.slug}.description`).toLowerCase()
const title = locText(item.titleZh, item.titleEn).toLowerCase()
const desc = locText(item.descriptionZh, item.descriptionEn).toLowerCase()
return title.includes(kw) || desc.includes(kw)
})
})