ThingModelDualView.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" :appendToBody="true" v-loading="loading">
  3. <div class="flex h-600px">
  4. <!-- 左侧物模型属性(view模式) -->
  5. <div class="w-1/2 border-r border-gray-200 pr-2 overflow-auto">
  6. <JsonEditor :model-value="thingModel" mode="view" height="600px" />
  7. </div>
  8. <!-- 右侧JSON编辑器(code模式) -->
  9. <div class="w-1/2 pl-2 overflow-auto">
  10. <JsonEditor v-model="editableModelTSL" mode="code" height="600px" @error="handleError" />
  11. </div>
  12. </div>
  13. <template #footer>
  14. <el-button @click="dialogVisible = false">取消</el-button>
  15. <el-button type="primary" @click="handleSave" :disabled="hasJsonError">保存</el-button>
  16. </template>
  17. </Dialog>
  18. </template>
  19. <script setup lang="ts">
  20. import { isEmpty } from '@/utils/is'
  21. defineOptions({ name: 'ThingModelDualView' })
  22. const props = defineProps<{
  23. modelValue: any // 物模型的值
  24. thingModel: any[] // 物模型
  25. }>()
  26. const emits = defineEmits(['update:modelValue', 'change'])
  27. const message = useMessage()
  28. const dialogVisible = ref(false) // 弹窗的是否展示
  29. const dialogTitle = ref('物模型编辑器') // 弹窗的标题
  30. const editableModelTSL = ref([
  31. {
  32. identifier: '对应左侧 identifier 属性值',
  33. value: '如果 identifier 是 int 类型则输入数字,具体查看产品物模型定义'
  34. }
  35. ]) // 物模型数据
  36. const hasJsonError = ref(false) // 是否有 JSON 格式错误
  37. const loading = ref(false) // 加载状态
  38. /** 打开弹窗 */
  39. const open = () => {
  40. try {
  41. // 数据回显
  42. if (props.modelValue) {
  43. editableModelTSL.value = JSON.parse(props.modelValue)
  44. }
  45. } catch (e) {
  46. message.error('物模型编辑器参数')
  47. console.error(e)
  48. } finally {
  49. dialogVisible.value = true
  50. // 重置状态
  51. hasJsonError.value = false
  52. }
  53. }
  54. defineExpose({ open }) // 暴露方法供父组件调用
  55. /** 保存修改 */
  56. const handleSave = async () => {
  57. try {
  58. await message.confirm('确定要保存物模型参数吗?')
  59. emits('update:modelValue', JSON.stringify(editableModelTSL.value))
  60. message.success('保存成功')
  61. dialogVisible.value = false
  62. } catch {}
  63. }
  64. /** 处理 JSON 编辑器错误的函数 */
  65. const handleError = (errors: any) => {
  66. if (isEmpty(errors)) {
  67. hasJsonError.value = false
  68. return
  69. }
  70. hasJsonError.value = true
  71. }
  72. </script>