515fceeb
“wangming”
框架初始化
|
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
<script setup lang="ts">
import type { Role } from '#/api/system/user/model';
import { computed, h, onMounted, ref } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { addFullName, cloneDeep, getPopupContainer } from '@vben/utils';
import { Tag } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { configInfoByKey } from '#/api/system/config';
import { postOptionSelect } from '#/api/system/post';
import { roleOptionSelect } from '#/api/system/role';
import {
findUserInfo,
getDeptTree,
userAdd,
userUpdate,
} from '#/api/system/user';
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
import { authScopeOptions } from '#/views/system/role/data';
import { drawerSchema } from './data';
const emit = defineEmits<{ reload: [] }>();
const isUpdate = ref(false);
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
formItemClass: 'col-span-2',
componentProps: {
class: 'w-full',
},
labelWidth: 80,
},
schema: drawerSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
/**
* 生成角色的自定义label
* 也可以用option插槽来做
* renderComponentContent: () => ({
option: ({value, label, [disabled, key, title]}) => '',
}),
*/
function genRoleOptionlabel(role: Role) {
const found = authScopeOptions.find((item) => item.value === role.dataScope);
if (!found) {
return role.roleName;
}
return h('div', { class: 'flex items-center gap-[6px]' }, [
h('span', null, role.roleName),
h(Tag, { color: found.color }, () => found.label),
]);
}
/**
* 根据部门ID加载岗位列表
* @param deptId 部门ID
*/
async function setupPostOptions(deptId?: string) {
if (!deptId) {
// 没有选择部门时,显示提示
formApi.updateSchema([
{
componentProps: {
disabled: true,
options: [],
placeholder: '请先选择部门',
},
fieldName: 'postIds',
},
]);
// 清空已选岗位
formApi.setFieldValue('postIds', []);
return;
}
try {
const postListResp = await postOptionSelect(deptId);
// 确保返回的是数组
const postList = Array.isArray(postListResp) ? postListResp : [];
const options = postList.map((item) => ({
label: item.postName,
value: item.id,
}));
const placeholder = options.length > 0 ? '请选择岗位' : '该部门暂无岗位';
formApi.updateSchema([
{
componentProps: {
disabled: options.length === 0,
options,
placeholder,
},
fieldName: 'postIds',
},
]);
// 部门变化时清空已选岗位
formApi.setFieldValue('postIds', []);
} catch (error) {
console.error('加载岗位信息失败:', error);
formApi.updateSchema([
{
componentProps: {
disabled: true,
options: [],
placeholder: '加载岗位失败',
},
fieldName: 'postIds',
},
]);
}
}
/**
* 初始化部门选择
*/
async function setupDeptSelect() {
try {
// updateSchema
const deptTree = await getDeptTree();
// 确保返回的是数组
const deptList = Array.isArray(deptTree) ? deptTree : [];
// 选中后显示在输入框的值 即父节点 / 子节点
addFullName(deptList, 'deptName', ' / ');
formApi.updateSchema([
{
componentProps: {
class: 'w-full',
fieldNames: {
label: 'deptName',
key: 'id',
value: 'id',
children: 'children',
},
getPopupContainer,
placeholder: '请选择',
showSearch: true,
treeData: deptList,
treeDefaultExpandAll: true,
treeLine: { showLeafIcon: false },
// 筛选的字段
treeNodeFilterProp: 'deptName',
// 选中后显示在输入框的值
treeNodeLabelProp: 'fullName',
// 部门选择变化时加载对应岗位
onChange: (value: string) => {
setupPostOptions(value);
},
},
fieldName: 'deptId',
},
]);
} catch (error) {
console.error('加载部门树失败:', error);
// 加载失败时设置空树
formApi.updateSchema([
{
componentProps: {
placeholder: '加载部门失败',
treeData: [],
},
fieldName: 'deptId',
},
]);
}
}
const defaultPassword = ref('');
onMounted(async () => {
const password = await configInfoByKey('sys.user.initPassword');
if (password) {
defaultPassword.value = password;
}
});
/**
* 新增时候 从参数设置获取默认密码
*/
async function loadDefaultPassword(update: boolean) {
if (!update && defaultPassword.value) {
formApi.setFieldValue('password', defaultPassword.value);
}
}
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
);
const [BasicDrawer, drawerApi] = useVbenDrawer({
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
async onOpenChange(isOpen) {
if (!isOpen) {
// 需要重置岗位选择
formApi.updateSchema([
{
componentProps: {
disabled: true,
options: [],
placeholder: '请先选择部门',
},
fieldName: 'postIds',
},
]);
return null;
}
drawerApi.drawerLoading(true);
try {
const { id } = drawerApi.getData() as { id?: number | string };
isUpdate.value = !!id;
/** update时 禁用用户名修改 不显示密码框 */
formApi.updateSchema([
{ componentProps: { disabled: isUpdate.value }, fieldName: 'userName' },
{
dependencies: { show: () => !isUpdate.value, triggerFields: ['id'] },
fieldName: 'password',
},
]);
let user: any | null = null;
if (isUpdate.value && id) {
// 编辑模式:从用户详情中获取用户信息(含岗位、角色ID)
user = await findUserInfo(id);
}
// 角色下拉统一使用 roleOptionSelect
const roleListResp = await roleOptionSelect();
const allRoles = Array.isArray(roleListResp) ? (roleListResp as Role[]) : [];
const userRoles = user?.roles ?? [];
const posts = user?.posts ?? [];
const postIds = posts.map((item: any) => item.id);
const roleIds = userRoles.map((item: any) => item.roleId ?? item.id);
const postOptions = posts.map((item: any) => ({
label: item.postName,
value: item.id,
}));
formApi.updateSchema([
{
componentProps: {
// title用于选中后回填到输入框 默认为label
optionLabelProp: 'title',
options: allRoles.map((item: any) => ({
label: genRoleOptionlabel(item),
// title用于选中后回填到输入框 默认为label
title: item.roleName,
value: item.roleId ?? item.id,
})),
},
fieldName: 'roleIds',
},
]);
// 部门选择、初始密码
const promises = [
setupDeptSelect(),
loadDefaultPassword(isUpdate.value),
];
if (user) {
// 编辑模式:使用用户已有的岗位数据
formApi.updateSchema([
{
componentProps: {
disabled: false,
options: postOptions,
placeholder: '请选择岗位',
},
fieldName: 'postIds',
},
]);
// 处理用户数据,确保 phone 字段是字符串类型
const userData = {
...user,
// 将数字类型的 phone 转换为字符串,null/undefined 转为空字符串
phone: user.phone != null ? String(user.phone) : '',
};
promises.push(
// 添加基础信息
formApi.setValues(userData),
// 添加角色和岗位
formApi.setFieldValue('postIds', postIds),
formApi.setFieldValue('roleIds', roleIds),
);
} else {
// 新增模式:等待选择部门后再加载岗位
await setupPostOptions();
}
// 并行处理
await Promise.all(promises);
await markInitialized();
} catch (error) {
console.error('加载用户信息失败:', error);
} finally {
drawerApi.drawerLoading(false);
}
},
});
async function handleConfirm() {
try {
drawerApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
const data = cloneDeep(await formApi.getValues());
await (isUpdate.value ? userUpdate(data) : userAdd(data));
resetInitialized();
emit('reload');
drawerApi.close();
} catch (error) {
console.error(error);
} finally {
drawerApi.lock(false);
}
}
async function handleClosed() {
formApi.resetForm();
resetInitialized();
}
</script>
<template>
<BasicDrawer :title="title" class="w-[600px]">
<BasicForm />
</BasicDrawer>
</template>
|