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

143 lines
3.5 KiB
Vue
Raw Normal View History

2025-06-19 14:26:08 +08:00
<script setup lang="ts">
2025-07-28 15:24:45 +08:00
import { computed, ref } from 'vue'
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
import { useVbenModal } from '@vben/common-ui'
import { $t } from '@vben/locales'
import { cloneDeep, handleNode } from '@vben/utils'
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
import { useVbenForm } from '#/adapter/form'
import { resident_unitAdd, resident_unitInfo, resident_unitUpdate } from '#/api/property/resident/unit'
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup'
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
import { modalSchema } from './data'
import { communityTree } from "#/api/property/community"
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
const emit = defineEmits<{ reload: [] }>()
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
const isUpdate = ref(false)
2025-06-19 14:26:08 +08:00
const title = computed(() => {
2025-07-28 15:24:45 +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-06-19 16:55:06 +08:00
formItemClass: 'col-span-1',
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',
}
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
2025-07-28 15:24:45 +08:00
})
2025-06-19 14:26:08 +08:00
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
2025-07-28 15:24:45 +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-28 15:24:45 +08:00
return null
2025-06-19 14:26:08 +08:00
}
2025-07-28 15:24:45 +08:00
modalApi.modalLoading(true)
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
const { id } = modalApi.getData() as { id?: number | string }
isUpdate.value = !!id
2025-07-07 17:30:51 +08:00
await initLocationOptions()
2025-06-19 14:26:08 +08:00
if (isUpdate.value && id) {
2025-07-28 15:24:45 +08:00
const record = await resident_unitInfo(id)
await formApi.setValues(record)
2025-06-19 14:26:08 +08:00
}
2025-07-28 15:24:45 +08:00
await markInitialized()
2025-06-19 14:26:08 +08:00
2025-07-28 15:24:45 +08:00
modalApi.modalLoading(false)
2025-06-19 14:26:08 +08:00
},
2025-07-28 15:24:45 +08:00
})
2025-06-19 14:26:08 +08:00
async function handleConfirm() {
try {
2025-07-28 15:24:45 +08:00
modalApi.lock(true)
const { valid } = await formApi.validate()
2025-06-19 14:26:08 +08:00
if (!valid) {
2025-07-28 15:24:45 +08:00
return
2025-06-19 14:26:08 +08:00
}
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
2025-07-28 15:24:45 +08:00
const data = cloneDeep(await formApi.getValues())
data.authBegDate = data.authTime[0]
data.authEndDate = data.authTime[1]
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-28 15:24:45 +08:00
console.error(error)
2025-06-19 14:26:08 +08:00
} finally {
2025-07-28 15:24:45 +08:00
modalApi.lock(false)
2025-06-19 14:26:08 +08:00
}
}
2025-06-30 14:45:06 +08:00
/**
* 入驻位置数据
*/
async function initLocationOptions() {
2025-07-28 15:24:45 +08:00
const locationList = await communityTree(5)
const splitStr = '/'
2025-06-30 14:45:06 +08:00
handleNode(locationList, 'label', splitStr, function (node: any) {
if (node.level != 5) {
2025-07-28 15:24:45 +08:00
node.disabled = true
2025-06-30 14:45:06 +08:00
}
2025-07-28 15:24:45 +08:00
})
2025-06-30 14:45:06 +08:00
formApi.updateSchema([
{
componentProps: () => ({
class: 'w-full',
fieldNames: {
key: 'id',
label: 'label',
value: 'code',
children: 'children',
},
placeholder: '请选择入驻位置',
showSearch: true,
treeData: locationList,
treeDefaultExpandAll: true,
treeLine: { showLeafIcon: false },
// 筛选的字段
treeNodeFilterProp: 'label',
// 选中后显示在输入框的值
treeNodeLabelProp: 'fullName',
}),
fieldName: 'location',
},
2025-07-28 15:24:45 +08:00
])
2025-06-30 14:45:06 +08:00
}
2025-06-19 14:26:08 +08:00
async function handleClosed() {
2025-07-28 15:24:45 +08:00
await formApi.resetForm()
resetInitialized()
2025-06-19 14:26:08 +08:00
}
</script>
<template>
<BasicModal :title="title">
2025-06-27 17:56:05 +08:00
<BasicForm>
</BasicForm>
2025-06-19 14:26:08 +08:00
</BasicModal>
</template>