2021-10-20 14:32:09 +08:00
|
|
|
|
<template>
|
2022-03-10 09:47:29 +08:00
|
|
|
|
<BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="600" @ok="handleSubmit" destroyOnClose>
|
|
|
|
|
<BasicForm @register="registerForm" />
|
2021-10-20 14:32:09 +08:00
|
|
|
|
</BasicDrawer>
|
|
|
|
|
</template>
|
|
|
|
|
<script lang="ts" setup>
|
2022-03-10 09:47:29 +08:00
|
|
|
|
import {defineComponent, ref, computed, unref,useAttrs} from 'vue';
|
2021-10-20 14:32:09 +08:00
|
|
|
|
import {BasicForm, useForm} from '/@/components/Form/index';
|
|
|
|
|
import {formSchema} from './user.data';
|
|
|
|
|
import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
|
2022-03-10 09:47:29 +08:00
|
|
|
|
import {saveOrUpdateUser,getUserRoles,getUserDepartList} from './user.api';
|
|
|
|
|
// 声明Emits
|
2021-10-20 14:32:09 +08:00
|
|
|
|
const emit = defineEmits(['success', 'register']);
|
2022-03-10 09:47:29 +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-03-10 09:47:29 +08:00
|
|
|
|
const [registerForm, {setProps,resetFields, setFieldsValue, validate, updateSchema}] = useForm({
|
2021-10-20 14:32:09 +08:00
|
|
|
|
labelWidth: 90,
|
|
|
|
|
schemas: formSchema,
|
|
|
|
|
showActionButtonGroup: false,
|
|
|
|
|
});
|
|
|
|
|
//表单赋值
|
|
|
|
|
const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
|
|
|
|
|
await resetFields();
|
2022-03-10 09:47:29 +08:00
|
|
|
|
let showFooter = data?.showFooter ?? true
|
|
|
|
|
setDrawerProps({ confirmLoading: false, showFooter })
|
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)) {
|
|
|
|
|
data.record.relTenantIds = data.record.relTenantIds.split(",");
|
|
|
|
|
}else{
|
|
|
|
|
data.record.relTenantIds = [];
|
2021-10-20 14:32:09 +08:00
|
|
|
|
}
|
2022-03-10 09:47:29 +08:00
|
|
|
|
//查角色/赋值/try catch 处理,不然编辑有问题
|
|
|
|
|
try {
|
|
|
|
|
const userRoles = await getUserRoles({userid: data.record.id});
|
|
|
|
|
if(userRoles && userRoles.length > 0 ){
|
|
|
|
|
data.record.selectedroles = userRoles
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查所属部门/赋值
|
|
|
|
|
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
|
|
|
|
//处理角色用户列表情况(和角色列表有关系)
|
|
|
|
|
data.selectedroles && await setFieldsValue({selectedroles:data.selectedroles});
|
|
|
|
|
//编辑时隐藏密码/角色列表隐藏角色信息/我的部门时隐藏所属部门
|
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',
|
|
|
|
|
show: !data.isRole
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'departIds',
|
|
|
|
|
componentProps: {options:departOptions},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
field: 'selecteddeparts',
|
|
|
|
|
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,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// 隐藏底部时禁用整个表单
|
|
|
|
|
setProps({ disabled: !showFooter })
|
2021-10-20 14:32:09 +08:00
|
|
|
|
});
|
|
|
|
|
//获取标题
|
|
|
|
|
const getTitle = computed(() => (!unref(isUpdate) ? '新增用户' : '编辑用户'));
|
|
|
|
|
|
2022-03-10 09:47:29 +08:00
|
|
|
|
//提交事件
|
2021-10-20 14:32:09 +08:00
|
|
|
|
async function handleSubmit() {
|
|
|
|
|
try {
|
|
|
|
|
let values = await validate();
|
|
|
|
|
setDrawerProps({confirmLoading: true});
|
2022-03-10 09:47:29 +08:00
|
|
|
|
values.userIdentity===1&&(values.departIds='');
|
2021-10-20 14:32:09 +08:00
|
|
|
|
//提交表单
|
|
|
|
|
await saveOrUpdateUser(values, unref(isUpdate));
|
|
|
|
|
//关闭弹窗
|
|
|
|
|
closeDrawer();
|
|
|
|
|
//刷新列表
|
|
|
|
|
emit('success', {isUpdate: unref(isUpdate), values: {...values, id: rowId.value}});
|
|
|
|
|
} finally {
|
|
|
|
|
setDrawerProps({confirmLoading: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|