king
2024-03-29 deb2536eaaa559d6c8ec94f81afb94b6c7806b4d
src/tabviews/custom/components/timeline/normal-timeline/index.jsx
@@ -1,7 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Spin, notification, Timeline, Empty, Modal } from 'antd'
import { Spin, notification, Timeline, Empty } from 'antd'
import Api from '@/api'
import MkIcon from '@/components/mk-icon'
@@ -16,16 +16,13 @@
class NormalTimeline extends Component {
  static propTpyes = {
    data: PropTypes.array,           // 统一查询数据
    config: PropTypes.object,        // 组件配置信息
    mainSearch: PropTypes.any,       // 外层搜索条件
    config: PropTypes.object
  }
  state = {
    BID: '',                   // 上级ID
    config: null,              // 图表配置信息
    loading: false,            // 数据加载状态
    sync: false,               // 是否统一请求数据
    data: null,                // 数据
    BData: '',
    card: null,
@@ -36,17 +33,13 @@
  /**
   * @description 初始化处理
   * 1、 initdata 为打印时使用的数据集
   */
  UNSAFE_componentWillMount () {
    const { data, initdata } = this.props
    let _config = fromJS(this.props.config).toJS()
    let _cols = new Map()
    const { config } = this.props
    let _config = fromJS(config).toJS()
    let _data = null
    let card = null
    let _sync = _config.setting.sync === 'true'
    let BID = ''
    let BData = ''
@@ -59,56 +52,53 @@
      BID = BData.$BID || ''
    }
    if (_config.setting.sync === 'true' && data) {
      _data = data[_config.dataName] || []
      _sync = false
    } else if (_config.setting.sync === 'true' && initdata) {
      _data = initdata || []
      _sync = false
    }
    if (_config.setting.sync === 'true') {
      _config.setting.onload = 'false'
    if (_data) {
      this.loaded = true
      _data = _data.map((item, index) => {
        item.key = index
        item.$$uuid = item[_config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        item.$$BData = BData || ''
        item.$Index = index + 1 + ''
        return item
      })
      if (window.GLOB.SyncData.has(_config.dataName)) {
        _data = window.GLOB.SyncData.get(_config.dataName) || []
        if (_config.$cache) {
          Api.writeCacheConfig(_config.uuid, fromJS(_data).toJS(), BID)
        }
        _config.setting.sync = 'false'
        _data = _data.map((item, index) => {
          item.key = index
          item.$$uuid = item[_config.setting.primaryKey] || ''
          item.$$BID = BID || ''
          item.$$BData = BData || ''
          item.$Index = index + 1 + ''
          return item
        })
        this.loaded = true
        window.GLOB.SyncData.delete(_config.dataName)
      }
    }
    _config.search = []
    _config.wrap.contentHeight = _config.wrap.title ? 'calc(100% - 45px)' : '100%'
    _config.columns.forEach(item => {
      if (item.type !== 'number') return
      _cols.set(item.field, item)
    })
    card = _config.subcards[0]
    card.elements = card.elements.map(item => {
      if (item.eleType === 'number' && item.field && _cols.has(item.field) && typeof(item.decimal) !== 'number') {
        item.decimal = _cols.get(item.field).decimal || 0
    if (_config.wrap.dotSign === 'adaptive') {
      try {
        let cl = Math.floor(document.body.clientWidth * _config.width / 24 / 320)
        _config.wrap.dotSign = ['', 'size16', 'size18', 'size20', 'size22', 'size24', 'size24', 'size24', 'size24'][cl]
      } catch(e) {
        _config.wrap.dotSign = 'size20'
      }
      return item
    })
    }
    this.setState({
      card,
      sync: _sync,
      BID: BID || '',
      BData: BData || '',
      data: _data,
      config: _config,
      arr_field: _config.columns.map(col => col.field).join(','),
    }, () => {
      if (_config.setting.sync !== 'true' && _config.setting.onload === 'true') {
        setTimeout(() => {
          this.loadData()
        }, _config.setting.delay || 0)
      }
    })
  }
@@ -116,30 +106,27 @@
    const { config } = this.state
    MKEmitter.addListener('reloadData', this.reloadData)
    MKEmitter.addListener('refreshLineData', this.refreshLineData)
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    MKEmitter.addListener('queryModuleParam', this.queryModuleParam)
    MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult)
    if (config.setting.useMSearch) {
      MKEmitter.addListener('searchRefresh', this.searchRefresh)
    }
    if (config.setting.sync === 'true') {
      MKEmitter.addListener('transferSyncData', this.transferSyncData)
    }
    if (config.timer) {
      this.timer = new TimerTask()
      this.timer.init(config.uuid, config.timer, config.timerRepeats, () => {
        this.loadData(true)
        this.loadData('timer')
      })
    }
    if (config.$cache && !this.loaded) {
      Api.getLCacheConfig(config.uuid).then(res => {
        if (!res || this.loaded) return
        this.setState({data: res.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$Index = index + 1 + ''
          return item
        })})
      })
    }
    this.initExec()
  }
  shouldComponentUpdate (nextProps, nextState) {
@@ -151,42 +138,107 @@
      return
    }
    MKEmitter.removeListener('reloadData', this.reloadData)
    MKEmitter.removeListener('searchRefresh', this.searchRefresh)
    MKEmitter.removeListener('refreshLineData', this.refreshLineData)
    MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
    MKEmitter.removeListener('queryModuleParam', this.queryModuleParam)
    MKEmitter.removeListener('transferSyncData', this.transferSyncData)
    MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult)
    this.timer && this.timer.stop()
  }
  /**
   * @description 图表数据更新,刷新内容
   */
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { sync, config, BID, BData } = this.state
  initExec = () => {
    const { config, BID } = this.state
    if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
      let _data = []
      if (nextProps.data && nextProps.data[config.dataName]) {
        _data = nextProps.data[config.dataName] || []
    if (config.$cache) {
      if (config.$time) {
        if (!this.loaded) {
          Api.getLCacheConfig(config.uuid, config.$time, BID).then(res => {
            if (!res.valid && config.setting.onload === 'true') {
              setTimeout(() => {
                this.loadData('init')
              }, config.setting.delay || 0)
            }
            if (!res.data || this.loaded) return
            this.setState({data: res.data.map((item, index) => {
              item.key = index
              item.$$uuid = item[config.setting.primaryKey] || ''
              item.$Index = index + 1 + ''
              return item
            })})
          })
        }
      } else {
        if (!this.loaded) {
          Api.getLCacheConfig(config.uuid, 0, BID).then(res => {
            if (!res.data || this.loaded) return
            this.setState({data: res.data.map((item, index) => {
              item.key = index
              item.$$uuid = item[config.setting.primaryKey] || ''
              item.$Index = index + 1 + ''
              return item
            })})
          })
        }
        if (config.setting.onload === 'true') {
          setTimeout(() => {
            this.loadData('init')
          }, config.setting.delay || 0)
        }
      }
      _data = _data.map((item, index) => {
        item.key = index
        item.$$uuid = item[config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        item.$$BData = BData || ''
        item.$Index = index + 1 + ''
        return item
      })
      this.loaded = true
      this.setState({sync: false, data: _data})
    } else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({}, () => {
    } else if (config.setting.onload === 'true') {
      setTimeout(() => {
        this.loadData()
      })
      }, config.setting.delay || 0)
    }
  }
  transferSyncData = (syncId) => {
    const { config } = this.state
    if (config.$syncId !== syncId) return
    const { BID, BData } = this.state
    let _data = window.GLOB.SyncData.get(config.dataName) || []
    if (config.$cache) {
      Api.writeCacheConfig(config.uuid, fromJS(_data).toJS(), BID)
    }
    _data = _data.map((item, index) => {
      item.key = index
      item.$$uuid = item[config.setting.primaryKey] || ''
      item.$$BID = BID || ''
      item.$$BData = BData || ''
      item.$Index = index + 1 + ''
      return item
    })
    this.loaded = true
    this.setState({data: _data})
    window.GLOB.SyncData.delete(config.dataName)
    MKEmitter.removeListener('transferSyncData', this.transferSyncData)
  }
  searchRefresh = (searchId) => {
    const { config } = this.state
    if (config.$searchId !== searchId) return
    this.setState({}, () => {
      this.loadData()
    })
  }
  /**
@@ -205,10 +257,23 @@
    } else {
      this.loadData()
    }
  }
    if (position === 'popclose') { // 执行启动弹窗的按钮所选择的刷新项
      btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId)
    }
  refreshLineData = (menuId, btn, uuid, count) => {
    const { config, data } = this.state
    if (config.uuid !== menuId) return
    let _data = fromJS(data).toJS().map(item => {
      if (item.$$uuid === uuid) {
        item[btn.field] = count
      }
      return item
    })
    this.setState({
      data: _data
    })
  }
  resetParentParam = (MenuID, id, data) => {
@@ -230,36 +295,27 @@
    this.loadData()
  }
   /**
  /**
   * @description 导出Excel时,获取页面搜索排序等参数
   */
  queryModuleParam = (menuId, callback) => {
    const { mainSearch } = this.props
    const { arr_field, config } = this.state
    const { config } = this.state
    if (config.uuid !== menuId) return
    let searches = []
    if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 主表搜索条件
      let keys = searches.map(item => item.key.toLowerCase())
      mainSearch.forEach(item => {
        if (!keys.includes(item.key.toLowerCase())) {
          searches.push(item)
        }
      })
    if (config.setting.useMSearch) { // 主表搜索条件
      searches = window.GLOB.SearchBox.get(config.$searchId) || []
    }
    callback({
      arr_field: arr_field,
      orderBy: config.setting.order || '',
      search: searches,
      menuName: config.name
      search: searches
    })
  }
  async loadData (hastimer) {
    const { mainSearch } = this.props
    const { config, arr_field, BID, BData } = this.state
  async loadData (type) {
    const { config, BID, BData } = this.state
    
    if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
      this.setState({
@@ -269,70 +325,66 @@
      return
    }
    // 内部函数为z_mk_express,表示查询快递信息
    if (config.setting.interType === 'inner' && config.setting.innerFunc === 'z_mk_express') {
      this.getExpress()
      return
    }
    let searches = []
    if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 主表搜索条件
      let keys = searches.map(item => item.key)
      mainSearch.forEach(item => {
        if (!keys.includes(item.key)) {
          searches.push(item)
        }
      })
    if (config.setting.useMSearch) { // 主表搜索条件
      searches = window.GLOB.SearchBox.get(config.$searchId) || []
    }
    let requireFields = searches.filter(item => item.required && item.value === '')
    if (requireFields.length > 0) {
    if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) {
      return
    }
    if (!hastimer) {
    if (type !== 'timer') {
      this.setState({
        loading: true
      })
    }
    let _orderBy = config.setting.order || ''
    let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, 1, config.setting.pageSize, BID)
    let param = UtilsDM.getQueryDataParams(config.setting, searches, _orderBy, 1, config.setting.pageSize, BID)
    let result = await Api.genericInterface(param)
    if (result.status) {
      this.loaded = true
      if (config.$cache) {
        Api.writeCacheConfig(config.uuid, result.data || '')
      if (config.$cache && type === 'init') {
        Api.writeCacheConfig(config.uuid, result.data || [], BID)
      }
      let data = result.data.map((item, index) => {
        item.key = index
        item.$$uuid = item[config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        item.$$BData = BData || ''
        item.$Index = index + 1 + ''
        return item
      })
      this.setState({
        data: result.data.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$$BID = BID || ''
          item.$$BData = BData || ''
          item.$Index = index + 1 + ''
          return item
        }),
        data: data,
        loading: false
      })
      if (config.timer && config.clearField && result.data && result.data[0]) {
        let vals = (config.clearValue || '').split(',')
        if (vals.includes(result.data[0][config.clearField])) {
          this.timer && this.timer.stop()
        }
      }
      UtilsDM.querySuccess(result)
    } else {
      this.setState({
        loading: false
      })
      this.timer && this.timer.stop()
      
      if (result.ErrCode === 'N') {
        Modal.error({
          title: result.message,
        })
      } else {
        notification.error({
          top: 92,
          message: result.message,
          duration: 10
        })
      }
      UtilsDM.queryFail(result)
    }
  }
@@ -456,7 +508,7 @@
        </div>
        <div className="mk-timeline-item-head">
          <div className="mk-timeline-item-tail" style={linebg}></div>
          <div className={'mk-dot ' + (dot ? 'mk-dot-icon' : '')} style={{background: color}}>
          <div className={'mk-dot ' + (dot ? 'mk-dot-icon' : '')} style={{background: color, color: color}}>
            {dot}
          </div>
        </div>
@@ -465,7 +517,7 @@
      return (<div className="mk-time-line-item" key={data.$Index}>
        <div className="mk-timeline-item-head">
          <div className="mk-timeline-item-tail" style={linebg}></div>
          <div className={'mk-dot ' + (dot ? 'mk-dot-icon' : '')} style={{background: color}}>
          <div className={'mk-dot ' + (dot ? 'mk-dot-icon' : '')} style={{background: color, color: color}}>
            {dot}
          </div>
        </div>
@@ -481,8 +533,10 @@
  render() {
    const { config, loading, data, description } = this.state
    if (config.wrap.empty === 'hidden' && (!data || data.length === 0)) return null
    return (
      <div className="normal-timeline-box" id={'anchor' + config.uuid} style={{...config.style}}>
      <div className={'normal-timeline-box ' + (config.wrap.btnControl || '')} id={'anchor' + config.uuid} style={{...config.style}}>
        {loading ?
          <div className="loading-mask">
            {data ? <div className="ant-spin-blur"></div> : null}
@@ -493,7 +547,7 @@
        {config.wrap.direction !== 'horizontal' && data && data.length > 0 ? <Timeline mode={config.wrap.mode} className={'card-row-list ' + (config.wrap.line || '')} style={{height: config.wrap.contentHeight}}>
          {data.map(item => this.getnodes(item))}
        </Timeline> : null}
        {config.wrap.direction === 'horizontal' && data && data.length > 0 ? <div className={'mk-time-line-wrap card-row-list ' + (config.wrap.line || '')} style={{height: config.wrap.contentHeight}}>
        {config.wrap.direction === 'horizontal' && data && data.length > 0 ? <div className={`mk-time-line-wrap card-row-list ${config.wrap.line || ''} ${config.wrap.iconSize || ''} ${config.wrap.dotSign || ''}`} style={{height: config.wrap.contentHeight}}>
          {data.map(item => this.getMknodes(item))}
        </div> : null}
        {data && data.length === 0 ? <div className="card-row-list" style={{height: config.wrap.contentHeight}}>