edit.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <script lang="ts" setup>
  2. import type { BasicOptionResult } from '#/api/model';
  3. import { provide, reactive, ref, unref } from 'vue';
  4. import { useVbenModal } from '@vben/common-ui';
  5. import { message, Steps } from 'ant-design-vue';
  6. import { QueryApi, TableApi } from '#/api';
  7. import StepBaseConfig from './step/baseConfig.vue';
  8. import StepButtonConfig from './step/buttonConfig.vue';
  9. import StepColumnConfig from './step/columnConfig.vue';
  10. import StepSearchConfig from './step/searchConfig.vue';
  11. defineOptions({
  12. name: 'TableEdit',
  13. });
  14. const emit = defineEmits(['success']);
  15. const modelRef = ref<Record<string, any>>({});
  16. const isUpdate = ref(true);
  17. const stepBaseConfig = ref();
  18. const stepSearchConfig = ref();
  19. const stepColumnConfig = ref();
  20. const stepButtonConfig = ref();
  21. const state = reactive<{
  22. dbBaseOptions: BasicOptionResult[];
  23. stepIndex: number;
  24. stepItems: any[];
  25. stepStyle: Record<string, any>;
  26. }>({
  27. stepIndex: 0,
  28. stepItems: [
  29. { title: '基本信息', description: '基本数据设置' },
  30. { title: '条件设置', description: '查询条件设置' },
  31. { title: '列表设置', description: '显示列表设置' },
  32. { title: '按钮设置', description: '操作按钮设置' },
  33. ],
  34. stepStyle: { marginBottom: '20px' },
  35. dbBaseOptions: [],
  36. });
  37. const columns = ref<QueryApi.Column[]>([]);
  38. provide('page-columns', columns);
  39. const [Modal, { close, getData, setState }] = useVbenModal({
  40. fullscreenButton: false,
  41. fullscreen: true,
  42. draggable: true,
  43. cancelText: '上一步',
  44. confirmText: '下一步',
  45. onCancel() {
  46. if (state.stepIndex === 0) {
  47. close();
  48. } else {
  49. state.stepIndex -= 1;
  50. }
  51. setState({
  52. confirmText:
  53. state.stepIndex === state.stepItems.length - 1 ? '确定' : '下一步',
  54. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  55. });
  56. },
  57. onConfirm: async () => {
  58. setState({ confirmLoading: true });
  59. try {
  60. if (
  61. state.stepIndex === 0 &&
  62. (await stepBaseConfig.value.stepValidate())
  63. ) {
  64. const values = await stepBaseConfig.value.stepGetValues();
  65. columns.value = await QueryApi.getColumns(values.databaseQueryCode);
  66. Object.assign(modelRef.value, values);
  67. await stepSearchConfig.value.stepSetValues({ ...unref(modelRef) });
  68. state.stepIndex += 1;
  69. setState({
  70. confirmText:
  71. state.stepIndex === state.stepItems.length - 1 ? '确定' : '下一步',
  72. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  73. });
  74. } else if (
  75. state.stepIndex === 1 &&
  76. (await stepSearchConfig.value.stepValidate())
  77. ) {
  78. await stepColumnConfig.value.stepSetValues({ ...unref(modelRef) });
  79. state.stepIndex += 1;
  80. setState({
  81. confirmText:
  82. state.stepIndex === state.stepItems.length - 1 ? '确定' : '下一步',
  83. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  84. });
  85. } else if (
  86. state.stepIndex === 2 &&
  87. (await stepColumnConfig.value.stepValidate())
  88. ) {
  89. await stepButtonConfig.value.stepSetValues({ ...unref(modelRef) });
  90. state.stepIndex += 1;
  91. setState({
  92. confirmText:
  93. state.stepIndex === state.stepItems.length - 1 ? '确定' : '下一步',
  94. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  95. });
  96. } else if (
  97. state.stepIndex === 3 &&
  98. (await stepButtonConfig.value.stepValidate())
  99. ) {
  100. const searchConfig = await stepSearchConfig.value.stepGetValues();
  101. const columnConfig = await stepColumnConfig.value.stepGetValues();
  102. const buttomConfig = await stepButtonConfig.value.stepGetValues();
  103. const postParams = unref(modelRef);
  104. postParams.config = {
  105. search: searchConfig,
  106. columns: columnConfig,
  107. buttons: buttomConfig,
  108. };
  109. await (unref(isUpdate)
  110. ? TableApi.editDetail(postParams as TableApi.RecordItem)
  111. : TableApi.addDetail(postParams as TableApi.BasicRecordItem));
  112. message.success('操作成功');
  113. close();
  114. emit('success');
  115. }
  116. } catch (error: any) {
  117. message.error(error?.message || '操作失败');
  118. } finally {
  119. setState({ confirmLoading: false });
  120. }
  121. },
  122. onOpenChange: async (isOpen: boolean) => {
  123. if (isOpen) {
  124. setState({ loading: true });
  125. const data = getData<Record<string, any>>();
  126. isUpdate.value = !!data.isUpdate;
  127. modelRef.value = { ...data.baseData };
  128. setState({
  129. title: unref(isUpdate) ? '编辑页面' : '新增页面',
  130. cancelText: '取消',
  131. confirmText: '下一步',
  132. });
  133. state.stepIndex = 0;
  134. if (unref(isUpdate)) {
  135. const entity = await TableApi.getDetail(data.baseData.id);
  136. modelRef.value = { ...entity };
  137. stepBaseConfig.value.stepSetValues(entity);
  138. }
  139. setState({ loading: false });
  140. }
  141. },
  142. title: '新增页面',
  143. });
  144. </script>
  145. <template>
  146. <Modal class="w-[1000px]">
  147. <div class="h-full">
  148. <Steps
  149. :current="state.stepIndex"
  150. :items="state.stepItems"
  151. :percent="60"
  152. :style="state.stepStyle"
  153. />
  154. <div style="height: calc(100% - 82px)">
  155. <StepBaseConfig v-show="state.stepIndex === 0" ref="stepBaseConfig" />
  156. <StepSearchConfig
  157. v-show="state.stepIndex === 1"
  158. ref="stepSearchConfig"
  159. />
  160. <StepColumnConfig
  161. v-show="state.stepIndex === 2"
  162. ref="stepColumnConfig"
  163. />
  164. <StepButtonConfig
  165. v-show="state.stepIndex === 3"
  166. ref="stepButtonConfig"
  167. />
  168. </div>
  169. </div>
  170. </Modal>
  171. </template>