2
0

fix:历史设备界面路径修改以及组件封装

This commit is contained in:
zhongzm
2024-12-24 18:39:26 +08:00
parent b40b81ffb8
commit a99456f70a
2 changed files with 208 additions and 199 deletions

View File

@@ -1,203 +1,7 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { TableColumnsType } from 'ant-design-vue'
import { HistoryOutlined } from '@ant-design/icons-vue'
import { useI18n } from "vue-i18n";
import { fetchHistoricalDevices } from '@/service/api/auth';
const { t } = useI18n();
// 格式化时间戳
function formatTimestamp(timestamp: number): string {
const date = new Date(timestamp);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
// 格式化流量
function formatDataUsage(bytes: number): string {
if (bytes < 1024) {
return `${bytes}B`;
} else if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(2)}KB`;
} else if (bytes < 1024 * 1024 * 1024) {
return `${(bytes / (1024 * 1024)).toFixed(2)}MB`;
} else {
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)}GB`;
}
}
// 定义记录类型
interface DeviceRecord {
id: number;
deviceName: string;
macAddress: string;
dataUsage: string;
connectionTime: string;
disconnectionTime: string;
}
// 设备列表数据
const deviceList = ref<DeviceRecord[]>([]);
const loading = ref(false);
const columns: TableColumnsType = [
{
title: t('page.records.clientname'),
dataIndex: 'deviceName',
key: 'deviceName',
width: '30%'
},
{
title: t('page.records.clientmac'),
dataIndex: 'macAddress',
key: 'macAddress',
width: '40%'
},
{
title: t('page.records.dataUsage'),
dataIndex: 'dataUsage',
key: 'dataUsage',
width: '30%',
align: 'right'
}
];
// 添加展开行控制
const expandedRowKeys = ref<number[]>([]);
// 处理展开行变化
function handleExpandChange(expanded: boolean, record: DeviceRecord) {
if (expanded) {
expandedRowKeys.value = [record.id];
} else {
expandedRowKeys.value = [];
}
}
// 获取历史设备列表数据
async function getHistoricalDeviceList() {
loading.value = true;
try {
const { data, error } = await fetchHistoricalDevices();
if (!error && data) {
deviceList.value = data.rows.map(item => ({
id: item.id,
deviceName: item.clientName,
macAddress: item.clientMac,
dataUsage: formatDataUsage(item.duration),
connectionTime: formatTimestamp(item.startTime),
disconnectionTime: formatTimestamp(item.endTime)
}));
}
} catch (err) {
console.error('Failed to fetch historical device list:', err);
} finally {
loading.value = false;
}
}
// 刷新设备列表
async function handleRefresh() {
await getHistoricalDeviceList();
}
// 组件挂载时获取数据
onMounted(() => {
getHistoricalDeviceList();
});
import records from '@/views/userInfo/records/index.vue'
</script>
<template>
<div class="device-list-container">
<a-card :bordered="false">
<template #title>
<div class="card-title">
<HistoryOutlined />
<span>{{ t('page.records.clienthistory') }}</span>
</div>
</template>
<template #extra>
<a-button type="primary" :loading="loading" @click="handleRefresh">
<template #icon>
<span class="i-carbon:refresh"></span>
</template>
{{ t('page.records.refresh') }}
</a-button>
</template>
<a-table
:loading="loading"
:columns="columns"
:data-source="deviceList"
:row-key="(record: DeviceRecord) => record.id"
:expanded-row-keys="expandedRowKeys"
@expand="handleExpandChange"
:pagination="{
total: deviceList.length,
pageSize: 10,
showSizeChanger: false,
showTotal: (total) => `${total}`
}"
>
<template #expandedRowRender="{ record }">
<div class="pl-4">
<div>{{ t('page.records.starttime') }}: {{ record.connectionTime }}</div>
<div>{{ t('page.records.endtime') }}: {{ record.disconnectionTime }}</div>
</div>
</template>
</a-table>
</a-card>
</div>
<records/>
</template>
<style scoped>
.device-list-container {
padding: 8px;
background-color: #f5f5f7;
min-height: 100vh;
}
.card-title {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
font-weight: 500;
}
.pl-4 {
padding-left: 1rem;
}
:deep(.ant-card-head) {
border-bottom: 1px solid #f0f0f0;
padding: 0 12px;
}
:deep(.ant-card-body) {
padding: 12px;
}
:deep(.ant-table-wrapper) {
width: 100%;
}
:deep(.ant-table) {
width: 100%;
}
@media screen and (min-width: 768px) {
.device-list-container {
padding: 16px;
}
}
</style>
<style scoped></style>

View File

@@ -0,0 +1,205 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { TableColumnsType } from 'ant-design-vue'
import { HistoryOutlined } from '@ant-design/icons-vue'
import { useI18n } from "vue-i18n";
import { fetchHistoricalDevices } from '@/service/api/auth';
defineOptions({
name: 'records'
});
const { t } = useI18n();
// 格式化时间戳
function formatTimestamp(timestamp: number): string {
const date = new Date(timestamp);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
// 格式化流量
function formatDataUsage(bytes: number): string {
if (bytes < 1024) {
return `${bytes}B`;
} else if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(2)}KB`;
} else if (bytes < 1024 * 1024 * 1024) {
return `${(bytes / (1024 * 1024)).toFixed(2)}MB`;
} else {
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)}GB`;
}
}
// 定义记录类型
interface DeviceRecord {
id: number;
deviceName: string;
macAddress: string;
dataUsage: string;
connectionTime: string;
disconnectionTime: string;
}
// 设备列表数据
const deviceList = ref<DeviceRecord[]>([]);
const loading = ref(false);
const columns: TableColumnsType = [
{
title: t('page.records.clientname'),
dataIndex: 'deviceName',
key: 'deviceName',
width: '30%'
},
{
title: t('page.records.clientmac'),
dataIndex: 'macAddress',
key: 'macAddress',
width: '40%'
},
{
title: t('page.records.dataUsage'),
dataIndex: 'dataUsage',
key: 'dataUsage',
width: '30%',
align: 'right'
}
];
// 添加展开行控制
const expandedRowKeys = ref<number[]>([]);
// 处理展开行变化
function handleExpandChange(expanded: boolean, record: DeviceRecord) {
if (expanded) {
expandedRowKeys.value = [record.id];
} else {
expandedRowKeys.value = [];
}
}
// 获取历史设备列表数据
async function getHistoricalDeviceList() {
loading.value = true;
try {
const { data, error } = await fetchHistoricalDevices();
if (!error && data) {
deviceList.value = data.rows.map(item => ({
id: item.id,
deviceName: item.clientName,
macAddress: item.clientMac,
dataUsage: formatDataUsage(item.duration),
connectionTime: formatTimestamp(item.startTime),
disconnectionTime: formatTimestamp(item.endTime)
}));
}
} catch (err) {
console.error('Failed to fetch historical device list:', err);
} finally {
loading.value = false;
}
}
// 刷新设备列表
async function handleRefresh() {
await getHistoricalDeviceList();
}
// 组件挂载时获取数据
onMounted(() => {
getHistoricalDeviceList();
});
</script>
<template>
<div class="device-list-container">
<a-card :bordered="false">
<template #title>
<div class="card-title">
<HistoryOutlined />
<span>{{ t('page.records.clienthistory') }}</span>
</div>
</template>
<template #extra>
<a-button type="primary" :loading="loading" @click="handleRefresh">
<template #icon>
<span class="i-carbon:refresh"></span>
</template>
{{ t('page.records.refresh') }}
</a-button>
</template>
<a-table
:loading="loading"
:columns="columns"
:data-source="deviceList"
:row-key="(record: DeviceRecord) => record.id"
:expanded-row-keys="expandedRowKeys"
@expand="handleExpandChange"
:pagination="{
total: deviceList.length,
pageSize: 10,
showSizeChanger: false,
showTotal: (total) => `${total}`
}"
>
<template #expandedRowRender="{ record }">
<div class="pl-4">
<div>{{ t('page.records.starttime') }}: {{ record.connectionTime }}</div>
<div>{{ t('page.records.endtime') }}: {{ record.disconnectionTime }}</div>
</div>
</template>
</a-table>
</a-card>
</div>
</template>
<style scoped>
.device-list-container {
padding: 8px;
background-color: #f5f5f7;
min-height: 100vh;
}
.card-title {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
font-weight: 500;
}
.pl-4 {
padding-left: 1rem;
}
:deep(.ant-card-head) {
border-bottom: 1px solid #f0f0f0;
padding: 0 12px;
}
:deep(.ant-card-body) {
padding: 12px;
}
:deep(.ant-table-wrapper) {
width: 100%;
}
:deep(.ant-table) {
width: 100%;
}
@media screen and (min-width: 768px) {
.device-list-container {
padding: 16px;
}
}
</style>