king
2024-06-21 2bccb9ec7bdefe23292a22bc153463cfa1479a49
src/tabviews/zshare/actionList/changeuserbutton/index.jsx
@@ -1,45 +1,39 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button, notification, Modal, Icon } from 'antd'
import { Button, notification, Modal } from 'antd'
import Api from '@/api'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
import MKEmitter from '@/utils/events.js'
import './index.scss'
import MkIcon from '@/components/mk-icon'
// import './index.scss'
const { confirm } = Modal
class NewPageButton extends Component {
  static propTpyes = {
    show: PropTypes.any,              // 按钮显示样式控制
    position: PropTypes.any,          // 按钮位置,工具栏为toolbar
    MenuID: PropTypes.any,            // 菜单ID
    BData: PropTypes.any,             // 主表数据
    BID: PropTypes.string,            // 主表ID
    btn: PropTypes.object,            // 按钮
    selectedData: PropTypes.any,      // 子表中选择数据
    setting: PropTypes.any,           // 页面通用设置
    disabled: PropTypes.any,          // 行按钮禁用
  }
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    disabled: false,
    hidden: false,
    loading: false
  }
  UNSAFE_componentWillMount () {
    const { btn, selectedData } = this.props
    let disabled = false
    const { btn, selectedData, BData, disabled } = this.props
    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})
    if (btn.controlField) {
      this.setStatus(btn, selectedData || [], BData, disabled)
    } else if (disabled) {
      this.setState({disabled: true})
    }
  }
@@ -52,20 +46,12 @@
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { btn, selectedData } = this.props
    const { btn } = 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})
    if (btn.controlField) {
      this.setStatus(btn, nextProps.selectedData || [], nextProps.BData, nextProps.disabled)
    } else {
      this.setState({disabled: nextProps.disabled === true})
    }
  }
@@ -75,15 +61,54 @@
    }
    MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
  }
  setStatus = (btn, data, BData, disprop) => {
    let disabled = false
    let hidden = false
    if (btn.control !== 'parent') {
      if (data.length > 0) {
        data.forEach(item => {
          let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
          if (btn.controlVals.includes(s)) {
            disabled = true
          }
        })
      } else if (btn.controlVals.includes('')) {
        disabled = true
      }
    } else {
      if (!BData || !BData.hasOwnProperty(btn.controlField)) {
        hidden = true
      } else {
        let s = BData[btn.controlField] + ''
        if (btn.controlVals.includes(s)) {
          hidden = true
        }
      }
    }
    if (disabled && btn.control === 'hidden') {
      hidden = true
    }
    if (disprop) {
      disabled = true
    }
    this.setState({hidden, disabled})
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (triggerId, record, type) => {
    const { setting, selectedData, btn, MenuID } = this.props
  actionTrigger = (triggerId, record, type, lid) => {
    const { setting, selectedData, btn, MenuID, LID } = this.props
    const { loading, disabled } = this.state
    
    if ((triggerId && btn.uuid !== triggerId) || loading || disabled) return
    if (loading || disabled) return
    if (triggerId && btn.uuid !== triggerId) return
    if (type === 'linkbtn' && !btn.$toolbtn && LID !== lid) return
    if (btn.funcType === 'closetab') {
      MKEmitter.emit('closeTabView', MenuID || btn.$MenuID)
@@ -92,10 +117,6 @@
        MKEmitter.emit('reloadMenuView', btn.refreshTab[btn.refreshTab.length - 1], 'table')
      }
      return
    } else if (type === 'linkbtn' && selectedData && selectedData.length === 1) {
      if (record[0].$Index !== selectedData[0].$Index) {
        return
      }
    }
    
    let data = record || selectedData || []
@@ -104,15 +125,7 @@
      // 需要选择单行时,校验数据
      notification.warning({
        top: 92,
        message: this.state.dict['main.action.confirm.selectSingleLine'],
        duration: 5
      })
      return
    } else if (!setting.primaryKey) {
      // 需要选择行时,校验是否设置主键
      notification.warning({
        top: 92,
        message: '未设置主键!',
        message: window.GLOB.dict['select_single_row'] || '请选择单行数据!',
        duration: 5
      })
      return
@@ -132,7 +145,10 @@
    let _this = this
    let param = {
      func: 'webapi_ChangeUser'
      func: 'webapi_ChangeUser',
      login_city: sessionStorage.getItem('city') || '',
      login_id_address: sessionStorage.getItem('ipAddress') || '',
      domain_name: window.btoa(window.encodeURIComponent(window.GLOB.host)),
    }
    if (this.props.BID) {
@@ -146,7 +162,9 @@
    param[setting.primaryKey] = primaryId
    confirm({
      title: this.state.dict['main.action.confirm.tip'],
      title: window.GLOB.dict['exec_sure'] || '确定要执行吗?',
      okText: window.GLOB.dict['ok'] || '确定',
      cancelText: window.GLOB.dict['cancel'] || '取消',
      onOk() {
        return new Promise(resolve => {
          Api.genericInterface(param).then(res => {
@@ -158,12 +176,18 @@
              sessionStorage.setItem('Full_Name', res.FullName)
              sessionStorage.setItem('avatar', res.icon || '')
              sessionStorage.setItem('dataM', res.dataM ? 'true' : '')
              sessionStorage.setItem('localDataM', res.dataM ? 'true' : '')
              sessionStorage.setItem('debug', res.debug || '')
              sessionStorage.setItem('role_id', res.role_id || '')
              sessionStorage.setItem('departmentcode', res.departmentcode || '')
              sessionStorage.setItem('organization', res.organization || '')
              sessionStorage.setItem('localRole_id', res.role_id || '')
              sessionStorage.setItem('mk_user_type', res.mk_user_type || '')
              if (res.paas_externalDatabase) {
                sessionStorage.setItem('externalDatabase', res.paas_externalDatabase)
                window.GLOB.externalDatabase = `[${res.paas_externalDatabase}]..`
              } else {
                sessionStorage.removeItem('externalDatabase')
              }
              
              sessionStorage.removeItem('CloudAvatar')
              sessionStorage.removeItem('cloudDataM')
@@ -176,7 +200,7 @@
            } else {
              notification.error({
                top: 92,
                message: res.message || res.ErrMesg,
                message: res.message || '执行失败!',
                duration: btn.verify && btn.verify.ntime ? btn.verify.ntime : 10
              })
              _this.setState({loading: false})
@@ -194,48 +218,32 @@
  }
  render() {
    const { btn, show } = this.props
    const { loading, disabled } = this.state
    const { btn } = this.props
    const { loading, disabled, hidden } = this.state
    if (show === 'actionList') {
      return (
        <Button
          icon={btn.icon}
          loading={loading}
          disabled={disabled}
          className={'mk-btn mk-' + btn.class}
          onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
        >{btn.label}</Button>
      )
    } else { // icon、text、 all 卡片
      let label = ''
      let icon = ''
    if (hidden) return null
      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 || ''
      // } else if (show === 'text') {
      } else {
        label = btn.label
      }
    let label = ''
      return (
        <Button
          type="link"
          title={show === 'icon' ? btn.label : ''}
          loading={loading}
          disabled={disabled}
          style={btn.style}
          icon={icon}
          onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
        >{label}</Button>
      )
    if (btn.show === 'link') {
      label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon} /> : ''}</span>
    } else if (btn.show === 'icon') {
      label = !loading ? <MkIcon type={btn.icon} /> : null
    } else {
      label = <span>{!loading && btn.icon ? <MkIcon style={{marginRight: '8px'}} type={btn.icon} /> : ''}{btn.label}</span>
    }
    return (
      <Button
        type="link"
        title={disabled ? (btn.reason || '') : (btn.show === 'icon' ? btn.label : '')}
        loading={loading}
        disabled={disabled}
        style={btn.style}
        className={btn.hover || ''}
        onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
      >{label}</Button>
    )
  }
}