2021-10-20 14:32:09 +08:00
|
|
|
|
<template>
|
2022-04-27 19:19:39 +08:00
|
|
|
|
<BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" :width="adaptiveWidth" @ok="handleSubmit" :showFooter="showFooter" destroyOnClose>
|
2022-03-10 09:47:29 +08:00
|
|
|
|
<BasicForm @register="registerForm" />
|
2021-10-20 14:32:09 +08:00
|
|
|
|
</BasicDrawer>
|
|
|
|
|
</template>
|
|
|
|
|
<script lang="ts" setup>
|
2022-06-10 10:44:44 +08:00
|
|
|
|
import { defineComponent, ref, computed, unref, useAttrs } from 'vue';
|
|
|
|
|
import { BasicForm, useForm } from '/@/components/Form/index';
|
|
|
|
|
import { formSchema } from './user.data';
|
|
|
|
|
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
|
|
|
|
|
import { saveOrUpdateUser, getUserRoles, getUserDepartList } from './user.api';
|
|
|
|
|
import { useDrawerAdaptiveWidth } from '/@/hooks/jeecg/useAdaptiveWidth';
|
2022-03-10 09:47:29 +08:00
|
|
|
|
// 声明Emits
|
2021-10-20 14:32:09 +08:00
|
|
|
|
const emit = defineEmits(['success', 'register']);
|
2022-06-10 10:44:44 +08:00
|
|
|
|
const attrs = useAttrs();
|
2021-10-20 14:32:09 +08:00
|
|
|
|
const isUpdate = ref(true);
|
|
|
|
|
const rowId = ref('');
|
2022-03-10 09:47:29 +08:00
|
|
|
|
const departOptions = ref([]);
|
2021-10-20 14:32:09 +08:00
|
|
|
|
//表单配置
|
2022-06-10 10:44:44 +08:00
|
|
|
|
const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
|
2021-10-20 14:32:09 +08:00
|
|
|
|
labelWidth: 90,
|
|
|
|
|
schemas: formSchema,
|
|
|
|
|
showActionButtonGroup: false,
|
|
|
|
|
});
|
2022-04-27 19:19:39 +08:00
|
|
|
|
// TODO [VUEN-527] https://www.teambition.com/task/6239beb894b358003fe93626
|
2022-06-10 10:44:44 +08:00
|
|
|
|
const showFooter = ref(true);
|
2021-10-20 14:32:09 +08:00
|
|
|
|
//表单赋值
|
2022-06-10 10:44:44 +08:00
|
|
|
|
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
|
2021-10-20 14:32:09 +08:00
|
|
|
|
await resetFields();
|
2022-06-10 10:44:44 +08:00
|
|
|
|
showFooter.value = data?.showFooter ?? true;
|
|
|
|
|
setDrawerProps({ confirmLoading: false, showFooter: showFooter.value });
|
2021-10-20 14:32:09 +08:00
|
|
|
|
isUpdate.value = !!data?.isUpdate;
|
|
|
|
|
if (unref(isUpdate)) {
|
2022-03-10 09:47:29 +08:00
|
|
|
|
rowId.value = data.record.id;
|
|
|
|
|
//租户信息定义成数组
|
|
|
|
|
if (data.record.relTenantIds && !Array.isArray(data.record.relTenantIds)) {
|
2022-06-10 10:44:44 +08:00
|
|
|
|
data.record.relTenantIds = data.record.relTenantIds.split(',');
|
|
|
|
|
} else {
|
2022-03-10 09:47:29 +08:00
|
|
|
|
data.record.relTenantIds = [];
|
2021-10-20 14:32:09 +08:00
|
|
|
|
}
|
2022-06-10 10:44:44 +08:00
|
|
|
|
//查角色/赋值/try catch 处理,不然编辑有问题
|
|
|
|
|
try {
|
|
|
|
|
const userRoles = await getUserRoles({ userid: data.record.id });
|
|
|
|
|
if (userRoles && userRoles.length > 0) {
|
|
|
|
|
data.record.selectedroles = userRoles;
|
2022-03-10 09:47:29 +08:00
|
|
|
|
}
|
2022-06-10 10:44:44 +08:00
|
|
|
|
} catch (error) {}
|
2022-03-10 09:47:29 +08:00
|
|
|
|
|
2022-06-10 10:44:44 +08:00
|
|
|
|
//查所属部门/赋值
|
|
|
|
|
const userDepart = await getUserDepartList({ userId: data.record.id });
|
|
|
|
|
if (userDepart && userDepart.length > 0) {
|
|
|
|
|
data.record.selecteddeparts = userDepart;
|
|
|
|
|
let selectDepartKeys = Array.from(userDepart, ({ key }) => key);
|
|
|
|
|
data.record.selecteddeparts = selectDepartKeys.join(',');
|
|
|
|
|
departOptions.value = userDepart.map((item) => {
|
|
|
|
|
return { label: item.title, value: item.key };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
//负责部门/赋值
|
|
|
|
|
data.record.departIds && !Array.isArray(data.record.departIds) && (data.record.departIds = data.record.departIds.split(','));
|
|
|
|
|
//update-begin---author:zyf Date:20211210 for:避免空值显示异常------------
|
|
|
|
|
data.record.departIds = data.record.departIds == '' ? [] : data.record.departIds;
|
|
|
|
|
//update-begin---author:zyf Date:20211210 for:避免空值显示异常------------
|
2021-10-20 14:32:09 +08:00
|
|
|
|
}
|
2022-03-10 09:47:29 +08:00
|
|
|
|
//处理角色用户列表情况(和角色列表有关系)
|
2022-06-10 10:44:44 +08:00
|
|
|
|
data.selectedroles && (await setFieldsValue({ selectedroles: data.selectedroles }));
|
2022-03-10 09:47:29 +08:00
|
|
|
|
//编辑时隐藏密码/角色列表隐藏角色信息/我的部门时隐藏所属部门
|
2021-10-20 14:32:09 +08:00
|
|
|
|
updateSchema([
|
|
|
|
|
{
|
|
|
|
|
field: 'password',
|
|
|
|
|
show: !unref(isUpdate),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'confirmPassword',
|
|
|
|
|
ifShow: !unref(isUpdate),
|
2022-03-10 09:47:29 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'selectedroles',
|
2022-06-10 10:44:44 +08:00
|
|
|
|
show: !data.isRole,
|
2022-03-10 09:47:29 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'departIds',
|
2022-06-10 10:44:44 +08:00
|
|
|
|
componentProps: { options: departOptions },
|
2022-03-10 09:47:29 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'selecteddeparts',
|
2022-06-10 10:44:44 +08:00
|
|
|
|
show: !data?.departDisabled ?? false,
|
|
|
|
|
},
|
2021-10-20 14:32:09 +08:00
|
|
|
|
]);
|
2022-03-10 09:47:29 +08:00
|
|
|
|
// 无论新增还是编辑,都可以设置表单值
|
|
|
|
|
if (typeof data.record === 'object') {
|
|
|
|
|
setFieldsValue({
|
|
|
|
|
...data.record,
|
2022-06-10 10:44:44 +08:00
|
|
|
|
});
|
2022-03-10 09:47:29 +08:00
|
|
|
|
}
|
|
|
|
|
// 隐藏底部时禁用整个表单
|
2022-06-10 10:44:44 +08:00
|
|
|
|
setProps({ disabled: !showFooter });
|
2021-10-20 14:32:09 +08:00
|
|
|
|
});
|
|
|
|
|
//获取标题
|
|
|
|
|
const getTitle = computed(() => (!unref(isUpdate) ? '新增用户' : '编辑用户'));
|
2022-06-10 10:44:44 +08:00
|
|
|
|
const { adaptiveWidth } = useDrawerAdaptiveWidth();
|
2021-10-20 14:32:09 +08:00
|
|
|
|
|
2022-03-10 09:47:29 +08:00
|
|
|
|
//提交事件
|
2021-10-20 14:32:09 +08:00
|
|
|
|
async function handleSubmit() {
|
|
|
|
|
try {
|
|
|
|
|
let values = await validate();
|
2022-06-10 10:44:44 +08:00
|
|
|
|
setDrawerProps({ confirmLoading: true });
|
|
|
|
|
values.userIdentity === 1 && (values.departIds = '');
|
2021-10-20 14:32:09 +08:00
|
|
|
|
//提交表单
|
|
|
|
|
await saveOrUpdateUser(values, unref(isUpdate));
|
|
|
|
|
//关闭弹窗
|
|
|
|
|
closeDrawer();
|
|
|
|
|
//刷新列表
|
2022-06-10 10:44:44 +08:00
|
|
|
|
emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
|
2021-10-20 14:32:09 +08:00
|
|
|
|
} finally {
|
2022-06-10 10:44:44 +08:00
|
|
|
|
setDrawerProps({ confirmLoading: false });
|
2021-10-20 14:32:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|