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

150 lines
3.6 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-18 11:03:42 +08:00
import { useVbenModal } from '@vben/common-ui'
import { $t } from '@vben/locales'
import { cloneDeep, getPopupContainer, handleNode } from '@vben/utils'
2025-06-18 11:03:42 +08:00
import { useVbenForm } from '#/adapter/form'
2025-07-21 03:38:19 +08:00
import {
deviceManageAdd,
deviceManageInfo,
deviceManageUpdate,
} from '#/api/sis/deviceManage'
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup'
2025-06-18 11:03:42 +08:00
import { modalSchema } from './data'
import { communityTree } from '#/api/property/community'
import { getDictOptions } from '#/utils/dict'
import { DictEnum } from '@vben/constants'
2025-06-18 11:03:42 +08:00
const emit = defineEmits<{ reload: [] }>()
2025-06-18 11:03:42 +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')
})
2025-06-18 11:03:42 +08:00
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
// 默认占满两列
2025-06-29 02:59:14 +08:00
formItemClass: 'col-span-1',
2025-06-18 11:03:42 +08:00
// 默认label宽度 px
2025-07-11 07:36:51 +08:00
labelWidth: 120,
2025-06-18 11:03:42 +08:00
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
2025-07-21 03:38:19 +08:00
},
2025-06-18 11:03:42 +08:00
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
})
2025-06-18 11:03:42 +08:00
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
)
2025-06-18 11:03:42 +08:00
const [BasicModal, modalApi] = useVbenModal({
// 在这里更改宽度
2025-06-29 02:59:14 +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
2025-06-18 11:03:42 +08:00
}
// 加载社区树
setupCommunitySelect()
modalApi.modalLoading(true)
const { id } = modalApi.getData() as { id?: number | string }
isUpdate.value = !!id
2025-06-18 11:03:42 +08:00
if (isUpdate.value && id) {
const record = await deviceManageInfo(id)
await formApi.setValues(record)
2025-06-18 11:03:42 +08:00
}
await markInitialized()
2025-06-18 11:03:42 +08:00
modalApi.modalLoading(false)
2025-06-18 11:03:42 +08:00
},
})
2025-06-18 11:03:42 +08:00
async function handleConfirm() {
try {
modalApi.lock(true)
const { valid } = await formApi.validate()
2025-06-18 11:03:42 +08:00
if (!valid) {
return
2025-06-18 11:03:42 +08:00
}
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
const data = cloneDeep(await formApi.getValues())
await (isUpdate.value ? deviceManageUpdate(data) : deviceManageAdd(data))
resetInitialized()
emit('reload')
modalApi.close()
2025-06-18 11:03:42 +08:00
} catch (error) {
console.error(error)
2025-06-18 11:03:42 +08:00
} finally {
modalApi.lock(false)
2025-06-18 11:03:42 +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: 'floorId',
},
])
}
2025-06-18 11:03:42 +08:00
async function handleClosed() {
await formApi.resetForm()
resetInitialized()
2025-06-18 11:03:42 +08:00
}
</script>
<template>
<BasicModal :title="title">
<BasicForm />
</BasicModal>
</template>