jsoneditor.vue 17.8 KB
Newer Older
何虹's avatar
何虹 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
<template>
 <el-dialog
   id="jsonEditorBox"
   @opened="dialogOpen"
   :title='pageTitle'
   :visible.sync="dialogVisible"
   @closed="dialogClose" width="95%"
   :append-to-body='true'
   :close-on-click-modal='false'>
  <div id="jsonEditBox">
    <div style="display:flex;">
      <div class="editItemBox1">
          <div class="editeTitleItem">配置</div>
          <div :id='ideditor1'  style="width: 100%; height: 100%;"></div>
      </div>
      <div  class="editItemBox2">
         <div class="editeTitleItem">模拟数据</div>
         <div :id='ideditor2' style="width: 100%; height: 100%;"></div>
      </div>
      <div  class="editItemBox3" id="sqlBox">
        <div class="editeTitleItem">SQL</div>
        <el-input
          style="width: 100%; height: 100%;border:1px solid #1160f1"
          type="textarea"
          placeholder="请输入内容"
          v-model="textareaSql">
        </el-input>
      </div>
    </div>
    <el-row :gutter="20">
      <div class="buttonBox">
        <div class="infopageItem">
          <div class="pageItem">
              <span>组件名称:{{elInfo.el}}</span>
          </div>
          <div class="pageItem">
              <span>版本号:{{this.editDataAll.version}}</span>
          </div>
          <div class="pageItem">
              <span>时间:{{this.editDataAll.update_time}}</span>
          </div>
        </div>
        <div class="button_hanlde">
              <el-switch v-model="is_mock" active-text="模拟" inactive-text="正式"></el-switch>
              <el-button v-if="elInfo.el==='hsDyncFormComponent'" size='mini' plain type="success" @click='editAnyncColums'>修改表单</el-button>
何虹's avatar
.  
何虹 committed
46
              <el-button v-if="elInfo.el&&elInfo.el.includes('hsTable')" size='mini' plain type="success" @click='editTableColums'>修改表头</el-button>
何虹's avatar
何虹 committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
              <el-button v-show="layout" size='mini' plain type="danger" @click='changControl(1)'>上一个</el-button>
              <el-button v-show="layout" size='mini' plain type="warning" @click='changControl(2)'>下一个</el-button>
              <el-button size='mini' plain type="success" @click='execSql'>模拟数据</el-button>
              <el-button size='mini' type="primary" @click='showView'>查看效果</el-button>
              <el-button size='mini' type="danger" @click="submitToDb">提交到数据库</el-button>
              <el-button size='mini' type="warning" @click='resetEdit'>撤销修改</el-button>
              <el-button size='mini' plain  @click='lookPageLogView'>查看日志</el-button>
              <el-button size='mini' type="success" @click='downSql_'>另存</el-button>
        </div>
      </div>
      <div v-show="!lookPageLogVisible">
      <template v-if='elInfo.el=="hsLineBar"||elInfo.el=="hsPie"||elInfo.el=="hsRadar"'>
        <hsLineBar :elInfo='elInfo'  :initStyle='initStyle' :allSourceData="commonData"></hsLineBar>
      </template>
      <template  v-if='elInfo.el==="hsCarousel"'>
        <hsCarousel :initStyle='initStyle'  :allSourceData='commonData'></hsCarousel>
      </template>
      <template  v-if='elInfo.el==="hsTable"'>
        <hsTable :initStyle='initStyle'  :allSourceData='commonData'></hsTable>
      </template>
       <template  v-if='elInfo.el==="hsSelectPlus"'>
        <hsSelectPlus  :allSourceData="commonData"></hsSelectPlus>
      </template>
      </div>
    </el-row>
  </div>
      <lookPageLog @revertVersion='revertVersion_' :isrequest='isrequest' @showView='showViewLog'
        v-if="lookPageLogVisible" :elInfo='elInfo'/>
      <tbaleColumsSet v-model="dialogVisibleTbaleColums" :tableDataColums='tableDataColums'
         :allSourceData='allSourceData' :mockSourceData='mockSourceData'
         :elInfo='elInfo'
         :isEditColumns='isEditColumns'
         @submit='submitTbaleColumsSet' :isSubmitSuccessFlage='isSubmitSuccessFlage' />
      <asyncFormPropSet v-model="dialogVisibleAsyncFormColumn" :asyncFormColumns='asyncFormColumns'
          @submit='asyncFormPropSubmit'
          :elInfo='elInfo'
          :controlType='controlType' />
      <el-dialog
            title="布局类型选择"
            :visible.sync="tipAdminDialogVisible"
            width="50%"
            :append-to-body='true'
            :close-on-click-modal='false'
            >
            <span class="tipLayoutStyle">请选择保存布局的类型?</span>
            <span slot="footer" class="dialog-footer">
              <el-button size="mini" type="primary" @click="submitConfig(0)">全局</el-button>
              <el-button size="mini" type="primary" @click="submitConfig(1)">强制并全局使用</el-button>
              <el-button size="mini" type="primary" @click="submitConfig(2)">个人</el-button>
              <el-button size="mini" @click="tipAdminDialogVisible = false">取 消</el-button>
            </span>
      </el-dialog>
 </el-dialog>
</template>
<script>
import uuidv1 from 'uuid/v1'
import mockData from './initDbConfigDataJson'
import commonUtility from '../funTools/commonUtility'
import request from '../funTools/request'
何虹's avatar
.  
何虹 committed
106 107 108
import lookPageLog from './lookPageLog.vue'
import asyncFormPropSet from './asyncFormPropSet.vue'
import tbaleColumsSet from './tbaleColumsSet.vue'
何虹's avatar
何虹 committed
109 110
import 'jsoneditor/dist/jsoneditor.min.css'
import jsoneditor from 'jsoneditor'
何虹's avatar
.  
何虹 committed
111 112 113
// const lookPageLog = r => require.ensure([], () => r(require('./lookPageLog.vue')), 'lookPageLog')
// const asyncFormPropSet = r => require.ensure([], () => r(require('./asyncFormPropSet.vue')), 'asyncFormPropSet')
// const tbaleColumsSet = r => require.ensure([], () => r(require('./tbaleColumsSet.vue')), 'tbaleColumsSet')
何虹's avatar
何虹 committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
export default {
  components: { lookPageLog, asyncFormPropSet, tbaleColumsSet },
  name: 'jsoneditor',
  props: {
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    initStyle: {
      type: Object,
      default() {
        return {}
      }
    },
    complatData: {
      type: Array,
      default() {
        return []
      }
    },
    value: Boolean,
    layout: {},
    jsoneditorCloseAfter: {
      type: Function,
      default() {
        return () => {}
      }
    },
    jsoneditorOpenAfter: {
      type: Function,
      default() {
        return () => {}
      }
    },
    allSourceData: {},
    controlType: {},
    isEditColumns: {}
  },
  watch: {
    value(val) {
      this.dialogVisible = val
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  data() {
    return {
      options: {
        mode: 'text',
        indentation: 2,
        search: false
      },
      ideditor1: 'b' + uuidv1(),
      ideditor2: 'b' + uuidv1(),
      editor1: null,
      editor2: null,
      commonData: {},
      copyJsoneditorData: null,
      pageTitle: '配置',
      dialogVisible: false,
      editDataAll: {},
      lookPageLogVisible: false,
      isrequest: false,
      is_mock: true,
      textareaSql: '',
      dialogVisibleTbaleColums: false,
      mockSourceData: [],
      tableDataColums: [],
      dialogVisibleAsyncFormColumn: false,
      asyncFormColumns: [],
      isSubmitSuccessFlage: '',
      errorEdit: 0,
      tipAdminDialogVisible: false
    }
  },
  methods: {
    submitConfig(num) {
      this.openTips('此操作将覆盖数据库中的数据, 是否继续?', this.submitData, num)
    },
    getAppCodePageName() {
      const { db_name, is_mock, db_code } = commonUtility.parseUrlQueryString()
      const { appCode, pagename } = commonUtility.parseUrlQueryBIDyncAppcodePageName()
      return {
        appCode,
        pagename,
        db_name,
        db_code,
        is_mock
      }
    },
    getPageLogFun(parm) {
      return request.getPageLog(parm)
    },
    submitTbaleColumsSet(columnsConfig, flage) {
      const config = this.editor1.get()
      config.columnsConfig = columnsConfig
      this.editor1.set(config)
      const sql = this.textareaSql
      const data = {
        dbName: this.editDataAll.dbName,
        sAppCode: this.editDataAll.app_code,
        sPage: this.editDataAll.sPage,
        sControl: this.editDataAll.control_name,
        sConfig: config, // this.editor1.get(),
        sData: this.editor2.get(),
        sQuerySql: sql,
        elInfo: this.elInfo,
        is_mock: this.is_mock ? 1 : 0
      }
      request.runSave(data).then(() => {
        this.$message.success('设置成功')
        if (flage === 'columnStyleSet') {
          this.isSubmitSuccessFlage = 'columnStyleSet'
        } else {
          this.isSubmitSuccessFlage = 'tbaleColumsSet'
          this.dialogVisibleTbaleColums = false
        }
        this.editor1.set(config)
      })
    },
    asyncFormPropSubmit(data) {
      const config = this.editor1.get()
      config.formColumn = data
      const datas = {
        config: config
      }
      this.saveData(datas).then(res => {
        this.dialogVisibleAsyncFormColumn = false
        this.editor1.set(datas.config)
      })
    },
    saveData(datas) {
      const data = {
        dbName: this.editDataAll.dbName,
        sAppCode: this.editDataAll.app_code,
        sPage: this.editDataAll.sPage,
        sControl: this.editDataAll.control_name,
        sConfig: datas.config || this.editor1.get(),
        sData: this.editor2.get(),
        sQuerySql: this.textareaSql,
        elInfo: this.elInfo,
        is_mock: this.is_mock ? 1 : 0
      }
      return request.runSave(data)
    },
    editAnyncColums() {
      this.dialogVisibleAsyncFormColumn = true
      const config = this.editor1.get()
      this.asyncFormColumns = config.formColumn
    },
    editTableColums() {
      this.dialogVisibleTbaleColums = true
      const config = this.editor1.get()
      this.mockSourceData = this.editor2.get()
      if (config && config.columnsConfig && config.columnsConfig.length) {
        this.tableDataColums = [...config.columnsConfig]
      } else {
        this.tableDataColums = [{}]
      }
      this.tableDataColums.forEach((item, index) => {
        if (item.childs) {
          this.tableDataColums.splice(index, 1, ...item.childs)
        }
      })
    },
    // 折线图的模拟数据
    getMockData(type) {
      return mockData[type]
    },
    changControl(flage) {
      if (this.layout) {
        const list = this.layout.filter(item => item.isRequestControl)
        const currIndex = list.findIndex(item => item.control.position === this.elInfo.position)
        let nextIndex = 0
        let target = null
        if (flage === 1) { // 上一个
          if (currIndex === 0) {
            this.$message('已经是第一个了')
            return
          }
          nextIndex = currIndex - 1
          target = list[nextIndex]
        } else { // 下一个
          if (currIndex === list.length - 1) {
            this.$message('已经是最后个了')
            return
          }
          nextIndex = currIndex + 1
          target = list[nextIndex]
        }
        this.elInfo = target.control
        this.getPageInfo(target.control)
      }
    },
    lookPageLogView() {
      this.lookPageLogVisible = true
      this.isrequest = !this.isrequest
    },
    showViewLog(data) {
      this.editor1.set(data.config)
      this.editor2.set(data.sourceData)
      this.textareaSql = data.sql || ''
      this.showView()
    },
    revertVersion_(parm) {
      request.revertPageLog(parm).then(res => {
        this.getPageInfo()
        this.isrequest = !this.isrequest
      })
    },
    execSql() {
      const data = {
        sQuerySql: this.query_sql,
        dbName: this.editDataAll.dbName
      }
      request.execSql(data).then(res => {
        this.editor2.set(res)
      })
    },
    getPageInfo(info = this.elInfo) {
      const routeParams = this.getAppCodePageName()
      const { position, page, sAppCode } = info
      const app_code = sAppCode || routeParams.appCode || ''
      const sPage = page || routeParams.pagename
      const data = {
        sControl: position,
        sAppCode: app_code,
        sPage: sPage
      }
      request.getSql(data).then(data => {
        if (data && data.length) {
          this.editDataAll = data[0]
          const { app_code: app_code_, control_name, json_config, json_data, page_name, query_sql, is_mock } = this.editDataAll
          const appCode = app_code || app_code_
          if (!app_code_ || app_code_ === 'undefined') {
            this.editDataAll.app_code = app_code
          }
          this.pageTitle = `配置: AppCode: <${appCode}>---page_name: <${page_name}>---control_name: <${control_name}>`
          const jsonData = {
            config: JSON.parse(json_config),
            sourceData: JSON.parse(json_data),
            sql: query_sql || ''
          }
          this.copyJsoneditorData = JSON.parse(JSON.stringify(jsonData))
          this.setJson(jsonData)
          this.textareaSql = query_sql || ''
          this.is_mock = !!is_mock
        } else {
          this.initData()
        }
      })
    },
    downSql_() {
      const data = {
        dbName: this.editDataAll.dbName,
        sAppCode: this.editDataAll.app_code,
        sPage: this.editDataAll.sPage
      }
      request.downSql(data)
    },
    dialogClose() {
      this.dialogVisible = false
      this.jsoneditorCloseAfter()
    },
    dialogOpen() {
      this.revertVersion = false
      this.getPageInfo()
      this.jsoneditorOpenAfter()
    },
    initData() {
      const AppCodePageOBj = this.getAppCodePageName()
      const app_code = AppCodePageOBj.appCode
      const page_name = AppCodePageOBj.pagename
      this.pageTitle = `配置: AppCode: <${app_code}>---page_name: <${page_name}>---control_name: <${this.elInfo.position}>`
      const mockdata = this.getMockData(this.elInfo.el)
      if (!mockdata) {
        this.$message.error(this.elInfo.el + '----请配置模拟数据!')
        this.setJson({ config: {}, sourceData: [] })
        return
      }
      this.copyJsoneditorData = JSON.parse(JSON.stringify(mockdata))
      this.setJson(mockdata)
      this.textareaSql = this.copyJsoneditorData.sql
    },
    resetEdit() {
      this.openTips('是否确定撤销修改?', this.setOldJson)
    },
    setOldJson() {
      this.setJson(this.copyJsoneditorData)
    },
    submitToDb() {
      this.openTips('此操作将覆盖数据库中的数据, 是否继续?', this.submitData)
    },
    openTips(message, fun, parm) {
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          fun(parm)
        })
        .catch((e) => {
          console.log(e)
          this.$message.error('已取消-请保证json格式的完整性!,最左边没有就配置为{},中间没有就配置为[]')
        })
    },
    submitData() {
      const editor1Config = this.editor1.get()
      const editor2Config = this.editor2.get()
      this.errorEdit = 2
      let data_ = {}
      if (this.complatData.length) {
        const complatData = JSON.parse(JSON.stringify(this.complatData))
        const index = complatData.findIndex(item => {
          return item.config.id === editor1Config.id
        })
        complatData[index].config = editor1Config
        complatData[index].sourceData = editor2Config
        const sql = this.textareaSql
        const data = {
          dbName: this.editDataAll.dbName,
          sAppCode: this.editDataAll.app_code,
          sPage: this.editDataAll.sPage,
          sControl: this.editDataAll.control_name,
          sConfig: editor1Config,
          sData: complatData,
          sQuerySql: sql,
          elInfo: this.elInfo,
          is_mock: this.is_mock ? 1 : 0
        }
        data_ = data
      } else {
        let app_code = this.editDataAll.app_code
        const routeParams = this.getAppCodePageName()
        if (app_code === 'undefined' || app_code === undefined || !app_code) {
          app_code = this.elInfo.sAppCode || routeParams.appCode
        }
        const sql = this.textareaSql
        const data = {
          dbName: this.editDataAll.dbName,
          sAppCode: app_code,
          sPage: this.editDataAll.sPage,
          sControl: this.editDataAll.control_name,
          sConfig: editor1Config,
          sData: editor2Config,
          sQuerySql: sql,
          elInfo: this.elInfo,
          is_mock: this.is_mock ? 1 : 0
        }
        data_ = data
      }
      request.runSave(data_).then(() => {
        this.$message.success('设置成功')
      })
    },
    setJson(data) {
      const { config, sourceData } = JSON.parse(JSON.stringify(data))
      if (!this.editor1) {
        this.drow(this.ideditor1, config, 'editor1')
        this.drow(this.ideditor2, sourceData, 'editor2')
      } else {
        this.editor1.set(config)
        this.editor2.set(sourceData)
      }
    },
    showView() {
      this.lookPageLogVisible = false
      this.showEchart()
    },
    showEchart() {
      const config = this.editor1.get()
      const sourceData = this.editor2.get()
      this.commonData = {
        config,
        sourceData
      }
    },
    drow(el, json = {}, flage) {
      const container = document.getElementById(el)
何虹's avatar
何虹 committed
497
      this[flage] = new jsoneditor(container, this.options)
何虹's avatar
何虹 committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
      this[flage].set(json)
    }
  }
}
</script>
<style scoped>
.jsonEditorBox >>> .el-message-box{
    background-color: #f6f6f9!important;
    color: #040404 !important;
}
.jsonEditorBox >>> .el-message-box__content{
    color: #040404 !important;
}
.jsonEditorBox >>> .el-message-box > .el-button{
   color: #040404 !important;
   background-color:#4a4d5d4f!important;
}
.buttonBox{
    padding-top: 30px;
    padding-bottom: 20px;
    display: flex;
}
.editItemBox1{
 flex:1;
 height: 400px;
}
.editItemBox2{
  flex:1;
  height: 400px;
}
.editItemBox3{
  flex:2;
   height: 400px;
}
.editeTitleItem{
  text-align: center;
  font-weight: bold;
   font-family:"宋体"
}
.pageItem{
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.infopageItem{
    display: flex;
}
.button_hanlde{
  position:absolute;
  right: 10px;
}
 #sqlBox >>> .el-textarea__inner{
  height: 400px;
}
.tipLayoutStyle{
text-align: center
}
</style>