刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { computed, inject, unref } from 'vue';
 
import { buildBitUUID } from '@jnpf/utils';
 
import { getDataInterfaceRes } from '#/api/systemData/dataInterface';
import { getDictionaryDataSelector } from '#/api/systemData/dictionary';
import { noAllowRelationList, noAllowSelectList } from '#/components/FormGenerator/src/helper/config';
import { getParamList } from '#/utils/jnpf';
 
export function useDynamic(activeData, initFieldData?) {
  const dataTypeOptions = [
    { id: 'static', fullName: '静态数据' },
    { id: 'dictionary', fullName: '数据字典' },
    { id: 'dynamic', fullName: '数据接口' },
    { id: 'formData', fullName: '表单数据' },
  ];
  const valueOptions = [
    { id: 'id', fullName: 'id' },
    { id: 'enCode', fullName: 'enCode' },
  ];
  const sizeOptions = [
    { id: 'large', fullName: '较大' },
    { id: 'default', fullName: '中等' },
    { id: 'small', fullName: '较小' },
  ];
  const optionTypeOptions = [
    { id: 'default', fullName: '默认' },
    { id: 'button', fullName: '按钮' },
  ];
  const getShowType: (() => string | undefined) | undefined = inject('getShowType');
  const showType = computed(() => (getShowType as () => string | undefined)());
  const getDrawingList: (() => any[]) | undefined = inject('getDrawingList');
 
  const isIncludesTable = (data) => {
    if ((!data.__config__.layout || data.__config__.layout === 'rowFormItem') && data.__config__.jnpfKey !== 'table') return true;
    if (activeData.__config__.isSubTable) return activeData.__config__.parentVModel === data.__vModel__;
    return data.__config__.jnpfKey !== 'table';
  };
  const drawingList = computed(() => (getDrawingList as () => any[])());
  const defaultValue = computed(() => {
    if (activeData.__config__.jnpfKey === 'checkbox' || activeData.__config__.jnpfKey === 'cascader' || activeData.multiple) return [];
    return '';
  });
  const formFieldsOptions = computed(() => {
    const list: any[] = [];
    const loop = (data, parent?) => {
      if (!data) return;
      if (data.__config__ && isIncludesTable(data) && data.__config__.children && Array.isArray(data.__config__.children)) {
        loop(data.__config__.children, data);
      }
      if (Array.isArray(data)) data.forEach((d) => loop(d, parent));
      if (data.__vModel__ && !noAllowRelationList.includes(data.__config__.jnpfKey) && data.__vModel__ !== activeData.__vModel__) {
        const isTableChild = parent && parent.__config__ && parent.__config__.jnpfKey === 'table';
        list.push({
          id: isTableChild ? `${parent.__vModel__}-${data.__vModel__}` : data.__vModel__,
          fullName: isTableChild ? `${parent.__config__.label}-${data.__config__.label}` : data.__config__.label,
          ...data,
          disabled: false,
        });
      }
    };
    loop(unref(drawingList));
    return list;
  });
 
  const formFieldsSelectOptions = computed(() => {
    const list: any[] = [];
    const loop = (data, parent?) => {
      if (!data) return;
      if (data.__config__ && isIncludesTable(data) && data.__config__.children && Array.isArray(data.__config__.children)) {
        loop(data.__config__.children, data);
      }
      if (Array.isArray(data)) data.forEach((d) => loop(d, parent));
      if (data.__vModel__ && !noAllowSelectList.includes(data.__config__.jnpfKey) && data.__vModel__ !== activeData.__vModel__) {
        const isTableChild = parent && parent.__config__ && parent.__config__.jnpfKey === 'table';
        list.push({
          id: isTableChild ? `${parent.__vModel__}-${data.__vModel__}` : data.__vModel__,
          fullName: isTableChild ? `${parent.__config__.label}-${data.__config__.label}` : data.__config__.label,
          ...data,
          disabled: false,
        });
      }
    };
    loop(unref(drawingList));
    return list;
  });
 
  const formDataTableOptions = computed(() => {
    const list: any[] = [];
    const loop = (data) => {
      if (!data) return;
      if (Array.isArray(data)) {
        data.forEach(loop);
        return;
      }
      const config = data.__config__;
      if (config?.jnpfKey === 'table' && data.__vModel__ && data.__vModel__ !== activeData.__config__.parentVModel) {
        list.push({ id: data.__vModel__, fullName: config.label });
      }
      if (config?.jnpfKey !== 'table' && Array.isArray(config?.children)) loop(config.children);
    };
    loop(unref(drawingList));
    return list;
  });
 
  const formDataFieldOptions = computed(() => {
    const findTable = (data): any => {
      if (!data) return undefined;
      if (Array.isArray(data)) {
        for (const item of data) {
          const table = findTable(item);
          if (table) return table;
        }
        return undefined;
      }
      if (data.__config__?.jnpfKey === 'table' && data.__vModel__ === activeData.__config__.formDataSource) return data;
      if (data.__config__?.jnpfKey !== 'table') return findTable(data.__config__?.children);
      return undefined;
    };
    const table = findTable(unref(drawingList));
    return (table?.__config__?.children || []).filter((item) => item.__vModel__).map((item) => ({ id: item.__vModel__, fullName: item.__config__.label }));
  });
 
  const formFieldsTransferOptions = computed(() => {
    const list: any[] = [];
    const loop = (data, parent?) => {
      if (!data) return;
      if (data.__config__ && isIncludesTable(data) && data.__config__.children && Array.isArray(data.__config__.children)) {
        loop(data.__config__.children, data);
      }
      if (Array.isArray(data)) data.forEach((d) => loop(d, parent));
      if (data.__vModel__ && !noAllowRelationList.includes(data.__config__.jnpfKey) && data.__vModel__ !== activeData.__vModel__) {
        const isTableChild = parent && parent.__config__ && parent.__config__.jnpfKey === 'table';
        list.push({
          id: isTableChild ? `${parent.__vModel__}-${data.__vModel__}` : data.__vModel__,
          fullName: isTableChild ? `${parent.__config__.label}-${data.__config__.label}` : data.__config__.label,
          ...data,
          disabled: false,
        });
      }
    };
    loop(unref(drawingList));
    return list;
  });
 
  const onDataTypeChange = () => {
    activeData.__config__.defaultValue = unref(defaultValue);
    activeData.options = [];
    activeData.props.value = 'id';
    activeData.props.label = 'fullName';
    if (Reflect.has(activeData.props, 'children')) activeData.props.children = 'children';
    activeData.__config__.dictionaryType = '';
    activeData.__config__.propsUrl = '';
    activeData.__config__.propsName = '';
    activeData.__config__.formDataSource = '';
    activeData.__config__.templateJson = [];
  };
  const onFormDataSourceChange = () => {
    activeData.__config__.defaultValue = unref(defaultValue);
    activeData.options = [];
    activeData.props.value = '';
    activeData.props.label = '';
  };
  const onDictionaryTypeChange = (val) => {
    activeData.__config__.defaultValue = unref(defaultValue);
    if (!val) return (activeData.options = []);
    getDictionaryDataSelector(val)
      .then((res) => {
        activeData.options = res.data.list;
      })
      .catch(() => {
        activeData.options = [];
      });
  };
  const onPropsUrlChange = (val, row) => {
    activeData.__config__.defaultValue = unref(defaultValue);
    if (!val) {
      activeData.__config__.propsUrl = '';
      activeData.__config__.propsName = '';
      activeData.__config__.templateJson = [];
      activeData.options = [];
      initFieldData && initFieldData();
      return;
    }
    if (activeData.__config__.propsUrl === val) return;
    const list = row.parameterJson ? JSON.parse(row.parameterJson) : [];
    activeData.__config__.propsUrl = val;
    activeData.__config__.propsName = row.fullName;
    activeData.__config__.templateJson = list.map((o) => ({ ...o, relationField: '', sourceType: 1 }));
    const query = { paramList: getParamList(activeData.__config__.templateJson) };
    getDataInterfaceRes(val, query)
      .then((res) => {
        activeData.options = Array.isArray(res.data) ? res.data : [];
        initFieldData && initFieldData();
      })
      .catch(() => {
        activeData.__config__.propsUrl = '';
        activeData.__config__.propsName = '';
        activeData.__config__.templateJson = [];
        activeData.options = [];
        initFieldData && initFieldData();
      });
  };
  const onRelationFieldChange = (val, row) => {
    if (!val) return (row.jnpfKey = '');
    const list = unref(formFieldsOptions).filter((o) => o.id === val);
    if (!list.length) return (row.jnpfKey = '');
    const item = list[0];
    row.jnpfKey = item.__config__.jnpfKey;
  };
  const onMultipleChange = (val) => {
    activeData.__config__.defaultValue = val ? [] : '';
  };
  const addSelectItem = () => {
    const uuid = buildBitUUID();
    activeData.options.push({
      fullName: `选项${uuid}`,
      id: uuid,
    });
  };
  const onBatchOperateConfirm = (options) => {
    activeData.options = options;
  };
 
  return {
    showType,
    onDataTypeChange,
    onDictionaryTypeChange,
    onPropsUrlChange,
    formFieldsOptions,
    formFieldsSelectOptions,
    formDataTableOptions,
    formDataFieldOptions,
    formFieldsTransferOptions,
    onRelationFieldChange,
    onFormDataSourceChange,
    sizeOptions,
    optionTypeOptions,
    dataTypeOptions,
    valueOptions,
    addSelectItem,
    onMultipleChange,
    onBatchOperateConfirm,
  };
}