<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 { getOrganizeSyncList } from '#/api/permission/organize';
|
import { create, getInfo, getPositionList, update } from '#/api/permission/position';
|
import { $t } from '#/locales';
|
|
interface State {
|
id: string;
|
selectedOrganize: any;
|
organizeTreeData: any[];
|
key: number;
|
}
|
|
const emit = defineEmits(['register', 'reload']);
|
const state = reactive<State>({
|
id: '',
|
selectedOrganize: { label: undefined, value: undefined },
|
organizeTreeData: [],
|
key: 0,
|
});
|
const constraintTypeOptions = [
|
{ fullName: '互斥约束', id: 0 },
|
{ fullName: '基数约束', id: 1 },
|
{ fullName: '先决约束', id: 2 },
|
];
|
const [registerForm, { setFieldsValue, resetFields, updateSchema, validate, clearValidate, getFieldsValue }] = useForm({
|
labelWidth: 100,
|
schemas: [
|
{
|
field: 'organizeId',
|
label: '所属组织',
|
component: 'TreeSelect',
|
slot: 'organizeId',
|
rules: [{ required: true, trigger: 'change', message: '必填' }],
|
},
|
{
|
field: 'parentId',
|
label: '上级岗位',
|
component: 'TreeSelect',
|
},
|
{
|
field: 'fullName',
|
label: '岗位名称',
|
component: 'Input',
|
componentProps: { maxlength: 50 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
field: 'enCode',
|
label: '岗位编码',
|
component: 'Input',
|
componentProps: { maxlength: 50 },
|
rules: [{ validator: formValidate('enCode', '只能输入英文、数字和小数点且小数点不能放在首尾'), trigger: 'blur' }],
|
},
|
{
|
field: 'isCondition',
|
label: '岗位约束',
|
component: 'Switch',
|
defaultValue: 0,
|
componentProps: { onChange: onIsConditionChange },
|
},
|
{
|
ifShow: ({ values }) => !!values.isCondition,
|
field: 'conditionJson.constraintType',
|
label: '约束类型',
|
component: 'Checkbox',
|
componentProps: { options: constraintTypeOptions },
|
rules: [{ required: true, trigger: 'change', message: '必填', type: 'array' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).includes(0) && !!values.isCondition,
|
field: 'conditionJson.mutualExclusion',
|
label: '互斥岗位',
|
component: 'PosSelect',
|
componentProps: { multiple: true },
|
rules: [{ required: true, trigger: 'change', message: '必填', type: 'array' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).includes(1) && !!values.isCondition,
|
field: 'conditionJson.userNum',
|
label: '用户基数',
|
helpMessage: '该岗位最多能添加几个用户',
|
component: 'InputNumber',
|
componentProps: { min: 1 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).includes(1) && !!values.isCondition,
|
field: 'conditionJson.permissionNum',
|
label: '权限基数',
|
helpMessage: '该岗位最多能添加几个菜单权限',
|
component: 'InputNumber',
|
componentProps: { min: 1 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).includes(2) && !!values.isCondition,
|
field: 'conditionJson.prerequisite',
|
label: '先决岗位',
|
component: 'PosSelect',
|
componentProps: { multiple: true },
|
rules: [{ required: true, trigger: 'change', message: '必填', type: 'array' }],
|
},
|
{
|
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')));
|
|
function init(data) {
|
resetFields();
|
state.id = data.id;
|
initOrganizeList();
|
if (state.id) {
|
changeLoading(true);
|
getInfo(state.id)
|
.then((res) => {
|
state.selectedOrganize = { label: res.data.organizeName, value: res.data.organizeId };
|
const conditionJson = res.data.conditionJson ? JSON.parse(res.data.conditionJson) : {};
|
const options = constraintTypeOptions.map((o) => ({ ...o, disabled: o.id == 2 ? !!res.data.isPrePosition : false }));
|
updateSchema([{ field: 'conditionJson.constraintType', componentProps: { options } }]);
|
setFieldsValue({ ...res.data, conditionJson });
|
initPositionList();
|
changeLoading(false);
|
})
|
.catch(() => {
|
changeLoading(false);
|
});
|
} else {
|
state.selectedOrganize = { label: data.organizeName, value: data.organizeId };
|
setFieldsValue({ organizeId: state.selectedOrganize.value });
|
initPositionList();
|
}
|
}
|
function initOrganizeList() {
|
getOrganizeSyncList({ parentId: '0' }).then((res) => {
|
state.organizeTreeData = res.data.list.map((o) => ({ id: o.id, fullName: o.fullName, isLeaf: o.isLeaf })) || [];
|
});
|
}
|
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 }));
|
state.organizeTreeData = [...state.organizeTreeData, ...list.filter((o) => !state.organizeTreeData.some((j) => o.id === j.id))];
|
resolve(true);
|
});
|
});
|
}
|
function onOrganizeChange({ value, label }) {
|
if (!value) return;
|
setFieldsValue({ organizeId: value, organizeName: label });
|
nextTick(() => {
|
clearValidate('organizeId');
|
setFieldsValue({ parentId: undefined, 'conditionJson.mutualExclusion': [], 'conditionJson.prerequisite': [] });
|
initPositionList();
|
});
|
}
|
function onIsConditionChange(val) {
|
if (val) return;
|
setFieldsValue({
|
'conditionJson.constraintType': [],
|
'conditionJson.mutualExclusion': [],
|
'conditionJson.prerequisite': [],
|
'conditionJson.userNum': undefined,
|
'conditionJson.permissionNum': undefined,
|
});
|
}
|
function initPositionList() {
|
if (!state.selectedOrganize.value) return;
|
getPositionList({ organizeId: state.selectedOrganize.value, defaultMark: 0 }).then((res) => {
|
const options = res.data.list || [];
|
updateSchema([{ field: 'parentId', componentProps: { options } }]);
|
});
|
}
|
async function handleSubmit() {
|
state.key = Date.now();
|
const values = await validate();
|
if (!values) return;
|
changeOkLoading(true);
|
const query = {
|
...values,
|
id: state.id,
|
conditionJson: JSON.stringify(getFieldsValue().conditionJson),
|
};
|
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 #organizeId>
|
<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.organizeTreeData"
|
:key="state.key"
|
:field-names="{ label: 'fullName', value: 'id' }"
|
:load-data="onLoadData"
|
@change="onOrganizeChange" />
|
</template>
|
</BasicForm>
|
</BasicModal>
|
</template>
|