import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Button, notification, Modal, Icon } 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'
|
|
const { confirm } = Modal
|
|
class NewPageButton extends Component {
|
static propTpyes = {
|
show: PropTypes.any, // 按钮显示样式控制
|
position: PropTypes.any, // 按钮位置,工具栏为toolbar
|
MenuID: PropTypes.any, // 菜单ID
|
BID: PropTypes.string, // 主表ID
|
btn: PropTypes.object, // 按钮
|
selectedData: PropTypes.any, // 子表中选择数据
|
setting: PropTypes.any, // 页面通用设置
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
disabled: false,
|
loading: false
|
}
|
|
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})
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentDidMount () {
|
const { position } = this.props
|
|
if (position === 'toolbar') {
|
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})
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (triggerId, record) => {
|
const { setting, selectedData, btn, MenuID } = this.props
|
const { loading } = this.state
|
|
if ((triggerId && btn.uuid !== triggerId) || loading) return
|
|
if (btn.funcType === 'closetab') {
|
MKEmitter.emit('closeTabView', MenuID || btn.$menuId)
|
|
if (btn.refreshTab && btn.refreshTab.length > 0) {
|
MKEmitter.emit('reloadMenuView', btn.refreshTab[btn.refreshTab.length - 1], 'table')
|
}
|
return
|
}
|
|
let data = record || selectedData || []
|
|
if (data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.state.dict['main.action.confirm.selectSingleLine'],
|
duration: 5
|
})
|
return
|
} else if (!setting.primaryKey) {
|
// 需要选择行时,校验是否设置主键
|
notification.warning({
|
top: 92,
|
message: '未设置主键!',
|
duration: 5
|
})
|
return
|
}
|
|
let primaryId = data[0][setting.primaryKey] || ''
|
|
this.setState({loading: true})
|
this.changeUser(primaryId)
|
}
|
|
/**
|
* @description 切换用户
|
*/
|
changeUser = (primaryId) => {
|
const { setting, btn } = this.props
|
let _this = this
|
|
let param = {
|
func: 'webapi_ChangeUser'
|
}
|
|
if (this.props.BID) {
|
param.BID = this.props.BID
|
}
|
|
if (window.GLOB.mainSystemApi) {
|
param.rduri = window.GLOB.mainSystemApi
|
}
|
|
param[setting.primaryKey] = primaryId
|
|
confirm({
|
title: this.state.dict['main.action.confirm.tip'],
|
onOk() {
|
return new Promise(resolve => {
|
Api.genericInterface(param).then(res => {
|
resolve()
|
if (res.status) {
|
sessionStorage.setItem('UserID', res.UserID)
|
sessionStorage.setItem('LoginUID', res.LoginUID)
|
sessionStorage.setItem('User_Name', res.UserName)
|
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.removeItem('CloudAvatar')
|
sessionStorage.removeItem('cloudDataM')
|
sessionStorage.removeItem('CloudUserID')
|
sessionStorage.removeItem('CloudUserName')
|
sessionStorage.removeItem('CloudLoginUID')
|
sessionStorage.removeItem('CloudFullName')
|
|
window.location.reload()
|
} else {
|
notification.error({
|
top: 92,
|
message: res.message || res.ErrMesg,
|
duration: btn.verify && btn.verify.ntime ? btn.verify.ntime : 10
|
})
|
_this.setState({loading: false})
|
}
|
}, () => {
|
resolve()
|
_this.setState({loading: false})
|
})
|
})
|
},
|
onCancel() {
|
_this.setState({loading: false})
|
}
|
})
|
}
|
|
render() {
|
const { btn, show } = this.props
|
const { loading, disabled } = 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 (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
|
}
|
|
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>
|
)
|
}
|
}
|
}
|
|
export default NewPageButton
|