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
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: '数据接口' },
  ];
  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 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__.templateJson = [];
  };
  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,
    onRelationFieldChange,
    sizeOptions,
    optionTypeOptions,
    dataTypeOptions,
    valueOptions,
    addSelectItem,
    onMultipleChange,
    onBatchOperateConfirm,
  };
}