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

160 lines
4.1 KiB
Vue
Raw Normal View History

2025-06-18 11:03:42 +08:00
<script setup lang="ts">
import { computed, ref } from 'vue';
2025-06-28 02:41:09 +08:00
2025-06-18 11:03:42 +08:00
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
2025-06-28 22:49:40 +08:00
import { cloneDeep, getPopupContainer, handleNode } from '@vben/utils';
2025-06-28 02:41:09 +08:00
2025-06-18 11:03:42 +08:00
import { useVbenForm } from '#/adapter/form';
2025-06-28 22:49:40 +08:00
import {
communityAdd,
communityInfo,
communityUpdate,
} from '#/api/property/community';
2025-06-18 11:03:42 +08:00
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
2025-06-28 02:41:09 +08:00
2025-06-18 11:03:42 +08:00
import { modalSchema } from './data';
2025-06-28 22:49:40 +08:00
import { getCityAreaTreeList } from '#/api/property/cityArea';
import type { CityAreaVO } from '#/api/property/cityArea/model';
import type { CommunityForm } from '#/api/property/community/model';
2025-06-18 11:03:42 +08:00
const emit = defineEmits<{ reload: [] }>();
2025-06-27 15:16:06 +08:00
2025-06-28 02:41:09 +08:00
const isUpdate = ref(false);
2025-06-18 11:03:42 +08:00
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
2025-06-28 02:41:09 +08:00
// 默认占满两列
2025-06-28 22:49:40 +08:00
formItemClass: 'col-span-1',
2025-06-28 02:41:09 +08:00
// 默认label宽度 px
2025-06-18 11:03:42 +08:00
labelWidth: 80,
2025-06-28 02:41:09 +08:00
// 通用配置项 会影响到所有表单项
2025-06-18 11:03:42 +08:00
componentProps: {
class: 'w-full',
2025-06-28 22:49:40 +08:00
},
2025-06-18 11:03:42 +08:00
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
);
const [BasicModal, modalApi] = useVbenModal({
2025-06-28 02:41:09 +08:00
// 在这里更改宽度
2025-06-28 22:49:40 +08:00
class: 'w-[60%]',
2025-06-18 11:03:42 +08:00
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null;
}
modalApi.modalLoading(true);
const { id } = modalApi.getData() as { id?: number | string };
isUpdate.value = !!id;
if (isUpdate.value && id) {
const record = await communityInfo(id);
await formApi.setValues(record);
}
2025-06-28 22:49:40 +08:00
setupDeptSelect();
2025-06-18 11:03:42 +08:00
await markInitialized();
modalApi.modalLoading(false);
},
});
2025-06-28 22:49:40 +08:00
let currentSelectNode: any = null;
2025-06-18 11:03:42 +08:00
async function handleConfirm() {
try {
modalApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
2025-06-28 02:41:09 +08:00
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
2025-06-28 22:49:40 +08:00
const data: CommunityForm = cloneDeep(await formApi.getValues());
data.cityFullName = currentSelectNode.fullName;
2025-06-18 11:03:42 +08:00
await (isUpdate.value ? communityUpdate(data) : communityAdd(data));
resetInitialized();
emit('reload');
modalApi.close();
} catch (error) {
console.error(error);
} finally {
modalApi.lock(false);
}
}
2025-06-28 22:49:40 +08:00
/**
* 初始化城市
*/
async function setupDeptSelect() {
const areaList = await getCityAreaTreeList();
// 选中后显示在输入框的值 即父节点 / 子节点
// addFullName(areaList, 'areaName', ' / ');
const splitStr = '/';
handleNode(
areaList,
'areaName',
splitStr,
function (node: any, parentNode: any) {
node.fullCode = parentNode
? parentNode.fullCode + splitStr + node.areaCode
: node.areaCode;
if (node.areaLevel != '303') {
node.disabled = true;
}
},
);
formApi.updateSchema([
{
componentProps: () => ({
class: 'w-full',
fieldNames: {
key: 'id',
label: 'areaName',
value: 'fullCode',
children: 'children',
},
getPopupContainer,
async onSelect(fullCode: any, node: CityAreaVO) {
currentSelectNode = node;
},
placeholder: '请选择城市',
showSearch: true,
treeData: areaList,
treeDefaultExpandAll: true,
treeLine: { showLeafIcon: false },
// 筛选的字段
treeNodeFilterProp: 'areaName',
// 选中后显示在输入框的值
treeNodeLabelProp: 'fullName',
}),
fieldName: 'cityFullCode',
},
]);
}
2025-06-18 11:03:42 +08:00
async function handleClosed() {
await formApi.resetForm();
resetInitialized();
}
</script>
<template>
<BasicModal :title="title">
<BasicForm />
</BasicModal>
</template>