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
+130
View File
@@ -0,0 +1,130 @@
<script setup lang="ts">
/**
* 后台正文内容块编辑器(动态/作品共用)。
* v-model 绑定块数组 `{ type, text }[]`,支持添加/删除/上移/下移。
* 图片块以 URL 文本承载。
*/
import { computed } from 'vue'
interface Block {
type: string
text: string
}
const props = withDefaults(defineProps<{ modelValue?: Block[]; label?: string; placeholder?: string }>(), {
modelValue: () => [],
label: '正文内容',
placeholder: '输入内容…',
})
const emit = defineEmits<{ 'update:modelValue': [value: Block[]] }>()
const blocks = computed({
get: () => props.modelValue,
set: (v) => emit('update:modelValue', v),
})
const BLOCK_TYPES = [
{ value: 'paragraph', label: '段落' },
{ value: 'heading', label: '标题' },
{ value: 'quote', label: '引用' },
{ value: 'image', label: '图片' },
]
const addBlock = () => {
blocks.value = [...blocks.value, { type: 'paragraph', text: '' }]
}
const removeBlock = (i: number) => {
blocks.value = blocks.value.filter((_, idx) => idx !== i)
}
const moveUp = (i: number) => {
if (i === 0) return
const arr = [...blocks.value]
const tmp = arr[i - 1]
arr[i - 1] = arr[i]
arr[i] = tmp
blocks.value = arr
}
const moveDown = (i: number) => {
if (i >= blocks.value.length - 1) return
const arr = [...blocks.value]
const tmp = arr[i + 1]
arr[i + 1] = arr[i]
arr[i] = tmp
blocks.value = arr
}
</script>
<template>
<div>
<div class="flex items-center justify-between mb-2">
<label class="text-sm font-semibold text-gray-800">{{ label }}</label>
<button
type="button"
@click="addBlock"
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-if="!blocks.length"
class="text-sm text-gray-400 py-6 text-center border border-dashed border-gray-200 rounded-xl"
>
暂无内容块,点击「添加块」开始
</div>
<div
v-for="(block, i) in blocks"
:key="i"
class="border border-gray-200 rounded-xl bg-white p-3"
>
<div class="flex items-center gap-2 mb-2">
<select
v-model="block.type"
class="px-2 py-1.5 rounded-lg border border-gray-200 text-xs outline-none focus:border-black bg-white"
>
<option v-for="t in BLOCK_TYPES" :key="t.value" :value="t.value">{{ t.label }}</option>
</select>
<span class="text-xs text-gray-400">
{{ block.type === 'image' ? '图片 URL' : '文本内容' }}
</span>
<div class="ml-auto flex items-center gap-1">
<button
type="button"
:disabled="i === 0"
@click="moveUp(i)"
class="w-7 h-7 rounded-lg hover:bg-gray-100 text-gray-500 disabled:opacity-30 flex items-center justify-center transition-colors"
aria-label="上移"
>
<Icon name="ph:arrow-up" class="w-3.5 h-3.5" />
</button>
<button
type="button"
:disabled="i >= blocks.length - 1"
@click="moveDown(i)"
class="w-7 h-7 rounded-lg hover:bg-gray-100 text-gray-500 disabled:opacity-30 flex items-center justify-center transition-colors"
aria-label="下移"
>
<Icon name="ph:arrow-down" class="w-3.5 h-3.5" />
</button>
<button
type="button"
@click="removeBlock(i)"
class="w-7 h-7 rounded-lg hover:bg-red-50 text-gray-400 hover:text-red-600 flex items-center justify-center transition-colors"
aria-label="删除"
>
<Icon name="ph:trash" class="w-3.5 h-3.5" />
</button>
</div>
</div>
<textarea
v-model="block.text"
:rows="block.type === 'image' ? 2 : 4"
:placeholder="block.type === 'image' ? '图片 URL(如 /uploads/xxx 或 https://…)' : placeholder"
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 resize-y"
></textarea>
</div>
</div>
</div>
</template>
@@ -0,0 +1,88 @@
<script setup lang="ts">
/**
* 后台通用确认弹窗(删除等危险操作)。
* 通过 Teleport 渲染到 body,支持危险色与加载态。
*/
withDefaults(
defineProps<{
open: boolean
title?: string
message?: string
confirmText?: string
danger?: boolean
loading?: boolean
}>(),
{
title: '确认操作',
message: '确定要执行该操作吗?',
confirmText: '确认',
danger: true,
loading: false,
},
)
const emit = defineEmits<{ confirm: []; cancel: [] }>()
</script>
<template>
<Teleport to="body">
<Transition name="confirm-fade">
<div v-if="open" class="fixed inset-0 z-[100] flex items-center justify-center p-4">
<div class="absolute inset-0 bg-black/40 backdrop-blur-sm" @click="emit('cancel')"></div>
<div class="relative w-full max-w-sm bg-white rounded-2xl shadow-2xl p-6">
<div class="flex items-start gap-4">
<div
class="w-10 h-10 rounded-full flex items-center justify-center flex-shrink-0"
:class="danger ? 'bg-red-50 text-red-600' : 'bg-gray-100 text-gray-600'"
>
<Icon name="ph:warning-circle" class="w-5 h-5" />
</div>
<div class="min-w-0">
<h3 class="text-lg font-bold text-black">{{ title }}</h3>
<p class="mt-1 text-sm text-gray-600 break-words">{{ message }}</p>
</div>
</div>
<div class="mt-6 flex justify-end gap-3">
<button
type="button"
@click="emit('cancel')"
class="px-4 py-2 rounded-lg border border-gray-200 text-sm font-semibold text-gray-700 hover:bg-gray-50 transition-colors"
>
取消
</button>
<button
type="button"
:disabled="loading"
@click="emit('confirm')"
class="px-4 py-2 rounded-lg text-sm font-semibold text-white flex items-center gap-2 transition-colors disabled:opacity-50"
:class="danger ? 'bg-red-600 hover:bg-red-700' : 'bg-black hover:bg-gray-800'"
>
<Icon v-if="loading" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ confirmText }}
</button>
</div>
</div>
</div>
</Transition>
</Teleport>
</template>
<style scoped>
.confirm-fade-enter-active,
.confirm-fade-leave-active {
transition: opacity 0.2s ease;
}
.confirm-fade-enter-active .relative,
.confirm-fade-leave-active .relative {
transition: transform 0.2s ease, opacity 0.2s ease;
}
.confirm-fade-enter-from,
.confirm-fade-leave-to {
opacity: 0;
}
.confirm-fade-enter-from .relative,
.confirm-fade-leave-to .relative {
transform: translateY(8px) scale(0.98);
opacity: 0;
}
</style>
+86
View File
@@ -0,0 +1,86 @@
<script setup lang="ts">
/**
* 后台图片字段:URL 输入 + 本地图片上传(调 /api/admin/upload)+ 预览。
* v-model 绑定 URL 字符串。
*/
import { ref } from 'vue'
withDefaults(defineProps<{ modelValue?: string | null; label?: string }>(), {
modelValue: '',
label: '图片',
})
const emit = defineEmits<{ 'update:modelValue': [value: string] }>()
const fileInput = ref<HTMLInputElement | null>(null)
const uploading = ref(false)
const error = ref('')
const choose = () => fileInput.value?.click()
const onFileChange = async (e: Event) => {
const input = e.target as HTMLInputElement
const file = input.files?.[0]
input.value = ''
if (!file) return
uploading.value = true
error.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,
})
emit('update:modelValue', res.url)
} catch (err: any) {
error.value = err?.data?.message || '上传失败'
} finally {
uploading.value = false
}
}
</script>
<template>
<div>
<div class="flex items-center justify-between mb-2">
<label class="text-sm font-semibold text-gray-800">{{ label }}</label>
<button
v-if="modelValue"
type="button"
@click="emit('update:modelValue', '')"
class="text-xs text-gray-400 hover:text-red-600 flex items-center gap-1 transition-colors"
>
<Icon name="ph:x-circle" class="w-3.5 h-3.5" /> 清除
</button>
</div>
<div class="flex items-center gap-3">
<input
:value="modelValue"
type="text"
placeholder="图片 URL(本地 /uploads/... 或 https://...)"
@input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"
class="flex-1 min-w-0 px-3 py-2 rounded-lg border border-gray-200 bg-white text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/5"
/>
<button
type="button"
:disabled="uploading"
@click="choose"
class="flex items-center gap-1.5 px-3 py-2 rounded-lg bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-60 flex-shrink-0"
>
<Icon v-if="uploading" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
<Icon v-else name="ph:upload-simple" class="w-4 h-4" />
{{ uploading ? '上传中…' : '上传图片' }}
</button>
<input ref="fileInput" type="file" accept="image/*" class="hidden" @change="onFileChange" />
</div>
<div v-if="modelValue" class="mt-3">
<img
:src="modelValue"
alt="图片预览"
class="h-32 w-auto max-w-full rounded-lg border border-gray-100 object-cover"
/>
</div>
<p v-if="error" class="mt-1 text-xs text-red-600">{{ error }}</p>
</div>
</template>
+68
View File
@@ -0,0 +1,68 @@
<script setup lang="ts">
/**
* 后台左侧导航栏(Dashboard / 动态 / 作品 / 用户 / 评论 / 联系 / 订阅 / 返回前台)。
* 高亮当前路由;点击任意导航项时向父级发出 navigate 事件(移动端关闭侧栏)。
*/
import { useRoute } from '#app'
import { siteConfig } from '~/config/site'
const route = useRoute()
const emit = defineEmits<{ navigate: [] }>()
const items = [
{ to: '/admin', label: 'Dashboard', icon: 'ph:gauge' },
{ to: '/admin/news', label: '动态管理', icon: 'ph:newspaper' },
{ to: '/admin/work', label: '作品管理', icon: 'ph:image-square' },
{ to: '/admin/users', label: '用户管理', icon: 'ph:users-three' },
{ to: '/admin/comments', label: '评论管理', icon: 'ph:chat-circle-text' },
{ to: '/admin/contacts', label: '联系留言', icon: 'ph:envelope-simple' },
{ to: '/admin/subscribers', label: '订阅管理', icon: 'ph:paper-plane-tilt' },
]
const isActive = (to: string) => route.path === to
</script>
<template>
<div class="flex flex-col h-full bg-black text-white">
<NuxtLink
to="/admin"
class="flex items-center gap-3 px-5 h-16 border-b border-white/10 shrink-0"
@click="emit('navigate')"
>
<div
class="w-8 h-8 rounded-full bg-white text-black flex items-center justify-center font-bold text-sm"
>
A
</div>
<div class="leading-tight">
<p class="text-sm font-bold">{{ siteConfig.brandZh }}</p>
<p class="text-[11px] text-white/50">后台管理</p>
</div>
</NuxtLink>
<nav class="flex-1 py-4 px-3 space-y-1 overflow-y-auto">
<NuxtLink
v-for="item in items"
:key="item.to"
:to="item.to"
class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium transition-colors"
:class="isActive(item.to) ? 'bg-white text-black' : 'text-white/70 hover:bg-white/10 hover:text-white'"
@click="emit('navigate')"
>
<Icon :name="item.icon" class="w-[18px] h-[18px]" />
{{ item.label }}
</NuxtLink>
</nav>
<div class="p-3 border-t border-white/10 shrink-0">
<NuxtLink
to="/"
class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-white/70 hover:bg-white/10 hover:text-white transition-colors"
@click="emit('navigate')"
>
<Icon name="ph:arrow-left" class="w-[18px] h-[18px]" />
返回前台
</NuxtLink>
</div>
</div>
</template>