index.vue
6.53 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
<template>
<div class="role-management">
<!-- 搜索表单 -->
<el-card class="search-form">
<el-form :model="searchForm" inline>
<el-form-item label="角色名称">
<el-input v-model="searchForm.roleName" placeholder="请输入角色名称" clearable />
</el-form-item>
<el-form-item label="角色标识">
<el-input v-model="searchForm.roleKey" placeholder="请输入角色标识" clearable />
</el-form-item>
<el-form-item label="状态">
<el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
<el-option label="正常" :value="1" />
<el-option label="停用" :value="0" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleSearch">
<el-icon><Search /></el-icon>
搜索
</el-button>
<el-button @click="handleReset">
<el-icon><Refresh /></el-icon>
重置
</el-button>
</el-form-item>
</el-form>
</el-card>
<!-- 操作按钮 -->
<el-card class="operation-buttons">
<el-button type="primary" @click="handleAdd">
<el-icon><Plus /></el-icon>
新增角色
</el-button>
<el-button type="danger" :disabled="!multipleSelection.length" @click="handleBatchDelete">
<el-icon><Delete /></el-icon>
批量删除
</el-button>
</el-card>
<!-- 数据表格 -->
<el-card class="table-container">
<el-table
v-loading="loading"
:data="tableData"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" />
<el-table-column prop="roleId" label="角色ID" width="80" />
<el-table-column prop="roleName" label="角色名称" width="120" />
<el-table-column prop="roleKey" label="角色标识" width="120" />
<el-table-column prop="roleSort" label="排序" width="80" />
<el-table-column prop="status" label="状态" width="80">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
{{ row.status === 1 ? '正常' : '停用' }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="createTime" label="创建时间" width="180" />
<el-table-column label="操作" width="250" fixed="right">
<template #default="{ row }">
<el-button type="primary" size="small" @click="handleEdit(row)">
编辑
</el-button>
<el-button type="success" size="small" @click="handleAssignPermissions(row)">
分配权限
</el-button>
<el-button type="danger" size="small" @click="handleDelete(row)">
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<el-pagination
v-model:current-page="pagination.current"
v-model:page-size="pagination.size"
:total="pagination.total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</el-card>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Role } from '@/types'
// 搜索表单
const searchForm = reactive({
roleName: '',
roleKey: '',
status: undefined
})
// 表格数据
const tableData = ref<Role[]>([])
const loading = ref(false)
const multipleSelection = ref<Role[]>([])
// 分页
const pagination = reactive({
current: 1,
size: 10,
total: 0
})
// 获取角色列表
const getRoleList = async () => {
loading.value = true
try {
// 模拟数据
tableData.value = [
{
roleId: 1,
roleName: '超级管理员',
roleKey: 'ADMIN',
roleSort: 1,
status: 1,
remark: '超级管理员',
createTime: '2024-01-01 10:00:00',
updateTime: '2024-01-01 10:00:00',
permissions: []
},
{
roleId: 2,
roleName: '普通用户',
roleKey: 'USER',
roleSort: 2,
status: 1,
remark: '普通用户',
createTime: '2024-01-01 10:00:00',
updateTime: '2024-01-01 10:00:00',
permissions: []
}
]
pagination.total = 2
} catch (error) {
ElMessage.error('获取角色列表失败')
} finally {
loading.value = false
}
}
// 搜索
const handleSearch = () => {
pagination.current = 1
getRoleList()
}
// 重置
const handleReset = () => {
Object.assign(searchForm, {
roleName: '',
roleKey: '',
status: undefined
})
handleSearch()
}
// 新增角色
const handleAdd = () => {
ElMessage.info('新增角色功能待实现')
}
// 编辑角色
const handleEdit = (row: Role) => {
ElMessage.info(`编辑角色: ${row.roleName}`)
}
// 分配权限
const handleAssignPermissions = (row: Role) => {
ElMessage.info(`分配权限: ${row.roleName}`)
}
// 删除角色
const handleDelete = async (row: Role) => {
try {
await ElMessageBox.confirm(`确定要删除角色 "${row.roleName}" 吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
ElMessage.success('删除成功')
getRoleList()
} catch (error) {
// 用户取消删除
}
}
// 批量删除
const handleBatchDelete = async () => {
try {
await ElMessageBox.confirm(`确定要删除选中的 ${multipleSelection.value.length} 个角色吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
ElMessage.success('批量删除成功')
getRoleList()
} catch (error) {
// 用户取消删除
}
}
// 选择变化
const handleSelectionChange = (selection: Role[]) => {
multipleSelection.value = selection
}
// 分页大小变化
const handleSizeChange = (size: number) => {
pagination.size = size
getRoleList()
}
// 当前页变化
const handleCurrentChange = (current: number) => {
pagination.current = current
getRoleList()
}
// 组件挂载时获取数据
onMounted(() => {
getRoleList()
})
</script>
<style lang="scss" scoped>
.role-management {
.search-form {
margin-bottom: 16px;
}
.operation-buttons {
margin-bottom: 16px;
}
.table-container {
.el-pagination {
margin-top: 16px;
text-align: right;
}
}
}
</style>