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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<script lang="ts" setup>
import { reactive, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import { useDebounceFn } from '@vueuse/core';
import { cloneDeep } from 'lodash-es';
 
import { getDataInterfaceInfo } from '#/api/systemData/dataInterface';
import { InterfaceModal } from '#/components/CommonModal';
import { sourceTypeOptions, templateJsonColumns } from '#/utils/define';
 
interface State {
  dataForm: any;
  formFieldsOptions: any[];
  allOptions: any[];
  options: any[];
  type: string;
}
 
const emit = defineEmits(['register', 'confirm']);
const state = reactive<State>({
  dataForm: {},
  formFieldsOptions: [],
  allOptions: [],
  options: [],
  type: 'add',
});
const { dataForm, formFieldsOptions, options, type } = toRefs(state);
const [registerAuxiliaryModal, { closeModal }] = useModalInner(init);
const columns = [
  { width: 50, title: '序号', align: 'center', customRender: ({ index }) => index + 1 },
  { title: '列名', dataIndex: 'label', key: 'label', width: 150 },
  { title: '字段', dataIndex: 'value', key: 'value' },
  { title: '宽度', dataIndex: 'width', key: 'width', width: 100 },
  { title: '显示', dataIndex: 'ifShow', key: 'ifShow', width: 50, align: 'center' },
  { title: '操作', dataIndex: 'action', key: 'action', width: 50 },
];
 
const debounceOnSearch = useDebounceFn(onSearch, 300);
const { createMessage } = useMessage();
function init(data) {
  state.type = data.type || 'add';
  state.dataForm = cloneDeep(data.config);
  state.formFieldsOptions = data.formFieldsOptions || [];
}
 
function getSourceTypeOptions(isRequired) {
  return isRequired ? sourceTypeOptions.filter((o) => o.id != 3 && o.id != 4) : sourceTypeOptions.filter((o) => o.id != 4);
}
function onInterfaceChange(val, row) {
  if (!val) {
    state.dataForm.interfaceId = '';
    state.dataForm.interfaceName = '';
    state.dataForm.templateJson = [];
    initFieldData();
    return;
  }
  if (state.dataForm.interfaceId === val) return;
  state.dataForm.interfaceId = val;
  state.dataForm.interfaceName = row.fullName;
  state.dataForm.templateJson = row.templateJson.map((o) => ({ ...o, sourceType: 1 })) || [];
  initFieldData();
}
 
function onSearch(searchText: string) {
  state.options = state.allOptions.filter((o) => o.value.toLowerCase().includes(searchText.toLowerCase()));
}
function onFocus(searchText) {
  onSearch(searchText);
}
function initFieldData() {
  if (!state.dataForm.interfaceId) return (state.allOptions = []);
  getDataInterfaceInfo(state.dataForm.interfaceId).then((res) => {
    const data = res.data;
    const list = data.fieldJson ? JSON.parse(data.fieldJson) : [];
    state.allOptions = list.map((o) => ({ ...o, value: o.defaultValue }));
  });
}
function handleDelItem(index) {
  state.dataForm.columnOptions.splice(index, 1);
}
function handleAddColumnOption() {
  (state.dataForm.columnOptions as any).push({
    value: '',
    label: '',
    width: null,
    ifShow: true,
  });
}
function exist() {
  let isOk = true;
  for (let i = 0; i < state.dataForm.columnOptions?.length; i++) {
    const e = state.dataForm.columnOptions[i];
    if (!e.value) {
      createMessage.error('列表字段中字段不能为空');
      isOk = false;
      break;
    }
  }
  return isOk;
}
function handleSubmit() {
  if (!state.dataForm.interfaceId) return createMessage.warning('请选择数据接口');
  if (!exist()) return;
  emit('confirm', state.dataForm);
  closeModal();
}
</script>
<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerAuxiliaryModal"
    :title="type == 'add' ? '新增' : '编辑'"
    :width="600"
    @ok="handleSubmit"
    class="add-config-modal"
    destroy-on-close>
    <a-form :colon="false" label-align="right" :model="dataForm" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="数据接口">
        <InterfaceModal :value="dataForm.interfaceId" :title="dataForm.interfaceName" :source-type="1" @change="onInterfaceChange" />
      </a-form-item>
      <a-form-item label="参数设置" style="margin-bottom: 0" />
      <a-table :data-source="dataForm.templateJson" :columns="templateJsonColumns" size="small" :pagination="false">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'field'">
            <span class="required-sign">{{ record.required ? '*' : '' }}</span>
            {{ record.field }}{{ record.fieldName ? `(${record.fieldName})` : '' }}
          </template>
          <template v-if="column.key === 'sourceType'">
            <jnpf-select
              v-model:value="record.sourceType"
              :options="getSourceTypeOptions(record.required)"
              class="!w-[100px]"
              @change="record.relationField = ''" />
          </template>
          <template v-if="column.key === 'relationField'">
            <jnpf-select
              v-model:value="record.relationField"
              placeholder="请选择"
              :options="formFieldsOptions"
              :field-names="{ options: 'options1' }"
              allow-clear
              show-search
              class="!w-[204px]"
              v-if="record.sourceType === 1" />
            <template v-else-if="record.sourceType == 2">
              <jnpf-input-number v-if="['int', 'decimal'].includes(record.dataType)" v-model:value="record.relationField" placeholder="请输入" allow-clear />
              <jnpf-date-picker
                v-else-if="record.dataType == 'datetime'"
                class="!w-full"
                v-model:value="record.relationField"
                placeholder="请选择"
                format="YYYY-MM-DD HH:mm:ss"
                allow-clear />
              <a-input v-else v-model:value="record.relationField" placeholder="请输入" allow-clear />
            </template>
          </template>
        </template>
        <template #emptyText>
          <p class="leading-[60px]">暂无数据</p>
        </template>
      </a-table>
      <a-form-item label="设置列表字段" style="margin-bottom: 0" :label-col="{ style: { width: '100px' } }" />
      <a-table :data-source="dataForm.columnOptions" :columns="columns" size="small" :pagination="false">
        <template #headerCell="{ column }">
          <template v-if="column.key === 'width'">{{ column.title }}<BasicHelp text="移动端不支持宽度设置" /></template>
        </template>
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.key === 'label'">
            <a-input v-model:value="record.label" placeholder="请输入" />
          </template>
          <template v-if="column.key === 'value'">
            <a-auto-complete
              v-model:value="record.value"
              placeholder="请输入"
              :options="options"
              @focus="onFocus(record.value)"
              @search="debounceOnSearch(record.value)" />
          </template>
          <template v-if="column.key === 'width'">
            <a-input-number v-model:value="record.width" placeholder="请输入" :min="0" :precision="0" />
          </template>
          <template v-if="column.key === 'ifShow'">
            <a-checkbox v-model:checked="record.ifShow" />
          </template>
          <template v-if="column.key === 'action'">
            <a-button class="action-btn" type="link" color="error" @click="handleDelItem(index)" size="small">删除</a-button>
          </template>
        </template>
        <template #emptyText>
          <p class="leading-[60px]">暂无数据</p>
        </template>
      </a-table>
      <div class="table-add-action" @click="handleAddColumnOption()">
        <a-button type="link" pre-icon="icon-ym icon-ym-btn-add">新增</a-button>
      </div>
    </a-form>
  </BasicModal>
</template>