stepSearchConfig.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <div class="h-full list-config">
  3. <BasicTable @register="registerTable">
  4. <template #toolbar>
  5. <a-button type="dashed" @click="addDetailRow()"> 添加 </a-button>
  6. </template>
  7. <template #headerCell="{ column }">
  8. <template v-if="column.key === 'sort'">
  9. <Icon size="14" icon="mingcute:move-line" />
  10. </template>
  11. </template>
  12. <template #bodyCell="{ column, record, index }">
  13. <template v-if="column.key === 'action'">
  14. <TableAction
  15. :actions="[
  16. {
  17. label: '删除',
  18. color: 'error',
  19. onClick: handleDelete.bind(null, index),
  20. },
  21. ]"
  22. />
  23. </template>
  24. <template v-if="column.key === 'field'">
  25. <Select
  26. style="width: 100%"
  27. v-model:value="record[column.key]"
  28. placeholder="请选择字段"
  29. show-search
  30. :filterOption="filterOption"
  31. @change="handleChange(index, $event)"
  32. :options="ledgerConfig.tableFieldConfigs"
  33. :fieldNames="{ label: 'fieldComment', value: 'fieldName' }"
  34. />
  35. </template>
  36. <template v-if="column.key === 'label'">
  37. <a-input v-model:value="record[column.key]" placeholder="输入显示名称" />
  38. </template>
  39. <template v-if="column.key === 'component'">
  40. <Select
  41. style="width: 100%"
  42. v-model:value="record[column.key]"
  43. placeholder="请控件类型"
  44. :options="fieldTypeList"
  45. />
  46. </template>
  47. <template v-if="column.key === 'bindObject'">
  48. <template v-if="record['component'] === 'ApiSelect'">
  49. <Select
  50. style="width: 100%"
  51. v-model:value="record[column.key]"
  52. placeholder="绑定对象"
  53. show-search
  54. :options="bindObjectList"
  55. />
  56. </template>
  57. <template v-else>
  58. <div></div>
  59. </template>
  60. </template>
  61. <template v-if="column.key === 'bindValue'">
  62. <template v-if="record['bindObject'] === 'api' && record['component'] === 'ApiSelect'">
  63. <a-input
  64. v-model:value="record['apiInfo'].path"
  65. placeholder="点击进行接口配置"
  66. @click="showApiConfig(index)"
  67. >
  68. <template #suffix>
  69. <Icon icon="ant-design:ellipsis-outlined" />
  70. </template>
  71. </a-input>
  72. </template>
  73. <template
  74. v-else-if="record['bindObject'] === 'dic' && record['component'] === 'ApiSelect'"
  75. >
  76. <DicTreeSelect v-model:value="record[column.key]" @change="handleDicChange" />
  77. </template>
  78. <template v-else>
  79. <div></div>
  80. </template>
  81. </template>
  82. <template v-if="column.key === 'defaultValue'">
  83. <a-input v-model:value="record[column.key]" placeholder="输入默认值" />
  84. </template>
  85. <template v-if="column.key === 'sort'">
  86. <Icon size="14" class="columnDraggable-icon" icon="tabler:arrows-move-vertical" />
  87. </template>
  88. </template>
  89. </BasicTable>
  90. <ApiConfig
  91. v-if="apiConfigDialog"
  92. v-model:apiConfigDialog="apiConfigDialog"
  93. v-model:apiConfig="apiConfigValue"
  94. :isCascader="isCascader"
  95. :isQrcode="isQrcode"
  96. :isSubForm="true"
  97. :title="isCascader ? '级联配置-API' : 'API配置'"
  98. @success="handelSuccess"
  99. />
  100. </div>
  101. </template>
  102. <script lang="ts" setup>
  103. import { Ref, inject, ref, unref, computed, provide, onMounted, nextTick, watch } from 'vue';
  104. import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
  105. import { LedgerConfig, LedgerSerachConfigs } from '/@/model/generator/ledgerConfig';
  106. import { buildUUID } from '/@/utils/uuid';
  107. import { Select } from 'ant-design-vue';
  108. import { useMessage } from '/@/hooks/web/useMessage';
  109. import { ApiConfig } from '/@/components/ApiConfig';
  110. import { Icon } from '/@/components/Icon';
  111. import { DicTreeSelect } from '/@/components/DicTreeSelect';
  112. import { getDicDetailList } from '/@/api/system/dic';
  113. import { cloneDeep } from 'lodash-es';
  114. import Sortable from 'sortablejs';
  115. import { isNullAndUnDef } from '/@/utils/is';
  116. const ledgerConfig = inject<LedgerConfig>('ledgerConfig') as LedgerConfig;
  117. const { createMessage, notification } = useMessage();
  118. const columns: BasicColumn[] = [
  119. {
  120. title: '排序',
  121. dataIndex: 'sort',
  122. width: 40,
  123. },
  124. {
  125. title: '参数字段',
  126. dataIndex: 'field',
  127. align: 'left',
  128. },
  129. {
  130. title: '显示名称',
  131. dataIndex: 'label',
  132. align: 'left',
  133. width: 250,
  134. },
  135. {
  136. title: '控件类型',
  137. dataIndex: 'component',
  138. align: 'left',
  139. width: 150,
  140. },
  141. {
  142. title: '绑定对象',
  143. dataIndex: 'bindObject',
  144. align: 'left',
  145. width: 150,
  146. },
  147. {
  148. title: '绑定值',
  149. dataIndex: 'bindValue',
  150. align: 'left',
  151. width: 250,
  152. },
  153. {
  154. title: '默认值',
  155. dataIndex: 'defaultValue',
  156. align: 'left',
  157. width: 150,
  158. },
  159. {
  160. title: '操作',
  161. dataIndex: 'action',
  162. align: 'left',
  163. width: 120,
  164. fixed: 'right',
  165. },
  166. ];
  167. const fieldTypeList = [
  168. {
  169. label: '文本',
  170. value: 'Input',
  171. },
  172. {
  173. label: '下拉框',
  174. value: 'ApiSelect',
  175. },
  176. {
  177. label: '日期',
  178. value: 'DatePicker',
  179. },
  180. ];
  181. const bindObjectList = [
  182. {
  183. label: '接口',
  184. value: 'api',
  185. },
  186. {
  187. label: '字典',
  188. value: 'dic',
  189. },
  190. ];
  191. const dataSource: Ref<LedgerSerachConfigs[]> = ref([]);
  192. const [registerTable] = useTable({
  193. title: '查询条件配置',
  194. rowKey: 'key',
  195. columns: columns,
  196. dataSource: dataSource,
  197. useSearchForm: false,
  198. showTableSetting: true,
  199. bordered: true,
  200. immediate: false,
  201. canResize: true,
  202. showIndexColumn: false,
  203. pagination: false,
  204. });
  205. const apiConfigDialog = ref<boolean>(false);
  206. const isCascader = ref<boolean>(false);
  207. const isQrcode = ref<boolean>(false);
  208. const apiConfigValue = ref<Recordable>({});
  209. const editIndex = ref(-1);
  210. const remoteDefaultOptions = ref<Recordable[]>([]);
  211. const widgetForm = computed(() => {
  212. const list = dataSource.value.map((item) => {
  213. return {
  214. key: buildUUID(),
  215. bindField: item.field,
  216. label: item.label,
  217. type: item.component,
  218. };
  219. });
  220. return { list: list };
  221. });
  222. provide('widgetForm', widgetForm);
  223. const showApiConfig = (index: number) => {
  224. apiConfigDialog.value = true;
  225. isCascader.value = false;
  226. apiConfigValue.value = dataSource.value[index].apiInfo;
  227. editIndex.value = index;
  228. };
  229. const handleDelete = (index: number) => {
  230. dataSource.value.splice(index, 1);
  231. };
  232. const validateStep = () => {
  233. try {
  234. const tableConfigs = unref(dataSource);
  235. if (tableConfigs!.length === 0) {
  236. notification.error({
  237. message: '提示',
  238. description: '数据表配置不能为空!',
  239. });
  240. return false;
  241. }
  242. for (const config of tableConfigs!) {
  243. if (!config.field) {
  244. notification.error({
  245. message: '提示',
  246. description: '列表字段未选择!',
  247. });
  248. return false;
  249. }
  250. if (!config.label) {
  251. notification.error({
  252. message: '提示',
  253. description: '显示名称未设置!',
  254. });
  255. return false;
  256. }
  257. }
  258. ledgerConfig.serachConfigs = cloneDeep(tableConfigs);
  259. } catch {
  260. return false;
  261. }
  262. return true;
  263. };
  264. const filterOption = (input: string, option: any) => {
  265. return (
  266. option.fieldName.toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
  267. option.fieldComment.toLowerCase().indexOf(input.toLowerCase()) >= 0
  268. );
  269. };
  270. const handleChange = (index: number, value: string) => {
  271. if (dataSource.value.filter((item) => item.field === value).length > 1) {
  272. createMessage.error('字段已经存在!');
  273. dataSource.value[index].field = '';
  274. } else {
  275. const filterItem = ledgerConfig?.tableFieldConfigs.filter((item) => item.fieldName === value);
  276. if (filterItem?.length > 0) {
  277. dataSource.value[index].label = filterItem[0].fieldComment;
  278. }
  279. }
  280. };
  281. const handleDicChange = async (value: string) => {
  282. const result = await getDicDetailList({ itemId: value });
  283. remoteDefaultOptions.value = result.map((item) => ({
  284. label: item.name,
  285. value: item.id,
  286. }));
  287. };
  288. const addDetailRow = () => {
  289. const newData: LedgerSerachConfigs = {
  290. key: buildUUID(),
  291. field: '',
  292. label: '',
  293. component: 'Input',
  294. bindObject: '',
  295. sort: 10,
  296. bindValue: '',
  297. defaultValue: '',
  298. apiInfo: { path: '' },
  299. };
  300. dataSource.value.push(newData);
  301. };
  302. const setTableData = (ledgerSerachConfigs: LedgerSerachConfigs[]) => {
  303. dataSource.value = cloneDeep(ledgerSerachConfigs);
  304. };
  305. const handelSuccess = (data) => {
  306. dataSource.value[editIndex.value].apiInfo = data;
  307. };
  308. const setDefaultData = (values) => {
  309. const data: LedgerSerachConfigs[] = [];
  310. values.forEach((item) => {
  311. const filterItem = ledgerConfig.tableFieldConfigs.filter((fl) => fl.fieldName === item.name);
  312. if (filterItem.length > 0) {
  313. const dataRow: LedgerSerachConfigs = {
  314. key: buildUUID(),
  315. field: item.name,
  316. label: filterItem[0].fieldComment,
  317. component: 'Input',
  318. bindObject: '',
  319. sort: 10,
  320. bindValue: '',
  321. defaultValue: item.defaultValue,
  322. apiInfo: { path: '' },
  323. };
  324. data.push(dataRow);
  325. }
  326. });
  327. if (data.length > 0) {
  328. dataSource.value = cloneDeep(data);
  329. }
  330. };
  331. watch(
  332. () => ledgerConfig?.apiInfo?.apiParams,
  333. (val) => {
  334. if (val && val[0]?.tableInfo && val[0]?.tableInfo.length) {
  335. setDefaultData(val[0]?.tableInfo);
  336. }
  337. },
  338. {
  339. deep: true,
  340. immediate: true,
  341. },
  342. );
  343. onMounted(() => {
  344. nextTick(() => {
  345. const tbody: any = document.querySelector('.list-config .ant-table-tbody');
  346. Sortable.create(tbody, {
  347. handle: '.columnDraggable-icon',
  348. onEnd: ({ oldIndex, newIndex }) => {
  349. if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || newIndex === oldIndex) {
  350. return;
  351. }
  352. const columns = cloneDeep(dataSource.value);
  353. if (oldIndex > newIndex) {
  354. columns.splice(newIndex - 1, 0, columns[oldIndex - 1]);
  355. columns.splice(oldIndex, 1);
  356. } else {
  357. columns.splice(newIndex - 1, 0, columns[oldIndex - 1]);
  358. columns.splice(oldIndex - 1, 1);
  359. }
  360. dataSource.value = cloneDeep(columns);
  361. },
  362. });
  363. });
  364. });
  365. defineExpose({ validateStep, setTableData });
  366. </script>
  367. <style lang="less" scoped>
  368. .icon {
  369. width: 1em;
  370. height: 1em;
  371. vertical-align: -0.15em;
  372. fill: currentcolor;
  373. overflow: hidden;
  374. }
  375. </style>