form.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <script lang="ts" setup>
  2. import type { Coupon2Entity } from '@vben/types';
  3. import { computed, ref } from 'vue';
  4. import { useVbenModal } from '@vben/common-ui';
  5. import dayjs from 'dayjs';
  6. import { ElMessage } from 'element-plus';
  7. import { useVbenForm, z } from '#/adapter/form';
  8. import {
  9. addCoupon2Api,
  10. editCoupon2Api,
  11. getCoupon2DetailApi,
  12. } from '#/api/coupon/coupon2';
  13. import { $t } from '#/locales';
  14. const emit = defineEmits(['finish']);
  15. const data = ref();
  16. const formType = ref<'create' | 'edit'>('create');
  17. const titleMap = {
  18. create: '新增优惠券',
  19. edit: '编辑优惠券',
  20. } as const;
  21. const getTitle = computed(() => titleMap[formType.value]);
  22. const [BaseForm, baseFormApi] = useVbenForm({
  23. showDefaultActions: false,
  24. // 所有表单项共用,可单独在表单内覆盖
  25. commonConfig: {
  26. labelWidth: 140,
  27. // 所有表单项
  28. componentProps: {
  29. class: 'w-full',
  30. },
  31. },
  32. wrapperClass: 'grid-cols-1 lg:grid-cols-2',
  33. schema: [
  34. {
  35. component: 'Input',
  36. fieldName: 'coupon2code',
  37. label: '优惠券代码',
  38. componentProps: {
  39. placeholder: $t('ui.placeholder.input'),
  40. clearable: true,
  41. },
  42. rules: z.string().min(1, { message: '请输入优惠券代码' }),
  43. },
  44. {
  45. component: 'DatePicker',
  46. fieldName: 'coupon2reviewdatetime',
  47. label: '审核时间',
  48. defaultValue: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  49. componentProps: {
  50. placeholder: $t('ui.placeholder.select'),
  51. clearable: true,
  52. type: 'datetime',
  53. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  54. style: { width: '100%' },
  55. },
  56. rules: z.string().min(1, { message: '请选择审核时间' }),
  57. },
  58. {
  59. component: 'Select',
  60. fieldName: 'coupon2sype',
  61. label: '优惠券状态',
  62. componentProps: {
  63. placeholder: $t('ui.placeholder.select'),
  64. clearable: true,
  65. options: [
  66. { label: '申请中', value: 0 },
  67. { label: '审核通过', value: 1 },
  68. { label: '审核不通过', value: 2 },
  69. ],
  70. },
  71. rules: z.number().min(0, { message: '请选择优惠券状态' }),
  72. },
  73. {
  74. component: 'Select',
  75. fieldName: 'coupon2isused',
  76. label: '使用状态',
  77. componentProps: {
  78. placeholder: $t('ui.placeholder.select'),
  79. clearable: true,
  80. options: [
  81. { label: '未使用', value: 0 },
  82. { label: '已使用', value: 1 },
  83. ],
  84. },
  85. rules: z.number().min(0, { message: '请选择使用状态' }),
  86. },
  87. {
  88. component: 'Input',
  89. fieldName: 'coupon2productids',
  90. label: '可用产品',
  91. componentProps: {
  92. placeholder: $t('ui.placeholder.input'),
  93. clearable: true,
  94. },
  95. rules: z.string().min(1, { message: '请输入可用产品' }),
  96. },
  97. {
  98. component: 'Input',
  99. fieldName: 'coupon2coupon1id',
  100. label: '关联主券ID',
  101. componentProps: {
  102. placeholder: $t('ui.placeholder.input'),
  103. clearable: true,
  104. },
  105. rules: z.string().min(1, { message: '请输入关联主券ID' }),
  106. },
  107. ],
  108. });
  109. const [Modal, modalApi] = useVbenModal({
  110. class: 'w-7/12',
  111. onCancel() {
  112. modalApi.close();
  113. },
  114. async onConfirm() {
  115. // 校验输入的数据
  116. const validate = await baseFormApi.validate();
  117. if (!validate.valid) {
  118. return;
  119. }
  120. try {
  121. // 调用新增或编辑接口
  122. const apiMap = {
  123. create: () => addCoupon2Api(validate.values as Coupon2Entity),
  124. edit: () =>
  125. editCoupon2Api({
  126. ...validate.values,
  127. 'coupon2sid.value': data.value.row.coupon2sid,
  128. } as any),
  129. };
  130. await apiMap[formType.value]();
  131. ElMessage.success('操作成功');
  132. emit('finish');
  133. modalApi.close();
  134. } catch {}
  135. },
  136. async onOpenChange(isOpen) {
  137. if (isOpen) {
  138. data.value = modalApi.getData();
  139. formType.value = data.value.formType;
  140. if (data.value.formType === 'create') {
  141. baseFormApi.setValues({
  142. coupon2reviewdatetime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  143. });
  144. return;
  145. }
  146. // 编辑模式,加载详情数据
  147. if (data.value.formType === 'edit') {
  148. try {
  149. modalApi.setState({ loading: true });
  150. const detailData = await getCoupon2DetailApi({
  151. coupon2sid: data.value.row.coupon2sid,
  152. });
  153. baseFormApi.setValues(detailData);
  154. } catch {
  155. // console.log(error);
  156. }
  157. modalApi.setState({ loading: false });
  158. }
  159. }
  160. },
  161. });
  162. </script>
  163. <template>
  164. <Modal :title="getTitle">
  165. <BaseForm />
  166. </Modal>
  167. </template>