OrganizationDrawer.vue
3.22 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
<template>
<BasicDrawer
v-bind="$attrs"
@register="registerDrawer"
showFooter
:title="getTitle"
width="30%"
@ok="handleSubmit"
>
<BasicForm @register="registerForm" />
</BasicDrawer>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form';
import { formSchema } from './organization.data';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { useI18n } from '/@/hooks/web/useI18n';
import { getOrganizationList, saveOrUpdateOrganization } from '/@/api/system/system';
import { useMessage } from '/@/hooks/web/useMessage';
export default defineComponent({
name: 'OrganizationDrawer',
components: { BasicDrawer, BasicForm },
emits: ['success', 'register'],
setup(_, { emit }) {
const { createMessage } = useMessage();
const isUpdate = ref(true);
let organizationId;
const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
labelWidth: 100,
schemas: formSchema,
showActionButtonGroup: false,
});
const { t } = useI18n(); // 加载国际化
//默认传递页面数据
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
await resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
let treeData: any[] = await getOrganizationList();
//如果是编辑操作,设置页面数据
if (unref(isUpdate)) {
//为表单赋值
await setFieldsValue(data.record);
organizationId = Reflect.get(data.record, 'id');
// 过滤当前选中的组织,
(function filterTransFun(treeData: any[]) {
treeData.forEach((item, index, arr) => {
if (item.id === organizationId) arr.splice(index, 1);
if (item.children.length) filterTransFun(item.children);
});
})(treeData);
}
await updateSchema({
field: 'parentId',
componentProps: { treeData },
});
});
//得到页面标题
const getTitle = computed(() =>
!unref(isUpdate)
? t('routes.common.organization.toolCreateOrganization')
: t('routes.common.organization.toolEditOrganization')
);
//提交按钮
async function handleSubmit() {
setDrawerProps({ confirmLoading: true });
try {
const values = await validate();
if (isUpdate.value) {
Reflect.set(values, 'id', organizationId);
}
await saveOrUpdateOrganization(
{ ...values, parentId: values.parentId ?? null },
isUpdate.value
);
closeDrawer(); //关闭侧框
createMessage.success(
`${unref(isUpdate) ? t('common.editOkText') : t('common.increaseOkText')}`
);
emit('success');
} catch {
} finally {
setTimeout(() => {
setDrawerProps({ confirmLoading: false });
}, 300);
}
}
return { registerDrawer, registerForm, getTitle, handleSubmit };
},
});
</script>