Vue3 + Element Plus版本iot前端迁移到vben版本

This commit is contained in:
Administrator
2025-10-07 19:58:59 +08:00
parent e4707ef380
commit 877a03df4a
124 changed files with 20425 additions and 168 deletions

View File

@@ -0,0 +1,196 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { getSimpleRuleSceneList } from '#/api/iot/rule/scene';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';
/** 新增/修改告警配置的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '配置名称',
component: 'Input',
componentProps: {
placeholder: '请输入配置名称',
},
rules: 'required',
},
{
fieldName: 'description',
label: '配置描述',
component: 'Textarea',
componentProps: {
placeholder: '请输入配置描述',
rows: 3,
},
},
{
fieldName: 'level',
label: '告警级别',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.IOT_ALERT_LEVEL, 'number'),
placeholder: '请选择告警级别',
},
rules: 'required',
},
{
fieldName: 'status',
label: '配置状态',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: 'required',
},
{
fieldName: 'sceneRuleIds',
label: '关联场景联动规则',
component: 'ApiSelect',
componentProps: {
api: getSimpleRuleSceneList,
labelField: 'name',
valueField: 'id',
mode: 'multiple',
placeholder: '请选择关联的场景联动规则',
},
rules: 'required',
},
{
fieldName: 'receiveUserIds',
label: '接收的用户',
component: 'ApiSelect',
componentProps: {
api: getSimpleUserList,
labelField: 'nickname',
valueField: 'id',
mode: 'multiple',
placeholder: '请选择接收的用户',
},
rules: 'required',
},
{
fieldName: 'receiveTypes',
label: '接收类型',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.IOT_ALERT_RECEIVE_TYPE, 'number'),
mode: 'multiple',
placeholder: '请选择接收类型',
},
rules: 'required',
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: '配置名称',
component: 'Input',
componentProps: {
placeholder: '请输入配置名称',
allowClear: true,
},
},
{
fieldName: 'status',
label: '配置状态',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
placeholder: '请选择配置状态',
allowClear: true,
},
},
{
fieldName: 'createTime',
label: '创建时间',
component: 'RangePicker',
componentProps: getRangePickerDefaultProps(),
},
];
}
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
return [
{ type: 'checkbox', width: 40 },
{
field: 'id',
title: '配置编号',
minWidth: 80,
},
{
field: 'name',
title: '配置名称',
minWidth: 150,
},
{
field: 'description',
title: '配置描述',
minWidth: 200,
},
{
field: 'level',
title: '告警级别',
minWidth: 100,
slots: { default: 'level' },
},
{
field: 'status',
title: '配置状态',
minWidth: 100,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS },
},
},
{
field: 'sceneRuleIds',
title: '关联场景联动规则',
minWidth: 150,
slots: { default: 'sceneRules' },
},
{
field: 'receiveUserNames',
title: '接收人',
minWidth: 150,
},
{
field: 'receiveTypes',
title: '接收类型',
minWidth: 150,
slots: { default: 'receiveTypes' },
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 160,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@@ -0,0 +1,187 @@
<script setup lang="ts">
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AlertConfigApi } from '#/api/iot/alert/config';
import { Page, useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteAlertConfig, getAlertConfigPage } from '#/api/iot/alert/config';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
import AlertConfigForm from '../modules/AlertConfigForm.vue';
defineOptions({ name: 'IoTAlertConfig' });
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: AlertConfigForm,
destroyOnClose: true,
});
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
// 获取告警级别文本
const getLevelText = (level?: number) => {
const levelMap: Record<number, string> = {
1: '提示',
2: '一般',
3: '警告',
4: '严重',
5: '紧急',
};
return level ? levelMap[level] || `级别${level}` : '-';
};
// 获取告警级别颜色
const getLevelColor = (level?: number) => {
const colorMap: Record<number, string> = {
1: 'blue',
2: 'green',
3: 'orange',
4: 'red',
5: 'purple',
};
return level ? colorMap[level] || 'default' : 'default';
};
// 获取接收类型文本
const getReceiveTypeText = (type?: number) => {
const typeMap: Record<number, string> = {
1: '站内信',
2: '邮箱',
3: '短信',
4: '微信',
5: '钉钉',
};
return type ? typeMap[type] || `类型${type}` : '-';
};
/** 创建告警配置 */
function handleCreate() {
formModalApi.setData(null).open();
}
/** 编辑告警配置 */
function handleEdit(row: AlertConfigApi.AlertConfig) {
formModalApi.setData(row).open();
}
/** 删除告警配置 */
async function handleDelete(row: AlertConfigApi.AlertConfig) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0,
});
try {
await deleteAlertConfig(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
});
onRefresh();
} finally {
hideLoading();
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getAlertConfigPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<AlertConfigApi.AlertConfig>,
});
</script>
<template>
<Page auto-content-height>
<FormModal @success="onRefresh" />
<Grid table-title="告警配置列表">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['告警配置']),
type: 'primary',
icon: ACTION_ICON.ADD,
onClick: handleCreate,
},
]"
/>
</template>
<!-- 告警级别列 -->
<template #level="{ row }">
<a-tag :color="getLevelColor(row.level)">
{{ getLevelText(row.level) }}
</a-tag>
</template>
<!-- 关联场景联动规则列 -->
<template #sceneRules="{ row }">
<span>{{ row.sceneRuleIds?.length || 0 }} </span>
</template>
<!-- 接收类型列 -->
<template #receiveTypes="{ row }">
<a-tag
v-for="(type, index) in row.receiveTypes"
:key="index"
class="mr-1"
>
{{ getReceiveTypeText(type) }}
</a-tag>
</template>
<!-- 操作列 -->
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,94 @@
<script setup lang="ts">
import type { AlertConfigApi } from '#/api/iot/alert/config';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createAlertConfig,
getAlertConfig,
updateAlertConfig,
} from '#/api/iot/alert/config';
import { $t } from '#/locales';
import { useFormSchema } from '../config/data';
defineOptions({ name: 'IoTAlertConfigForm' });
const emit = defineEmits(['success']);
const formData = ref<AlertConfigApi.AlertConfig>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['告警配置'])
: $t('ui.actionTitle.create', ['告警配置']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
},
wrapperClass: 'grid-cols-2',
layout: 'horizontal',
schema: useFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
// 提交表单
const data = (await formApi.getValues()) as AlertConfigApi.AlertConfig;
try {
await (formData.value?.id ? updateAlertConfig(data) : createAlertConfig(data));
// 关闭并提示
await modalApi.close();
emit('success');
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
// 加载数据
const data = modalApi.getData<AlertConfigApi.AlertConfig>();
if (!data || !data.id) {
// 新增时设置默认值
await formApi.setValues({
status: 0,
sceneRuleIds: [],
receiveUserIds: [],
receiveTypes: [],
});
return;
}
modalApi.lock();
try {
formData.value = await getAlertConfig(data.id);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal class="w-3/5" :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>

View File

@@ -0,0 +1,148 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { getSimpleAlertConfigList } from '#/api/iot/alert/config';
import { getSimpleDeviceList } from '#/api/iot/device/device';
import { getSimpleProductList } from '#/api/iot/product/product';
import { getRangePickerDefaultProps } from '#/utils';
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'configId',
label: '告警配置',
component: 'ApiSelect',
componentProps: {
api: getSimpleAlertConfigList,
labelField: 'name',
valueField: 'id',
placeholder: '请选择告警配置',
allowClear: true,
showSearch: true,
},
},
{
fieldName: 'configLevel',
label: '告警级别',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.IOT_ALERT_LEVEL, 'number'),
placeholder: '请选择告警级别',
allowClear: true,
},
},
{
fieldName: 'productId',
label: '产品',
component: 'ApiSelect',
componentProps: {
api: getSimpleProductList,
labelField: 'name',
valueField: 'id',
placeholder: '请选择产品',
allowClear: true,
showSearch: true,
},
},
{
fieldName: 'deviceId',
label: '设备',
component: 'ApiSelect',
componentProps: {
api: getSimpleDeviceList,
labelField: 'deviceName',
valueField: 'id',
placeholder: '请选择设备',
allowClear: true,
showSearch: true,
},
},
{
fieldName: 'processStatus',
label: '是否处理',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING),
placeholder: '请选择是否处理',
allowClear: true,
},
},
{
fieldName: 'createTime',
label: '创建时间',
component: 'RangePicker',
componentProps: getRangePickerDefaultProps(),
},
];
}
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
return [
{ type: 'checkbox', width: 40 },
{
field: 'id',
title: '记录编号',
minWidth: 80,
},
{
field: 'configName',
title: '告警名称',
minWidth: 150,
},
{
field: 'configLevel',
title: '告警级别',
minWidth: 100,
slots: { default: 'configLevel' },
},
{
field: 'productId',
title: '产品名称',
minWidth: 120,
slots: { default: 'product' },
},
{
field: 'deviceId',
title: '设备名称',
minWidth: 120,
slots: { default: 'device' },
},
{
field: 'deviceMessage',
title: '触发的设备消息',
minWidth: 150,
slots: { default: 'deviceMessage' },
},
{
field: 'processStatus',
title: '是否处理',
minWidth: 100,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
},
},
{
field: 'processRemark',
title: '处理结果',
minWidth: 150,
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 100,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@@ -0,0 +1,239 @@
<script setup lang="ts">
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { h, onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { Modal, message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import type { AlertRecord } from '#/api/iot/alert/record';
import { getAlertRecordPage, processAlertRecord } from '#/api/iot/alert/record';
import { getSimpleDeviceList } from '#/api/iot/device/device';
import { getSimpleProductList } from '#/api/iot/product/product';
import { useGridColumns, useGridFormSchema } from './data';
defineOptions({ name: 'IoTAlertRecord' });
const productList = ref<any[]>([]);
const deviceList = ref<any[]>([]);
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
// 加载产品和设备列表
const loadData = async () => {
productList.value = await getSimpleProductList();
deviceList.value = await getSimpleDeviceList();
};
// 获取告警级别文本
const getLevelText = (level?: number) => {
const levelMap: Record<number, string> = {
1: '提示',
2: '一般',
3: '警告',
4: '严重',
5: '紧急',
};
return level ? levelMap[level] || `级别${level}` : '-';
};
// 获取告警级别颜色
const getLevelColor = (level?: number) => {
const colorMap: Record<number, string> = {
1: 'blue',
2: 'green',
3: 'orange',
4: 'red',
5: 'purple',
};
return level ? colorMap[level] || 'default' : 'default';
};
// 获取产品名称
const getProductName = (productId?: number) => {
if (!productId) return '-';
const product = productList.value.find((p: any) => p.id === productId);
return product?.name || '加载中...';
};
// 获取设备名称
const getDeviceName = (deviceId?: number) => {
if (!deviceId) return '-';
const device = deviceList.value.find((d: any) => d.id === deviceId);
return device?.deviceName || '加载中...';
};
// 处理告警记录
const handleProcess = async (row: AlertRecord) => {
Modal.confirm({
title: '处理告警记录',
content: h('div', [
h('p', '请输入处理原因:'),
h('textarea', {
id: 'processRemark',
class: 'ant-input',
rows: 3,
placeholder: '请输入处理原因',
}),
]),
async onOk() {
const textarea = document.getElementById('processRemark') as HTMLTextAreaElement;
const processRemark = textarea?.value || '';
if (!processRemark) {
message.warning('请输入处理原因');
return Promise.reject();
}
const hideLoading = message.loading({
content: '正在处理...',
duration: 0,
});
try {
await processAlertRecord(row.id as number, processRemark);
message.success('处理成功');
onRefresh();
} catch (error) {
console.error('处理失败:', error);
return Promise.reject();
} finally {
hideLoading();
}
},
});
};
// 查看告警记录详情
const handleView = (row: AlertRecord) => {
Modal.info({
title: '告警记录详情',
width: 600,
content: h('div', { class: 'space-y-2' }, [
h('div', [
h('span', { class: 'font-semibold' }, '告警名称:'),
h('span', row.configName || '-'),
]),
h('div', [
h('span', { class: 'font-semibold' }, '告警级别:'),
h('span', getLevelText(row.configLevel)),
]),
h('div', [
h('span', { class: 'font-semibold' }, '设备消息:'),
h('pre', { class: 'mt-1 text-xs bg-gray-50 p-2 rounded' }, row.deviceMessage || '-'),
]),
h('div', [
h('span', { class: 'font-semibold' }, '处理结果:'),
h('span', row.processRemark || '-'),
]),
h('div', [
h('span', { class: 'font-semibold' }, '处理时间:'),
h('span', row.processTime ? new Date(row.processTime).toLocaleString('zh-CN') : '-'),
]),
]),
});
};
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getAlertRecordPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<AlertRecord>,
});
onMounted(() => {
loadData();
});
</script>
<template>
<Page auto-content-height>
<Grid table-title="告警记录列表">
<!-- 告警级别列 -->
<template #configLevel="{ row }">
<a-tag :color="getLevelColor(row.configLevel)">
{{ getLevelText(row.configLevel) }}
</a-tag>
</template>
<!-- 产品名称列 -->
<template #product="{ row }">
<span>{{ getProductName(row.productId) }}</span>
</template>
<!-- 设备名称列 -->
<template #device="{ row }">
<span>{{ getDeviceName(row.deviceId) }}</span>
</template>
<!-- 设备消息列 -->
<template #deviceMessage="{ row }">
<a-popover
v-if="row.deviceMessage"
placement="topLeft"
trigger="hover"
:overlayStyle="{ maxWidth: '600px' }"
>
<template #content>
<pre class="text-xs">{{ row.deviceMessage }}</pre>
</template>
<VbenButton size="small" type="link">
<Icon icon="ant-design:eye-outlined" class="mr-1" />
查看消息
</VbenButton>
</a-popover>
<span v-else class="text-gray-400">-</span>
</template>
<!-- 操作列 -->
<template #actions="{ row }">
<TableAction
:actions="[
{
label: '处理',
type: 'link',
icon: ACTION_ICON.EDIT,
onClick: handleProcess.bind(null, row),
ifShow: !row.processStatus,
},
{
label: '查看',
type: 'link',
icon: ACTION_ICON.VIEW,
onClick: handleView.bind(null, row),
ifShow: row.processStatus,
},
]"
/>
</template>
</Grid>
</Page>
</template>