UserManagement.vue
12.3 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<template>
<el-container class="UserManagement">
<el-header height="50px">
<div>
<span class="title-text">用户管理</span>
</div>
</el-header>
<el-main>
<el-button type="primary" @click="addUser">新建</el-button>
<el-table :data="userList" v-loading="isLoading">
<el-table-column prop="name" label="用户名称" align="center"></el-table-column>
<el-table-column prop="username" label="账号" align="center"></el-table-column>
<el-table-column prop="accountId" label="所属公司" align="center">
<template #default="scope">
{{ companyMap[scope.row.accountId] }}
</template>
</el-table-column>
<el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
<el-table-column prop="operation" label="操作" align="center">
<template #default="scope">
<el-button @click="editUser(scope.row)" type="text">编辑
</el-button>
<el-button @click="confirmDeleteUser(scope.row.id)" type="text">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-drawer title="新建用户" v-model="addUserVisible" :destroy-on-close="true" :direction="'rtl'">
<div class="user-form">
<el-form :model="addUserForm" :rules="rules" ref="ref_addUserForm">
<el-form-item label="用户名称:" prop="username" :label-width="'120px'">
<el-input v-model.trim="addUserForm.name"></el-input>
</el-form-item>
<el-form-item label="账号:" prop="name" :label-width="'120px'">
<el-input v-model.trim="addUserForm.username" style="width: 100%"></el-input>
</el-form-item>
<el-form-item label="密码:" prop="password" :label-width="'120px'">
<el-input v-model.trim="addUserForm.password"></el-input>
</el-form-item>
<el-form-item label="所属公司:" prop="accountId" :label-width="'120px'">
<el-select v-model="addUserForm.accountId" style="width: 100%">
<el-option v-for="(name, value) in companyMap" :label="name" :value="parseInt(value)"></el-option>
</el-select>
</el-form-item>
<el-form-item label="数据权限管理:" prop="type" :label-width="'120px'">
<el-select v-model="addUserForm.type" style="width: 100%">
<el-option v-for="(value, name) in getCanCreateUsernameIdMap()" :label="name" :value="value"></el-option>
</el-select>
</el-form-item>
<el-form-item style="text-align:center">
<el-button @click="addUserVisible = false">取 消</el-button>
<el-button type="primary" @click="confirmAddUser">确 定</el-button>
</el-form-item>
</el-form>
</div>
</el-drawer>
<el-drawer title="编辑用户" v-model="editUserVisible" :destroy-on-close="true" :direction="'rtl'">
<div class="user-form">
<el-form :model="editUserForm" :rules="rules" ref="ref_editUserForm">
<el-form-item label="用户名称:" prop="username" :label-width="'120px'">
<el-input v-model.trim="editUserForm.name"></el-input>
</el-form-item>
<el-form-item label="账号:" prop="name" :label-width="'120px'">
<el-input v-model.trim="editUserForm.username" style="width: 100%"></el-input>
</el-form-item>
<el-form-item label="密码:" prop="password" :label-width="'120px'">
<el-input v-model.trim="editUserForm.password"></el-input>
</el-form-item>
<el-form-item label="所属公司:" prop="accountId" :label-width="'120px'">
<el-select v-model="editUserForm.accountId" style="width: 100%">
<el-option v-for="(name, value) in companyMap" :label="name" :value="parseInt(value)"></el-option>
</el-select>
</el-form-item>
<el-form-item label="数据权限管理:" prop="type" :label-width="'120px'">
<el-select v-model="editUserForm.type" style="width: 100%">
<el-option v-for="(value, name) in getCanCreateUsernameIdMap()" :label="name" :value="value"></el-option>
</el-select>
</el-form-item>
<el-form-item style="text-align:center">
<el-button @click="editUserVisible = false">取 消</el-button>
<el-button type="primary" @click="confirmEditUser">确 定</el-button>
</el-form-item>
</el-form>
</div>
</el-drawer>
</el-main>
</el-container>
</template>
<script>
import {reactive, ref, toRaw,} from "vue"
import {
filterEmptyValueInObject, getKeyByValueInObject,
resetForm
} from "@/PublicUtil/PublicUtil"
import userApi from './UserManagementApi'
import {ElMessageBox} from 'element-plus'
import {ElMessage} from 'element-plus'
import {getCanCreateUsernameIdMap, getCompanyMap} from '@/Request/DictionaryRequest'
export default {
setup() {
// scalar
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref()
const isLoading = ref(false)
const addUserVisible = ref(false)
const editUserVisible = ref(false)
const userList = ref([])
const ref_addUserForm = ref()
const ref_editUserForm = ref()
// computed
// mapping
const rules = reactive(
{
// userName: [
// {
// required: true,
// message: '请输入用户名称',
// },
// ],
// userYear: [
// {
// type: 'date',
// required: true,
// message: '请输入建成年份',
// trigger: 'change'
// }
// ],
// groupId: [
// {
// required: true,
// message: '请选择集团',
// },
// ],
}
)
const companyMap = reactive({})
const addUserForm = reactive(
{
username: '', // 用户名称
name: '', // 账号
password: '', // 密码
accountId: '', // 所属公司
type: '', // 数据权限管理
}
)
const editUserForm = reactive(
{
username: '', // 用户名称
name: '', // 账号
password: '', // 密码
accountId: '', // 所属公司
type: '', // 数据权限管理
id: '',
}
)
// function
const handlePageNumChange = function(num) {
pageNum.value = num
getUserList()
}
const handlePageSizeChange = function(size) {
pageNum.value = 1
pageSize.value = size
getUserList()
}
const confirmAddUser = function() {
ref_addUserForm.value.validate(
(isValid) => {
if (isValid)
{
const rawData = toRaw(addUserForm)
const data = filterEmptyValueInObject(rawData)
userApi.addUser(data).then(
(r) => {
addUserVisible.value = false
const message = r.msg
ElMessage(`${message}`)
getUserList()
}
)
}
}
)
}
const editUser = function(row) {
editUserVisible.value = true
editUserForm.username = row.username
editUserForm.name = row.name
editUserForm.password = row.password
editUserForm.accountId = row.accountId
editUserForm.type = row.type
editUserForm.id = row.id
}
const confirmEditUser = function() {
ref_editUserForm.value.validate(
(isValid) => {
if (isValid)
{
const rawData = toRaw(editUserForm)
const data = filterEmptyValueInObject(
{
username: rawData.username,
name: rawData.name,
password: rawData.password,
accountId: rawData.accountId,
type: rawData.type,
}
)
userApi.editUser(rawData.id, data).then(
(r) => {
const message = r.msg
getUserList()
ElMessage(`${message}`)
}
)
editUserVisible.value = false
}
}
)
}
const confirmDeleteUser = function(id) {
ElMessageBox.confirm(
"删除后此用户相关数据全部都会消失,你确定要删除吗?",
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
).then(
() => {
userApi.deleteUser(id).then(
(r) => {
const message = r.msg
getUserList()
ElMessage(`${message}`)
}
)
}
)
}
const addUser = function() {
resetForm(addUserForm)
addUserVisible.value = true
}
const getUserList = function() {
isLoading.value = true
userApi.getUserList().then(
({data}) => {
isLoading.value = false
userList.value = data
// formatPaginatedTableData(
// userList,
// data,
// total,
// )
}
)
}
const initialize = function() {
getUserList()
getCompanyMap(companyMap)
}
initialize()
return {
// scalar
pageNum,
pageSize,
total,
isLoading,
userList,
ref_addUserForm,
ref_editUserForm,
addUserVisible,
editUserVisible,
// mapping
companyMap,
addUserForm,
editUserForm,
rules,
// function
handlePageNumChange,
handlePageSizeChange,
addUser,
editUser,
confirmEditUser,
confirmAddUser,
confirmDeleteUser,
getCanCreateUsernameIdMap,
getKeyByValueInObject,
}
},
}
</script>
<style lang="less" scoped>
@import "./UserManagement.less";
</style>