refactor(property): 重构能源管理相关页面布局

This commit is contained in:
2025-08-25 15:46:05 +08:00
parent a43cb1b390
commit 84ff3d21b4
31 changed files with 904 additions and 1027 deletions

View File

@@ -0,0 +1,83 @@
import type { FormSchemaGetter } from '#/adapter/form'
import type { VxeGridProps } from '#/adapter/vxe-table'
export const querySchema: FormSchemaGetter = () => [
{
component: 'Input',
fieldName: 'locationRemark',
label: '位置描述',
},
]
// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// export const columns: () => VxeGridProps['columns'] = () => [
export const columns: VxeGridProps['columns'] = [
{ type: 'checkbox', width: 60 },
// {
// title: '主键id',
// field: 'id',
// },
{
title: '位置描述',
field: 'locationRemark',
},
{
title: '楼 层',
field: 'floorName',
},
{
title: '开关状态',
field: 'isOn',
slots: { default: 'isOn' },
},
{
field: 'action',
fixed: 'right',
slots: { default: 'action' },
title: '操作',
width: 180,
},
]
export const drawerSchema: FormSchemaGetter = () => [
{
label: '主键id',
fieldName: 'id',
component: 'Input',
dependencies: {
show: () => false,
triggerFields: [''],
},
},
{
label: '楼层',
fieldName: 'floorId',
component: 'TreeSelect',
defaultValue: undefined,
rules: 'required',
},
{
label: '位置描述',
fieldName: 'locationRemark',
component: 'Input',
rules: 'required',
},
{
label: '开关状态',
fieldName: 'isOn',
component: 'Switch',
rules: 'required',
defaultValue: false,
componentProps: {
class: 'w-auto',
},
},
{
label: '灯控模块编码',
fieldName: 'code',
component: 'Input',
rules: 'required',
},
]

View File

@@ -0,0 +1,81 @@
<script setup lang="ts">
import type { PropType } from 'vue'
import { onMounted, ref } from 'vue'
import { handleNode } from '@vben/utils'
import { Empty, Skeleton, Tree } from 'ant-design-vue'
import { communityTree } from "#/api/property/community"
import type { CommunityVO } from "#/api/property/community/model"
defineOptions({ inheritAttrs: false })
withDefaults(defineProps<{ showSearch?: boolean }>(), { showSearch: true })
const emit = defineEmits<{
/**
* 点击刷新按钮的事件
*/
reload: []
/**
* 点击节点的事件
*/
select: []
}>()
const selectFloorId = defineModel('selectFloorId', {
type: Array as PropType<string[]>,
})
const searchValue = defineModel('searchValue', {
type: String,
default: '',
})
type TreeArray = CommunityVO[]
const treeArray = ref<TreeArray>([])
/** 骨架屏加载 */
const showTreeSkeleton = ref<boolean>(true)
async function loadTree() {
showTreeSkeleton.value = true
searchValue.value = ''
selectFloorId.value = []
const ret = await communityTree(3)
const splitStr = '/'
handleNode(ret, 'label', splitStr, function (node: any) {
if (node.level != 3) {
node.disabled = true
}
})
treeArray.value = ret
showTreeSkeleton.value = false
}
onMounted(loadTree);
</script>
<template>
<div :class="$attrs.class">
<Skeleton :loading="showTreeSkeleton" :paragraph="{ rows: 8 }" active class="p-[8px] flex-1 min-h-0">
<div class="bg-background flex h-full flex-col overflow-y-auto rounded-lg">
<div class="h-full overflow-x-hidden px-[8px]">
<Tree v-bind="$attrs" v-if="treeArray.length > 0" v-model:selected-keys="selectFloorId"
:field-names="{ title: 'label', key: 'id' }" :show-line="{ showLeafIcon: false }" :tree-data="treeArray"
:virtual="false" default-expand-all @select="$emit('select')">
<template #title="{ label }">
<span v-if="label.indexOf(searchValue) > -1">
{{ label.substring(0, label.indexOf(searchValue)) }}
<span style="color: #f50">{{ searchValue }}</span>
{{ label.substring(label.indexOf(searchValue) + searchValue.length) }}
</span>
<span v-else>{{ label }}</span>
</template>
</Tree>
<div v-else class="mt-5">
<Empty :image="Empty.PRESENTED_IMAGE_SIMPLE" description="暂无数据" />
</div>
</div>
</div>
</Skeleton>
</div>
</template>

View File

@@ -0,0 +1,173 @@
<script setup lang="ts">
import { TableSwitch } from "#/components/table"
import { Page, useVbenDrawer, type VbenFormProps } from '@vben/common-ui'
import { getVxePopupContainer } from '@vben/utils'
import { ref } from 'vue'
import { columns, querySchema } from './data'
import { Modal, Popconfirm, Space } from 'ant-design-vue'
import FloorTree from "./floor-tree.vue"
import lightInfoDrawer from './lightInfo-drawer.vue'
import {
useVbenVxeGrid,
vxeCheckboxChecked,
type VxeGridProps
} from '#/adapter/vxe-table'
import {
lightInfoList,
lightInfoRemove,
switchSingleLight,
} from '#/api/property/energyManagement/lightInfo'
import type { LightInfoForm } from '#/api/property/energyManagement/lightInfo/model'
// 左边楼层用
const selectFloorId = ref<string[]>([])
const formOptions: VbenFormProps = {
commonConfig: {
labelWidth: 80,
componentProps: {
allowClear: true,
},
},
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
handleReset: async () => {
selectFloorId.value = []
const { formApi, reload } = tableApi
await formApi.resetForm()
const formValues = formApi.form.values
formApi.setLatestSubmissionValues(formValues)
await reload(formValues)
},
}
const gridOptions: VxeGridProps = {
checkboxConfig: {
// 高亮
highlight: true,
// 翻页时保留选中状态
reserve: true,
// 点击行选中
// trigger: 'row',
},
// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
// columns: columns(),
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page }, formValues = {}) => {
// 部门树选择处理
if (selectFloorId.value.length === 1) {
formValues.floorId = selectFloorId.value[0]
} else {
Reflect.deleteProperty(formValues, 'floorId')
}
return await lightInfoList({
pageNum: page.currentPage,
pageSize: page.pageSize,
...formValues,
})
},
},
},
rowConfig: {
keyField: 'id',
},
// 表格全局唯一表示 保存列配置需要用到
id: 'property-lightInfo-index'
}
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
})
const [LightInfoDrawer, drawerApi] = useVbenDrawer({
connectedComponent: lightInfoDrawer,
})
function handleAdd() {
drawerApi.setData({})
drawerApi.open()
}
async function handleEdit(row: Required<LightInfoForm>) {
drawerApi.setData({ id: row.id })
drawerApi.open()
}
async function handleDelete(row: Required<LightInfoForm>) {
await lightInfoRemove(row.id)
await tableApi.query()
}
function handleMultiDelete() {
const rows = tableApi.grid.getCheckboxRecords()
const ids = rows.map((row: Required<LightInfoForm>) => row.id)
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条记录吗?`,
onOk: async () => {
await lightInfoRemove(ids)
await tableApi.query()
},
})
}
</script>
<template>
<Page :auto-content-height="true">
<div class="flex h-full gap-[8px]">
<FloorTree class="w-[260px]" @reload="() => tableApi.reload()" @select="() => tableApi.reload()"
v-model:select-floor-id="selectFloorId"></FloorTree>
<BasicTable class="flex-1 overflow-hidden" table-title="灯控开关信息列表">
<template #toolbar-tools>
<Space>
<a-button type="primary" v-access:code="['property:lightInfo:add']" @click="handleAdd">
{{ $t('pages.common.add') }}
</a-button>
<a-button :disabled="!vxeCheckboxChecked(tableApi)" danger type="primary"
v-access:code="['property:lightInfo:remove']" @click="handleMultiDelete">
{{ $t('pages.common.delete') }}
</a-button>
<a-button :disabled="!vxeCheckboxChecked(tableApi)" danger type="primary">
关灯
</a-button>
<a-button :disabled="!vxeCheckboxChecked(tableApi)" success type="primary">
开灯
</a-button>
</Space>
</template>
<template #action="{ row }">
<Space>
<ghost-button v-access:code="['property:lightInfo:edit']" @click.stop="handleEdit(row)">
{{ $t('pages.common.edit') }}
</ghost-button>
<Popconfirm :get-popup-container="getVxePopupContainer" placement="left" title="确认删除?"
@confirm="handleDelete(row)">
<ghost-button danger v-access:code="['property:lightInfo:remove']" @click.stop="">
{{ $t('pages.common.delete') }}
</ghost-button>
</Popconfirm>
</Space>
</template>
<template #isOn="{ row }">
<TableSwitch :checkedValue=true :unCheckedValue=false v-model:value="row.isOn" checked-children=""
un-checked-children="" :api="() => switchSingleLight({'id':row.id, 'isOn':row.isOn})"
@reload="() => tableApi.query()" />
</template>
</BasicTable>
</div>
<LightInfoDrawer @reload="tableApi.query()" />
</Page>
</template>

View File

@@ -0,0 +1,140 @@
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useVbenDrawer } from '@vben/common-ui'
import { $t } from '@vben/locales'
import { cloneDeep, getPopupContainer, handleNode } from '@vben/utils'
import { useVbenForm } from '#/adapter/form'
import { communityTree } from '#/api/property/community'
import { lightInfoAdd, lightInfoInfo, lightInfoUpdate } from '#/api/property/energyManagement/lightInfo'
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup'
import { drawerSchema } from './data'
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-2',
// 默认label宽度 px
labelWidth: 100,
// 通用配置项 会影响到所有表单项
componentProps: {
class: 'w-full',
}
},
schema: drawerSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
})
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
{
initializedGetter: defaultFormValueGetter(formApi),
currentGetter: defaultFormValueGetter(formApi),
},
)
const [BasicModal, modalApi] = useVbenDrawer({
// 在这里更改宽度
class: 'w-[550px]',
fullscreenButton: false,
onBeforeClose,
onClosed: handleClosed,
onConfirm: handleConfirm,
onOpenChange: async (isOpen) => {
if (!isOpen) {
return null
}
modalApi.drawerLoading(true)
const { id } = modalApi.getData() as { id?: number | string }
isUpdate.value = !!id
setupCommunitySelect()
if (isUpdate.value && id) {
const record = await lightInfoInfo(id)
await formApi.setValues(record)
}
await markInitialized()
modalApi.drawerLoading(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 ? lightInfoUpdate(data) : lightInfoAdd(data))
resetInitialized()
emit('reload')
modalApi.close()
} catch (error) {
console.error(error)
} finally {
modalApi.lock(false)
}
}
/**
* 初始化城市
*/
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: () => ({
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',
},
])
}
async function handleClosed() {
await formApi.resetForm()
resetInitialized()
}
</script>
<template>
<BasicModal :title="title">
<BasicForm />
</BasicModal>
</template>