ny
昨天 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
179
180
181
182
183
184
185
186
187
<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>