11
11
12
12
<!-- JSON 编辑器:读模式 -->
13
13
<Vue3Jsoneditor
14
- ref =" editor"
15
14
v-if =" isEditing"
16
- v-model =" deviceConfigState "
15
+ v-model =" config "
17
16
:options =" editorOptions"
18
17
height =" 500px"
19
18
currentMode =" code"
20
19
@error =" onError"
21
20
/>
22
21
<!-- JSON 编辑器:写模式 -->
23
22
<Vue3Jsoneditor
24
- ref =" editor"
25
23
v-else
26
- v-model =" deviceConfigState "
24
+ v-model =" config "
27
25
:options =" editorOptions"
28
26
height =" 500px"
29
27
currentMode =" view"
30
28
v-loading.fullscreen.lock =" loading"
31
29
@error =" onError"
32
30
/>
33
- <div class =" flex justify-center mt-24 " >
31
+ <div class =" mt-5 text-center " >
34
32
<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
+ >
36
36
<el-button v-else @click =" enableEdit" >编辑</el-button >
37
+ <!-- TODO @芋艿:缺一个下发按钮 -->
37
38
</div >
38
39
</div >
39
40
</template >
40
41
41
42
<script lang="ts" setup>
42
- import { ref , computed } from ' vue'
43
43
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'
47
46
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
+ }>()
55
50
51
+ const emit = defineEmits <{
52
+ (e : ' success' ): void // 定义 success 事件,不需要参数
53
+ }>()
56
54
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 格式错误
69
59
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 )
77
63
})
78
64
79
-
80
65
const isEditing = ref (false ) // 编辑状态
81
66
const editorOptions = computed (() => ({
82
67
mainMenuBar: false ,
@@ -87,40 +72,48 @@ const editorOptions = computed(() => ({
87
72
/** 启用编辑模式的函数 */
88
73
const enableEdit = () => {
89
74
isEditing .value = true
75
+ hasJsonError .value = false // 重置错误状态
90
76
}
91
77
92
78
/** 取消编辑的函数 */
93
79
const cancelEdit = () => {
80
+ config .value = jsonParse (props .device .config )
94
81
isEditing .value = false
95
- // 逻辑代码
96
- console .log (' 取消编辑' )
82
+ hasJsonError .value = false // 重置错误状态
97
83
}
98
84
99
85
/** 保存配置的函数 */
100
86
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 ()
105
92
isEditing .value = false
106
93
}
107
94
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 () => {
115
97
try {
98
+ // 提交请求
116
99
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 )
119
104
message .success (' 更新成功!' )
105
+ // 触发 success 事件
106
+ emit (' success' )
120
107
} catch (error ) {
121
108
console .error (error )
122
109
} finally {
123
110
loading .value = false
124
111
}
125
112
}
113
+
114
+ /** 处理 JSON 编辑器错误的函数 */
115
+ const onError = (e : any ) => {
116
+ console .log (' onError' , e )
117
+ hasJsonError .value = true
118
+ }
126
119
</script >
0 commit comments