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

167 lines
4.0 KiB
Vue
Raw Normal View History

2025-07-23 14:12:02 +08:00
<script setup lang="ts">
import { computed, ref, onMounted } from 'vue'
2025-07-23 14:12:02 +08:00
import { useVbenModal } from '@vben/common-ui'
import { $t } from '@vben/locales'
import { cloneDeep } from '@vben/utils'
2025-07-23 14:12:02 +08:00
import { useVbenForm } from '#/adapter/form'
import { authGroupAdd, authGroupInfo, authGroupUpdate } from '#/api/sis/authGroup'
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup'
import { queryTree } from '#/api/sis/authRecord'
import { message, Tree } from 'ant-design-vue'
2025-07-23 14:12:02 +08:00
import { modalSchema } from './data'
2025-07-23 14:12:02 +08:00
const emit = defineEmits<{ reload: [] }>()
2025-07-23 14:12:02 +08:00
const isUpdate = ref(false)
2025-07-23 14:12:02 +08:00
const title = computed(() => {
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add')
})
2025-07-23 14:12:02 +08:00
const [BasicForm, formApi] = useVbenForm({
commonConfig: {
// 默认占满两列
formItemClass: 'col-span-2',
// 默认label宽度 px
labelWidth: 80,
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
}
},
schema: modalSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
})
2025-07-23 14:12:02 +08:00
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
)
2025-07-23 14:12:02 +08:00
const [BasicModal, modalApi] = useVbenModal({
// 在这里更改宽度
class: 'w-[550px]',
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null
2025-07-23 14:12:02 +08:00
}
modalApi.modalLoading(true)
loadDeviceTree()
const { id } = modalApi.getData() as { id?: number | string }
isUpdate.value = !!id
2025-07-23 14:12:02 +08:00
if (isUpdate.value && id) {
const record = await authGroupInfo(id)
await formApi.setValues(record)
2025-07-23 14:12:02 +08:00
}
await markInitialized()
2025-07-23 14:12:02 +08:00
modalApi.modalLoading(false)
2025-07-23 14:12:02 +08:00
},
})
2025-07-23 14:12:02 +08:00
async function handleConfirm() {
try {
modalApi.lock(true)
const { valid } = await formApi.validate()
2025-07-23 14:12:02 +08:00
if (!valid) {
return
2025-07-23 14:12:02 +08:00
}
if (eleIds.value.length === 0 && acIds.value.length === 0) {
message.error('请选择授权设备')
return
}
const data = cloneDeep(await formApi.getValues())
data.acIds = acIds.value;
data.eleIds = eleIds.value;
data.floorIds = floorIds.value;
await (isUpdate.value ? authGroupUpdate(data) : authGroupAdd(data))
resetInitialized()
emit('reload')
modalApi.close()
2025-07-23 14:12:02 +08:00
} catch (error) {
console.error(error)
2025-07-23 14:12:02 +08:00
} finally {
modalApi.lock(false)
2025-07-23 14:12:02 +08:00
}
}
async function handleClosed() {
await formApi.resetForm()
resetInitialized()
}
const treeData = ref([])
function loadDeviceTree() {
queryTree().then((data: any) => {
treeData.value = data
})
}
const fieldNames = {
title: 'title',
label: 'label',
key: 'code',
children: 'children',
}
const checkedKeys = ref<any[]>([])
type Key = string | number
const eleIds = ref<any[]>([])
const acIds = ref<any[]>([])
const floorIds = ref<any[]>([])
function handleCheck(checked: Key[] | { checked: Key[]; halfChecked: Key[] }, info: any) {
acIds.value = []
eleIds.value = []
floorIds.value = []
info.checkedNodesPositions.forEach((item: any) => {
switch (item.node.label) {
case 'accessControl':
console.log('item.node', item.node)
acIds.value = acIds.value.concat(item.node.code)
break
case 'floor':
console.log('item.node', item.node)
floorIds.value = floorIds.value.concat(item.node.code)
eleIds.value = eleIds.value.concat(item.node.parentCode)
break
case 'elevator':
eleIds.value = eleIds.value.concat(item.node.code)
break
}
})
// 去重
acIds.value = [...new Set(acIds.value)]
eleIds.value = [...new Set(eleIds.value)]
floorIds.value = [...new Set(floorIds.value)]
2025-07-23 14:12:02 +08:00
}
</script>
<template>
<BasicModal :title="title">
<BasicForm />
<div class="p-4">
<Tree checkable :tree-data="treeData" :fieldNames="fieldNames" v-model:checkedKeys="checkedKeys"
:onCheck="handleCheck">
</Tree>
</div>
2025-07-23 14:12:02 +08:00
</BasicModal>
</template>