AccountModal.vue
7.71 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
<template>
<BasicModal
width="650px"
v-bind="$attrs"
@register="registerModal"
:title="getTitle"
@ok="handleSubmit"
>
<div style="height: 50vh">
<BasicForm @register="registerForm">
<template #organizationId="{ model, field }">
<BasicTree
v-if="organizationTreeData.length"
v-model:value="model[field]"
:treeData="organizationTreeData"
:checked-keys="checkGroup"
:expandedKeys="treeExpandData"
ref="basicTreeRef"
checkable
toolbar
/>
</template>
<template #roleSlot="{ model, field }">
<a-select
mode="multiple"
allowClear
placeholder="请选择角色"
v-model:value="model[field]"
: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>
</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 {
findCurrentUserRelation,
SaveOrUpdateUserInfo,
filterRoleList,
} from '/@/api/system/system';
import { BasicTree, TreeItem } 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';
export default defineComponent({
name: 'AccountModal',
components: {
BasicModal,
BasicForm,
BasicTree,
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 getRoleList = async () => {
const res = await filterRoleList();
console.log(res);
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 handleSuccess = async () => {
await getRoleList();
};
const [
registerForm,
{ setFieldsValue, updateSchema, resetFields, validate, getFieldsValue },
] = 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',
]);
values.accountExpireTime =
typeof values.accountExpireTime != 'undefined' && values.accountExpireTime != null
? values.accountExpireTime.format('YYYY-MM-DD HH:mm:ss')
: null;
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);
}
}
return {
registerModal,
registerForm,
handleSubmit,
getTitle,
organizationTreeData,
checkGroup,
basicTreeRef,
treeExpandData,
roleOptions,
registerRoleDrawer,
handleOpenRole,
handleSuccess,
};
},
});
</script>
<style scoped lang="less">
:deep(.vben-basic-tree) {
width: 100% !important;
}
:deep(.is-unflod) {
display: none !important;
}
:deep(.is-flod) {
display: none !important;
}
</style>