form.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <script lang="ts" setup>
  2. import type { Coupon1Entity } from '@vben/types';
  3. import { computed, ref } from 'vue';
  4. import { useVbenModal } from '@vben/common-ui';
  5. import { ElMessage } from 'element-plus';
  6. import { useVbenForm, z } from '#/adapter/form';
  7. import {
  8. addCoupon1Api,
  9. editCoupon1Api,
  10. getCoupon1DetailApi,
  11. } from '#/api/coupon/coupon1';
  12. import { getProductListApi } from '#/api/product';
  13. import { $t } from '#/locales';
  14. const emit = defineEmits(['finish']);
  15. const data = ref();
  16. const formType = ref<'create' | 'detail' | 'edit'>('create');
  17. const titleMap = {
  18. create: '新增优惠券',
  19. detail: '优惠券详情',
  20. edit: '编辑优惠券',
  21. } as const;
  22. const getTitle = computed(() => titleMap[formType.value]);
  23. const productOptions = ref<{ label: string; value: string }[]>([]);
  24. // 获取产品列表
  25. const fetchProductOptions = async () => {
  26. try {
  27. const response = await getProductListApi({
  28. pageindex: 1,
  29. rows: 100,
  30. });
  31. if (response && response.Data && Array.isArray(response.Data)) {
  32. productOptions.value = response.Data.map((item: any) => ({
  33. label: item.productsname || '未命名产品',
  34. value: item.productsid || '',
  35. })).filter((item: any) => item.value);
  36. }
  37. } catch (error) {
  38. console.error('获取产品列表失败', error);
  39. }
  40. };
  41. const [BaseForm, baseFormApi] = useVbenForm({
  42. showDefaultActions: false,
  43. // 所有表单项共用,可单独在表单内覆盖
  44. commonConfig: {
  45. labelWidth: 140,
  46. // 所有表单项
  47. componentProps: {
  48. class: 'w-full',
  49. },
  50. },
  51. wrapperClass: 'grid-cols-1 lg:grid-cols-2',
  52. schema: [
  53. // { title: '优惠券名称', field: 'couponmc' },
  54. // { title: '优惠券可用产品', field: 'couponproductids' },
  55. // { title: '优惠券是否可用', field: 'couponsfky' },
  56. {
  57. component: 'Input',
  58. fieldName: 'couponmc',
  59. label: '名称',
  60. componentProps: {
  61. placeholder: $t('ui.placeholder.input'),
  62. allowClear: true,
  63. },
  64. rules: z.string().min(1, { message: '请输入优惠券名称' }),
  65. },
  66. {
  67. component: 'Select',
  68. fieldName: 'couponproductids',
  69. label: '可用产品',
  70. componentProps: {
  71. placeholder: '请选择可用产品',
  72. options: productOptions,
  73. clearable: true,
  74. filterable: true,
  75. noDataText: '暂无产品数据',
  76. style: { width: '100%' },
  77. },
  78. rules: z.string().min(1, { message: '请选择优惠券可用产品' }),
  79. },
  80. {
  81. component: 'Switch',
  82. fieldName: 'couponsfky',
  83. label: '是否可用',
  84. componentProps: {
  85. activeValue: 1,
  86. inactiveValue: 0,
  87. },
  88. defaultValue: 1,
  89. rules: z.string().min(1, { message: '请选择是否可用' }),
  90. },
  91. ],
  92. });
  93. const [Modal, modalApi] = useVbenModal({
  94. class: 'w-7/12',
  95. onCancel() {
  96. modalApi.close();
  97. },
  98. async onConfirm() {
  99. // 校验输入的数据
  100. const validate = await baseFormApi.validate();
  101. if (!validate.valid) {
  102. return;
  103. }
  104. try {
  105. // 调用新增或编辑接口
  106. const apiMap = {
  107. create: () => addCoupon1Api(validate.values as Coupon1Entity),
  108. edit: () =>
  109. editCoupon1Api({
  110. ...validate.values,
  111. 'couponid.value': data.value.row.couponid,
  112. } as any),
  113. };
  114. if (formType.value === 'detail') {
  115. modalApi.close();
  116. return;
  117. }
  118. await apiMap[formType.value]();
  119. ElMessage.success('操作成功');
  120. emit('finish');
  121. modalApi.close();
  122. } catch {}
  123. },
  124. async onOpenChange(isOpen) {
  125. if (isOpen) {
  126. data.value = modalApi.getData();
  127. formType.value = data.value.formType;
  128. baseFormApi.setState({
  129. commonConfig: { disabled: formType.value === 'detail' },
  130. });
  131. // 获取产品列表
  132. await fetchProductOptions();
  133. if (data.value.formType === 'create') {
  134. baseFormApi.setValues({
  135. couponsfky: 1,
  136. });
  137. return;
  138. }
  139. try {
  140. modalApi.setState({ loading: true });
  141. const detailData = await getCoupon1DetailApi({
  142. couponid: data.value.row.couponid,
  143. });
  144. baseFormApi.setValues(detailData);
  145. } catch {
  146. // console.log(error);
  147. }
  148. modalApi.setState({ loading: false });
  149. }
  150. },
  151. });
  152. </script>
  153. <template>
  154. <Modal :title="getTitle">
  155. <BaseForm />
  156. </Modal>
  157. </template>