User.vue
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<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>