Skip to content

Commit cc45fd5

Browse files
committed
【功能新增】IoT:设备管理界面增加设备分组功能
1 parent a2dec90 commit cc45fd5

File tree

3 files changed

+127
-11
lines changed

3 files changed

+127
-11
lines changed

src/api/iot/device/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ export const DeviceApi = {
8282
return await request.put({ url: `/iot/device/update-status`, data })
8383
},
8484

85+
// 修改设备分组
86+
updateDeviceGroup: async (data: {
87+
ids: number[]
88+
groupIds: number[]
89+
}) => {
90+
return await request.put({ url: `/iot/device/update-group`, data })
91+
},
92+
8593
// 删除单个设备
8694
deleteDevice: async (id: number) => {
8795
return await request.delete({ url: `/iot/device/delete?id=` + id })
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<template>
2+
<Dialog :title="'添加设备到分组'" v-model="dialogVisible">
3+
<el-form
4+
ref="formRef"
5+
:model="formData"
6+
:rules="formRules"
7+
label-width="100px"
8+
v-loading="formLoading"
9+
>
10+
<el-form-item label="设备分组" prop="groupIds">
11+
<el-select v-model="formData.groupIds" placeholder="请选择设备分组" multiple clearable>
12+
<el-option
13+
v-for="group in deviceGroups"
14+
:key="group.id"
15+
:label="group.name"
16+
:value="group.id"
17+
/>
18+
</el-select>
19+
</el-form-item>
20+
</el-form>
21+
<template #footer>
22+
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
23+
<el-button @click="dialogVisible = false">取 消</el-button>
24+
</template>
25+
</Dialog>
26+
</template>
27+
28+
<script setup lang="ts">
29+
import { DeviceApi } from '@/api/iot/device'
30+
import { DeviceGroupApi } from '@/api/iot/device/group'
31+
32+
defineOptions({ name: 'IoTDeviceGroupForm' })
33+
34+
const { t } = useI18n() // 国际化
35+
const message = useMessage() // 消息窗
36+
37+
const dialogVisible = ref(false) // 弹窗的是否展示
38+
const formLoading = ref(false) // 表单的加载中
39+
const formData = ref({
40+
ids: [] as number[],
41+
groupIds: [] as number[]
42+
})
43+
const formRules = reactive({
44+
groupIds: [{ required: true, message: '设备分组不能为空', trigger: 'change' }]
45+
})
46+
const formRef = ref() // 表单 Ref
47+
const deviceGroups = ref<any[]>([]) // 设备分组列表
48+
49+
/** 打开弹窗 */
50+
const open = async (ids: number[]) => {
51+
dialogVisible.value = true
52+
resetForm()
53+
formData.value.ids = ids
54+
55+
// 加载设备分组列表
56+
try {
57+
deviceGroups.value = await DeviceGroupApi.getSimpleDeviceGroupList()
58+
} catch (error) {
59+
console.error('加载设备分组列表失败:', error)
60+
}
61+
}
62+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
63+
64+
/** 提交表单 */
65+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
66+
const submitForm = async () => {
67+
// 校验表单
68+
await formRef.value.validate()
69+
// 提交请求
70+
formLoading.value = true
71+
try {
72+
await DeviceApi.updateDeviceGroup(formData.value)
73+
message.success(t('common.updateSuccess'))
74+
dialogVisible.value = false
75+
// 发送操作成功的事件
76+
emit('success')
77+
} finally {
78+
formLoading.value = false
79+
}
80+
}
81+
82+
/** 重置表单 */
83+
const resetForm = () => {
84+
formData.value = {
85+
ids: [],
86+
groupIds: []
87+
}
88+
formRef.value?.resetFields()
89+
}
90+
</script>

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@
113113
>
114114
<Icon icon="ep:download" class="mr-5px" /> 导出
115115
</el-button>
116+
<el-button
117+
type="primary"
118+
plain
119+
@click="openGroupForm"
120+
:disabled="selectedIds.length === 0"
121+
v-hasPermi="['iot:device:update']"
122+
>
123+
<Icon icon="ep:folder-add" class="mr-5px" /> 添加到分组
124+
</el-button>
116125
<el-button
117126
type="danger"
118127
plain
@@ -213,6 +222,8 @@
213222

214223
<!-- 表单弹窗:添加/修改 -->
215224
<DeviceForm ref="formRef" @success="getList" />
225+
<!-- 分组表单组件 -->
226+
<DeviceGroupForm ref="groupFormRef" @success="getList" />
216227
</template>
217228

218229
<script setup lang="ts">
@@ -223,8 +234,9 @@ import DeviceForm from './DeviceForm.vue'
223234
import { ProductApi, ProductVO } from '@/api/iot/product/product'
224235
import { DeviceGroupApi, DeviceGroupVO } from '@/api/iot/device/group'
225236
import download from '@/utils/download'
237+
import DeviceGroupForm from './DeviceGroupForm.vue'
226238
227-
/** IoT 设备 列表 */
239+
/** IoT 设备列表 */
228240
defineOptions({ name: 'IoTDevice' })
229241
230242
const message = useMessage() // 消息弹窗
@@ -299,16 +311,6 @@ const handleDelete = async (id: number) => {
299311
} catch {}
300312
}
301313
302-
/** 初始化 **/
303-
onMounted(async () => {
304-
getList()
305-
306-
// 获取产品列表
307-
products.value = await ProductApi.getSimpleProductList()
308-
// 获取分组列表
309-
deviceGroups.value = await DeviceGroupApi.getSimpleDeviceGroupList()
310-
})
311-
312314
/** 导出方法 */
313315
const handleExport = async () => {
314316
try {
@@ -340,4 +342,20 @@ const handleDeleteList = async () => {
340342
await getList()
341343
} catch {}
342344
}
345+
346+
/** 添加到分组操作 */
347+
const groupFormRef = ref()
348+
const openGroupForm = () => {
349+
groupFormRef.value.open(selectedIds.value)
350+
}
351+
352+
/** 初始化 **/
353+
onMounted(async () => {
354+
getList()
355+
356+
// 获取产品列表
357+
products.value = await ProductApi.getSimpleProductList()
358+
// 获取分组列表
359+
deviceGroups.value = await DeviceGroupApi.getSimpleDeviceGroupList()
360+
})
343361
</script>

0 commit comments

Comments
 (0)