<script lang="ts" setup>
|
import { computed, nextTick, reactive } from 'vue';
|
|
import { useMessage } from '@jnpf/hooks';
|
import { BasicForm, useForm } from '@jnpf/ui/form';
|
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
|
import { formValidate } from '@jnpf/utils';
|
|
import { TreeSelect } from 'ant-design-vue';
|
|
import { create, getInfo, getOrganizeSyncList, update } from '#/api/permission/organize';
|
import { $t } from '#/locales';
|
|
interface State {
|
id: string;
|
selectedOrganize: any;
|
treeData: any[];
|
parentCategory: string;
|
key: number;
|
}
|
|
const emit = defineEmits(['register', 'reload']);
|
const state = reactive<State>({
|
id: '',
|
selectedOrganize: { label: undefined, value: undefined },
|
treeData: [],
|
parentCategory: '',
|
key: 0,
|
});
|
const [registerForm, { setFieldsValue, resetFields, validate, clearValidate }] = useForm({
|
schemas: [
|
{
|
field: 'parentId',
|
label: '上级组织',
|
component: 'TreeSelect',
|
slot: 'parentId',
|
rules: [{ required: true, trigger: 'change', message: '必填' }],
|
},
|
{
|
field: 'category',
|
label: '组织类型',
|
component: 'Radio',
|
slot: 'category',
|
rules: [{ required: true, trigger: 'change', message: '必填' }],
|
},
|
{
|
field: 'fullName',
|
label: '组织名称',
|
component: 'Input',
|
componentProps: { placeholder: '请输入', maxlength: 50 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
field: 'enCode',
|
label: '组织编码',
|
component: 'Input',
|
componentProps: { maxlength: 50 },
|
rules: [{ validator: formValidate('enCode', '只能输入英文、数字和小数点且小数点不能放在首尾'), trigger: 'blur' }],
|
},
|
{
|
field: 'sortCode',
|
label: '排序',
|
defaultValue: 0,
|
component: 'InputNumber',
|
componentProps: { min: 0, max: 999999 },
|
},
|
{
|
field: 'description',
|
label: '说明',
|
component: 'Textarea',
|
componentProps: { placeholder: '请输入' },
|
},
|
],
|
});
|
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
|
const { createMessage } = useMessage();
|
|
const getTitle = computed(() => (state.id ? $t('common.editText') : $t('common.addText')));
|
const getCategoryOptions = computed(() => {
|
const group = { fullName: '集团', id: 'group' };
|
const company = { fullName: '公司', id: 'company' };
|
const agency = { fullName: '机构', id: 'agency' };
|
const office = { fullName: '机关', id: 'office' };
|
const unit = { fullName: '单位', id: 'unit' };
|
const department = { fullName: '部门', id: 'department' };
|
const subsidiary = { fullName: '子公司', id: 'subsidiary' };
|
if (state.parentCategory == 'group') return [company, department];
|
if (state.parentCategory == 'company') return [subsidiary, department];
|
if (state.parentCategory == 'subsidiary') return [department];
|
if (state.parentCategory == 'agency') return [office, unit];
|
if (state.parentCategory == 'office') return [unit];
|
if (['department', 'subsidiary', 'unit'].includes(state.parentCategory)) return [department];
|
return [group, company, agency, office, unit];
|
});
|
|
function init(data) {
|
resetFields();
|
state.id = data.id;
|
state.selectedOrganize = { value: undefined, label: undefined };
|
initOrganizeList();
|
if (state.id) {
|
changeLoading(true);
|
getInfo(state.id).then((res) => {
|
state.selectedOrganize = { label: res.data.parentName || '根节点', value: res.data.parentId };
|
state.parentCategory = res.data.parentCategory;
|
setFieldsValue(res.data);
|
changeLoading(false);
|
});
|
} else {
|
state.selectedOrganize = { value: data.parentId || '-1', label: data.parentName || '根节点' };
|
state.parentCategory = data.category;
|
setFieldsValue({ parentId: state.selectedOrganize.value });
|
}
|
}
|
function initOrganizeList() {
|
getOrganizeSyncList({ parentId: '0' }).then((res) => {
|
const topItem = { fullName: '根节点', id: '-1', isLeaf: true };
|
const list = res.data.list.map((o) => ({ id: o.id, fullName: o.fullName, isLeaf: o.isLeaf, category: o.category })) || [];
|
state.treeData = [topItem, ...list];
|
});
|
}
|
function onLoadData(treeNode) {
|
return new Promise((resolve) => {
|
const { id } = treeNode.dataRef;
|
getOrganizeSyncList({ parentId: id }).then((res) => {
|
const list = res.data.list.map((o) => ({ id: o.id, fullName: o.fullName, pId: id, isLeaf: o.isLeaf, category: o.category }));
|
state.treeData = [...state.treeData, ...list.filter((o) => !state.treeData.some((j) => o.id === j.id))];
|
resolve(true);
|
});
|
});
|
}
|
function onOrganizeChange(value, node) {
|
if (!value) return;
|
setFieldsValue({ parentId: value, parentName: node.fullName });
|
state.parentCategory = node.category;
|
nextTick(() => {
|
setFieldsValue({ category: '' });
|
clearValidate('parentId');
|
});
|
}
|
async function handleSubmit() {
|
state.key = Date.now();
|
const values = await validate();
|
if (!values) return;
|
changeOkLoading(true);
|
const query = {
|
...values,
|
id: state.id,
|
};
|
const formMethod = state.id ? update : create;
|
formMethod(query)
|
.then((res) => {
|
createMessage.success(res.msg);
|
changeOkLoading(false);
|
closeModal();
|
setTimeout(() => {
|
emit('reload');
|
}, 300);
|
})
|
.catch(() => {
|
changeOkLoading(false);
|
});
|
}
|
</script>
|
<template>
|
<BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit" destroy-on-close>
|
<BasicForm @register="registerForm">
|
<template #parentId>
|
<TreeSelect
|
v-model:value="state.selectedOrganize"
|
placeholder="请选择"
|
tree-data-simple-mode
|
label-in-value
|
:show-search="false"
|
:tree-default-expand-all="false"
|
:tree-data="state.treeData"
|
:key="state.key"
|
:field-names="{ label: 'fullName', value: 'id' }"
|
:load-data="onLoadData"
|
@select="onOrganizeChange" />
|
</template>
|
<template #category="{ model, field }">
|
<jnpf-radio v-model:value="model[field]" :options="getCategoryOptions" option-type="button" button-style="solid" />
|
</template>
|
</BasicForm>
|
</BasicModal>
|
</template>
|