| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <script lang="ts" setup>
- import type { Coupon2Entity } from '@vben/types';
- import { computed, ref } from 'vue';
- import { useVbenModal } from '@vben/common-ui';
- import dayjs from 'dayjs';
- import { ElMessage } from 'element-plus';
- import { useVbenForm, z } from '#/adapter/form';
- import {
- addCoupon2Api,
- editCoupon2Api,
- getCoupon2DetailApi,
- } from '#/api/coupon/coupon2';
- import { $t } from '#/locales';
- const emit = defineEmits(['finish']);
- const data = ref();
- const formType = ref<'create' | 'edit'>('create');
- const titleMap = {
- create: '新增优惠券',
- edit: '编辑优惠券',
- } as const;
- const getTitle = computed(() => titleMap[formType.value]);
- const [BaseForm, baseFormApi] = useVbenForm({
- showDefaultActions: false,
- // 所有表单项共用,可单独在表单内覆盖
- commonConfig: {
- labelWidth: 140,
- // 所有表单项
- componentProps: {
- class: 'w-full',
- },
- },
- wrapperClass: 'grid-cols-1 lg:grid-cols-2',
- schema: [
- {
- component: 'Input',
- fieldName: 'coupon2code',
- label: '优惠券代码',
- componentProps: {
- placeholder: $t('ui.placeholder.input'),
- clearable: true,
- },
- rules: z.string().min(1, { message: '请输入优惠券代码' }),
- },
- {
- component: 'DatePicker',
- fieldName: 'coupon2reviewdatetime',
- label: '审核时间',
- defaultValue: dayjs().format('YYYY-MM-DD HH:mm:ss'),
- componentProps: {
- placeholder: $t('ui.placeholder.select'),
- clearable: true,
- type: 'datetime',
- valueFormat: 'YYYY-MM-DD HH:mm:ss',
- style: { width: '100%' },
- },
- rules: z.string().min(1, { message: '请选择审核时间' }),
- },
- {
- component: 'Select',
- fieldName: 'coupon2sype',
- label: '优惠券状态',
- componentProps: {
- placeholder: $t('ui.placeholder.select'),
- clearable: true,
- options: [
- { label: '申请中', value: 0 },
- { label: '审核通过', value: 1 },
- { label: '审核不通过', value: 2 },
- ],
- },
- rules: z.number().min(0, { message: '请选择优惠券状态' }),
- },
- {
- component: 'Select',
- fieldName: 'coupon2isused',
- label: '使用状态',
- componentProps: {
- placeholder: $t('ui.placeholder.select'),
- clearable: true,
- options: [
- { label: '未使用', value: 0 },
- { label: '已使用', value: 1 },
- ],
- },
- rules: z.number().min(0, { message: '请选择使用状态' }),
- },
- {
- component: 'Input',
- fieldName: 'coupon2productids',
- label: '可用产品',
- componentProps: {
- placeholder: $t('ui.placeholder.input'),
- clearable: true,
- },
- rules: z.string().min(1, { message: '请输入可用产品' }),
- },
- {
- component: 'Input',
- fieldName: 'coupon2coupon1id',
- label: '关联主券ID',
- componentProps: {
- placeholder: $t('ui.placeholder.input'),
- clearable: true,
- },
- rules: z.string().min(1, { message: '请输入关联主券ID' }),
- },
- ],
- });
- const [Modal, modalApi] = useVbenModal({
- class: 'w-7/12',
- onCancel() {
- modalApi.close();
- },
- async onConfirm() {
- // 校验输入的数据
- const validate = await baseFormApi.validate();
- if (!validate.valid) {
- return;
- }
- try {
- // 调用新增或编辑接口
- const apiMap = {
- create: () => addCoupon2Api(validate.values as Coupon2Entity),
- edit: () =>
- editCoupon2Api({
- ...validate.values,
- 'coupon2sid.value': data.value.row.coupon2sid,
- } as any),
- };
- await apiMap[formType.value]();
- ElMessage.success('操作成功');
- emit('finish');
- modalApi.close();
- } catch {}
- },
- async onOpenChange(isOpen) {
- if (isOpen) {
- data.value = modalApi.getData();
- formType.value = data.value.formType;
- if (data.value.formType === 'create') {
- baseFormApi.setValues({
- coupon2reviewdatetime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
- });
- return;
- }
- // 编辑模式,加载详情数据
- if (data.value.formType === 'edit') {
- try {
- modalApi.setState({ loading: true });
- const detailData = await getCoupon2DetailApi({
- coupon2sid: data.value.row.coupon2sid,
- });
- baseFormApi.setValues(detailData);
- } catch {
- // console.log(error);
- }
- modalApi.setState({ loading: false });
- }
- }
- },
- });
- </script>
- <template>
- <Modal :title="getTitle">
- <BaseForm />
- </Modal>
- </template>
|