541e24c6f4
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie) - /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导 - 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦 - Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量 - 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态 - 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
287 lines
11 KiB
Vue
287 lines
11 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台管理员账号管理(/admin/admins):
|
||
* 独立于前台 Casdoor 用户的后台账号,列表 + 新建 + 改密 + 删除(不能删自己)。
|
||
*/
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { useAdminStore } from '~/stores/admin'
|
||
import { useToastStore } from '~/stores/toast'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
|
||
useHead({ title: '管理员' })
|
||
|
||
interface AdminRow {
|
||
id: number
|
||
username: string
|
||
createdAt: string
|
||
}
|
||
|
||
const toast = useToastStore()
|
||
const adminStore = useAdminStore()
|
||
const meId = computed(() => adminStore.admin?.id ?? -1)
|
||
|
||
/* ---------- 列表 ---------- */
|
||
const admins = ref<AdminRow[]>([])
|
||
const loading = ref(true)
|
||
const loadError = ref('')
|
||
|
||
const loadAdmins = async () => {
|
||
loading.value = true
|
||
loadError.value = ''
|
||
try {
|
||
const data = await $fetch<{ admins: AdminRow[] }>('/api/admin/admin-users')
|
||
admins.value = data.admins
|
||
} catch (err: any) {
|
||
loadError.value = err?.data?.message || '加载失败,请重试'
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(loadAdmins)
|
||
|
||
/* ---------- 新建 ---------- */
|
||
const showCreate = ref(false)
|
||
const createForm = reactive({
|
||
username: '',
|
||
password: '',
|
||
confirm: '',
|
||
error: '',
|
||
})
|
||
|
||
const openCreate = () => {
|
||
createForm.username = ''
|
||
createForm.password = ''
|
||
createForm.confirm = ''
|
||
createForm.error = ''
|
||
showCreate.value = true
|
||
}
|
||
|
||
const doCreate = async () => {
|
||
createForm.error = ''
|
||
if (!/^[a-zA-Z0-9_]{2,20}$/.test(createForm.username.trim())) {
|
||
createForm.error = '用户名需为 2-20 位字母、数字或下划线'
|
||
return
|
||
}
|
||
if (createForm.password.length < 8) {
|
||
createForm.error = '密码至少需要 8 位'
|
||
return
|
||
}
|
||
if (createForm.password !== createForm.confirm) {
|
||
createForm.error = '两次输入的密码不一致'
|
||
return
|
||
}
|
||
|
||
try {
|
||
const data = await $fetch<{ admin: AdminRow }>('/api/admin/admin-users', {
|
||
method: 'POST',
|
||
body: { username: createForm.username.trim(), password: createForm.password },
|
||
})
|
||
admins.value = [...admins.value, data.admin].sort((a, b) => a.id - b.id)
|
||
showCreate.value = false
|
||
toast.success(`已添加管理员「${data.admin.username}」`)
|
||
} catch (err: any) {
|
||
createForm.error = err?.data?.message || '创建失败,请重试'
|
||
}
|
||
}
|
||
|
||
/* ---------- 修改密码 ---------- */
|
||
const editTarget = ref<AdminRow | null>(null)
|
||
const editForm = reactive({ password: '', confirm: '', error: '' })
|
||
|
||
const openEdit = (a: AdminRow) => {
|
||
editTarget.value = a
|
||
editForm.password = ''
|
||
editForm.confirm = ''
|
||
editForm.error = ''
|
||
}
|
||
|
||
const doEdit = async () => {
|
||
const a = editTarget.value
|
||
if (!a) return
|
||
editForm.error = ''
|
||
if (editForm.password.length < 8) {
|
||
editForm.error = '密码至少需要 8 位'
|
||
return
|
||
}
|
||
if (editForm.password !== editForm.confirm) {
|
||
editForm.error = '两次输入的密码不一致'
|
||
return
|
||
}
|
||
try {
|
||
await $fetch(`/api/admin/admin-users/${a.id}`, { method: 'PATCH', body: { password: editForm.password } })
|
||
toast.success(`已更新「${a.username}」的密码`)
|
||
editTarget.value = null
|
||
} catch (err: any) {
|
||
editForm.error = err?.data?.message || '更新失败,请重试'
|
||
}
|
||
}
|
||
|
||
/* ---------- 删除 ---------- */
|
||
const confirmDelete = ref<AdminRow | null>(null)
|
||
const deleting = ref(false)
|
||
|
||
const doDelete = async () => {
|
||
const a = confirmDelete.value
|
||
if (!a) return
|
||
deleting.value = true
|
||
try {
|
||
await $fetch(`/api/admin/admin-users/${a.id}`, { method: 'DELETE' })
|
||
admins.value = admins.value.filter((x) => x.id !== a.id)
|
||
toast.success(`已删除管理员「${a.username}」`)
|
||
confirmDelete.value = null
|
||
} catch (err: any) {
|
||
toast.error(err?.data?.message || '删除失败,请重试')
|
||
} finally {
|
||
deleting.value = false
|
||
}
|
||
}
|
||
|
||
const formatDate = (raw?: string) => {
|
||
if (!raw) return ''
|
||
const d = new Date(String(raw).replace(' ', 'T') + 'Z')
|
||
if (Number.isNaN(d.getTime())) return raw
|
||
return new Intl.DateTimeFormat('zh-CN', { year: 'numeric', month: 'short', day: 'numeric' }).format(d)
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<AdminPageHeader title="管理员" subtitle="后台账号独立于前台用户,负责内容与系统管理">
|
||
<template #actions>
|
||
<AdminButton variant="secondary" size="sm" @click="loadAdmins">
|
||
<Icon name="ph:arrows-clockwise" class="w-4 h-4" /> 刷新
|
||
</AdminButton>
|
||
<AdminButton size="sm" @click="openCreate">
|
||
<Icon name="ph:user-plus" class="w-4 h-4" /> 添加管理员
|
||
</AdminButton>
|
||
</template>
|
||
</AdminPageHeader>
|
||
|
||
<div class="mb-4">
|
||
<span class="text-sm text-gray-500">共 {{ admins.length }} 个管理员</span>
|
||
</div>
|
||
|
||
<AdminCard v-if="loading">
|
||
<AdminSkeleton :rows="4" />
|
||
</AdminCard>
|
||
|
||
<div v-else-if="loadError" 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">{{ loadError }}</p>
|
||
<AdminButton size="sm" @click="loadAdmins">重新加载</AdminButton>
|
||
</div>
|
||
|
||
<AdminEmptyState v-else-if="!admins.length" icon="ph:users-three" title="暂无管理员" description="还没有后台管理员账号。" />
|
||
|
||
<!-- 列表(桌面表格 + 移动端卡片) -->
|
||
<template v-else>
|
||
<div class="hidden md:block rounded-2xl border border-gray-100 bg-white 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 bg-gray-50">
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">ID</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">用户名</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">创建时间</th>
|
||
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider text-right">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody class="divide-y divide-gray-50">
|
||
<tr v-for="a in admins" :key="a.id" class="hover:bg-gray-50/60 transition-colors">
|
||
<td class="px-5 py-4 text-gray-400 font-mono text-xs whitespace-nowrap">#{{ a.id }}</td>
|
||
<td class="px-5 py-4 font-semibold text-black whitespace-nowrap">
|
||
{{ a.username }}
|
||
<AdminBadge v-if="a.id === meId" tone="black" class="ml-2">当前账号</AdminBadge>
|
||
</td>
|
||
<td class="px-5 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(a.createdAt) }}</td>
|
||
<td class="px-5 py-4">
|
||
<div class="flex items-center justify-end gap-2">
|
||
<AdminButton size="sm" variant="secondary" @click="openEdit(a)">
|
||
<Icon name="ph:key" class="w-3.5 h-3.5" /> 改密
|
||
</AdminButton>
|
||
<AdminButton v-if="a.id !== meId" size="sm" variant="danger" @click="confirmDelete = a">
|
||
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
|
||
</AdminButton>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 移动端卡片 -->
|
||
<div class="md:hidden space-y-3">
|
||
<div v-for="a in admins" :key="a.id" class="bg-white rounded-2xl border border-gray-100 shadow-sm p-4">
|
||
<div class="flex items-center justify-between gap-3">
|
||
<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">
|
||
{{ (a.username || '?').charAt(0).toUpperCase() }}
|
||
</div>
|
||
<div class="min-w-0">
|
||
<p class="font-semibold text-black truncate">
|
||
{{ a.username }}
|
||
<AdminBadge v-if="a.id === meId" tone="black" class="ml-1">当前</AdminBadge>
|
||
</p>
|
||
<p class="text-xs text-gray-400 font-mono">#{{ a.id }}</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<dl class="mt-3 space-y-1.5 text-sm">
|
||
<div class="flex justify-between gap-3">
|
||
<dt class="text-gray-400 flex-shrink-0">创建时间</dt>
|
||
<dd class="text-gray-700 text-right">{{ formatDate(a.createdAt) }}</dd>
|
||
</div>
|
||
</dl>
|
||
<div class="mt-4 flex flex-wrap items-center gap-2">
|
||
<AdminButton size="sm" variant="secondary" class="min-h-10" @click="openEdit(a)">
|
||
<Icon name="ph:key" class="w-3.5 h-3.5" /> 修改密码
|
||
</AdminButton>
|
||
<AdminButton v-if="a.id !== meId" size="sm" variant="danger" class="min-h-10" @click="confirmDelete = a">
|
||
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
|
||
</AdminButton>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<!-- 新建弹窗 -->
|
||
<AdminModal :open="showCreate" title="添加管理员" @update:open="showCreate = $event">
|
||
<div class="space-y-4">
|
||
<AdminInput v-model="createForm.username" label="用户名" placeholder="2-20 位字母、数字或下划线" />
|
||
<AdminInput v-model="createForm.password" label="登录密码" type="password" placeholder="至少 8 位" autocomplete="new-password" />
|
||
<AdminInput v-model="createForm.confirm" label="确认密码" type="password" placeholder="再次输入密码" autocomplete="new-password" />
|
||
<p v-if="createForm.error" class="text-sm text-red-600">{{ createForm.error }}</p>
|
||
</div>
|
||
<template #footer>
|
||
<AdminButton variant="secondary" size="sm" @click="showCreate = false">取消</AdminButton>
|
||
<AdminButton size="sm" @click="doCreate">创建</AdminButton>
|
||
</template>
|
||
</AdminModal>
|
||
|
||
<!-- 改密弹窗 -->
|
||
<AdminModal :open="!!editTarget" :title="`修改密码 · ${editTarget?.username ?? ''}`" @update:open="editTarget = null">
|
||
<div class="space-y-4">
|
||
<AdminInput v-model="editForm.password" label="新密码" type="password" placeholder="至少 8 位" autocomplete="new-password" />
|
||
<AdminInput v-model="editForm.confirm" label="确认密码" type="password" placeholder="再次输入密码" autocomplete="new-password" />
|
||
<p v-if="editForm.error" class="text-sm text-red-600">{{ editForm.error }}</p>
|
||
</div>
|
||
<template #footer>
|
||
<AdminButton variant="secondary" size="sm" @click="editTarget = null">取消</AdminButton>
|
||
<AdminButton size="sm" @click="doEdit">保存</AdminButton>
|
||
</template>
|
||
</AdminModal>
|
||
|
||
<!-- 删除确认 -->
|
||
<AdminConfirm
|
||
:open="!!confirmDelete"
|
||
:loading="deleting"
|
||
danger
|
||
:message="`确定删除管理员「${confirmDelete?.username}」吗?此操作不可恢复。`"
|
||
@update:open="confirmDelete = null"
|
||
@confirm="doDelete"
|
||
/>
|
||
</div>
|
||
</template> |