Skip to content

Commit 3685e43

Browse files
committed
【功能完善】IoT:增加 device config 配置
1 parent 6636068 commit 3685e43

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface DeviceVO {
2727
areaId: number // 地区编码
2828
address: string // 设备详细地址
2929
serialNumber: string // 设备序列号
30+
config: string // 设备配置
3031
groupIds?: number[] // 添加分组 ID
3132
}
3233

@@ -72,7 +73,7 @@ export interface IotDeviceDownstreamReqVO {
7273
data: any // 请求参数
7374
}
7475

75-
// MQTT连接参数 VO
76+
// MQTT 连接参数 VO
7677
export interface MqttConnectionParamsVO {
7778
mqttClientId: string // MQTT 客户端 ID
7879
mqttUsername: string // MQTT 用户名

src/views/iot/device/device/detail/DeviceDetailConfig.vue

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,57 @@
1111

1212
<!-- JSON 编辑器:读模式 -->
1313
<Vue3Jsoneditor
14-
ref="editor"
1514
v-if="isEditing"
16-
v-model="deviceConfigState"
15+
v-model="config"
1716
:options="editorOptions"
1817
height="500px"
1918
currentMode="code"
2019
@error="onError"
2120
/>
2221
<!-- JSON 编辑器:写模式 -->
2322
<Vue3Jsoneditor
24-
ref="editor"
2523
v-else
26-
v-model="deviceConfigState"
24+
v-model="config"
2725
:options="editorOptions"
2826
height="500px"
2927
currentMode="view"
3028
v-loading.fullscreen.lock="loading"
3129
@error="onError"
3230
/>
33-
<div class="flex justify-center mt-24">
31+
<div class="mt-5 text-center">
3432
<el-button v-if="isEditing" @click="cancelEdit">取消</el-button>
35-
<el-button v-if="isEditing" type="primary" @click="saveConfig">保存</el-button>
33+
<el-button v-if="isEditing" type="primary" @click="saveConfig" :disabled="hasJsonError"
34+
>保存</el-button
35+
>
3636
<el-button v-else @click="enableEdit">编辑</el-button>
37+
<!-- TODO @芋艿:缺一个下发按钮 -->
3738
</div>
3839
</div>
3940
</template>
4041

4142
<script lang="ts" setup>
42-
import { ref, computed } from 'vue'
4343
import Vue3Jsoneditor from 'v3-jsoneditor/src/Vue3Jsoneditor.vue'
44-
import { DeviceApi } from '@/api/iot/device/device/index'
45-
import { useTagsViewStore } from '@/store/modules/tagsView'
46-
import { DeviceVO } from '../../../../../api/iot/device/device/index';
44+
import { DeviceApi, DeviceVO } from '@/api/iot/device/device'
45+
import { jsonParse } from '@/utils'
4746
48-
const route = useRoute()
49-
const message = useMessage()
50-
const { delView } = useTagsViewStore() // 视图操作
51-
const { currentRoute } = useRouter() // 路由
52-
const id = Number(route.params.id) // 将字符串转换为数字
53-
const loading = ref(true) // 加载中
54-
const deviceConfigState = ref({}) // 设置配置
47+
const props = defineProps<{
48+
device: DeviceVO
49+
}>()
5550
51+
const emit = defineEmits<{
52+
(e: 'success'): void // 定义 success 事件,不需要参数
53+
}>()
5654
57-
// 获取设备配置
58-
const getDeviceConfig = async (id: number) => {
59-
try {
60-
loading.value = true
61-
const res = await DeviceApi.getDevice(id)
62-
deviceConfigState.value = res
63-
} catch (error) {
64-
console.error(error)
65-
} finally {
66-
loading.value = false
67-
}
68-
}
55+
const message = useMessage()
56+
const loading = ref(false) // 加载中
57+
const config = ref<any>({}) // 只存储 config 字段
58+
const hasJsonError = ref(false) // 是否有 JSON 格式错误
6959
70-
onMounted(async () => {
71-
if (!id) {
72-
message.warning('参数错误,产品不能为空!')
73-
delView(unref(currentRoute))
74-
return
75-
}
76-
await getDeviceConfig(id)
60+
/** 监听 props.device 的变化,只更新 config 字段 */
61+
watchEffect(() => {
62+
config.value = jsonParse(props.device.config)
7763
})
7864
79-
8065
const isEditing = ref(false) // 编辑状态
8166
const editorOptions = computed(() => ({
8267
mainMenuBar: false,
@@ -87,40 +72,48 @@ const editorOptions = computed(() => ({
8772
/** 启用编辑模式的函数 */
8873
const enableEdit = () => {
8974
isEditing.value = true
75+
hasJsonError.value = false // 重置错误状态
9076
}
9177
9278
/** 取消编辑的函数 */
9379
const cancelEdit = () => {
80+
config.value = jsonParse(props.device.config)
9481
isEditing.value = false
95-
// 逻辑代码
96-
console.log('取消编辑')
82+
hasJsonError.value = false // 重置错误状态
9783
}
9884
9985
/** 保存配置的函数 */
10086
const saveConfig = async () => {
101-
const params = {
102-
...deviceConfigState.value
103-
} as DeviceVO
104-
await updateDeviceConfig(params)
87+
if (hasJsonError.value) {
88+
message.error('JSON格式错误,请修正后再提交!')
89+
return
90+
}
91+
await updateDeviceConfig()
10592
isEditing.value = false
10693
}
10794
108-
/** 处理 JSON 编辑器错误的函数 */
109-
const onError = (e: any) => {
110-
console.log('onError', e)
111-
}
112-
113-
// 更新设备配置
114-
const updateDeviceConfig = async (params: DeviceVO) => {
95+
/** 更新设备配置 */
96+
const updateDeviceConfig = async () => {
11597
try {
98+
// 提交请求
11699
loading.value = true
117-
await DeviceApi.updateDevice(params)
118-
await getDeviceConfig(id)
100+
await DeviceApi.updateDevice({
101+
id: props.device.id,
102+
config: JSON.stringify(config.value)
103+
} as DeviceVO)
119104
message.success('更新成功!')
105+
// 触发 success 事件
106+
emit('success')
120107
} catch (error) {
121108
console.error(error)
122109
} finally {
123110
loading.value = false
124111
}
125112
}
113+
114+
/** 处理 JSON 编辑器错误的函数 */
115+
const onError = (e: any) => {
116+
console.log('onError', e)
117+
hasJsonError.value = true
118+
}
126119
</script>

src/views/iot/device/device/detail/DeviceDetailsInfo.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@
6363
</el-input>
6464
</el-form-item>
6565
<el-form-item label="passwd">
66-
<el-input v-model="mqttParams.mqttPassword" readonly :type="passwordVisible ? 'text' : 'password'">
66+
<el-input
67+
v-model="mqttParams.mqttPassword"
68+
readonly
69+
:type="passwordVisible ? 'text' : 'password'"
70+
>
6771
<template #append>
6872
<el-button @click="passwordVisible = !passwordVisible" type="primary">
6973
<Icon :icon="passwordVisible ? 'ph:eye-slash' : 'ph:eye'" />
@@ -117,13 +121,14 @@ const copyToClipboard = async (text: string) => {
117121
const openMqttParams = async () => {
118122
try {
119123
const res = await DeviceApi.getMqttConnectionParams(device.id)
120-
121-
// 根据API响应结构正确获取数据
124+
// 根据 API 响应结构正确获取数据
122125
mqttParams.value = {
123126
mqttClientId: res.mqttClientId || 'N/A',
124127
mqttUsername: res.mqttUsername || 'N/A',
125128
mqttPassword: res.mqttPassword || 'N/A'
126129
}
130+
131+
// 显示 MQTT 弹框
127132
mqttDialogVisible.value = true
128133
} catch (error) {
129134
console.error('获取MQTT连接参数出错:', error)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
/>
2828
</el-tab-pane>
2929
<el-tab-pane label="设备配置" name="config">
30-
<DeviceDetailConfig />
30+
<DeviceDetailConfig
31+
v-if="activeTab === 'config'"
32+
:device="device"
33+
@success="getDeviceData"
34+
/>
3135
</el-tab-pane>
3236
</el-tabs>
3337
</el-col>
@@ -41,7 +45,7 @@ import DeviceDetailsInfo from './DeviceDetailsInfo.vue'
4145
import DeviceDetailsModel from './DeviceDetailsModel.vue'
4246
import DeviceDetailsLog from './DeviceDetailsLog.vue'
4347
import DeviceDetailsSimulator from './DeviceDetailsSimulator.vue'
44-
import DeviceDetailConfig from './DeviceDetailConfig.vue';
48+
import DeviceDetailConfig from './DeviceDetailConfig.vue'
4549
4650
defineOptions({ name: 'IoTDeviceDetail' })
4751
@@ -54,7 +58,7 @@ const device = ref<DeviceVO>({} as DeviceVO) // 设备详情
5458
const activeTab = ref('info') // 默认激活的标签页
5559
5660
/** 获取设备详情 */
57-
const getDeviceData = async (id: number) => {
61+
const getDeviceData = async () => {
5862
loading.value = true
5963
try {
6064
device.value = await DeviceApi.getDevice(id)
@@ -78,7 +82,7 @@ onMounted(async () => {
7882
delView(unref(currentRoute))
7983
return
8084
}
81-
await getDeviceData(id)
85+
await getDeviceData()
8286
activeTab.value = (route.query.tab as string) || 'info'
8387
})
8488
</script>

0 commit comments

Comments
 (0)