<script lang="ts" setup>
|
import { computed, 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 { create, getInfo, update } from '#/api/permission/role';
|
import { $t } from '#/locales';
|
import { useOrganizeStore } from '#/store';
|
|
interface State {
|
id: string;
|
type: string;
|
}
|
|
const emit = defineEmits(['register', 'reload']);
|
const state = reactive<State>({
|
id: '',
|
type: '',
|
});
|
const organizeStore = useOrganizeStore();
|
const constraintTypeOptions = [
|
{ fullName: '互斥约束', id: 0 },
|
{ fullName: '基数约束', id: 1 },
|
{ fullName: '先决约束', id: 2 },
|
];
|
const [registerForm, { setFieldsValue, resetFields, updateSchema, validate, getFieldsValue }] = useForm({
|
labelWidth: 100,
|
schemas: [
|
{
|
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' }],
|
},
|
{
|
ifShow: () => state.type == 'user',
|
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'] || []).some((o) => o == 0) && !!values.isCondition,
|
field: 'conditionJson.mutualExclusion',
|
label: '互斥角色',
|
component: 'RoleSelect',
|
componentProps: { multiple: true },
|
rules: [{ required: true, trigger: 'change', message: '必填', type: 'array' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).some((o) => o == 1) && !!values.isCondition,
|
field: 'conditionJson.userNum',
|
label: '用户基数',
|
helpMessage: '该角色最多能添加几个用户',
|
component: 'InputNumber',
|
componentProps: { min: 1 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).some((o) => o == 1) && !!values.isCondition,
|
field: 'conditionJson.permissionNum',
|
label: '权限基数',
|
helpMessage: '该角色最多能添加几个菜单权限',
|
component: 'InputNumber',
|
componentProps: { min: 1 },
|
rules: [{ required: true, trigger: 'blur', message: '必填' }],
|
},
|
{
|
ifShow: ({ values }) => (values['conditionJson.constraintType'] || []).some((o) => o == 2) && !!values.isCondition,
|
field: 'conditionJson.prerequisite',
|
label: '先决角色',
|
component: 'RoleSelect',
|
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(() => {
|
const typeTile = state.type == 'user' ? '用户' : state.type == 'organize' ? '组织' : '岗位';
|
return `${(state.id ? $t('common.editText') : $t('common.addText')) + typeTile}角色`;
|
});
|
|
function init(data) {
|
resetFields();
|
state.id = data.id;
|
state.type = data.type || 'user';
|
if (state.id) {
|
changeLoading(true);
|
getInfo(state.id)
|
.then((res) => {
|
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 });
|
changeLoading(false);
|
})
|
.catch(() => {
|
changeLoading(false);
|
});
|
}
|
}
|
function onIsConditionChange(val) {
|
if (val) return;
|
setFieldsValue({
|
'conditionJson.constraintType': [],
|
'conditionJson.mutualExclusion': [],
|
'conditionJson.prerequisite': [],
|
'conditionJson.userNum': undefined,
|
'conditionJson.permissionNum': undefined,
|
});
|
}
|
async function handleSubmit() {
|
const values = await validate();
|
if (!values) return;
|
changeOkLoading(true);
|
const query = {
|
...values,
|
id: state.id,
|
conditionJson: JSON.stringify(getFieldsValue().conditionJson),
|
type: state.type,
|
};
|
const formMethod = state.id ? update : create;
|
formMethod(query)
|
.then((res) => {
|
createMessage.success(res.msg);
|
changeOkLoading(false);
|
organizeStore.$reset();
|
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" />
|
</BasicModal>
|
</template>
|