Files
arcticnewone/app/components/admin/AdminSidebar.vue
T
virtheart 541e24c6f4 feat: 后台管理员独立账号体系,Casdoor 配置改为后台管理
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie)
- /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导
- 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦
- Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量
- 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态
- 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
2026-08-09 02:01:14 +08:00

137 lines
4.6 KiB
Vue

<script setup lang="ts">
/**
* 后台左侧导航栏:按「内容 / 数据 / 系统」分组,底部显示当前管理员可退出。
* 高亮当前路由;点击任意导航项时向父级发出 navigate 事件(移动端关闭侧栏)。
*/
import { useRoute } from '#app'
import { useSiteSettings } from '~/composables/useSiteSettings'
import { useAdminStore } from '~/stores/admin'
const route = useRoute()
const emit = defineEmits<{ navigate: [] }>()
const { settings } = useSiteSettings()
const adminStore = useAdminStore()
const handleLogout = async () => {
await adminStore.logout()
emit('navigate')
navigateTo('/admin/login')
}
interface SidebarItem {
to: string
label: string
icon: string
}
interface SidebarGroup {
label: string
items: SidebarItem[]
}
const groups: SidebarGroup[] = [
{
label: '内容',
items: [
{ to: '/admin', label: 'Dashboard', icon: 'ph:gauge' },
{ to: '/admin/news', label: '动态管理', icon: 'ph:newspaper' },
{ to: '/admin/work', label: '作品管理', icon: 'ph:image-square' },
],
},
{
label: '数据',
items: [
{ 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' },
],
},
{
label: '系统',
items: [
{ to: '/admin/casdoor', label: '统一认证', icon: 'ph:sign-in' },
{ to: '/admin/admins', label: '管理员', icon: 'ph:shield-check' },
{ to: '/admin/uploads', label: '图片管理', icon: 'ph:image' },
{ to: '/admin/audit', label: '操作日志', icon: 'ph:list-checks' },
{ to: '/admin/settings', label: '站点设置', icon: 'ph:gear-six' },
],
},
]
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">{{ settings?.site.brand }}</p>
<p class="text-[11px] text-white/50">后台管理</p>
</div>
</NuxtLink>
<nav class="flex-1 py-5 px-3 overflow-y-auto space-y-6">
<div v-for="group in groups" :key="group.label">
<p
class="px-3 mb-1.5 text-[11px] font-semibold uppercase tracking-wider text-white/40"
>
{{ group.label }}
</p>
<div class="space-y-1">
<NuxtLink
v-for="item in group.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>
</div>
</div>
</nav>
<div class="p-3 border-t border-white/10 shrink-0 space-y-1">
<div class="flex items-center gap-3 px-3 py-2.5">
<div
class="w-8 h-8 rounded-full bg-white/10 text-white flex items-center justify-center font-bold text-xs flex-shrink-0"
>
{{ (adminStore.admin?.username || 'A').charAt(0).toUpperCase() }}
</div>
<div class="min-w-0 leading-tight">
<p class="text-sm font-semibold text-white truncate">{{ adminStore.admin?.username || '管理员' }}</p>
<p class="text-[11px] text-white/40">后台账号</p>
</div>
</div>
<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>
<button
type="button"
class="w-full 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="handleLogout"
>
<Icon name="ph:sign-out" class="w-[18px] h-[18px]" />
退出登录
</button>
</div>
</div>
</template>