ThingModelTSL.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle">
  3. <el-scrollbar height="600px">
  4. <pre><code v-dompurify-html="highlightedCode()" class="hljs"></code></pre>
  5. </el-scrollbar>
  6. </Dialog>
  7. </template>
  8. <script setup lang="ts">
  9. import hljs from 'highlight.js' // 导入代码高亮文件
  10. import 'highlight.js/styles/github.css' // 导入代码高亮样式
  11. import json from 'highlight.js/lib/languages/json'
  12. import { ThingModelApi } from '@/api/iot/thingmodel'
  13. import { ProductVO } from '@/api/iot/product/product'
  14. import { IOT_PROVIDE_KEY } from '@/views/iot/utils/constants'
  15. defineOptions({ name: 'ThingModelTSL' })
  16. const dialogVisible = ref(false) // 弹窗的是否展示
  17. const dialogTitle = ref('物模型 TSL') // 弹窗的标题
  18. const product = inject<Ref<ProductVO>>(IOT_PROVIDE_KEY.PRODUCT) // 注入产品信息
  19. /** 打开弹窗 */
  20. const open = () => {
  21. dialogVisible.value = true
  22. }
  23. defineExpose({ open })
  24. const thingModelTSL = ref('')
  25. /** 获取 TSL */
  26. const getTsl = async () => {
  27. const res = await ThingModelApi.getThingModelTSLByProductId(product?.value?.id || 0)
  28. thingModelTSL.value = JSON.stringify(res, null, 2)
  29. }
  30. /**
  31. * 代码高亮
  32. */
  33. const highlightedCode = () => {
  34. const result = hljs.highlight('json', thingModelTSL.value, true)
  35. return result.value || '&nbsp;'
  36. }
  37. /** 初始化 **/
  38. onMounted(async () => {
  39. // 注册代码高亮的各种语言
  40. hljs.registerLanguage('json', json)
  41. await getTsl()
  42. })
  43. </script>