ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<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>