admin-vben5/apps/web-antd/src/views/sis/elevatorInfo/elevatorInfo-modal.vue

200 lines
5.0 KiB
Vue
Raw Normal View History

2025-07-10 08:45:59 +08:00
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { cloneDeep, getPopupContainer, handleNode } from '@vben/utils';
import { useVbenForm } from '#/adapter/form';
2025-07-11 07:36:51 +08:00
import {
elevatorInfoAdd,
elevatorInfoInfo,
elevatorInfoUpdate,
} from '#/api/sis/elevatorInfo';
2025-07-10 08:45:59 +08:00
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
import { modalSchema } from './data';
import { communityTree } from '#/api/property/community';
2025-07-11 07:36:51 +08:00
import type { DeviceManageForm, DeviceManageQuery } from '#/api/sis/deviceManage/model';
import { deviceManageList } from '#/api/sis/deviceManage';
2025-07-10 08:45:59 +08:00
const emit = defineEmits<{ reload: [] }>();
const isUpdate = ref(false);
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
});
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
// 默认占满两列
formItemClass: 'col-span-1',
// 默认label宽度 px
2025-07-11 07:36:51 +08:00
labelWidth: 200,
2025-07-10 08:45:59 +08:00
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
2025-07-11 07:36:51 +08:00
},
2025-07-10 08:45:59 +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({
// 在这里更改宽度
class: 'w-[60%]',
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null;
}
modalApi.modalLoading(true);
2025-07-11 07:36:51 +08:00
// 加载社区树
setupCommunitySelect();
// 加载未绑定的设备
loadDeviceList();
2025-07-10 08:45:59 +08:00
const { id } = modalApi.getData() as { id?: number | string };
isUpdate.value = !!id;
if (isUpdate.value && id) {
const record = await elevatorInfoInfo(id);
await formApi.setValues(record);
}
await markInitialized();
modalApi.modalLoading(false);
},
});
async function handleConfirm() {
try {
modalApi.lock(true);
const { valid } = await formApi.validate();
if (!valid) {
return;
}
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
const data = cloneDeep(await formApi.getValues());
await (isUpdate.value ? elevatorInfoUpdate(data) : elevatorInfoAdd(data));
resetInitialized();
emit('reload');
modalApi.close();
} catch (error) {
console.error(error);
} finally {
modalApi.lock(false);
}
}
2025-07-11 07:36:51 +08:00
let rows: any = [];
async function loadDeviceList() {
if (rows.length === 0) {
const params: DeviceManageQuery = {
pageNum: 1,
pageSize: 500,
};
const res = await deviceManageList(params);
if (res && res.rows && res.rows.length > 0) {
rows = res.rows;
}
}
const arr = rows.map((item: DeviceManageForm) => {
return {
label: item.deviceName,
value: item.id,
deviceIp: item.deviceIp,
deviceId: item.id,
};
});
formApi.updateSchema([
{
componentProps: () => ({
options: arr,
mode: 'multiple', // 关键属性,启用多选模式
onChange: async (value: string, option: any) => {
const data = cloneDeep(await formApi.getValues());
data.remoteCallElevatorDeviceId = option;
formApi.setValues(data);
},
}),
fieldName: 'remoteCallElevatorDeviceId',
},
{
componentProps: () => ({
options: arr,
allowClear: true,
onChange: async (value: string, option: any) => {
const data = cloneDeep(await formApi.getValues());
data.elevatorControlDeviceId = option;
formApi.setValues(data);
},
}),
fieldName: 'elevatorControlDeviceId',
},
]);
}
2025-07-10 08:45:59 +08:00
/**
* 初始化城市
*/
async function setupCommunitySelect() {
const areaList = await communityTree(3);
// 选中后显示在输入框的值 即父节点 / 子节点
// addFullName(areaList, 'areaName', ' / ');
const splitStr = '/';
handleNode(areaList, 'label', splitStr, function (node: any) {
if (node.level != 3) {
node.disabled = true;
}
});
formApi.updateSchema([
{
componentProps: () => ({
class: 'w-full',
fieldNames: {
key: 'id',
label: 'label',
value: 'code',
children: 'children',
},
getPopupContainer,
placeholder: '请选择建筑',
showSearch: true,
treeData: areaList,
treeDefaultExpandAll: true,
treeLine: { showLeafIcon: false },
// 筛选的字段
treeNodeFilterProp: 'label',
// 选中后显示在输入框的值
treeNodeLabelProp: 'fullName',
}),
fieldName: 'unitId',
},
]);
}
async function handleClosed() {
await formApi.resetForm();
resetInitialized();
}
</script>
<template>
<BasicModal :title="title">
<BasicForm />
</BasicModal>
</template>