337eb4f05a
- 移除内置密码注册/登录/改密/删号/头像,统一走 Casdoor OIDC 授权码 - 服务端 casdoor-nodejs-sdk:code 换 token + 证书公钥验签 + get-account 补齐资料 - 用户表重建为 provider+provider_id(Casdoor sub 为准),邮箱不再唯一 - 管理员由 Casdoor token 的 isAdmin/IsAdmin 字段或 admin 角色判定,删除角色切换接口 - 前端登录弹窗改为申统认证跳转,注册去 Casdoor;后台用户列表只读角色 - 会话沿用 an_session httpOnly Cookie,requireUser/requireAdmin 守卫保留 - 配置注入 deploy.sh/.env.example;新增 casdoor 单测,删除 password 单测
292 lines
11 KiB
Vue
292 lines
11 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 后台用户管理:列表 + 搜索 + 删除确认 + 前端分页。
|
||
* - GET /api/admin/users;DELETE /api/admin/users/[id]
|
||
* - 角色由 Casdoor 统一身份认证决定(token 的 IsAdmin claim),本地仅缓存展示,不提供切换。
|
||
*/
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { useUserStore } from '~/stores/user'
|
||
import { useToastStore } from '~/stores/toast'
|
||
|
||
definePageMeta({ middleware: 'admin', layout: 'admin' })
|
||
useHead({ title: '用户管理' })
|
||
|
||
interface AdminUser {
|
||
id: number
|
||
username: string
|
||
email: string | null
|
||
role: 'user' | 'admin'
|
||
createdAt: string
|
||
}
|
||
|
||
const toast = useToastStore()
|
||
const userStore = useUserStore()
|
||
const meId = computed(() => userStore.user?.id)
|
||
|
||
/* ---------- 列表 ---------- */
|
||
const users = ref<AdminUser[]>([])
|
||
const loading = ref(true)
|
||
const loadError = ref('')
|
||
|
||
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
|
||
}
|
||
}
|
||
|
||
onMounted(loadUsers)
|
||
|
||
/* ---------- 搜索 / 筛选 ---------- */
|
||
const keyword = ref('')
|
||
const roleFilter = ref<'all' | 'admin' | 'user'>('all')
|
||
|
||
const filteredUsers = computed(() => {
|
||
const kw = keyword.value.trim().toLowerCase()
|
||
return users.value.filter((u) => {
|
||
if (roleFilter.value !== 'all' && u.role !== roleFilter.value) return false
|
||
if (!kw) return true
|
||
return u.username.toLowerCase().includes(kw) || (u.email || '').toLowerCase().includes(kw)
|
||
})
|
||
})
|
||
|
||
const adminCount = computed(() => users.value.filter((u) => u.role === 'admin').length)
|
||
|
||
/* ---------- 前端分页 ---------- */
|
||
const pageSize = 15
|
||
const page = ref(1)
|
||
const totalPages = computed(() => Math.max(1, Math.ceil(filteredUsers.value.length / pageSize)))
|
||
|
||
const pagedUsers = computed(() => {
|
||
const p = Math.min(page.value, totalPages.value)
|
||
const start = (p - 1) * pageSize
|
||
return filteredUsers.value.slice(start, start + pageSize)
|
||
})
|
||
|
||
watch([keyword, roleFilter], () => {
|
||
page.value = 1
|
||
})
|
||
|
||
const changePage = (p: number) => {
|
||
page.value = Math.min(Math.max(1, p), totalPages.value)
|
||
}
|
||
|
||
/* ---------- 删除 ---------- */
|
||
const confirmDelete = ref<AdminUser | null>(null)
|
||
const deleting = ref(false)
|
||
|
||
const askDelete = (u: AdminUser) => {
|
||
confirmDelete.value = u
|
||
}
|
||
|
||
const doDelete = async () => {
|
||
const u = confirmDelete.value
|
||
if (!u) return
|
||
deleting.value = true
|
||
try {
|
||
await $fetch(`/api/admin/users/${u.id}`, { method: 'DELETE' })
|
||
users.value = users.value.filter((x) => x.id !== u.id)
|
||
toast.success(`已删除用户「${u.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',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
}).format(d)
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<AdminPageHeader title="用户管理" subtitle="查看站点 SSO 用户,角色由 Casdoor 统一身份判定。">
|
||
<template #actions>
|
||
<AdminButton variant="secondary" size="sm" @click="loadUsers">
|
||
<Icon name="ph:arrows-clockwise" class="w-4 h-4" /> 刷新
|
||
</AdminButton>
|
||
</template>
|
||
</AdminPageHeader>
|
||
|
||
<!-- 统计 + 工具栏 -->
|
||
<div class="mb-4 flex flex-wrap items-center gap-x-4 gap-y-2">
|
||
<span class="text-sm text-gray-500">共 {{ users.length }} 个用户</span>
|
||
<span class="text-sm text-gray-400">· 管理员 {{ adminCount }} · 普通用户 {{ users.length - adminCount }}</span>
|
||
</div>
|
||
|
||
<div class="mb-5 flex flex-col gap-3 md:flex-row">
|
||
<div class="relative flex-1 md:max-w-xs">
|
||
<Icon name="ph:magnifying-glass" class="w-4 h-4 text-gray-400 absolute left-3.5 top-1/2 -translate-y-1/2 pointer-events-none" />
|
||
<input
|
||
v-model="keyword"
|
||
type="text"
|
||
placeholder="搜索用户名 / 邮箱"
|
||
class="w-full rounded-xl border border-gray-200 bg-white py-2.5 pl-10 pr-9 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10"
|
||
/>
|
||
<button
|
||
v-if="keyword"
|
||
@click="keyword = ''"
|
||
class="absolute right-2.5 top-1/2 -translate-y-1/2 w-5 h-5 rounded-full text-gray-400 hover:text-gray-600 hover:bg-gray-100 flex items-center justify-center"
|
||
aria-label="清空筛选"
|
||
>
|
||
<Icon name="ph:x" class="w-3.5 h-3.5" />
|
||
</button>
|
||
</div>
|
||
<AdminSelect
|
||
v-model="roleFilter"
|
||
:options="[
|
||
{ value: 'all', label: '全部角色' },
|
||
{ value: 'admin', label: '管理员' },
|
||
{ value: 'user', label: '普通用户' },
|
||
]"
|
||
class="w-full md:w-40"
|
||
/>
|
||
</div>
|
||
|
||
<!-- 加载态 -->
|
||
<AdminCard v-if="loading">
|
||
<AdminSkeleton :rows="6" />
|
||
</AdminCard>
|
||
|
||
<!-- 错误态 -->
|
||
<AdminCard v-else-if="loadError">
|
||
<div class="py-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="loadUsers">重新加载</AdminButton>
|
||
</div>
|
||
</AdminCard>
|
||
|
||
<!-- 空态 -->
|
||
<AdminEmptyState v-else-if="!users.length" icon="ph:users-three" title="暂无用户" description="还没有登录用户。" />
|
||
|
||
<AdminEmptyState
|
||
v-else-if="!filteredUsers.length"
|
||
icon="ph:magnifying-glass"
|
||
title="没有匹配的用户"
|
||
description="换个关键词或筛选条件试试。"
|
||
/>
|
||
|
||
<!-- 用户表格 -->
|
||
<template v-else>
|
||
<!-- 桌面表格(md+) -->
|
||
<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 whitespace-nowrap sticky left-0 bg-gray-50 z-10 border-r border-gray-100">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">邮箱</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="u in pagedUsers" :key="u.id" class="hover:bg-gray-50/60 transition-colors">
|
||
<td class="px-5 py-4 text-gray-400 font-mono text-xs whitespace-nowrap sticky left-0 bg-white z-10 border-r border-gray-100">#{{ u.id }}</td>
|
||
<td class="px-5 py-4 font-semibold text-black whitespace-nowrap">{{ u.username }}</td>
|
||
<td class="px-5 py-4 text-gray-600">{{ u.email || '—' }}</td>
|
||
<td class="px-5 py-4 whitespace-nowrap">
|
||
<AdminBadge :tone="u.role === 'admin' ? 'black' : 'gray'">
|
||
<Icon v-if="u.role === 'admin'" name="ph:crown-simple" class="w-3 h-3" />
|
||
{{ u.role === 'admin' ? '管理员' : '用户' }}
|
||
</AdminBadge>
|
||
</td>
|
||
<td class="px-5 py-4 text-gray-500 text-xs whitespace-nowrap">{{ formatDate(u.createdAt) }}</td>
|
||
<td class="px-5 py-4">
|
||
<div class="flex items-center justify-end gap-2">
|
||
<template v-if="u.id !== meId">
|
||
<AdminButton size="sm" variant="danger" @click="askDelete(u)">
|
||
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
|
||
</AdminButton>
|
||
</template>
|
||
<AdminBadge v-else tone="gray">当前账号</AdminBadge>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 移动端卡片(<md) -->
|
||
<div class="md:hidden space-y-3">
|
||
<div
|
||
v-for="u in pagedUsers"
|
||
:key="u.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"
|
||
>
|
||
{{ (u.username || '?').charAt(0).toUpperCase() }}
|
||
</div>
|
||
<div class="min-w-0">
|
||
<p class="font-semibold text-black truncate">{{ u.username }}</p>
|
||
<p class="text-xs text-gray-400 font-mono">#{{ u.id }}</p>
|
||
</div>
|
||
</div>
|
||
<AdminBadge :tone="u.role === 'admin' ? 'black' : 'gray'" class="flex-shrink-0">
|
||
<Icon v-if="u.role === 'admin'" name="ph:crown-simple" class="w-3 h-3" />
|
||
{{ u.role === 'admin' ? '管理员' : '用户' }}
|
||
</AdminBadge>
|
||
</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 break-all">{{ u.email || '—' }}</dd>
|
||
</div>
|
||
<div class="flex justify-between gap-3">
|
||
<dt class="text-gray-400 flex-shrink-0">加入时间</dt>
|
||
<dd class="text-gray-700 text-right">{{ formatDate(u.createdAt) }}</dd>
|
||
</div>
|
||
</dl>
|
||
<div class="mt-4 flex flex-wrap items-center gap-2">
|
||
<template v-if="u.id !== meId">
|
||
<AdminButton size="sm" variant="danger" class="min-h-10" @click="askDelete(u)">
|
||
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
|
||
</AdminButton>
|
||
</template>
|
||
<AdminBadge v-else tone="gray">当前账号</AdminBadge>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="mt-4">
|
||
<AdminPagination :total="filteredUsers.length" :page="page" :page-size="pageSize" @page="changePage" />
|
||
</div>
|
||
</template>
|
||
|
||
<!-- 删除确认 -->
|
||
<AdminConfirm
|
||
:open="!!confirmDelete"
|
||
:loading="deleting"
|
||
danger
|
||
:message="`确定删除用户「${confirmDelete?.username}」吗?此操作不可恢复。`"
|
||
@update:open="confirmDelete = null"
|
||
@confirm="doDelete"
|
||
/>
|
||
</div>
|
||
</template> |