king
2022-06-08 c6f8e27d35cd31bb6393a1e7f747b6b2593dbd7a
src/tabviews/custom/components/card/data-card/index.jsx
@@ -1,7 +1,6 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { connect } from 'react-redux'
import { Spin, Empty, notification, message, Row, Col, Pagination } from 'antd'
import Api from '@/api'
@@ -11,7 +10,6 @@
import nextImg from '@/assets/img/next.png'
import MKEmitter from '@/utils/events.js'
import asyncComponent from '@/utils/asyncComponent'
import { modifyTabview } from '@/store/action'
import './index.scss'
const CardItem = asyncComponent(() => import('../cardItem'))
@@ -43,23 +41,37 @@
    total: null,
    precards: [],
    nextcards: [],
    selected: 'false',
    supNodes: [],
    supComs: null
  }
  UNSAFE_componentWillMount () {
    const { data, initdata, BID } = this.props
    const { data, initdata, BID, BData } = this.props
    let _config = fromJS(this.props.config).toJS()
    let _card = null
    let precards = []
    let nextcards = []
    if (_config.wrap.controlField) {
      if (_config.wrap.controlVal) {
        _config.wrap.controlVal = _config.wrap.controlVal.split(',')
      } else {
        _config.wrap.controlVal = ['']
      }
    }
    _config.subcards.forEach(item => {
      item.setting.$click = ''
      if (item.setting.click === 'button') {
        if (item.elements.filter(ele => ele.eleType === 'button').length < 2) {
          item.setting.$click = ' trigger-button'
      if (item.setting.click === 'button' && !item.setting.linkbtn) {
        item.elements.forEach(ele => {
          if (ele.eleType === 'button') {
            item.setting.linkbtn = ele.uuid
          }
        })
        if (!item.setting.linkbtn) {
          item.setting.click = ''
        }
        item.setting.click = ''
      }
      if (item.$cardType !== 'extendCard') {
@@ -86,18 +98,11 @@
      _sync = false
    }
    if (_config.wrap.cardFloat && _config.wrap.cardFloat !== 'left') {
      let _width = 0
      precards.forEach(card => {
        _width += card.setting.width
      })
      nextcards.forEach(card => {
        _width += card.setting.width
      })
      _config.$offset = _width
    let selected = 'false'
    if (_config.wrap.selected === 'always' || _config.wrap.selected === 'init') {
      selected = _config.wrap.selected
    } else {
      _config.wrap.cardFloat = null
      _config.wrap.selected = 'false'
    }
    if (_data) {
@@ -105,41 +110,74 @@
        item.key = index
        item.$$uuid = item[_config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        item.$$BData = BData || ''
        item.$Index = index + 1 + ''
        if (_config.wrap.controlField) {
          if (_config.wrap.controlVal.includes(item[_config.wrap.controlField])) {
            item.$disabled = true
          }
        }
        return item
      })
      if (selected !== 'false') {
        setTimeout(() => {
          this.checkTopLine()
        }, 200)
        if (selected === 'init') {
          selected = 'false'
        }
      }
    }
    _config.columns.forEach(item => {
      if (item.type !== 'number') return
      _cols.set(item.field, item)
    })
    _card.elements = _card.elements.map(item => {
      if (item.field && _cols.has(item.field)) {
        item.col = _cols.get(item.field)
      if (item.eleType === 'number' && item.field && _cols.has(item.field) && typeof(item.decimal) !== 'number') {
        item.decimal = _cols.get(item.field).decimal || 0
      }
      return item
    })
    _card.backElements = _card.backElements.map(item => {
      if (item.field && _cols.has(item.field)) {
        item.col = _cols.get(item.field)
      if (item.eleType === 'number' && item.field && _cols.has(item.field) && typeof(item.decimal) !== 'number') {
        item.decimal = _cols.get(item.field).decimal || 0
      }
      return item
    })
    let supComs = null
    if (_config.wrap.supType === 'multi') {
      supComs = _config.supNodes.map(item => item.componentId)
    }
    _config.wrap.selStyle = _config.wrap.selStyle || 'active'
    _config.wrap.scale = _config.wrap.scale === 'true' ? 'scale' : ''
    _config.wrap.layout = (_config.wrap.layout || 'grid') + '-layout float-' + (_config.wrap.cardFloat || 'left')
    _config.wrap.wrapClass =  `${_config.wrap.selStyle} ${_config.wrap.cardType || ''} ${_config.wrap.scale}`
    this.setState({
      supComs,
      selected,
      precards,
      nextcards,
      sync: _sync,
      data: _data,
      BID: BID || '',
      BData: BData || '',
      config: _config,
      card: _card,
      search: Utils.initMainSearch(_config.search),
      arr_field: _config.columns.map(col => col.field).join(','),
    }, () => {
      if (_config.setting.sync !== 'true' && _config.setting.onload === 'true') {
        this.loadData()
        setTimeout(() => {
          this.loadData()
        }, _config.setting.delay || 0)
      }
    })
  }
@@ -157,21 +195,37 @@
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { sync, config, BID } = this.state
    const { sync, config, BID, BData, selected } = 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] || []
      }
        _data = _data.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$$BID = BID || ''
          item.$$BData = BData || ''
          item.$Index = index + 1 + ''
      _data = _data.map((item, index) => {
        item.key = index
        item.$$uuid = item[config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        item.$Index = index + 1 + ''
        return item
      })
          if (config.wrap.controlField) {
            if (config.wrap.controlVal.includes(item[config.wrap.controlField])) {
              item.$disabled = true
            }
          }
          return item
        })
        if (selected !== 'false') {
          setTimeout(() => {
            this.checkTopLine()
          }, 200)
          if (selected === 'init') {
            this.setState({selected: 'false'})
          }
        }
      }
      this.setState({sync: false, data: _data})
    } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
@@ -198,30 +252,101 @@
   * @param {*} position   // 刷新位置
   * @param {*} btn        // 执行的按钮
   */
  refreshByButtonResult = (menuId, position, btn) => {
    const { config, BID } = this.state
  refreshByButtonResult = (menuId, position, btn, id = '', lines) => {
    const { config, BID, supComs, supNodes } = this.state
    if (config.uuid !== menuId) return
    if (!btn || btn.resetPageIndex !== 'false') {
      this.setState({
        pageIndex: 1
      }, () => {
        this.loadData()
      })
    if (supComs) {
      if (position === 'mainline' || position === 'popclose') { // 主表刷新,去除同步刷新组件
        let supNode = supNodes[supNodes.length - 1]
        supComs.forEach((item, i) => {
          setTimeout(() => {
            if (supNode && supNode.key === item) {
              MKEmitter.emit('reloadData', item, supNode.value)
            } else {
              MKEmitter.emit('reloadData', item)
            }
          }, i * 10)
        })
      } else {
        if (position === 'line') {
          if (lines && lines.length === 1) {
            this.loadLinedata(lines[0].$$uuid)
          } else {
            this.loadData(id)
          }
        } else if (!btn || btn.resetPageIndex !== 'false') {
          this.setState({
            pageIndex: 1
          }, () => {
            this.loadData(id)
          })
        } else {
          this.loadData(id)
        }
      }
    } else {
      this.loadData()
      let supModule = config.setting.supModule
      if (position === 'line') {
        if (lines && lines.length === 1) {
          this.loadLinedata(lines[0].$$uuid)
        } else {
          this.loadData(id)
        }
      } else if ((position === 'mainline' || position === 'popclose') && supModule && BID) { // 刷新源组件时,附带刷新上级行与当前组件
        MKEmitter.emit('reloadData', supModule, BID)
      } else if (!btn || btn.resetPageIndex !== 'false') {
        this.setState({
          pageIndex: 1
        }, () => {
          this.loadData(id)
        })
      } else {
        this.loadData(id)
      }
    }
    if (btn.syncComponentId && btn.syncComponentId !== config.uuid && btn.syncComponentId !== config.setting.supModule) {
      MKEmitter.emit('reloadData', btn.syncComponentId)                        // 同级标签刷新
    }
    if (position === 'mainline' && config.setting.supModule) {                 // 主表行刷新
      MKEmitter.emit('reloadData', config.setting.supModule, (BID || 'empty'))
    } else if (position === 'popclose') {                                      // 标签关闭刷新
      config.setting.supModule && MKEmitter.emit('reloadData', config.setting.supModule, (BID || 'empty'))
    if (position === 'popclose') { // 执行启动弹窗的按钮所选择的刷新项
      btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId)
    }
  }
  checkTopLine = (id) => {
    const { config, data } = this.state
    let index = 0
    if (id && data) {
      index = data.findIndex(item => item.$$uuid === id)
      if (index === -1) {
        index = 0
      }
    }
    if (!data || data.length === 0 || data[index].$disabled) {
      this.setState({
        activeKey: '',
        selectKeys: [],
        selectedData: []
      })
      MKEmitter.emit('resetSelectLine', config.uuid, '', '')
      if (config.setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', config.uuid, [], false)
      }
      return
    }
    this.setState({
      activeKey: index,
      selectKeys: [index],
      selectedData: [data[index]]
    })
    MKEmitter.emit('resetSelectLine', config.uuid, data[index].$$uuid, data[index])
    if (config.setting.$hasSyncModule) {
      MKEmitter.emit('syncBalconyData', config.uuid, [data[index]], data.length === 1)
    }
  }
@@ -258,7 +383,6 @@
    const { config } = this.state
    if (config.uuid !== menuId) return
    if (id === 'empty') return
    if (!id) {
      this.loadData()
@@ -268,13 +392,36 @@
  }
  resetParentParam = (MenuID, id, data) => {
    const { config } = this.state
    const { config, supComs } = this.state
    if (!config.setting.supModule || config.setting.supModule !== MenuID) return
    if (id !== this.state.BID) {
      this.setState({ BID: id, BData: data, pageIndex: 1 }, () => {
        this.loadData()
      })
    if (supComs) {
      if (!supComs.includes(MenuID)) return
      let supNodes = this.state.supNodes.filter(item => item.key !== MenuID)
      let bid = ''
      let _data = null
      if (id) {
        supNodes.push({key: MenuID, value: id, data})
      }
      if (supNodes.length > 0) {
        bid = supNodes[supNodes.length - 1].value
        _data = supNodes[supNodes.length - 1].data
      }
      if (bid !== this.state.BID || bid !== '') {
        this.setState({ BID: bid, BData: _data, pageIndex: 1, supNodes }, () => {
          this.loadData()
        })
      } else {
        this.setState({ supNodes })
      }
    } else {
      if (!config.setting.supModule || config.setting.supModule !== MenuID) return
      if (id !== this.state.BID || id !== '') {
        this.setState({ BID: id, BData: data, pageIndex: 1 }, () => {
          this.loadData()
        })
      }
    }
  }
@@ -305,11 +452,11 @@
    })
  }
  async loadData () {
  async loadData (id) {
    const { mainSearch, menuType } = this.props
    const { config, arr_field, pageIndex, search, BID } = this.state
    const { config, arr_field, pageIndex, search, BID, BData, selected } = this.state
    if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
    if (config.setting.supModule && !BID && config.wrap.supKey !== 'false') { // BID 不存在时,不做查询
      this.setState({
        activeKey: '',
        selectKeys: [],
@@ -319,9 +466,19 @@
        total: 0,
        loading: false
      })
      MKEmitter.emit('resetSelectLine', config.uuid, '', '')
      if (config.setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', config.uuid, [], false)
      if (selected !== 'false' || (id && config.wrap.selected !== 'false')) {
        setTimeout(() => {
          this.checkTopLine(id)
        }, 200)
        if (selected === 'init') {
          this.setState({selected: 'false'})
        }
      } else {
        MKEmitter.emit('resetSelectLine', config.uuid, '', '')
        if (config.setting.$hasSyncModule) {
          MKEmitter.emit('syncBalconyData', config.uuid, [], false)
        }
      }
      return
    }
@@ -355,6 +512,20 @@
        start = config.setting.pageSize * (pageIndex - 1) + 1
      }
      if (selected !== 'false' || (id && config.wrap.selected !== 'false')) {
        setTimeout(() => {
          this.checkTopLine(id)
        }, 200)
        if (selected === 'init') {
          this.setState({selected: 'false'})
        }
      } else {
        MKEmitter.emit('resetSelectLine', config.uuid, '', '')
        if (config.setting.$hasSyncModule) {
          MKEmitter.emit('syncBalconyData', config.uuid, [], false)
        }
      }
      this.setState({
        activeKey: '',
        selectKeys: [],
@@ -363,16 +534,20 @@
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$$BID = BID || ''
          item.$$BData = BData || ''
          item.$Index = index + start + ''
          if (config.wrap.controlField) {
            if (config.wrap.controlVal.includes(item[config.wrap.controlField])) {
              item.$disabled = true
            }
          }
          return item
        }),
        total: result.total,
        loading: false
      })
      MKEmitter.emit('resetSelectLine', config.uuid, '', '')
      if (config.setting.$hasSyncModule) {
        MKEmitter.emit('syncBalconyData', config.uuid, [], false)
      }
    } else {
      this.setState({
        loading: false
@@ -390,7 +565,7 @@
   */ 
  async loadLinedata (id) {
    const { mainSearch, menuType } = this.props
    const { config, arr_field, pageIndex, search, BID } = this.state
    const { config, arr_field, pageIndex, search, BID, BData } = this.state
    let searches = fromJS(search).toJS()
    if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 主表搜索条件
@@ -421,15 +596,18 @@
              _data.key = item.key
              _data.$$uuid = _data[config.setting.primaryKey] || ''
              _data.$$BID = BID || ''
              _data.$$BData = BData || ''
              _data.$Index = item.$Index
              return _data
            } else {
              return item
            }
          })
        } catch {
        } catch (e) {
          console.warn('数据查询错误')
        }
        MKEmitter.emit('resetSelectLine', config.uuid, _data.$$uuid || '', _data)
      }
      this.setState({
@@ -493,9 +671,8 @@
  changeCard = (index, item) => {
    const { config, selectKeys, selectedData, activeKey, data } = this.state
    this.openView(item)
    if (!config.wrap.cardType) return
    if (item.$disabled) return
    
    let _selectKeys = []
    let _selectedData = []
@@ -506,12 +683,12 @@
        _selectKeys = selectKeys.filter(key => key !== index)
        _selectedData = selectedData.filter(cell => cell.key !== index)
        _activeKey = _selectKeys.slice(-1)[0]
        _item = selectedData.slice(-1)[0] || ''
        _item = _selectedData.slice(-1)[0] || ''
      } else if (selectKeys.indexOf(index) > -1) {
        _selectKeys = selectKeys.filter(key => key !== index)
        _selectedData = selectedData.filter(cell => cell.key !== index)
        _activeKey = activeKey
        _item = selectedData.filter(cell => cell.key === activeKey)[0] || ''
        _item = _selectedData.filter(cell => cell.key === activeKey)[0] || ''
      } else {
        _selectKeys = [...selectKeys, index]
        _selectedData = [...selectedData, item]
@@ -536,85 +713,10 @@
    }
  }
  openView = (item) => {
    const { card } = this.state
    if (card.setting.click === 'menu') {
      let menu = null
      if (card.setting.menu && card.setting.menu.length > 0) {
        let menu_id = card.setting.menu.slice(-1)[0]
        menu = this.props.permMenus.filter(m => m.MenuID === menu_id)[0] || ''
      }
      if (!menu) {
        notification.warning({
          top: 92,
          message: '菜单已删除或没有访问权限!',
          duration: 5
        })
        return
      }
      let newtab = {
        ...menu,
        selected: true,
        param: {}
      }
      if (card.setting.joint === 'true') {
        newtab.param.$BID = item.$$uuid
      }
      if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) {
        this.props.modifyTabview([newtab])
      } else {
        let tabs = this.props.tabviews.filter((tab, i) => {
          tab.selected = false
          return tab.MenuID !== newtab.MenuID
        })
        if (this.props.tabviews.length > tabs.length) {
          this.props.modifyTabview(fromJS(tabs).toJS())
        }
        this.setState({}, () => {
          tabs.push(newtab)
          this.props.modifyTabview(tabs)
        })
      }
    } else if (card.setting.click === 'link') {
      let src = card.setting.linkurl
      if (src.indexOf('paramsmain/') > -1) {
        try {
          let _url = src.split('paramsmain/')[0] + 'paramsmain/'
          let _param = JSON.parse(window.decodeURIComponent(window.atob(src.split('paramsmain/')[1])))
          _param.UserID = sessionStorage.getItem('UserID')
          _param.LoginUID = sessionStorage.getItem('LoginUID')
          _param.User_Name = sessionStorage.getItem('User_Name')
          _param.param = { BID: item.$$uuid }
          src = _url + window.btoa(window.encodeURIComponent(JSON.stringify(_param)))
        } catch {
          console.warn('菜单参数解析错误!')
        }
      } else if (card.setting.joint === 'true') {
        let con = '?'
        if (/\?/ig.test(src)) {
          con = '&'
        }
        src = src + `${con}id=${item.$$uuid}&appkey=${window.GLOB.appkey}&userid=${sessionStorage.getItem('UserID')}&LoginUID=${sessionStorage.getItem('LoginUID') || ''}`
      }
      window.open(src)
    }
  }
  render() {
    const { config, precards, nextcards, loading, data, pageIndex, total, card, activeKey, BID, BData, selectedData, selectKeys } = this.state
    if (config.wrap.empty === 'hidden' && (!data || data.length === 0)) return null
    let _total = 0
    let switchable = false
@@ -622,20 +724,15 @@
      _total = config.setting.pageSize * pageIndex
      switchable = true
    }
    let offset = 0
    if (config.wrap.cardFloat) {
      let length = data ? data.length : 0
      if (card.setting.width * length + config.$offset < 24) {
        offset = 24 - card.setting.width * length - config.$offset
        if (config.wrap.cardFloat === 'center') {
          offset = Math.floor(offset / 2)
        }
      }
    let extendData = {$$BID: BID, $$BData: BData, $$selectedData: selectedData, $$type: 'extendCard'}
    if (data && data[0]) {
      extendData = {...extendData, ...data[0]}
    }
    return (
      <div className="custom-data-card-box" style={config.style}>
      <div className="custom-data-card-box" id={'anchor' + config.uuid} style={config.style}>
        {loading ?
          <div className="loading-mask">
            {data ? <div className="ant-spin-blur"></div> : null}
@@ -653,23 +750,33 @@
            selectedData={selectedData}
          /> : null
        }
        <div className={`data-zoom ${config.wrap.cardType || ''} ${config.wrap.scale || ''}`}>
        <div className={`data-zoom ${config.wrap.wrapClass}`}>
          {switchable ? <div className={'prev-page ' + (pageIndex === 1 ? 'disabled' : '')} onClick={this.prevPage}><div><div><img src={preImg} alt=""/></div></div></div> : null}
          <Row className="card-row-list">
            {offset ? <Col span={offset} style={{height: '10px'}}> </Col> : null}
          <Row className={'card-row-list ' + config.wrap.layout}>
            {precards.map((item, index) => (
              <Col key={'pre' + index} className={item.setting.$click} span={item.setting.width || 6}>
                <CardItem card={item} cards={config} data={{$$BID: BID, $$type: 'extendCard'}}/>
              <Col key={'pre' + index} className="extend-card" span={item.setting.width || 6}>
                <CardItem card={item} cards={config} data={extendData}/>
              </Col>
            ))}
            {data && data.map((item, index) => (
              <Col className={(activeKey === index ? ' active' : (selectKeys.indexOf(index) > -1 ? ' selected' : '')) + (card.setting.$click ? ' pointer' : '') + card.setting.$click} key={index} span={card.setting.width} onClick={() => {this.changeCard(index, item)}}>
                <CardItem card={card} cards={config} data={item}/>
              </Col>
            ))}
            {data && data.map((item, index) => {
              let className = card.setting.click ? 'mk-card pointer ' : 'mk-card '
              if (item.$disabled) {
                className = 'mk-disabled'
              } else if (activeKey === index) {
                className += 'active'
              } else if (selectKeys.indexOf(index) > -1) {
                className += 'selected'
              }
              return (
                <Col className={className} key={index} span={card.setting.width} onClick={() => {this.changeCard(index, item)}}>
                  <CardItem card={card} cards={config} data={item}/>
                </Col>
              )
            })}
            {nextcards.map((item, index) => (
              <Col key={'next' + index} className={item.setting.$click} span={item.setting.width || 6}>
                <CardItem card={item} cards={config} data={{$$BID: BID, $$type: 'extendCard'}}/>
              <Col key={'next' + index} className="extend-card" span={item.setting.width || 6}>
                <CardItem card={item} cards={config} data={extendData}/>
              </Col>
            ))}
          </Row>
@@ -682,17 +789,6 @@
  }
}
const mapStateToProps = (state) => {
  return {
    permMenus: state.permMenus,
    tabviews: state.tabviews,
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    modifyTabview: (tabviews) => dispatch(modifyTabview(tabviews))
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(DataCard)
export default DataCard