Jelajahi Sumber

优化历史数据显示和导出

yanglzh 7 bulan lalu
induk
melakukan
60319ddf1c

+ 1 - 0
src/api/device/index.ts

@@ -104,6 +104,7 @@ export default {
   },
   device: {
     getDeviceAttributesHistoryList: (params: object) => get('/analysis/getDeviceAttributesHistoryList', params),
+    getDeviceAttributesHistoryListExport: (data: object) => file('/analysis/getDeviceAttributesHistoryListExport', data),
     getList: (params: object) => get('/product/device/bind_list', params),
     allList: (params: object) => get('/product/device/list', params),
     getSubList: (params: object) => get('/product/device/sub_list', params),

+ 3 - 34
src/views/iot/device/instance/component/chart.vue

@@ -79,6 +79,7 @@ import api from '/@/api/device'
 import * as echarts from 'echarts'
 import dayjs from 'dayjs'
 import { Download, FullScreen } from '@element-plus/icons-vue'
+import downloadFile from '/@/utils/download'
 
 const data = ref({ name: '', key: '', unit: '' })
 const historyDateRangeType = ref('1')
@@ -264,40 +265,8 @@ const handleCurrentChange = (page: number) => {
 
 // 导出数据
 const exportData = () => {
-	const historyParams = {
-		...params,
-		pageNum: currentPage.value,
-		pageSize: pageSize.value,
-	}
-
-	api.device.getDeviceAttributesHistoryList(historyParams).then((res: any) => {
-		// 创建CSV内容
-		let csvContent = '时间,属性值'
-		if (data.value.unit) {
-			csvContent += ',数据单位'
-		}
-		csvContent += '\n'
-
-		res.List.forEach((item: any) => {
-			csvContent += `${item.ts},${item.value}`
-			if (data.value.unit) {
-				csvContent += `,${data.value.unit}`
-			}
-			csvContent += '\n'
-		})
-
-		// 创建Blob对象
-		const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' })
-		const url = URL.createObjectURL(blob)
-
-		// 创建下载链接
-		const link = document.createElement('a')
-		link.setAttribute('href', url)
-		link.setAttribute('download', `${data.value.name}_历史数据_${dayjs().format('YYYYMMDD_HHmmss')}.csv`)
-		link.style.visibility = 'hidden'
-		document.body.appendChild(link)
-		link.click()
-		document.body.removeChild(link)
+	api.device.getDeviceAttributesHistoryListExport(params).then((res: any) => {
+		downloadFile(res, `${data.value.name}(${data.value.key}) 历史数据导出.xlsx`)
 	})
 }