king
2021-11-17 c51f5e007a3e03c9d6731ab7f28f0080de009990
src/tabviews/zshare/actionList/excelInbutton/index.jsx
@@ -3,40 +3,49 @@
import moment from 'moment'
import {connect} from 'react-redux'
import { is, fromJS } from 'immutable'
import { Button, Modal, notification, message } from 'antd'
import { Button, Modal, notification, message, Icon } from 'antd'
import ExcelIn from './excelin'
import Utils from '@/utils/utils.js'
import Utils, { getExcelInSql } from '@/utils/utils.js'
import options from '@/store/options.js'
import Api from '@/api'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
import './index.scss'
import MKEmitter from '@/utils/events.js'
// import './index.scss'
class ExcelInButton extends Component {
  static propTpyes = {
    show: PropTypes.any,              // 显示样式
    position: PropTypes.any,          // 按钮位置,工具栏为toolbar
    BID: PropTypes.string,            // 主表ID
    selectedData: PropTypes.any,      // 子表中选择数据
    Tab: PropTypes.any,               // 如果当前元素为标签时,tab为标签信息
    btn: PropTypes.object,            // 按钮
    setting: PropTypes.any,           // 页面通用设置
    updateStatus: PropTypes.func,     // 按钮状态更新
    triggerBtn: PropTypes.any,
  }
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    loading: false,
    selines: null,
    disabled: false,
    hidden: false,
    primaryId: '',
  }
  /**
   * @description 外部触发按钮点击
   */
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.triggerBtn && !is(fromJS(this.props.triggerBtn), fromJS(nextProps.triggerBtn)) && nextProps.triggerBtn.button.uuid === this.props.btn.uuid) {
      this.actionTrigger(nextProps.triggerBtn.data)
  UNSAFE_componentWillMount () {
    const { btn, selectedData } = this.props
    let disabled = false
    if (btn.controlField && selectedData && selectedData.length > 0) { // 表格中按钮隐藏控制
      selectedData.forEach(item => {
        let s = item[btn.controlField] + ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          disabled = true
        }
      })
      this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
    }
  }
@@ -44,45 +53,58 @@
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
  /**
   * @description 按钮状态改变
   */
  updateStatus = (type) => {
    if (type === 'start') {
      this.setState({
        loading: true
      })
      this.props.updateStatus('start')
    } else if (type === 'over') {
      this.setState({
        loading: false
      })
      this.props.updateStatus('over')
  componentDidMount () {
    MKEmitter.addListener('triggerBtnId', this.actionTrigger)
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { btn, selectedData } = this.props
    if (btn.controlField && !is(fromJS(nextProps.selectedData || []), fromJS(selectedData || []))) {
      let disabled = false
      if (nextProps.selectedData && nextProps.selectedData.length > 0) { // 表格中按钮隐藏控制
        nextProps.selectedData.forEach(item => {
          let s = item[btn.controlField] + ''
          if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
            disabled = true
          }
        })
      }
      this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
    }
  }
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (record) => {
  actionTrigger = (triggerId, record, type) => {
    const { setting, Tab, BID, btn, selectedData } = this.props
    const { loading, disabled } = this.state
    if (Tab && Tab.supMenu && !BID) {
    if ((triggerId && btn.uuid !== triggerId) || loading || disabled) return
    if (((Tab && Tab.supMenu) || setting.supModule) && !BID) {
      notification.warning({
        top: 92,
        message: '需要上级主键值!',
        duration: 5
      })
      return
    } else if (type === 'linkbtn' && selectedData && selectedData.length === 1) {
      if (record[0].$Index !== selectedData[0].$Index) {
        return
      }
    }
    let data = []
    if (record) { // 表格中触发按钮
      data = [record]
    } else {
      data = selectedData || []
    }
    let data = record || selectedData || []
    
    if (btn.Ot === 'requiredSgl' && data.length !== 1) {
      // 需要选择单行时,校验数据
@@ -92,25 +114,29 @@
        duration: 5
      })
      return
    }
    if (btn.verify && btn.verify.sheet && btn.verify.columns && btn.verify.columns.length > 0) {
      let primaryId = '' // 导入时行Id
      if (btn.Ot === 'requiredSgl') {
        primaryId = data[0][setting.primaryKey] || ''
      }
      this.setState({
        primaryId: primaryId
      }, () => {
        this.refs.excelIn.exceltrigger()
      })
    } else {
    } else if (!btn.verify || !btn.verify.sheet || !btn.verify.columns || btn.verify.columns.length === 0) {
      notification.warning({
        top: 92,
        message: 'excel导入验证信息未设置!',
        duration: 5
      })
      return
    }
    let primaryId = '' // 导入时行Id
    if (btn.Ot === 'requiredSgl' && setting.primaryKey) {
      primaryId = data[0][setting.primaryKey] || ''
    }
    this.setState({
      selines: data,
      primaryId: primaryId
    }, () => {
      this.refs.excelIn.exceltrigger()
    })
    if (window.GLOB.systemType === 'production') {
      MKEmitter.emit('queryTrigger', {menuId: btn.uuid, name: '导入Excel'})
    }
  }
@@ -143,7 +169,14 @@
      loading: false
    })
    this.props.updateStatus('refresh', btn.execSuccess)
    if (btn.execSuccess !== 'never') {
      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execSuccess, btn, '', this.state.selines)
    }
    if (btn.switchTab && btn.switchTab.length > 0) {
      let id = btn.switchTab[btn.switchTab.length - 1]
      let node = document.getElementById('tab' + id)
      node && node.click()
    }
  }
  /**
@@ -180,47 +213,49 @@
      loading: false
    })
    this.props.updateStatus('refresh', btn.execError)
    if (btn.execError !== 'never') {
      MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execError, btn, '', this.state.selines)
    }
  }
  /**
   * @description Excel 导入
   */
  getexceldata = (data, errors) => {
  getexceldata = (data, errors, sheetName, errDetail) => {
    const { btn } = this.props
    if (errors) {
      if (errors === 'notexit') {
        notification.warning({
          top: 92,
          message: '工作表《' + btn.verify.sheet + '》不存在!',
          message: '工作表《' + sheetName + '》不存在!',
          duration: 5
        })
      } else if (errors === 'empty') {
        notification.warning({
          top: 92,
          message: '工作表《' + btn.verify.sheet + '》为空!',
          message: '工作表《' + sheetName + '》为空!',
          duration: 5
        })
      } else if (errors === 'headerError') {
        notification.warning({
          top: 92,
          message: '工作表《' + btn.verify.sheet + '》表头设置错误!',
          message: `工作表《${sheetName}》表头错误,${errDetail}`,
          duration: 5
        })
      }
      this.updateStatus('over')
      this.setState({ loading: false })
      return
    }
    if (!data || data.length === 0) {
      notification.warning({
        top: 92,
        message: '未获取到工作表《' + btn.verify.sheet + '》数据!',
        message: '未获取到工作表《' + sheetName + '》数据!',
        duration: 5
      })
      this.updateStatus('over')
      this.setState({ loading: false })
      return
    } else if (data.length * btn.verify.columns.length > 30000) {
      notification.warning({
@@ -230,7 +265,7 @@
      })
    }
    let result = Utils.getExcelInSql(btn, data, this.state.dict)
    let result = getExcelInSql(btn, data, this.state.dict, (this.props.BID || ''))
    if (result.errors) {
      notification.warning({
@@ -238,7 +273,7 @@
        message: result.errors,
        duration: 5
      })
      this.updateStatus('over')
      this.setState({ loading: false })
      return
    }
@@ -251,10 +286,10 @@
      param.BID = this.props.BID
    }
    if (btn.intertype === 'inner' && !btn.innerFunc) { // 系统存储过程
    if (btn.intertype === 'system') { // 系统存储过程
      param.func = 'sPC_TableData_InUpDe'
      
      if (this.props.dataManager) { // 数据权限
      if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
        result.sql = result.sql.replace(/\$@/ig, '/*')
        result.sql = result.sql.replace(/@\$/ig, '*/')
        result.bottom = result.bottom.replace(/\$@/ig, '/*')
@@ -268,10 +303,16 @@
      param.LText1 = Utils.formatOptions(result.insert)
      param.LText2 = Utils.formatOptions(result.bottom)
      param.LText = Utils.formatOptions(result.sql)
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
      if (this.props.menuType === 'HS' && param.timestamp) { // 云端验证
        param.open_key = Utils.encrypt(param.secretkey, param.timestamp, true)
        param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
      }
      param.menuname = btn.logLabel
      if (window.GLOB.probation) {
        param.s_debug_type = 'Y'
      }
      Api.genericInterface(param).then((res) => {
@@ -335,13 +376,21 @@
          if (btn.sysInterface === 'true' && options.cloudServiceApi) {
            param.rduri = options.cloudServiceApi
          } else if (btn.sysInterface !== 'true') {
            param.rduri = btn.interface
            if (window.GLOB.systemType === 'production' && btn.proInterface) {
              param.rduri = btn.proInterface
            } else {
              param.rduri = btn.interface
            }
          }
        } else {
          if (btn.sysInterface === 'true' && window.GLOB.mainSystemApi) {
            param.rduri = window.GLOB.mainSystemApi
          } else if (btn.sysInterface !== 'true') {
            param.rduri = btn.interface
            if (window.GLOB.systemType === 'production' && btn.proInterface) {
              param.rduri = btn.proInterface
            } else {
              param.rduri = btn.interface
            }
          }
        }
@@ -351,9 +400,9 @@
        
        if (this.props.menuType === 'HS' && res.func === 's_sDataDictb_excelIn') { // s_sDataDictb_excelIn 云端验证
          param.LText = Utils.formatOptions(result.sql)
          param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
          param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
          param.secretkey = Utils.encrypt(param.LText, param.timestamp)
          param.open_key = Utils.encrypt(param.secretkey, param.timestamp, true)
          param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
        }
        return Api.genericInterface(res)
@@ -390,33 +439,57 @@
  render() {
    const { btn, show } = this.props
    const { loading } = this.state
    const { loading, disabled, hidden } = this.state
    return (
      <div className="mk-btn-wrap">
        {!show ? <Button
          className={'mk-btn mk-' + btn.class}
    if (hidden) return null
    if (show === 'actionList') {
      return <div style={{display: 'inline-block'}} onClick={(e) => e.stopPropagation()}>
        <Button
          icon={btn.icon}
          onClick={() => {this.actionTrigger()}}
          loading={loading}
        >{btn.label}</Button> : null}
        {show === 'icon' ? <Button
          className="import-icon"
          icon="upload"
          disabled={disabled}
          className={'mk-btn mk-' + btn.class}
          onClick={() => {this.actionTrigger()}}
          loading={loading}
          title={btn.label}
        ></Button> : null}
        <ExcelIn btn={btn} triggerExcelIn={() => this.updateStatus('start')} returndata={this.getexceldata} ref="excelIn" />
        >{btn.label}</Button>
        <ExcelIn btn={btn} triggerExcelIn={() => this.setState({ loading: true })} returndata={this.getexceldata} ref="excelIn" />
      </div>
    )
    } else { // icon、text、 all 卡片
      let label = ''
      let icon = ''
      if (show === 'button') {
        label = btn.label
        icon = btn.icon || ''
      } else if (show === 'link') {
        label = <span>{btn.label}{btn.icon ? <Icon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
        icon = ''
      } else if (show === 'icon') {
        icon = btn.icon || 'upload'
      // } else if (show === 'text') {
      } else {
        label = btn.label
      }
      return <div style={{display: 'inline-block'}} onClick={(e) => e.stopPropagation()}>
        <Button
          type="link"
          title={show === 'icon' ? btn.label : ''}
          loading={loading}
          disabled={disabled}
          style={btn.style}
          icon={icon}
          onClick={() => {this.actionTrigger()}}
        >{label}</Button>
        <ExcelIn btn={btn} triggerExcelIn={() => this.setState({ loading: true })} returndata={this.getexceldata} ref="excelIn" />
      </div>
    }
  }
}
const mapStateToProps = (state) => {
  return {
    menuType: state.editLevel,
    dataManager: state.dataManager
    menuType: state.editLevel
  }
}