AccountModal.vue
11 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
333
334
335
<template>
<BasicModal
width="650px"
v-bind="$attrs"
@register="registerModal"
:title="getTitle"
@ok="handleSubmit"
>
<div style="height: 50vh">
<BasicForm @register="registerForm">
<template #organizationId="{ model, field }">
<Button type="link" @click="handleOpenCreate" style="padding: 0; z-index: 9999"
>新增组织
</Button>
<BasicTree
v-if="organizationTreeData.length"
v-model:value="model[field]"
:treeData="organizationTreeData"
:checked-keys="checkGroup"
:expandedKeys="treeExpandData"
ref="basicTreeRef"
@check="handleCheckClick"
@unSelectAll="handleUnSelectAll"
@strictlyStatus="handleStrictlyStatus"
checkable
toolbar
@change="handleTreeSelect"
/>
</template>
<template #roleSlot="{ model, field }">
<a-select
mode="multiple"
allowClear
placeholder="请选择角色"
v-model:value="model[field]"
@change="handleRoleSelect"
:options="roleOptions.map((item) => ({ value: item.value, label: item.label }))"
>
<template #dropdownRender="{ menuNode: menu }">
<v-nodes :vnodes="menu" />
<a-divider style="margin: 4px 0" />
<div @click="handleOpenRole" style="padding: 4px 0; cursor: pointer">
<plus-outlined />
新增角色
</div>
</template>
</a-select>
</template>
</BasicForm>
<OrganizationDrawer @register="registerDrawer" @success="handleReload" />
</div>
</BasicModal>
<RoleDrawer @register="registerRoleDrawer" @success="handleSuccess" />
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, reactive, onMounted } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { accountFormSchema } from './account.data';
import { Button } from 'ant-design-vue';
import {
findCurrentUserRelation,
SaveOrUpdateUserInfo,
filterRoleList,
} from '/@/api/system/system';
import { BasicTree, TreeItem, CheckKeys, CheckEvent } from '/@/components/Tree';
import { findCurrentUserGroups } from '/@/api/system/group';
import { RoleOrOrganizationParam } from '/@/api/system/model/systemModel';
import { useMessage } from '/@/hooks/web/useMessage';
import { copyTransTreeFun } from '/@/utils/fnUtils';
import { TOption } from '/@/views/rule/linkedge/config/config.data';
import { PlusOutlined } from '@ant-design/icons-vue';
import { useDrawer } from '/@/components/Drawer';
import RoleDrawer from '../role/RoleDrawer.vue';
import OrganizationDrawer from '/@/views/system/organization/OrganizationDrawer.vue';
export default defineComponent({
name: 'AccountModal',
components: {
BasicModal,
BasicForm,
Button,
BasicTree,
OrganizationDrawer,
PlusOutlined,
RoleDrawer,
VNodes: (_, { attrs }) => {
return attrs.vnodes;
},
},
emits: ['success', 'register'],
setup(_, { emit }) {
const roleOptions = ref<TOption[]>([]);
const isUpdate = ref(true);
const rowId = ref('');
const organizationTreeData = ref<TreeItem[]>([]);
const basicTreeRef = ref();
const checkGroup = ref<string[]>([]);
const treeExpandData = ref([]);
const olderPhoneNumber = ref();
const postData = reactive({});
const singleEditPostPhoneNumber = reactive({
phoneNumber: '',
});
const checkedKeysWithHalfChecked = ref<(string | number)[]>([]);
const getRoleList = async () => {
const res = await filterRoleList();
roleOptions.value = res.map((m) => {
return {
label: m.name,
value: m.id,
};
});
};
onMounted(async () => {
await getRoleList();
});
const [registerRoleDrawer, { openDrawer }] = useDrawer();
const handleOpenRole = () => {
openDrawer(true, {
isUpdate: false,
});
};
const clearValidateByField = (field: string) => {
clearValidate(field);
};
const handleRoleSelect = (e) => {
if (e?.length > 0) clearValidateByField('roleIds');
else validateFields(['roleIds']);
};
const handleTreeSelect = (e) => {
if (e) clearValidateByField('organizationIds');
};
const handleSuccess = async () => {
await getRoleList();
};
const [
registerForm,
{
setFieldsValue,
updateSchema,
resetFields,
validate,
getFieldsValue,
clearValidate,
validateFields,
},
] = useForm({
labelWidth: 100,
schemas: accountFormSchema,
showActionButtonGroup: false,
actionColOptions: {
span: 18,
},
});
//获取所有父级id
function findForAllId(data = [], arr = []) {
for (const item of data) {
arr.push(item.id);
}
return arr;
}
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
await resetFields();
setModalProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
const groupListModel = await findCurrentUserGroups();
if (!unref(organizationTreeData).length) {
copyTransTreeFun(groupListModel);
organizationTreeData.value = groupListModel;
const getAllIds = findForAllId(organizationTreeData.value as any, []);
//设置要展开的id
treeExpandData.value = getAllIds;
}
if (unref(isUpdate)) {
rowId.value = data.record.id;
const roleParams = new RoleOrOrganizationParam(rowId.value, true, false);
olderPhoneNumber.value = data.record.phoneNumber;
singleEditPostPhoneNumber.phoneNumber = data.record.phoneNumber;
findCurrentUserRelation(roleParams).then((result) => {
Reflect.set(data.record, 'roleIds', result);
Reflect.set(data.record, 'password', '******');
setFieldsValue(data.record);
});
const organizationParams = new RoleOrOrganizationParam(rowId.value, false, true);
checkGroup.value = await findCurrentUserRelation(organizationParams);
}
await updateSchema([
{
field: 'username',
dynamicDisabled: unref(isUpdate),
},
{
field: 'password',
ifShow: !unref(isUpdate),
},
]);
});
const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
async function handleSubmit() {
setModalProps({ confirmLoading: true });
try {
const { createMessage } = useMessage();
if (unref(isUpdate)) {
Object.assign(postData, singleEditPostPhoneNumber);
}
const values = await validate([
'id',
'username',
'realName',
'password',
'roleIds',
'email',
'accountExpireTime',
'enabled',
'remark',
'organizationIds',
olderPhoneNumber.value === getFieldsValue().phoneNumber ? '' : 'phoneNumber',
]);
let treeCheckedKeys: string[] | CheckKeys =
(unref(basicTreeRef)?.getCheckedKeys() as string[] | CheckKeys) || [];
//fix 取消层级独立后(unref(treeRef)?.getCheckedKeys() as string[])的数据不是数组,是{checked:[],halfChecked:[]}对象,迭代报错
if (!Array.isArray(treeCheckedKeys)) {
treeCheckedKeys = treeCheckedKeys?.checked;
}
const organizationIds = [
...new Set([...unref(checkedKeysWithHalfChecked), ...treeCheckedKeys]),
];
values.accountExpireTime =
typeof values.accountExpireTime != 'undefined' && values.accountExpireTime != null
? values.accountExpireTime.format('YYYY-MM-DD HH:mm:ss')
: null;
values.organizationIds = organizationIds;
Object.assign(postData, values);
if (unref(isUpdate)) {
if (values.email == '') {
delete postData.email;
}
} else {
if (values.email == '') {
delete postData.email;
}
}
await SaveOrUpdateUserInfo(postData as any, unref(isUpdate));
closeModal();
emit('success');
createMessage.success(unref(isUpdate) ? '编辑成功' : '新增成功');
} finally {
setTimeout(() => {
setModalProps({ confirmLoading: false });
}, 300);
}
}
// 取消全部的时候清除回显时获取的
const handleUnSelectAll = () => {
checkedKeysWithHalfChecked.value = [];
};
const strictlyStatus = ref(false); //层级关联或独立的状态 false为层级关联 true为层级独立
const handleStrictlyStatus = (status) => (strictlyStatus.value = status);
const handleCheckClick = (selectedKeys: CheckKeys, event: CheckEvent) => {
//fix 取消层级独立后selectedKeys不是数组,是{checked:[],halfChecked:[]}对象 迭代报错
// 层级独立
if (strictlyStatus.value) {
if (!Array.isArray(selectedKeys)) {
selectedKeys = selectedKeys?.checked;
event.halfCheckedKeys = [];
}
} else {
// 层级关联
event.halfCheckedKeys = [];
}
checkedKeysWithHalfChecked.value = [
...selectedKeys,
...(event.halfCheckedKeys as string[]),
];
};
const [registerDrawer, { openDrawer: addOpenDrawer }] = useDrawer();
const handleOpenCreate = () => {
addOpenDrawer(true, { isUpdate: false });
};
const handleReload = async () => {
const groupListModel = await findCurrentUserGroups();
copyTransTreeFun(groupListModel);
organizationTreeData.value = groupListModel;
};
return {
registerModal,
registerForm,
handleSubmit,
getTitle,
organizationTreeData,
checkGroup,
basicTreeRef,
treeExpandData,
roleOptions,
registerRoleDrawer,
handleOpenRole,
handleSuccess,
handleRoleSelect,
handleTreeSelect,
handleCheckClick,
handleUnSelectAll,
handleStrictlyStatus,
handleOpenCreate,
registerDrawer,
handleReload,
};
},
});
</script>
<style scoped lang="less">
:deep(.vben-basic-tree) {
width: 100% !important;
margin-top: -28px !important;
padding: 0;
}
:deep(.is-unflod) {
display: none !important;
}
:deep(.is-flod) {
display: none !important;
}
</style>