User.vue 5.22 KB
<template>
    <div class="user-view">
        <div class="content-card">
            <h1>👥 用户管理</h1>

            <div class="toolbar">
                <button class="btn-add" @click="addUser">
                    ➕ 新增用户
                </button>
            </div>

            <div class="table-container">
                <table class="user-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>姓名</th>
                            <th>邮箱</th>
                            <th>角色</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr v-for="user in users" :key="user.id">
                            <td>{{ user.id }}</td>
                            <td>{{ user.name }}</td>
                            <td>{{ user.email }}</td>
                            <td>
                                <span class="badge" :class="`badge-${user.role}`">
                                    {{ user.role }}
                                </span>
                            </td>
                            <td>
                                <span class="status" :class="user.active ? 'status-active' : 'status-inactive'">
                                    {{ user.active ? "在线" : "离线" }}
                                </span>
                            </td>
                            <td>
                                <button class="btn-edit" @click="editUser(user)">
                                    编辑
                                </button>
                                <button class="btn-delete" @click="deleteUser(user.id)">
                                    删除
                                </button>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";

interface User {
	active: boolean;
	email: string;
	id: number;
	name: string;
	role: "admin" | "guest" | "user";
}

const users = ref<User[]>([
	{
		id: 1,
		name: "张三",
		email: "zhangsan@example.com",
		role: "admin",
		active: true,
	},
	{
		id: 2,
		name: "李四",
		email: "lisi@example.com",
		role: "user",
		active: true,
	},
	{
		id: 3,
		name: "王五",
		email: "wangwu@example.com",
		role: "user",
		active: false,
	},
	{
		id: 4,
		name: "赵六",
		email: "zhaoliu@example.com",
		role: "guest",
		active: true,
	},
]);

const addUser = () => {
	const newUser: User = {
		id: users.value.length + 1,
		name: `新用户${users.value.length + 1}`,
		email: `user${users.value.length + 1}@example.com`,
		role: "user",
		active: true,
	};
	users.value.push(newUser);
};

const editUser = (user: User) => {
	console.log("编辑用户:", user);
	// eslint-disable-next-line
	alert(`编辑用户: ${user.name}`);
};

const deleteUser = (id: number) => {
	const index = users.value.findIndex((u) => u.id === id);
	// eslint-disable-next-line
	if (index !== -1 && confirm(`确定要删除用户 ${users.value[index].name} 吗?`)) {
		users.value.splice(index, 1);
	}
};
</script>

<style scoped>
.user-view {
	max-width: 1200px;
	margin: 0 auto;
}

.content-card {
	padding: 2.5rem;
	background: rgb(255 255 255 / 10%);
	border: 1px solid rgb(255 255 255 / 20%);
	border-radius: 1rem;
	backdrop-filter: blur(10px);
}

.content-card h1 {
	margin: 0 0 1.5rem;
	font-size: 2rem;
}

.toolbar {
	margin-bottom: 1.5rem;
}

.btn-add {
	padding: 0.75rem 1.5rem;
	font-weight: 600;
	color: white;
	cursor: pointer;
	background: rgb(76 175 80 / 80%);
	border: none;
	border-radius: 0.5rem;
	transition: all 0.3s ease;
}

.btn-add:hover {
	background: rgb(76 175 80 / 100%);
	transform: translateY(-2px);
}

.table-container {
	overflow-x: auto;
}

.user-table {
	width: 100%;
	border-collapse: collapse;
}

.user-table thead {
	background: rgb(255 255 255 / 10%);
}

.user-table th,
.user-table td {
	padding: 1rem;
	text-align: left;
	border-bottom: 1px solid rgb(255 255 255 / 10%);
}

.user-table th {
	font-weight: 600;
	color: rgb(255 255 255 / 95%);
}

.user-table tbody tr {
	transition: background 0.3s ease;
}

.user-table tbody tr:hover {
	background: rgb(255 255 255 / 5%);
}

.badge {
	padding: 0.25rem 0.75rem;
	font-size: 0.85rem;
	font-weight: 600;
	border-radius: 1rem;
}

.badge-admin {
	background: rgb(244 67 54 / 80%);
}

.badge-user {
	background: rgb(33 150 243 / 80%);
}

.badge-guest {
	background: rgb(158 158 158 / 80%);
}

.status {
	padding: 0.25rem 0.75rem;
	font-size: 0.85rem;
	font-weight: 600;
	border-radius: 1rem;
}

.status-active {
	background: rgb(76 175 80 / 80%);
}

.status-inactive {
	background: rgb(158 158 158 / 80%);
}

.btn-edit,
.btn-delete {
	padding: 0.5rem 1rem;
	margin-right: 0.5rem;
	font-weight: 600;
	cursor: pointer;
	border: none;
	border-radius: 0.5rem;
	transition: all 0.3s ease;
}

.btn-edit {
	color: white;
	background: rgb(33 150 243 / 80%);
}

.btn-edit:hover {
	background: rgb(33 150 243 / 100%);
}

.btn-delete {
	color: white;
	background: rgb(244 67 54 / 80%);
}

.btn-delete:hover {
	background: rgb(244 67 54 / 100%);
}
</style>