Files
wer-admin/src/views/biz/device/index.vue
2026-11-05 09:27:00 +08:00

432 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="p-2 app-container deviceManage-device-page">
<div class="search-wrap">
<el-card shadow="hover" class="search-panel" :class="{ 'is-collapsed': !showSearch }">
<template #header>
<div class="panel-heading search-panel-toggle" @click.stop="showSearch = !showSearch">
<div><h3>筛选条件</h3></div>
</div>
</template>
<el-form ref="queryFormRef" :model="queryParams" :inline="true" class="query-form">
<el-form-item label="设备编号" prop="deviceNo">
<el-input v-model="queryParams.deviceNo" placeholder="请输入设备编号" clearable @keyup.enter="handleQuery" />
</el-form-item>
<el-form-item label="设备名称" prop="deviceName">
<el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
</el-form-item>
<el-form-item label="设备类型" prop="deviceType">
<el-select v-model="queryParams.deviceType" placeholder="全部类型" clearable>
<el-option v-for="item in deviceTypeDict" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item label="状态" prop="switchStatus">
<el-select v-model="queryParams.switchStatus" placeholder="全部状态" clearable>
<el-option v-for="item in switchStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
<el-button icon="Refresh" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
</el-card>
</div>
<el-card shadow="hover" class="table-panel">
<template #header>
<div class="toolbar-shell">
<div class="table-heading">
<h3>设备列表</h3>
</div>
<div class="toolbar-actions">
<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['deviceManage:device:add']">添加设备</el-button>
<el-button type="info" plain icon="Document" @click="handleImportTemplate" v-hasPermi="['deviceManage:device:import']">下载导入模板</el-button>
<el-button type="success" plain icon="Upload" @click="handleImport" v-hasPermi="['deviceManage:device:import']">批量导入</el-button>
<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['deviceManage:device:export']">批量导出</el-button>
<!-- <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['deviceManage:device:remove']">删除</el-button> -->
<right-toolbar v-model:show-search="showSearch" :search="false" @query-table="getList"></right-toolbar>
</div>
</div>
</template>
<el-table v-loading="loading" border class="data-table" :data="deviceList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="设备编号" align="center" prop="deviceNo" min-width="140" show-overflow-tooltip />
<el-table-column label="设备名称" align="center" prop="deviceName" min-width="140" show-overflow-tooltip />
<el-table-column label="设备类型" align="center" min-width="100">
<template #default="scope">{{ getDeviceTypeLabel(deviceTypeDict, scope.row.deviceType) }}</template>
</el-table-column>
<el-table-column label="所属商户" align="center" min-width="130" show-overflow-tooltip>
<template #default="scope">{{ scope.row.merchantName}}</template>
</el-table-column>
<el-table-column label="二维码" align="center" prop="qrCode" width="100">
<template #default="scope">
<image-preview v-if="scope.row.qrCode" :src="scope.row.qrCode" :width="50" :height="50" />
<span v-else class="text-muted"></span>
</template>
</el-table-column>
<el-table-column label="剩余金额" align="center" min-width="110">
<template #default="scope">
<span v-if="scope.row.remainingBalance == null" class="text-muted">—</span>
<span v-else class="amount-text">{{ scope.row.remainingBalance }}</span>
</template>
</el-table-column>
<el-table-column label="状态" align="center" min-width="110">
<template #default="scope">
<span class="switch-status" :class="getSwitchStatusClass(scope.row.switchStatus)">
{{ getSwitchStatusLabel(scope.row.switchStatus) }}
</span>
</template>
</el-table-column>
<el-table-column label="单价" align="center" prop="currentUnitPrice" min-width="90" />
<el-table-column label="操作" align="center" width="270" fixed="right">
<template #default="scope">
<div class="table-actions">
<el-button link type="primary" @click="handleView(scope.row)">详情</el-button>
<el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['deviceManage:device:edit']">编辑</el-button>
<!-- 离线设备只保留详情/编辑;清零、抄表、拉闸仅电表/水表支持 -->
<template v-if="canControl(scope.row)">
<el-button link type="primary" @click="handleClear(scope.row)">清零</el-button>
<el-button link type="primary" @click="handleReadMeter(scope.row)">抄表</el-button>
<!-- 已拉闸(强制/欠费)只显示恢复,正常状态才显示拉闸;水表恢复用供水措辞 -->
<el-button v-if="isCutOff(scope.row)" link type="success" @click="handleSwitch(scope.row)">
{{ getRestoreLabel(scope.row.deviceType) }}
</el-button>
<el-button v-else link type="warning" @click="handleSwitch(scope.row)">拉闸</el-button>
</template>
<!-- <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['deviceManage:device:remove']">删除</el-button> -->
</div>
</template>
</el-table-column>
</el-table>
<pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
</el-card>
<!-- 添加或修改智能水电设备对话框 -->
<DeviceFormDialog ref="deviceFormRef" @success="getList" />
<!-- 抄表对话框 -->
<MeterReadingDialog ref="meterReadingRef" @success="getList" />
<!-- 设备导入对话框:POST /deviceManage/device/importData -->
<el-dialog v-model="upload.open" :title="upload.title" width="420px" append-to-body>
<el-upload
ref="uploadRef"
:limit="1"
accept=".xlsx, .xls"
:disabled="upload.isUploading"
:http-request="uploadFile"
:auto-upload="false"
drag
>
<el-icon class="el-icon--upload">
<UploadFilled />
</el-icon>
<div class="el-upload__text">
将文件拖到此处,或
<em>点击上传</em>
</div>
<template #tip>
<div class="el-upload__tip text-center">仅允许导入 xls、xlsx 格式文件。</div>
</template>
</el-upload>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="submitFileForm">确 定</el-button>
<el-button @click="upload.open = false">取 消</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup name="Device" lang="ts">
import {
delDevice,
forceOffDevice,
importDeviceData,
listDevice,
resetDevice,
restoreDevice
} from '@/api/deviceManage/device';
import { DeviceQuery, DeviceVO } from '@/api/deviceManage/device/types';
import { useLoading } from '@/hooks/async/useLoading';
import { useSearchReset } from '@/hooks/form/useSearchReset';
import { useSearchToggle } from '@/hooks/form/useSearchToggle';
import { useTableSelection } from '@/hooks/table/useTableSelection';
import modal from '@/plugins/modal';
import { download as requestDownload } from '@/utils/request';
import { useRouter } from 'vue-router';
import DeviceFormDialog from './components/DeviceFormDialog.vue';
import MeterReadingDialog from './components/MeterReadingDialog.vue';
import {
CUT_OFF_STATUS,
METER_TYPES,
getDeviceTypeLabel,
getRestoreLabel,
getSwitchStatusClass,
getSwitchStatusLabel,
isWaterMeter,
switchStatusOptions,
useDeviceTypeDict
} from './constants';
const router = useRouter();
/** 设备类型字典(后端 ur_device_type 维护,只有电表/水表) */
const deviceTypeDict = useDeviceTypeDict();
const deviceList = ref<DeviceVO[]>([]);
const { loading, withLoading } = useLoading(true);
const { showSearch } = useSearchToggle();
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const deviceFormRef = ref<InstanceType<typeof DeviceFormDialog>>();
const meterReadingRef = ref<InstanceType<typeof MeterReadingDialog>>();
const uploadRef = ref<ElUploadInstance>();
/** 设备导入弹窗状态(上传请求见 importDeviceData) */
const upload = reactive({
open: false,
title: '',
isUploading: false
});
const queryParams = ref<DeviceQuery>({
pageNum: 1,
pageSize: 10,
merchantId: undefined,
roomId: undefined,
deviceNo: undefined,
deviceName: undefined,
deviceType: undefined,
communicationType: undefined,
unitName: undefined,
qrCode: undefined,
remainingBalance: undefined,
currentQuantity: undefined,
currentPower: undefined,
currentUnitPrice: undefined,
switchStatus: undefined,
onlineStatus: undefined,
status: undefined,
createBy: undefined,
updateBy: undefined
});
const { ids, multiple, handleSelectionChange } = useTableSelection<DeviceVO>(item => item.deviceId);
/** 是否支持清零/抄表/拉闸:仅电表、水表,且设备不在离线状态 */
const canControl = (row: Partial<DeviceVO>) => {
return METER_TYPES.includes(String(row.deviceType)) && String(row.switchStatus) !== '3';
};
/** 是否处于拉闸状态,拉闸后操作列为“恢复供电” */
const isCutOff = (row: Partial<DeviceVO>) => CUT_OFF_STATUS.includes(String(row.switchStatus));
/** 查询智能水电设备列表 */
const getList = async () => {
await withLoading(async () => {
const res = await listDevice(queryParams.value);
deviceList.value = res.data?.rows;
total.value = res.data?.total;
});
};
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.value.pageNum = 1;
getList();
};
const { resetQuery } = useSearchReset({
queryFormRef,
queryParams,
pageNumKey: 'pageNum',
pageSizeKey: 'pageSize',
initialPageSize: 10,
afterReset: () => {
handleQuery();
}
});
/** 新增按钮操作 */
const handleAdd = () => {
deviceFormRef.value?.openDialog();
};
/** 修改按钮操作 */
const handleUpdate = (row?: Partial<DeviceVO>) => {
const deviceId = row?.deviceId || ids.value[0];
deviceFormRef.value?.openDialog(deviceId);
};
/** 详情按钮操作:跳转到设备详情页 */
const handleView = (row: Partial<DeviceVO>) => {
router.push({ path: '/device/deviceDetail', query: { deviceId: String(row.deviceId) } });
};
/** 删除按钮操作 */
const handleDelete = async (row?: Partial<DeviceVO>) => {
const deviceIds = row?.deviceId || ids.value;
await modal.confirm('是否确认删除智能水电设备编号为"' + deviceIds + '"的数据项?');
await delDevice(deviceIds);
modal.msgSuccess('删除成功');
await getList();
};
/** 导出按钮操作 */
const handleExport = () => {
requestDownload(
'deviceManage/device/export',
{
...queryParams.value
},
`device_${new Date().getTime()}.xlsx`
);
};
/** 清零按钮操作 */
const handleClear = async (row: Partial<DeviceVO>) => {
const quantityText = isWaterMeter(row.deviceType) ? '总用水量和剩余水量' : '总电量和剩余电量';
await modal.confirm(`是否确认清零设备"${row.deviceName || row.deviceNo}"的余额/用量?\n清零后${quantityText}将归零,此操作不可撤销`);
// 清零走独立接口:POST /deviceManage/device/{deviceId}/reset
await resetDevice(row.deviceId as string | number);
modal.msgSuccess('清零成功');
await getList();
};
/** 抄表按钮操作:打开抄表弹窗 */
const handleReadMeter = (row: Partial<DeviceVO>) => {
meterReadingRef.value?.openDialog(row);
};
/** 拉闸 / 恢复按钮操作:水表说供水,电表说供电 */
const handleSwitch = async (row: Partial<DeviceVO>) => {
const deviceLabel = row.deviceName || row.deviceNo;
// 已拉闸的设备走恢复接口:POST /deviceManage/device/{deviceId}/restore
if (isCutOff(row)) {
const restoreLabel = getRestoreLabel(row.deviceType);
await modal.confirm(`是否确认对设备"${deviceLabel}"执行${restoreLabel}?\n${restoreLabel}后用户可以正常使用`);
await restoreDevice(row.deviceId as string | number);
modal.msgSuccess(`${restoreLabel}成功`);
await getList();
return;
}
const cutOffHint = `拉闸后该设备将停止${isWaterMeter(row.deviceType) ? '供水' : '供电'}`;
await modal.confirm(`是否确认对设备"${deviceLabel}"执行拉闸?\n${cutOffHint},用户无法正常使用。恢复需要管理员手动操作`);
await forceOffDevice(row.deviceId as string | number);
modal.msgSuccess('拉闸成功');
await getList();
};
/** 导入按钮操作 */
const handleImport = () => {
upload.title = '设备导入';
upload.open = true;
};
/** 下载导入模板(POST /deviceManage/device/importTemplate) */
const handleImportTemplate = () => {
requestDownload('deviceManage/device/importTemplate', {}, `设备导入模板.xlsx`);
};
/** 后端导入结果可能带 html 换行,转成纯文本再弹窗 */
const formatImportResultMessage = (message: unknown) => {
return String(message ?? '')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/&nbsp;/gi, ' ')
.replace(/<[^>]+>/g, '');
};
/** 自定义上传:走统一 axios 实例(自动带 token,失败由拦截器提示) */
const uploadFile = async (options: { file: File }) => {
upload.isUploading = true;
try {
const res: any = await importDeviceData(options.file);
upload.open = false;
uploadRef.value?.clearFiles();
ElMessageBox.alert(formatImportResultMessage(res?.msg), '导入结果', {
customClass: 'import-result-box'
});
getList();
} finally {
upload.isUploading = false;
}
};
/** 提交上传文件 */
const submitFileForm = () => {
uploadRef.value?.submit();
};
onMounted(() => {
getList();
});
</script>
<style lang="scss" scoped>
@use '@/assets/styles/components/page-shell' as pageShell;
@include pageShell.table-crud-page;
.data-table {
/* 操作列用文字按钮,覆盖 pageShell 默认的 32x32 图标按钮样式 */
:deep(.table-actions) {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 4px 10px;
}
/* 全局 _search-panel.scss 给 .data-table 内的 link 按钮加了 28x28 圆角底色且带 !important,
这里必须同样用 !important 才能覆盖成纯文字按钮 */
:deep(.table-actions .el-button.is-link) {
width: auto !important;
min-width: auto !important;
height: auto !important;
padding: 0 !important;
border-radius: 0 !important;
background: transparent !important;
font-size: 13px;
font-weight: 500;
}
:deep(.table-actions .el-button.is-link + .el-button.is-link) {
margin-left: 0 !important;
}
.amount-text {
color: var(--el-color-primary);
font-variant-numeric: tabular-nums;
}
.text-muted {
color: var(--el-text-color-placeholder);
}
.switch-status {
font-size: 13px;
font-weight: 500;
}
.switch-status--success {
color: var(--el-color-success);
}
.switch-status--danger {
color: var(--el-color-danger);
}
.switch-status--warning {
color: var(--el-color-warning);
}
.switch-status--info {
color: var(--el-text-color-secondary);
}
}
</style>