d67849a311
- 内容迁入数据库: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 中间件 + 客户端会话恢复插件
213 lines
8.3 KiB
Vue
213 lines
8.3 KiB
Vue
<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>
|