ef168e3d90
- 组件改为render函数,避免模板编译器依赖 - 自动注入样式,无需页面额外CSS - 自适应布局:PC固定侧边栏,H5底部导航 - 所有页面统一使用,admin权限动态控制 - 自动调整main-content边距,防止遮挡
233 lines
12 KiB
HTML
233 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>宇之然内容创作平台 - 用户管理</title>
|
|
<link rel="stylesheet" href="element-plus.css">
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: #f5f7fa; }
|
|
.navbar { background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%); color: white; padding: 16px 24px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
|
|
.navbar-content { display: flex; justify-content: space-between; align-items: center; max-width: 1400px; margin: 0 auto; }
|
|
.navbar-title { font-size: 20px; font-weight: 600; }
|
|
.navbar-user { display: flex; align-items: center; gap: 16px; }
|
|
.user-info { display: flex; align-items: center; gap: 8px; }
|
|
.avatar { width: 32px; height: 32px; border-radius: 50%; background: rgba(255,255,255,0.2); display: flex; align-items: center; justify-content: center; font-size: 14px; }
|
|
.main-content { display: flex; flex: 1; max-width: 1400px; margin: 0 auto; width: 100%; }
|
|
.sidebar { width: 180px; background: white; padding: 16px; box-shadow: 2px 0 8px rgba(0,0,0,0.05); }
|
|
|
|
|
|
|
|
.content-area { flex: 1; padding: 24px; overflow-y: auto; }
|
|
.card { background: white; border-radius: 12px; padding: 24px; margin-bottom: 24px; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
|
|
.mobile-nav { display: none; position: fixed; bottom: 0; left: 0; right: 0; background: white; box-shadow: 0 -2px 8px rgba(0,0,0,0.1); padding: 8px 0; z-index: 1000; }
|
|
|
|
|
|
@media (max-width: 768px) {
|
|
.sidebar { display: none; }
|
|
.mobile-nav { display: flex; }
|
|
.content-area { padding: 16px; padding-bottom: 80px; }
|
|
}
|
|
|
|
.user-card-list { display: none; }
|
|
/* users.html 移动端优化 */
|
|
@media (max-width: 768px) {
|
|
.user-table { display: none; }
|
|
.user-card-list { display: block; margin: 0 -16px; }
|
|
.user-card {
|
|
background: white;
|
|
border-radius: 8px;
|
|
padding: 16px;
|
|
margin-bottom: 12px;
|
|
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
|
|
}
|
|
.user-card-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 12px;
|
|
}
|
|
.user-card-name { font-size: 16px; font-weight: 600; }
|
|
.user-card-meta {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 8px;
|
|
font-size: 12px;
|
|
color: #606266;
|
|
margin-bottom: 12px;
|
|
}
|
|
.user-card-actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
justify-content: flex-end;
|
|
padding-top: 8px;
|
|
border-top: 1px solid #ebeef5;
|
|
}
|
|
}
|
|
</style>
|
|
|
|
<script src="navigation-component.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="app">
|
|
<nav class="navbar">
|
|
<div class="navbar-content">
|
|
<h1 class="navbar-title">宇之然内容创作平台 - 用户管理</h1>
|
|
<div class="navbar-user">
|
|
<div class="user-info"><div class="avatar">{{ currentUser.username.charAt(0).toUpperCase() }}</div><span>{{ currentUser.username }}</span></div>
|
|
<el-button type="danger" size="small" @click="handleLogout">退出</el-button>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<navigation-component
|
|
current-page="users"
|
|
:is-admin="isAdmin"
|
|
@navigate="redirectToPage"
|
|
></navigation-component>
|
|
<div class="main-content">
|
|
|
|
<main class="content-area">
|
|
<div class="card">
|
|
<h2 style="font-size: 24px; font-weight: 600; margin-bottom: 24px;">👥 用户管理</h2>
|
|
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px;">
|
|
<h3 style="font-size: 18px; font-weight: 600;">用户列表</h3>
|
|
<el-button type="primary" @click="addUser">+ 新建用户</el-button>
|
|
</div>
|
|
<el-table :data="users" stripe :cell-class-name="getMobileUserCellClass" class="user-table">
|
|
<el-table-column prop="id" label="ID" width="80"></el-table-column>
|
|
<el-table-column prop="username" label="用户名"></el-table-column>
|
|
<el-table-column prop="role" label="角色" width="100">
|
|
<template #default="scope"><el-tag :type="scope.row.role === 'admin' ? 'danger' : 'info'">{{ scope.row.role === 'admin' ? '管理员' : '编辑' }}</el-tag></template>
|
|
</el-table-column>
|
|
<el-table-column prop="created_at" label="创建时间" width="180"><template #default="scope">{{ formatDate(scope.row.created_at) }}</template></el-table-column>
|
|
<el-table-column label="操作" width="150">
|
|
<template #default="scope">
|
|
<el-button size="small" type="danger" @click="deleteUser(scope.row.id)" :disabled="scope.row.role === 'admin'">删除</el-button>
|
|
</template>
|
|
</el-table-column>
|
|
</el-table>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
</div>
|
|
<script src="vue.global.prod.js"></script>
|
|
<script src="element-plus.full.js"></script>
|
|
<script>
|
|
const UsersApp = {
|
|
data() { return { isLoggedIn: false, isAdmin: false, currentUser: { username: '' }, users: [] } },
|
|
methods: {
|
|
async fetchUsers() {
|
|
try {
|
|
const token = localStorage.getItem('authToken');
|
|
if (!token) {
|
|
this.$message.error('请先登录');
|
|
return;
|
|
}
|
|
const response = await fetch('/api/admin/users', {
|
|
headers: { 'Authorization': 'Bearer ' + token }
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `请求失败: ${response.status}`);
|
|
}
|
|
const data = await response.json();
|
|
this.users = data || [];
|
|
this.$message.success('用户列表加载成功');
|
|
} catch (error) {
|
|
console.error('获取用户失败:', error);
|
|
this.$message.error(`获取用户失败: ${error.message}`);
|
|
this.users = [];
|
|
}
|
|
},
|
|
async addUser() {
|
|
try {
|
|
const token = localStorage.getItem('authToken');
|
|
if (!token) {
|
|
this.$message.error('请先登录');
|
|
return;
|
|
}
|
|
// 使用默认用户名,添加时间戳避免重复
|
|
const username = '新用户' + Date.now().toString().slice(-4);
|
|
const response = await fetch('/api/admin/users', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + token,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ username: username, role: 'editor' })
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `请求失败: ${response.status}`);
|
|
}
|
|
const newUser = await response.json();
|
|
this.users.push(newUser);
|
|
this.$message.success('添加用户成功');
|
|
} catch (error) {
|
|
console.error('添加用户失败:', error);
|
|
this.$message.error(`添加用户失败: ${error.message}`);
|
|
}
|
|
},
|
|
async deleteUser(id) {
|
|
if (id === 'admin') {
|
|
this.$message.warning('不能删除管理员用户');
|
|
return;
|
|
}
|
|
this.$confirm('确定删除该用户?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
|
|
.then(async () => {
|
|
try {
|
|
const token = localStorage.getItem('authToken');
|
|
if (!token) {
|
|
this.$message.error('请先登录');
|
|
window.location.href = '/';
|
|
return;
|
|
}
|
|
const response = await fetch(`/api/admin/users/${encodeURIComponent(id)}`, {
|
|
method: 'DELETE',
|
|
headers: { 'Authorization': 'Bearer ' + token }
|
|
});
|
|
if (!response.ok) {
|
|
const errorData = await response.json().catch(() => ({}));
|
|
throw new Error(errorData.detail || `请求失败: ${response.status}`);
|
|
}
|
|
this.users = this.users.filter(u => u.id !== id);
|
|
this.$message.success('删除用户成功');
|
|
} catch (error) {
|
|
console.error('删除用户失败:', error);
|
|
this.$message.error(`删除用户失败: ${error.message}`);
|
|
}
|
|
})
|
|
.catch(() => {});
|
|
},
|
|
handleLogout() { localStorage.removeItem('authToken'); window.location.href = '/'; },
|
|
redirectToPage(page) {
|
|
if (!page) return;
|
|
// 处理绝对URL或相对路径
|
|
if (page.startsWith('/') || page.startsWith('http://') || page.startsWith('https://')) {
|
|
window.location.href = page;
|
|
} else {
|
|
window.location.href = '/' + page;
|
|
}
|
|
},
|
|
formatDate(dateStr) { if (!dateStr) return '-'; return new Date(dateStr.replace(' ', 'T')).toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); }
|
|
},
|
|
mounted() {
|
|
const token = localStorage.getItem('authToken');
|
|
if (!token) { window.location.href = '/'; return; }
|
|
fetch('/api/auth/me', { headers: { 'Authorization': 'Bearer ' + token } })
|
|
.then(response => response.ok ? response.json() : Promise.reject())
|
|
.then(data => { this.currentUser = data.user; this.isAdmin = data.user.role === 'admin'; this.isLoggedIn = true; if (!this.isAdmin) { this.$message.warning('需要管理员权限'); window.location.href = '/'; } else { this.fetchUsers(); } })
|
|
.catch(() => { localStorage.removeItem('authToken'); window.location.href = '/'; });
|
|
}
|
|
};
|
|
const app = Vue.createApp(UsersApp);
|
|
app.use(ElementPlus);
|
|
// 安装导航组件
|
|
if (window.installNavigation) { window.installNavigation(app); }
|
|
app.mount('#app');
|
|
</script>
|
|
</body>
|
|
</html> |