|
@@ -11,9 +11,8 @@
|
|
|
|
|
|
<!-- JSON 编辑器:读模式 -->
|
|
<!-- JSON 编辑器:读模式 -->
|
|
<Vue3Jsoneditor
|
|
<Vue3Jsoneditor
|
|
- ref="editor"
|
|
|
|
v-if="isEditing"
|
|
v-if="isEditing"
|
|
- v-model="deviceConfigState"
|
|
|
|
|
|
+ v-model="config"
|
|
:options="editorOptions"
|
|
:options="editorOptions"
|
|
height="500px"
|
|
height="500px"
|
|
currentMode="code"
|
|
currentMode="code"
|
|
@@ -21,62 +20,48 @@
|
|
/>
|
|
/>
|
|
<!-- JSON 编辑器:写模式 -->
|
|
<!-- JSON 编辑器:写模式 -->
|
|
<Vue3Jsoneditor
|
|
<Vue3Jsoneditor
|
|
- ref="editor"
|
|
|
|
v-else
|
|
v-else
|
|
- v-model="deviceConfigState"
|
|
|
|
|
|
+ v-model="config"
|
|
:options="editorOptions"
|
|
:options="editorOptions"
|
|
height="500px"
|
|
height="500px"
|
|
currentMode="view"
|
|
currentMode="view"
|
|
v-loading.fullscreen.lock="loading"
|
|
v-loading.fullscreen.lock="loading"
|
|
@error="onError"
|
|
@error="onError"
|
|
/>
|
|
/>
|
|
- <div class="flex justify-center mt-24">
|
|
|
|
|
|
+ <div class="mt-5 text-center">
|
|
<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" :disabled="hasJsonError"
|
|
|
|
+ >保存</el-button
|
|
|
|
+ >
|
|
<el-button v-else @click="enableEdit">编辑</el-button>
|
|
<el-button v-else @click="enableEdit">编辑</el-button>
|
|
|
|
+ <!-- TODO @芋艿:缺一个下发按钮 -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script lang="ts" setup>
|
|
<script lang="ts" setup>
|
|
-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';
|
|
|
|
|
|
+import { DeviceApi, DeviceVO } from '@/api/iot/device/device'
|
|
|
|
+import { jsonParse } from '@/utils'
|
|
|
|
|
|
-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 props = defineProps<{
|
|
|
|
+ device: DeviceVO
|
|
|
|
+}>()
|
|
|
|
|
|
|
|
+const emit = defineEmits<{
|
|
|
|
+ (e: 'success'): void // 定义 success 事件,不需要参数
|
|
|
|
+}>()
|
|
|
|
|
|
-// 获取设备配置
|
|
|
|
-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
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+const message = useMessage()
|
|
|
|
+const loading = ref(false) // 加载中
|
|
|
|
+const config = ref<any>({}) // 只存储 config 字段
|
|
|
|
+const hasJsonError = ref(false) // 是否有 JSON 格式错误
|
|
|
|
|
|
-onMounted(async () => {
|
|
|
|
- if (!id) {
|
|
|
|
- message.warning('参数错误,产品不能为空!')
|
|
|
|
- delView(unref(currentRoute))
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- await getDeviceConfig(id)
|
|
|
|
|
|
+/** 监听 props.device 的变化,只更新 config 字段 */
|
|
|
|
+watchEffect(() => {
|
|
|
|
+ config.value = jsonParse(props.device.config)
|
|
})
|
|
})
|
|
|
|
|
|
-
|
|
|
|
const isEditing = ref(false) // 编辑状态
|
|
const isEditing = ref(false) // 编辑状态
|
|
const editorOptions = computed(() => ({
|
|
const editorOptions = computed(() => ({
|
|
mainMenuBar: false,
|
|
mainMenuBar: false,
|
|
@@ -87,40 +72,48 @@ const editorOptions = computed(() => ({
|
|
/** 启用编辑模式的函数 */
|
|
/** 启用编辑模式的函数 */
|
|
const enableEdit = () => {
|
|
const enableEdit = () => {
|
|
isEditing.value = true
|
|
isEditing.value = true
|
|
|
|
+ hasJsonError.value = false // 重置错误状态
|
|
}
|
|
}
|
|
|
|
|
|
/** 取消编辑的函数 */
|
|
/** 取消编辑的函数 */
|
|
const cancelEdit = () => {
|
|
const cancelEdit = () => {
|
|
|
|
+ config.value = jsonParse(props.device.config)
|
|
isEditing.value = false
|
|
isEditing.value = false
|
|
- // 逻辑代码
|
|
|
|
- console.log('取消编辑')
|
|
|
|
|
|
+ hasJsonError.value = false // 重置错误状态
|
|
}
|
|
}
|
|
|
|
|
|
/** 保存配置的函数 */
|
|
/** 保存配置的函数 */
|
|
const saveConfig = async () => {
|
|
const saveConfig = async () => {
|
|
- const params = {
|
|
|
|
- ...deviceConfigState.value
|
|
|
|
- } as DeviceVO
|
|
|
|
- await updateDeviceConfig(params)
|
|
|
|
|
|
+ if (hasJsonError.value) {
|
|
|
|
+ message.error('JSON格式错误,请修正后再提交!')
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ await updateDeviceConfig()
|
|
isEditing.value = false
|
|
isEditing.value = false
|
|
}
|
|
}
|
|
|
|
|
|
-/** 处理 JSON 编辑器错误的函数 */
|
|
|
|
-const onError = (e: any) => {
|
|
|
|
- console.log('onError', e)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 更新设备配置
|
|
|
|
-const updateDeviceConfig = async (params: DeviceVO) => {
|
|
|
|
|
|
+/** 更新设备配置 */
|
|
|
|
+const updateDeviceConfig = async () => {
|
|
try {
|
|
try {
|
|
|
|
+ // 提交请求
|
|
loading.value = true
|
|
loading.value = true
|
|
- await DeviceApi.updateDevice(params)
|
|
|
|
- await getDeviceConfig(id)
|
|
|
|
|
|
+ await DeviceApi.updateDevice({
|
|
|
|
+ id: props.device.id,
|
|
|
|
+ config: JSON.stringify(config.value)
|
|
|
|
+ } as DeviceVO)
|
|
message.success('更新成功!')
|
|
message.success('更新成功!')
|
|
|
|
+ // 触发 success 事件
|
|
|
|
+ emit('success')
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error(error)
|
|
console.error(error)
|
|
} finally {
|
|
} finally {
|
|
loading.value = false
|
|
loading.value = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+/** 处理 JSON 编辑器错误的函数 */
|
|
|
|
+const onError = (e: any) => {
|
|
|
|
+ console.log('onError', e)
|
|
|
|
+ hasJsonError.value = true
|
|
|
|
+}
|
|
</script>
|
|
</script>
|