Skip to content

Commit 6636068

Browse files
committed
feat: 设备配置接口对接
1 parent c65c056 commit 6636068

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

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

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<Vue3Jsoneditor
1414
ref="editor"
1515
v-if="isEditing"
16-
v-model="deviceConfig"
16+
v-model="deviceConfigState"
1717
:options="editorOptions"
1818
height="500px"
1919
currentMode="code"
@@ -23,13 +23,14 @@
2323
<Vue3Jsoneditor
2424
ref="editor"
2525
v-else
26-
v-model="deviceConfig"
26+
v-model="deviceConfigState"
2727
:options="editorOptions"
2828
height="500px"
2929
currentMode="view"
30+
v-loading.fullscreen.lock="loading"
3031
@error="onError"
3132
/>
32-
<div class="button-group">
33+
<div class="flex justify-center mt-24">
3334
<el-button v-if="isEditing" @click="cancelEdit">取消</el-button>
3435
<el-button v-if="isEditing" type="primary" @click="saveConfig">保存</el-button>
3536
<el-button v-else @click="enableEdit">编辑</el-button>
@@ -39,11 +40,43 @@
3940

4041
<script lang="ts" setup>
4142
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+
4379
44-
const deviceConfig = ref({
45-
name: 'dyla1n'
46-
}) // 定义设备配置 TODO @dylan:从后端读取
4780
const isEditing = ref(false) // 编辑状态
4881
const editorOptions = computed(() => ({
4982
mainMenuBar: false,
@@ -64,23 +97,30 @@ const cancelEdit = () => {
6497
}
6598
6699
/** 保存配置的函数 */
67-
const saveConfig = () => {
100+
const saveConfig = async () => {
101+
const params = {
102+
...deviceConfigState.value
103+
} as DeviceVO
104+
await updateDeviceConfig(params)
68105
isEditing.value = false
69-
// 逻辑代码
70-
console.log('保存配置')
71106
}
72107
73108
/** 处理 JSON 编辑器错误的函数 */
74109
const onError = (e: any) => {
75110
console.log('onError', e)
76111
}
77-
</script>
78112
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+
}
85125
}
86-
</style>
126+
</script>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
:device="device"
2727
/>
2828
</el-tab-pane>
29-
<!-- <el-tab-pane label="设备配置" name="config">
29+
<el-tab-pane label="设备配置" name="config">
3030
<DeviceDetailConfig />
31-
</el-tab-pane> -->
31+
</el-tab-pane>
3232
</el-tabs>
3333
</el-col>
3434
</template>

0 commit comments

Comments
 (0)