Skip to content

Commit a5565bb

Browse files
committed
【功能新增】IoT:设备管理,增加批量导入
1 parent 8730056 commit a5565bb

File tree

3 files changed

+159
-3
lines changed

3 files changed

+159
-3
lines changed

src/api/iot/device/device/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,18 @@ export const DeviceApi = {
123123
return await request.get({ url: `/iot/device/simple-list?`, params: { deviceType } })
124124
},
125125

126-
// 获取设备属性最新数据
126+
// 获取设备属性最���数据
127127
getDevicePropertiesLatestData: async (params: any) => {
128128
return await request.get({ url: `/iot/device/data/latest`, params })
129129
},
130130

131131
// 获取设备属性历史数据
132132
getDevicePropertiesHistoryData: async (params: any) => {
133133
return await request.get({ url: `/iot/device/data/history`, params })
134+
},
135+
136+
// 获取导入模板
137+
importDeviceTemplate: async () => {
138+
return await request.download({ url: `/iot/device/get-import-template` })
134139
}
135140
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<template>
2+
<Dialog v-model="dialogVisible" title="设备导入" width="400">
3+
<el-upload
4+
ref="uploadRef"
5+
v-model:file-list="fileList"
6+
:action="importUrl + '?updateSupport=' + updateSupport"
7+
:auto-upload="false"
8+
:disabled="formLoading"
9+
:headers="uploadHeaders"
10+
:limit="1"
11+
:on-error="submitFormError"
12+
:on-exceed="handleExceed"
13+
:on-success="submitFormSuccess"
14+
accept=".xlsx, .xls"
15+
drag
16+
>
17+
<Icon icon="ep:upload" />
18+
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
19+
<template #tip>
20+
<div class="el-upload__tip text-center">
21+
<div class="el-upload__tip">
22+
<el-checkbox v-model="updateSupport" />
23+
是否更新已经存在的设备数据
24+
</div>
25+
<span>仅允许导入 xls、xlsx 格式文件。</span>
26+
<el-link
27+
:underline="false"
28+
style="font-size: 12px; vertical-align: baseline"
29+
type="primary"
30+
@click="importTemplate"
31+
>
32+
下载模板
33+
</el-link>
34+
</div>
35+
</template>
36+
</el-upload>
37+
<template #footer>
38+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
39+
<el-button @click="dialogVisible = false">取 消</el-button>
40+
</template>
41+
</Dialog>
42+
</template>
43+
44+
<script lang="ts" setup>
45+
import { DeviceApi } from '@/api/iot/device/device'
46+
import { getAccessToken, getTenantId } from '@/utils/auth'
47+
import download from '@/utils/download'
48+
49+
defineOptions({ name: 'IoTDeviceImportForm' })
50+
51+
const message = useMessage() // 消息弹窗
52+
53+
const dialogVisible = ref(false) // 弹窗的是否展示
54+
const formLoading = ref(false) // 表单的加载中
55+
const uploadRef = ref()
56+
const importUrl =
57+
import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/iot/device/import'
58+
const uploadHeaders = ref() // 上传 Header 头
59+
const fileList = ref([]) // 文件列表
60+
const updateSupport = ref(0) // 是否更新已经存在的设备数据
61+
62+
/** 打开弹窗 */
63+
const open = () => {
64+
dialogVisible.value = true
65+
updateSupport.value = 0
66+
fileList.value = []
67+
resetForm()
68+
}
69+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
70+
71+
/** 提交表单 */
72+
const submitForm = async () => {
73+
if (fileList.value.length == 0) {
74+
message.error('请上传文件')
75+
return
76+
}
77+
// 提交请求
78+
uploadHeaders.value = {
79+
Authorization: 'Bearer ' + getAccessToken(),
80+
'tenant-id': getTenantId()
81+
}
82+
formLoading.value = true
83+
uploadRef.value!.submit()
84+
}
85+
86+
/** 文件上传成功 */
87+
const emits = defineEmits(['success'])
88+
const submitFormSuccess = (response: any) => {
89+
if (response.code !== 0) {
90+
message.error(response.msg)
91+
formLoading.value = false
92+
return
93+
}
94+
// 拼接提示语
95+
const data = response.data
96+
let text = '上传成功数量:' + data.createDeviceNames.length + ';'
97+
for (let deviceName of data.createDeviceNames) {
98+
text += '< ' + deviceName + ' >'
99+
}
100+
text += '更新成功数量:' + data.updateDeviceNames.length + ';'
101+
for (const deviceName of data.updateDeviceNames) {
102+
text += '< ' + deviceName + ' >'
103+
}
104+
text += '更新失败数量:' + Object.keys(data.failureDeviceNames).length + ';'
105+
for (const deviceName in data.failureDeviceNames) {
106+
text += '< ' + deviceName + ': ' + data.failureDeviceNames[deviceName] + ' >'
107+
}
108+
message.alert(text)
109+
formLoading.value = false
110+
dialogVisible.value = false
111+
// 发送操作成功的事件
112+
emits('success')
113+
}
114+
115+
/** 上传错误提示 */
116+
const submitFormError = (): void => {
117+
message.error('上传失败,请您重新上传!')
118+
formLoading.value = false
119+
}
120+
121+
/** 重置表单 */
122+
const resetForm = async (): Promise<void> => {
123+
// 重置上传状态和文件
124+
formLoading.value = false
125+
await nextTick()
126+
uploadRef.value?.clearFiles()
127+
}
128+
129+
/** 文件数超出提示 */
130+
const handleExceed = (): void => {
131+
message.error('最多只能上传一个文件!')
132+
}
133+
134+
/** 下载模板操作 */
135+
const importTemplate = async () => {
136+
const res = await DeviceApi.importDeviceTemplate()
137+
download.excel(res, '设备导入模版.xls')
138+
}
139+
</script>

src/views/iot/device/device/index.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
>
124124
<Icon icon="ep:download" class="mr-5px" /> 导出
125125
</el-button>
126+
<el-button type="warning" plain @click="handleImport" v-hasPermi="['iot:device:import']">
127+
<Icon icon="ep:upload" /> 导入
128+
</el-button>
126129
<el-button
127130
type="primary"
128131
plain
@@ -355,6 +358,8 @@
355358
<DeviceForm ref="formRef" @success="getList" />
356359
<!-- 分组表单组件 -->
357360
<DeviceGroupForm ref="groupFormRef" @success="getList" />
361+
<!-- 导入表单组件 -->
362+
<DeviceImportForm ref="importFormRef" @success="getList" />
358363
</template>
359364

360365
<script setup lang="ts">
@@ -366,14 +371,15 @@ import { ProductApi, ProductVO } from '@/api/iot/product/product'
366371
import { DeviceGroupApi, DeviceGroupVO } from '@/api/iot/device/group'
367372
import download from '@/utils/download'
368373
import DeviceGroupForm from './DeviceGroupForm.vue'
374+
import DeviceImportForm from './DeviceImportForm.vue'
369375
370376
/** IoT 设备列表 */
371377
defineOptions({ name: 'IoTDevice' })
372378
373379
const message = useMessage() // 消息弹窗
374380
const { t } = useI18n() // 国际化
375381
376-
const loading = ref(true) // 列表���加载中
382+
const loading = ref(true) // 列表加载中
377383
const list = ref<DeviceVO[]>([]) // 列表的数据
378384
const total = ref(0) // 列表的总页数
379385
const queryParams = reactive({
@@ -420,7 +426,7 @@ const resetQuery = () => {
420426
handleQuery()
421427
}
422428
423-
/** ���加/修改操作 */
429+
/** 添加/修改操作 */
424430
const formRef = ref()
425431
const openForm = (type: string, id?: number) => {
426432
formRef.value.open(type, id)
@@ -488,6 +494,12 @@ const openModel = (id: number) => {
488494
push({ name: 'IoTDeviceDetail', params: { id }, query: { tab: 'model' } })
489495
}
490496
497+
/** 设备导入 */
498+
const importFormRef = ref()
499+
const handleImport = () => {
500+
importFormRef.value.open()
501+
}
502+
491503
/** 初始化 **/
492504
onMounted(async () => {
493505
getList()

0 commit comments

Comments
 (0)