AccountModal.vue
4.52 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
<template>
<BasicModal
width="650px"
v-bind="$attrs"
@register="registerModal"
:title="getTitle"
@ok="handleSubmit"
>
<BasicForm @register="registerForm">
<template #organizationId="{ model, field }">
<BasicTree
v-model:value="model[field]"
:treeData="organizationTreeData"
:replaceFields="{ title: 'name' }"
:checked-keys="checkGroup"
checkable
toolbar
title="所属组织"
/>
</template>
</BasicForm>
</BasicModal>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { accountFormSchema } from './account.data';
import {
findCurrentUserRelation,
getOrganizationList,
SaveOrUpdateUserInfo,
} 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';
export default defineComponent({
name: 'AccountModal',
components: { BasicModal, BasicForm, BasicTree },
emits: ['success', 'register'],
setup(_, { emit }) {
const isUpdate = ref(true);
const rowId = ref('');
const organizationTreeData = ref<TreeItem[]>([]);
const checkGroup = ref<string[]>([]);
const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
labelWidth: 100,
schemas: accountFormSchema,
showActionButtonGroup: false,
actionColOptions: {
span: 18,
},
});
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
await resetFields();
setModalProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
const groupListModel = await findCurrentUserGroups();
if (unref(organizationTreeData).length === 0) {
let treeValues = new Array<TreeItem>();
groupListModel.map((item) => {
const groupData = {
name: item.name,
key: item.id,
id: item.id,
children: item.children as any as TreeItem[],
};
treeValues.push(groupData);
});
organizationTreeData.value = treeValues;
}
if (unref(isUpdate)) {
rowId.value = data.record.id;
const roleParams = new RoleOrOrganizationParam(rowId.value, true, false);
findCurrentUserRelation(roleParams).then((result) => {
console.log(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);
}
const deptData = await getOrganizationList();
await updateSchema([
{
field: 'username',
dynamicDisabled: unref(isUpdate),
},
{
field: 'organizationIds',
componentProps: {
treeData: deptData,
},
},
]);
});
const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));
async function handleSubmit() {
try {
const { createMessage } = useMessage();
const values = await validate();
console.log(values);
values.accountExpireTime =
typeof values.accountExpireTime != 'undefined' && values.accountExpireTime != null
? values.accountExpireTime.format('YYYY-MM-DD HH:mm:ss')
: null;
setModalProps({ confirmLoading: true });
await SaveOrUpdateUserInfo(values, unref(isUpdate));
closeModal();
emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
createMessage.success(unref(isUpdate) ? '编辑成功' : '新增成功');
} finally {
setModalProps({ confirmLoading: false });
}
}
return {
registerModal,
registerForm,
getTitle,
handleSubmit,
organizationTreeData,
checkGroup,
};
},
});
</script>