feat: 设备配置接口对接

This commit is contained in:
dylanmay 2025-03-05 16:22:05 +08:00
parent c65c056c8f
commit 6636068bd5
2 changed files with 60 additions and 20 deletions

View File

@ -13,7 +13,7 @@
<Vue3Jsoneditor <Vue3Jsoneditor
ref="editor" ref="editor"
v-if="isEditing" v-if="isEditing"
v-model="deviceConfig" v-model="deviceConfigState"
:options="editorOptions" :options="editorOptions"
height="500px" height="500px"
currentMode="code" currentMode="code"
@ -23,13 +23,14 @@
<Vue3Jsoneditor <Vue3Jsoneditor
ref="editor" ref="editor"
v-else v-else
v-model="deviceConfig" v-model="deviceConfigState"
:options="editorOptions" :options="editorOptions"
height="500px" height="500px"
currentMode="view" currentMode="view"
v-loading.fullscreen.lock="loading"
@error="onError" @error="onError"
/> />
<div class="button-group"> <div class="flex justify-center mt-24">
<el-button v-if="isEditing" @click="cancelEdit">取消</el-button> <el-button v-if="isEditing" @click="cancelEdit">取消</el-button>
<el-button v-if="isEditing" type="primary" @click="saveConfig">保存</el-button> <el-button v-if="isEditing" type="primary" @click="saveConfig">保存</el-button>
<el-button v-else @click="enableEdit">编辑</el-button> <el-button v-else @click="enableEdit">编辑</el-button>
@ -39,11 +40,43 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
// import Vue3Jsoneditor from 'v3-jsoneditor/src/Vue3Jsoneditor.vue' import Vue3Jsoneditor from 'v3-jsoneditor/src/Vue3Jsoneditor.vue'
import { DeviceApi } from '@/api/iot/device/device/index'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { DeviceVO } from '../../../../../api/iot/device/device/index';
const route = useRoute()
const message = useMessage()
const { delView } = useTagsViewStore() //
const { currentRoute } = useRouter() //
const id = Number(route.params.id) //
const loading = ref(true) //
const deviceConfigState = ref({}) //
//
const getDeviceConfig = async (id: number) => {
try {
loading.value = true
const res = await DeviceApi.getDevice(id)
deviceConfigState.value = res
} catch (error) {
console.error(error)
} finally {
loading.value = false
}
}
onMounted(async () => {
if (!id) {
message.warning('参数错误,产品不能为空!')
delView(unref(currentRoute))
return
}
await getDeviceConfig(id)
})
const deviceConfig = ref({
name: 'dyla1n'
}) // TODO @dylan
const isEditing = ref(false) // const isEditing = ref(false) //
const editorOptions = computed(() => ({ const editorOptions = computed(() => ({
mainMenuBar: false, mainMenuBar: false,
@ -64,23 +97,30 @@ const cancelEdit = () => {
} }
/** 保存配置的函数 */ /** 保存配置的函数 */
const saveConfig = () => { const saveConfig = async () => {
const params = {
...deviceConfigState.value
} as DeviceVO
await updateDeviceConfig(params)
isEditing.value = false isEditing.value = false
//
console.log('保存配置')
} }
/** 处理 JSON 编辑器错误的函数 */ /** 处理 JSON 编辑器错误的函数 */
const onError = (e: any) => { const onError = (e: any) => {
console.log('onError', e) console.log('onError', e)
} }
</script>
<!-- TODO dylan建议使用 unocss 替代哈AI 模型友好 --> //
<style scoped> const updateDeviceConfig = async (params: DeviceVO) => {
.button-group { try {
display: flex; loading.value = true
justify-content: center; await DeviceApi.updateDevice(params)
margin-top: 20px; await getDeviceConfig(id)
message.success('更新成功!')
} catch (error) {
console.error(error)
} finally {
loading.value = false
}
} }
</style> </script>

View File

@ -26,9 +26,9 @@
:device="device" :device="device"
/> />
</el-tab-pane> </el-tab-pane>
<!-- <el-tab-pane label="设备配置" name="config"> <el-tab-pane label="设备配置" name="config">
<DeviceDetailConfig /> <DeviceDetailConfig />
</el-tab-pane> --> </el-tab-pane>
</el-tabs> </el-tabs>
</el-col> </el-col>
</template> </template>