Files
admin-vben5/apps/web-antd/src/views/property/resident/unit/unit-modal.vue

130 lines
3.2 KiB
Vue
Raw Normal View History

2025-06-19 14:26:08 +08:00
<script setup lang="ts">
2025-09-12 15:56:54 +08:00
import {computed, ref} from 'vue';
2025-06-19 14:26:08 +08:00
2025-09-12 15:56:54 +08:00
import {useVbenModal} from '@vben/common-ui';
import {$t} from '@vben/locales';
import {cloneDeep} from '@vben/utils';
2025-06-19 14:26:08 +08:00
2025-09-12 15:56:54 +08:00
import {useVbenForm} from '#/adapter/form';
2025-07-30 14:57:42 +08:00
import {
resident_unitAdd,
resident_unitInfo,
resident_unitUpdate,
} from '#/api/property/resident/unit';
2025-09-12 15:56:54 +08:00
import {defaultFormValueGetter, useBeforeCloseDiff} from '#/utils/popup';
2025-06-19 14:26:08 +08:00
2025-09-12 15:56:54 +08:00
import {modalSchema} from './data';
import RoomTree from "./components/room-tree.vue";
import {message} from "ant-design-vue";
2025-06-19 14:26:08 +08:00
2025-07-30 14:57:42 +08:00
const emit = defineEmits<{ reload: [] }>();
2025-06-19 14:26:08 +08:00
2025-07-30 14:57:42 +08:00
const isUpdate = ref(false);
2025-06-19 14:26:08 +08:00
const title = computed(() => {
2025-07-30 14:57:42 +08:00
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
2025-06-19 14:26:08 +08:00
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
// 默认占满两列
2025-09-12 15:56:54 +08:00
formItemClass: 'col-span-2',
2025-06-19 14:26:08 +08:00
// 默认label宽度 px
2025-06-23 09:55:23 +08:00
labelWidth: 100,
2025-06-19 14:26:08 +08:00
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
2025-07-30 14:57:42 +08:00
},
2025-06-19 14:26:08 +08:00
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
2025-07-30 14:57:42 +08:00
});
2025-06-19 14:26:08 +08:00
2025-09-12 15:56:54 +08:00
const {onBeforeClose, markInitialized, resetInitialized} = useBeforeCloseDiff(
2025-06-19 14:26:08 +08:00
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
2025-07-30 14:57:42 +08:00
);
2025-06-19 14:26:08 +08:00
const [BasicModal, modalApi] = useVbenModal({
// 在这里更改宽度
2025-06-19 16:55:06 +08:00
class: 'w-[70%]',
2025-06-19 14:26:08 +08:00
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
2025-07-30 14:57:42 +08:00
return null;
2025-06-19 14:26:08 +08:00
}
2025-07-30 14:57:42 +08:00
modalApi.modalLoading(true);
2025-06-19 14:26:08 +08:00
2025-09-12 15:56:54 +08:00
const {id} = modalApi.getData() as { id?: number | string };
2025-07-30 14:57:42 +08:00
isUpdate.value = !!id;
2025-06-19 14:26:08 +08:00
if (isUpdate.value && id) {
2025-07-30 14:57:42 +08:00
const record = await resident_unitInfo(id);
2025-09-12 15:56:54 +08:00
let roomIds = record.location.split(',')
await formApi.setValues({
...record,
authTime: [record.authBegDate, record.authEndDate],
2025-08-25 17:00:32 +08:00
});
2025-09-12 15:56:54 +08:00
checkedRoomId.value = roomIds
2025-06-19 14:26:08 +08:00
}
2025-07-30 14:57:42 +08:00
await markInitialized();
2025-06-19 14:26:08 +08:00
2025-07-30 14:57:42 +08:00
modalApi.modalLoading(false);
2025-06-19 14:26:08 +08:00
},
2025-07-30 14:57:42 +08:00
});
2025-06-19 14:26:08 +08:00
async function handleConfirm() {
try {
2025-07-30 14:57:42 +08:00
modalApi.lock(true);
2025-09-12 15:56:54 +08:00
const {valid} = await formApi.validate();
if (!checkedRoomId.value.length) {
message.error('请选择入驻位置');
return;
}
2025-06-19 14:26:08 +08:00
if (!valid) {
2025-07-30 14:57:42 +08:00
return;
2025-06-19 14:26:08 +08:00
}
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
2025-07-30 14:57:42 +08:00
const data = cloneDeep(await formApi.getValues());
2025-07-28 15:24:45 +08:00
2025-07-30 14:57:42 +08:00
data.authBegDate = data.authTime[0];
data.authEndDate = data.authTime[1];
2025-09-12 15:56:54 +08:00
data.location = checkedRoomId.value.join(',')
2025-07-30 14:57:42 +08:00
await (isUpdate.value ? resident_unitUpdate(data) : resident_unitAdd(data));
resetInitialized();
emit('reload');
modalApi.close();
2025-06-19 14:26:08 +08:00
} catch (error) {
2025-07-30 14:57:42 +08:00
console.error(error);
2025-06-19 14:26:08 +08:00
} finally {
2025-07-30 14:57:42 +08:00
modalApi.lock(false);
2025-06-19 14:26:08 +08:00
}
}
async function handleClosed() {
2025-07-30 14:57:42 +08:00
await formApi.resetForm();
resetInitialized();
2025-06-19 14:26:08 +08:00
}
2025-09-12 15:56:54 +08:00
const checkedRoomId = ref<string[]>([]);
2025-06-19 14:26:08 +08:00
</script>
<template>
<BasicModal :title="title">
2025-09-12 15:56:54 +08:00
<div class="flex gap-[8px]">
<div class="w-[260px]">
<RoomTree
:checkable="true"
class="max-h-[calc(100vh-45vh)]"
v-model:checked-room-id="checkedRoomId"
/>
</div>
<BasicForm class="flex-1"></BasicForm>
</div>
2025-06-19 14:26:08 +08:00
</BasicModal>
</template>