From a1d7e81859f554f3a53680cc35f0f49bf1f77098 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期四, 14 五月 2026 14:37:02 +0800
Subject: [PATCH] 导入项目
---
src/views/iot/device/device/detail/DeviceDetailConfig.vue | 134 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 134 insertions(+), 0 deletions(-)
diff --git a/src/views/iot/device/device/detail/DeviceDetailConfig.vue b/src/views/iot/device/device/detail/DeviceDetailConfig.vue
new file mode 100644
index 0000000..d382f79
--- /dev/null
+++ b/src/views/iot/device/device/detail/DeviceDetailConfig.vue
@@ -0,0 +1,134 @@
+<!-- 璁惧閰嶇疆 -->
+<template>
+ <div>
+ <el-alert
+ title="鏀寔杩滅▼鏇存柊璁惧鐨勯厤缃枃浠�(JSON 鏍煎紡)锛屽彲浠ュ湪涓嬫柟缂栬緫閰嶇疆妯℃澘锛屽璁惧鐨勭郴缁熷弬鏁般�佺綉缁滃弬鏁扮瓑杩涜杩滅▼閰嶇疆銆傞厤缃畬鎴愬悗锛岄渶鐐瑰嚮銆屼笅鍙戙�嶆寜閽紝璁惧鍗冲彲杩涜杩滅▼閰嶇疆銆�"
+ type="info"
+ show-icon
+ class="my-4"
+ description="濡傞渶缂栬緫鏂囦欢锛岃鐐瑰嚮涓嬫柟缂栬緫鎸夐挳"
+ />
+ <JsonEditor
+ v-model="config"
+ :mode="isEditing ? 'code' : 'view'"
+ height="600px"
+ @error="onError"
+ />
+ <div class="mt-5 text-center">
+ <el-button v-if="isEditing" @click="cancelEdit">鍙栨秷</el-button>
+ <el-button v-if="isEditing" type="primary" @click="saveConfig" :disabled="hasJsonError">
+ 淇濆瓨
+ </el-button>
+ <el-button v-else @click="enableEdit">缂栬緫</el-button>
+ <el-button v-if="!isEditing" type="success" @click="handleConfigPush" :loading="pushLoading">
+ 閰嶇疆鎺ㄩ��
+ </el-button>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { DeviceApi, DeviceVO } from '@/api/iot/device/device'
+import { IotDeviceMessageMethodEnum } from '@/views/iot/utils/constants'
+import { jsonParse } from '@/utils'
+import { isEmpty } from '@/utils/is'
+
+defineOptions({ name: 'DeviceDetailConfig' })
+
+const props = defineProps<{
+ device: DeviceVO
+}>()
+
+const emit = defineEmits<{
+ (e: 'success'): void // 瀹氫箟 success 浜嬩欢锛屼笉闇�瑕佸弬鏁�
+}>()
+
+const message = useMessage()
+const loading = ref(false) // 鍔犺浇涓�
+const pushLoading = ref(false) // 鎺ㄩ�佸姞杞戒腑
+const config = ref<any>({}) // 鍙瓨鍌� config 瀛楁
+const hasJsonError = ref(false) // 鏄惁鏈� JSON 鏍煎紡閿欒
+
+/** 鐩戝惉 props.device 鐨勫彉鍖栵紝鍙洿鏂� config 瀛楁 */
+watchEffect(() => {
+ config.value = jsonParse(props.device.config)
+})
+
+const isEditing = ref(false) // 缂栬緫鐘舵��
+/** 鍚敤缂栬緫妯″紡鐨勫嚱鏁� */
+const enableEdit = () => {
+ isEditing.value = true
+ hasJsonError.value = false // 閲嶇疆閿欒鐘舵��
+}
+
+/** 鍙栨秷缂栬緫鐨勫嚱鏁� */
+const cancelEdit = () => {
+ config.value = jsonParse(props.device.config)
+ isEditing.value = false
+ hasJsonError.value = false // 閲嶇疆閿欒鐘舵��
+}
+
+/** 淇濆瓨閰嶇疆鐨勫嚱鏁� */
+const saveConfig = async () => {
+ if (hasJsonError.value) {
+ message.error('JSON鏍煎紡閿欒锛岃淇鍚庡啀鎻愪氦锛�')
+ return
+ }
+ await updateDeviceConfig()
+ isEditing.value = false
+}
+
+/** 閰嶇疆鎺ㄩ�佸鐞嗗嚱鏁� */
+const handleConfigPush = async () => {
+ try {
+ // 浜屾纭
+ await message.confirm('纭畾瑕佹帹閫侀厤缃埌璁惧鍚楋紵姝ゆ搷浣滃皢杩滅▼鏇存柊璁惧閰嶇疆銆�', '閰嶇疆鎺ㄩ�佺‘璁�')
+
+ pushLoading.value = true
+
+ // 璋冪敤閰嶇疆鎺ㄩ�佹帴鍙�
+ await DeviceApi.sendDeviceMessage({
+ deviceId: props.device.id,
+ method: IotDeviceMessageMethodEnum.CONFIG_PUSH.method,
+ params: config.value
+ })
+
+ message.success('閰嶇疆鎺ㄩ�佹垚鍔燂紒')
+ } catch (error) {
+ if (error !== 'cancel') {
+ message.error('閰嶇疆鎺ㄩ�佸け璐ワ紒')
+ console.error('閰嶇疆鎺ㄩ�侀敊璇�:', error)
+ }
+ } finally {
+ pushLoading.value = false
+ }
+}
+
+/** 鏇存柊璁惧閰嶇疆 */
+const updateDeviceConfig = async () => {
+ try {
+ // 鎻愪氦璇锋眰
+ loading.value = true
+ await DeviceApi.updateDevice({
+ id: props.device.id,
+ config: JSON.stringify(config.value)
+ } as DeviceVO)
+ message.success('鏇存柊鎴愬姛锛�')
+ // 瑙﹀彂 success 浜嬩欢
+ emit('success')
+ } catch (error) {
+ console.error(error)
+ } finally {
+ loading.value = false
+ }
+}
+
+/** 澶勭悊 JSON 缂栬緫鍣ㄩ敊璇殑鍑芥暟 */
+const onError = (errors: any) => {
+ if (isEmpty(errors)) {
+ hasJsonError.value = false
+ return
+ }
+ hasJsonError.value = true
+}
+</script>
--
Gitblit v1.8.0