king
2022-02-28 79943222ed0edc6bd35ab2858c5eda2bc3246f66
src/menu/replaceField/index.jsx
@@ -9,6 +9,7 @@
import Utils from '@/utils/utils.js'
import SettingForm from './settingform'
import { queryTableSql } from '@/utils/option.js'
import MKEmitter from '@/utils/events.js'
import './index.scss'
class ReplaceField extends Component {
@@ -84,11 +85,28 @@
  }
  submit = () => {
    let config = fromJS(this.props.config).toJS()
    this.settingRef.handleConfirm().then(res => {
      this.setState({confirming: true})
      if (res.resource === 'custom') {
        if (res.reType === 'name') {
          let map = {[res.label]: {
            FieldDec: res.label,
            FieldName: res.field,
            datatype: ''
          }}
          this.exec(map)
        } else {
          let map = {[res.field]: {
            FieldDec: res.label,
            FieldName: res.field,
            datatype: ''
          }}
          this.execLabel(map)
        }
      } else {
      let param = {func: 'sPC_Get_FieldName', TBName: res.table}
      if (options.cloudServiceApi) { // 且存在云端地址
        param.rduri = options.cloudServiceApi
@@ -109,6 +127,7 @@
          return
        }
          if (res.reType === 'name') {
        let map = {}
        result.FDName.forEach(item => {
@@ -117,6 +136,24 @@
          }
          map[item.FieldDec] = item
        })
            this.exec(map)
          } else {
            let map = {}
            result.FDName.forEach(item => {
              map[item.FieldName] = item
            })
            this.execLabel(map)
          }
        })
      }
    })
  }
  exec = (map) => {
    let config = fromJS(this.props.config).toJS()
        if (this.props.type === 'custom') {
          let _replace = (components) => {
@@ -133,10 +170,11 @@
              
              if (item.columns) {
                item.columns = item.columns.map(col => {
                  if (map[col.field]) {
                    col.field = map[col.field].FieldName
                    if (map[col.field].datatype) {
                      col.datatype = map[col.field].datatype
              let cell = map[col.field]
              if (cell) {
                col.field = cell.FieldName
                if (cell.datatype) {
                  col.datatype = cell.datatype
                    }
                  }
                  return col
@@ -350,9 +388,132 @@
          duration: 3
        })
        this.props.updateConfig(config)
    setTimeout(() => {
      MKEmitter.emit('revert')
    }, 500)
  }
  execLabel = (map) => {
    let config = fromJS(this.props.config).toJS()
    if (this.props.type === 'custom') {
      let _replace = (components) => {
        return components.map(item => {
          if (item.type === 'tabs') {
            item.subtabs.forEach(tab => {
              tab.components = _replace(tab.components)
      })
            return item
          } else if (item.type === 'group') {
            item.components = _replace(item.components)
            return item
          }
          if (item.columns) {
            item.columns = item.columns.map(col => {
              if (map[col.field]) {
                col.label = map[col.field].FieldDec
              }
              return col
    })
  }
          if (item.search) {
            item.search = item.search.map(col => {
              if (map[col.field]) {
                col.label = map[col.field].FieldDec
              }
              return col
            })
          }
          if (item.action) {
            item.action.forEach(m => {
              if (m.modal && m.modal.fields) {
                m.modal.fields = m.modal.fields.map(col => {
                  if (col.field && map[col.field]) {
                    col.label = map[col.field].FieldDec
                  }
                  return col
                })
              }
            })
          }
          if (item.cols) {
            let _update = (cols) => {
              return cols.map(col => {
                if (col.type === 'action' && col.elements) {
                  col.elements = col.elements.map(m => {
                    if (m.modal && m.modal.fields) {
                      m.modal.fields = m.modal.fields.map(col => {
                        if (col.field && map[col.field]) {
                          col.label = map[col.field].FieldDec
                        }
                        return col
                      })
                    }
                    return m
                  })
                } else if (col.type === 'colspan') {
                  col.subcols = _update(col.subcols)
                } else if (col.field) {
                  if (map[col.field]) {
                    col.label = map[col.field].FieldDec
                  }
                }
                return col
              })
            }
            item.cols = _update(item.cols)
          }
          return item
        })
      }
      config.components = _replace(config.components)
    } else if (this.props.type === 'table') {
      config.columns = config.columns.map(col => {
        if (col.field && map[col.field]) {
          col.label = map[col.field].FieldDec
        }
        return col
      })
      config.search = config.search.map(col => {
        if (col.field && map[col.field]) {
          col.label = map[col.field].FieldDec
        }
        return col
      })
    } else if (this.props.type === 'form') {
      config.fields = config.fields.map(col => {
        if (col.field && map[col.field]) {
          col.label = map[col.field].FieldDec
        }
        return col
      })
    }
    this.setState({
      confirming: false,
      visible: false
    })
    notification.success({
      top: 92,
      message: '更新已完成。',
      duration: 3
    })
    this.props.updateConfig(config)
    setTimeout(() => {
      MKEmitter.emit('revert')
    }, 500)
  }
  render() {
    const { visible, loadingTable, tables, confirming } = this.state