edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <script lang="ts" setup>
  2. import type { BasicOptionResult } from '#/api/model';
  3. import { reactive, ref, unref } from 'vue';
  4. import { useVbenModal } from '@vben/common-ui';
  5. import { message, Steps } from 'ant-design-vue';
  6. import { QueryApi } from '#/api';
  7. import StepBaseConfig from './stepBaseConfig.vue';
  8. import StepDetailConfig from './stepDetailConfig.vue';
  9. defineOptions({
  10. name: 'QueryEdit',
  11. });
  12. const emit = defineEmits(['success']);
  13. const modelRef = ref<Record<string, any>>({});
  14. const isUpdate = ref(true);
  15. const stepBaseConfig = ref(null);
  16. const stepDetailConfig = ref(null);
  17. const state = reactive<{
  18. dbBaseOptions: BasicOptionResult[];
  19. stepIndex: number;
  20. stepItems: any[];
  21. stepStyle: Record<string, any>;
  22. }>({
  23. stepIndex: 0,
  24. stepItems: [
  25. { title: '基本信息', description: '基本数据设置' },
  26. { title: '查询设置', description: '查询列表设置' },
  27. ],
  28. stepStyle: { marginBottom: '20px' },
  29. dbBaseOptions: [],
  30. });
  31. const [Modal, { close, getData, setState }] = useVbenModal({
  32. fullscreenButton: false,
  33. fullscreen: true,
  34. draggable: true,
  35. cancelText: '上一步',
  36. confirmText: '下一步',
  37. onCancel() {
  38. if (state.stepIndex === 0) {
  39. close();
  40. } else {
  41. state.stepIndex -= 1;
  42. }
  43. setState({
  44. confirmText: state.stepIndex === 0 ? '下一步' : '确定',
  45. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  46. });
  47. },
  48. onConfirm: async () => {
  49. setState({ confirmLoading: true });
  50. try {
  51. if (
  52. state.stepIndex === 0 &&
  53. (await stepBaseConfig.value.stepValidate())
  54. ) {
  55. const values = await stepBaseConfig.value.stepGetValues();
  56. Object.assign(modelRef.value, values);
  57. await stepDetailConfig.value.stepSetValues({ ...unref(modelRef) });
  58. state.stepIndex += 1;
  59. setState({
  60. confirmText: state.stepIndex === 0 ? '下一步' : '确定',
  61. cancelText: state.stepIndex === 0 ? '取消' : '上一步',
  62. });
  63. } else if (
  64. state.stepIndex === 1 &&
  65. (await stepDetailConfig.value.stepValidate())
  66. ) {
  67. const { tables, columns } =
  68. await stepDetailConfig.value.stepGetValues();
  69. const postParams = unref(modelRef);
  70. postParams.tables = tables;
  71. postParams.columns = columns;
  72. await (unref(isUpdate)
  73. ? QueryApi.editDetail(postParams as QueryApi.RecordItem)
  74. : QueryApi.addDetail(postParams as QueryApi.BasicRecordItem));
  75. message.success('操作成功');
  76. close();
  77. emit('success');
  78. }
  79. } catch (error) {
  80. message.error(error?.message || '操作失败');
  81. } finally {
  82. setState({ confirmLoading: false });
  83. }
  84. },
  85. onOpenChange: async (isOpen: boolean) => {
  86. if (isOpen) {
  87. setState({ loading: true });
  88. const data = getData<Record<string, any>>();
  89. isUpdate.value = !!data.isUpdate;
  90. modelRef.value = { ...data.baseData };
  91. setState({
  92. title: unref(isUpdate) ? '编辑查询' : '新增查询',
  93. cancelText: '取消',
  94. confirmText: '下一步',
  95. });
  96. state.stepIndex = 0;
  97. if (unref(isUpdate)) {
  98. const entity = await QueryApi.getDetail(data.baseData.id);
  99. modelRef.value = { ...entity };
  100. stepBaseConfig.value.stepSetValues(entity);
  101. }
  102. setState({ loading: false });
  103. }
  104. },
  105. title: '新增查询',
  106. });
  107. </script>
  108. <template>
  109. <Modal class="w-[1000px]">
  110. <div>
  111. <Steps
  112. :current="state.stepIndex"
  113. :items="state.stepItems"
  114. :percent="60"
  115. :style="state.stepStyle"
  116. />
  117. <div>
  118. <StepBaseConfig v-show="state.stepIndex === 0" ref="stepBaseConfig" />
  119. <StepDetailConfig
  120. v-show="state.stepIndex === 1"
  121. ref="stepDetailConfig"
  122. />
  123. </div>
  124. </div>
  125. </Modal>
  126. </template>