index.vue 4.06 KB
<!--
 * @Description  :配置菜单
 * @Autor        : miaofang
 * @LastEditTime : 2023-05-18 13:25:19
-->
<template>
  <div>
    <i class="el-icon-s-management icon" :title="title" @click="openDialog" />
    <el-dialog
      class="configuration"
      :key="key"
      :title="title"
      :inner-dialog="true"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      append-to-body
      @cancel="cancel">
      <vue-json-editor
        id="minejson"
        v-model="resultInfo"
        :mode="'code'"
        lang="zh"
        @json-change="onJsonChange"
        @json-save="onJsonSave"
        @has-error="onError" />
      <el-tooltip
        content="全屏缩放"
        effect="dark"
        placement="bottom"
        fullscreen
        class="fullScreen">
        <i class="el-icon-full-screen" @click="enLarge" />
      </el-tooltip>
      <template slot="footer">
        <div class="dialog-footer flex flex-pack-center">
          <btn nativeType="cx" @click="onJsonSave">保存</btn>
          <btn nativeType="cx" @click="cancel">关闭</btn>

        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
  import vueJsonEditor from 'vue-json-editor'
  export default {
    components: {
      vueJsonEditor
    },
    props: {
      title: {
        type: String,
        default: '配置参数'
      },
      resultInfos: {
        type: String,
        default: ''
      }
    },
    data () {
      return {
        activeNames: [],
        resultInfo: {},
        tmpResultInfo: {},
        dialogVisible: false,
        hasJsonFlag: true,
        key: 0,
        isEnlarge: false
      }
    },
    watch: {
      resultInfos: {
        handler: function (val) {
          ++this.key
          this.resultInfo =
            this.resultInfos === '' ? {} : JSON.parse(this.resultInfos)
          this.tmpResultInfo = this.resultInfo
        },
        deep: true,
        immediate: true
      }
    },

    mounted () {
      this.resultInfo =
        this.resultInfos === '' ? {} : JSON.parse(this.resultInfos)
    },

    methods: {
      /**
       * @description: onJsonChange
       * @param {*} value
       * @author: renchao
       */
      onJsonChange (value) {
        // 只有在格式正确的时候进入此事件
        this.hasJsonFlag = true
      },
      /**
       * @description: onJsonSave
       * @author: renchao
       */
      onJsonSave () {
        const value = this.resultInfo
        if (this.hasJsonFlag === false) {
          this.$message.error({ message: 'json格式验证失败', showClose: true })
          // alert("json验证失败")
          return false
        } else {
          this.dialogVisible = false
          this.$emit('getJsonString', JSON.stringify(value))
          return true
        }
      },
      /**
       * @description: onError
       * @param {*} value
       * @author: renchao
       */
      onError (value) {
        this.hasJsonFlag = false
      },
      /**
       * @description: openDialog
       * @author: renchao
       */
      openDialog () {
        this.dialogVisible = true
      },
      /**
       * @description: cancel
       * @author: renchao
       */
      cancel () {
        this.resultInfo = this.tmpResultInfo
        this.dialogVisible = false
      },
      // 放大
      /**
       * @description: 放大
       * @author: renchao
       */
      enLarge () {
        const fullarea = document.getElementById('minejson')
        if (fullarea.requestFullscreen) {
          fullarea.requestFullscreen()
        } else if (fullarea.webkitRequestFullScreen) {
          fullarea.webkitRequestFullScreen() // webkit内核(chrome、safari、Opera等)
        } else if (fullarea.mozRequestFullScreen) {
          fullarea.mozRequestFullScreen() // moz内核(firefox)
        } else if (fullarea.msRequestFullscreen) {
          fullarea.msRequestFullscreen() // IE11、edge
        }
        this.isEnlarge = true
      }
    }
  }
</script>

<style scoped lang="scss">
  /* jsoneditor右上角默认有一个链接,加css去掉了 */
  .icon {
    color: #349af3;
  }
</style>