13
13
<Vue3Jsoneditor
14
14
ref =" editor"
15
15
v-if =" isEditing"
16
- v-model =" deviceConfig "
16
+ v-model =" deviceConfigState "
17
17
:options =" editorOptions"
18
18
height =" 500px"
19
19
currentMode =" code"
23
23
<Vue3Jsoneditor
24
24
ref =" editor"
25
25
v-else
26
- v-model =" deviceConfig "
26
+ v-model =" deviceConfigState "
27
27
:options =" editorOptions"
28
28
height =" 500px"
29
29
currentMode =" view"
30
+ v-loading.fullscreen.lock =" loading"
30
31
@error =" onError"
31
32
/>
32
- <div class =" button-group " >
33
+ <div class =" flex justify-center mt-24 " >
33
34
<el-button v-if =" isEditing" @click =" cancelEdit" >取消</el-button >
34
35
<el-button v-if =" isEditing" type =" primary" @click =" saveConfig" >保存</el-button >
35
36
<el-button v-else @click =" enableEdit" >编辑</el-button >
39
40
40
41
<script lang="ts" setup>
41
42
import { ref , computed } from ' vue'
42
- // import Vue3Jsoneditor from 'v3-jsoneditor/src/Vue3Jsoneditor.vue'
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' ;
47
+
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 ({}) // 设置配置
55
+
56
+
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
+ }
69
+
70
+ onMounted (async () => {
71
+ if (! id ) {
72
+ message .warning (' 参数错误,产品不能为空!' )
73
+ delView (unref (currentRoute ))
74
+ return
75
+ }
76
+ await getDeviceConfig (id )
77
+ })
78
+
43
79
44
- const deviceConfig = ref ({
45
- name: ' dyla1n'
46
- }) // 定义设备配置 TODO @dylan:从后端读取
47
80
const isEditing = ref (false ) // 编辑状态
48
81
const editorOptions = computed (() => ({
49
82
mainMenuBar: false ,
@@ -64,23 +97,30 @@ const cancelEdit = () => {
64
97
}
65
98
66
99
/** 保存配置的函数 */
67
- const saveConfig = () => {
100
+ const saveConfig = async () => {
101
+ const params = {
102
+ ... deviceConfigState .value
103
+ } as DeviceVO
104
+ await updateDeviceConfig (params )
68
105
isEditing .value = false
69
- // 逻辑代码
70
- console .log (' 保存配置' )
71
106
}
72
107
73
108
/** 处理 JSON 编辑器错误的函数 */
74
109
const onError = (e : any ) => {
75
110
console .log (' onError' , e )
76
111
}
77
- </script >
78
112
79
- <!-- TODO dylan:建议使用 unocss 替代哈,AI 模型友好 -->
80
- <style scoped>
81
- .button-group {
82
- display : flex ;
83
- justify-content : center ;
84
- margin-top : 20px ;
113
+ // 更新设备配置
114
+ const updateDeviceConfig = async (params : DeviceVO ) => {
115
+ try {
116
+ loading .value = true
117
+ await DeviceApi .updateDevice (params )
118
+ await getDeviceConfig (id )
119
+ message .success (' 更新成功!' )
120
+ } catch (error ) {
121
+ console .error (error )
122
+ } finally {
123
+ loading .value = false
124
+ }
85
125
}
86
- </style >
126
+ </script >
0 commit comments