91 lines
2.1 KiB
Vue
91 lines
2.1 KiB
Vue
<script lang="ts" setup>
|
|
import { computed, ref } from 'vue';
|
|
|
|
import { useVbenModal } from '@vben/common-ui';
|
|
import { $t } from '@vben/locales';
|
|
import { cloneDeep } from '@vben/utils';
|
|
|
|
import { useVbenForm } from '#/adapter/form';
|
|
import * as PayApi from '#/api/pay/app';
|
|
|
|
import { modalSchema } from '../data';
|
|
|
|
const emit = defineEmits<{ reload: [] }>();
|
|
|
|
const isUpdate = ref(false);
|
|
const title = computed(() => {
|
|
return isUpdate.value
|
|
? $t('ui.actionTitle.edit', '应用')
|
|
: $t('ui.actionTitle.create', '应用');
|
|
});
|
|
|
|
const [BasicForm, formApi] = useVbenForm({
|
|
commonConfig: {
|
|
// 默认占满两列
|
|
formItemClass: 'col-span-2',
|
|
// 默认label宽度 px
|
|
labelWidth: 160,
|
|
// 通用配置项 会影响到所有表单项
|
|
componentProps: {
|
|
class: 'w-full',
|
|
},
|
|
},
|
|
schema: modalSchema(),
|
|
showDefaultActions: false,
|
|
wrapperClass: 'grid-cols-2',
|
|
});
|
|
|
|
const [BasicModal, modalApi] = useVbenModal({
|
|
fullscreenButton: false,
|
|
onCancel: handleCancel,
|
|
onConfirm: handleConfirm,
|
|
onOpenChange: async (isOpen) => {
|
|
if (!isOpen) {
|
|
return null;
|
|
}
|
|
modalApi.modalLoading(true);
|
|
|
|
const { id } = modalApi.getData() as {
|
|
id?: number;
|
|
};
|
|
isUpdate.value = !!id;
|
|
|
|
if (isUpdate.value && id) {
|
|
const record = await PayApi.getApp(id);
|
|
await formApi.setValues(record);
|
|
}
|
|
|
|
modalApi.modalLoading(false);
|
|
},
|
|
});
|
|
|
|
async function handleConfirm() {
|
|
try {
|
|
modalApi.modalLoading(true);
|
|
const { valid } = await formApi.validate();
|
|
if (!valid) {
|
|
return;
|
|
}
|
|
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
|
|
const data = cloneDeep(await formApi.getValues()) as PayApi.PayAppApi.App;
|
|
await (isUpdate.value ? PayApi.updateApp(data) : PayApi.createApp(data));
|
|
emit('reload');
|
|
await handleCancel();
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
modalApi.modalLoading(false);
|
|
}
|
|
}
|
|
|
|
async function handleCancel() {
|
|
modalApi.close();
|
|
await formApi.resetForm();
|
|
}
|
|
</script>
|
|
<template>
|
|
<BasicModal :close-on-click-modal="false" :title="title" class="w-[40%]">
|
|
<BasicForm />
|
|
</BasicModal>
|
|
</template>
|