import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import {connect} from 'react-redux'
|
import { is, fromJS } from 'immutable'
|
import { Button, Modal, notification, message } from 'antd'
|
|
import ExcelIn from './excelin'
|
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 MKEmitter from '@/utils/events.js'
|
import MkIcon from '@/components/mk-icon'
|
// import './index.scss'
|
|
class ExcelInButton extends Component {
|
static propTpyes = {
|
show: PropTypes.any, // 显示样式
|
BID: PropTypes.string, // 主表ID
|
BData: PropTypes.any, // 主表数据
|
selectedData: PropTypes.any, // 子表中选择数据
|
Tab: PropTypes.any, // 如果当前元素为标签时,tab为标签信息
|
btn: PropTypes.object, // 按钮
|
setting: PropTypes.any, // 页面通用设置
|
disabled: PropTypes.any, // 行按钮禁用
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
loading: false,
|
selines: null,
|
disabled: false,
|
hidden: false,
|
primaryId: '',
|
}
|
|
UNSAFE_componentWillMount () {
|
const { btn, selectedData, BData } = 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({hidden: disabled && btn.control === 'hidden'})
|
} else if (btn.control === 'parent') {
|
if (!BData || !BData.hasOwnProperty(btn.controlField)) {
|
this.setState({hidden: true})
|
} else {
|
let s = BData[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
this.setState({hidden: true})
|
} else {
|
this.setState({hidden: false})
|
}
|
}
|
}
|
|
if (this.props.disabled || disabled) {
|
this.setState({disabled: true})
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('triggerBtnId', this.actionTrigger)
|
}
|
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { btn, selectedData, BData } = this.props
|
|
let disabled = false
|
if (btn.controlField && !is(fromJS(nextProps.selectedData || []), fromJS(selectedData || []))) {
|
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'})
|
} else if (btn.control === 'parent' && !is(fromJS(nextProps.BData || {}), fromJS(BData || {}))) {
|
if (!nextProps.BData || !nextProps.BData.hasOwnProperty(btn.controlField)) {
|
this.setState({hidden: true})
|
} else {
|
let s = nextProps.BData[btn.controlField] + ''
|
if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
|
this.setState({hidden: true})
|
} else {
|
this.setState({hidden: false})
|
}
|
}
|
}
|
|
if (nextProps.disabled || disabled) {
|
this.setState({disabled: true})
|
} else {
|
this.setState({disabled: false})
|
}
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (triggerId, record, type) => {
|
const { setting, Tab, BID, btn, selectedData } = this.props
|
const { loading, disabled } = this.state
|
|
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 = record || selectedData || []
|
|
if (btn.Ot === 'requiredSgl' && data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.state.dict['main.action.confirm.selectSingleLine'],
|
duration: 5
|
})
|
return
|
} 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'})
|
}
|
}
|
|
/**
|
* @description 操作成功后处理
|
* 1、excel导出,成功后取消导出按钮加载中状态
|
* 2、状态码为 S 时,显示成功信息后系统默认信息
|
* 3、状态码为 -1 时,不显示任何信息
|
* 4、模态框执行成功后是否关闭
|
* 5、通知主列表刷新
|
*/
|
execSuccess = (res) => {
|
const { btn } = this.props
|
|
if (res && res.ErrCode === 'S') { // 执行成功
|
notification.success({
|
top: 92,
|
message: res.ErrMesg || this.state.dict['main.action.confirm.success'],
|
duration: btn.verify && btn.verify.stime ? btn.verify.stime : 2
|
})
|
} else if (res && res.ErrCode === 'Y') { // 执行成功
|
Modal.success({
|
title: res.ErrMesg || this.state.dict['main.action.confirm.success']
|
})
|
} else if (res && res.ErrCode === '-1') { // 完成后不提示
|
|
}
|
|
this.setState({
|
loading: false
|
})
|
|
if (btn.execSuccess === 'closepoptab') {
|
MKEmitter.emit('popclose')
|
} else 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()
|
}
|
}
|
|
/**
|
* @description 操作失败后处理
|
* 1、状态码为 E、N、F、NM 时,显示相应提示信息
|
* 2、excel导出,失败后取消导出按钮加载中状态
|
* 3、通知主列表刷新
|
*/
|
execError = (res) => {
|
const { btn } = this.props
|
|
if (res.ErrCode === 'E') {
|
Modal.error({
|
title: res.message || res.ErrMesg,
|
})
|
} else if (res.ErrCode === 'N') {
|
notification.error({
|
top: 92,
|
message: res.message || res.ErrMesg,
|
duration: btn.verify && btn.verify.ntime ? btn.verify.ntime : 10
|
})
|
} else if (res.ErrCode === 'F') {
|
notification.error({
|
className: 'notification-custom-error',
|
top: 92,
|
message: res.message || res.ErrMesg,
|
duration: btn.verify && btn.verify.ftime ? btn.verify.ftime : 10
|
})
|
} else if (res.ErrCode === 'NM') {
|
message.error(res.message || res.ErrMesg)
|
}
|
|
this.setState({
|
loading: false
|
})
|
|
if (btn.execError === 'closepoptab') {
|
MKEmitter.emit('popclose')
|
} else if (btn.execError !== 'never') {
|
MKEmitter.emit('refreshByButtonResult', btn.$menuId, btn.execError, btn, '', this.state.selines)
|
}
|
}
|
|
/**
|
* @description Excel 导入
|
*/
|
getexceldata = (data, errors, sheetName, errDetail) => {
|
const { btn } = this.props
|
|
if (errors) {
|
if (errors === 'notexit') {
|
notification.warning({
|
top: 92,
|
message: '工作表《' + sheetName + '》不存在!',
|
duration: 5
|
})
|
} else if (errors === 'empty') {
|
notification.warning({
|
top: 92,
|
message: '工作表《' + sheetName + '》为空!',
|
duration: 5
|
})
|
} else if (errors === 'headerError') {
|
notification.warning({
|
top: 92,
|
message: `工作表《${sheetName}》表头错误,${errDetail}`,
|
duration: 5
|
})
|
}
|
|
this.setState({ loading: false })
|
return
|
}
|
|
if (!data || data.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '未获取到工作表《' + sheetName + '》数据!',
|
duration: 5
|
})
|
this.setState({ loading: false })
|
return
|
} else if (data.length * btn.verify.columns.length > 30000) {
|
notification.warning({
|
top: 92,
|
message: '当前导入数据量过大,如遇错误提示,请分批导入!',
|
duration: 5
|
})
|
}
|
|
let result = getExcelInSql(btn, data, this.state.dict, (this.props.BID || ''))
|
|
if (result.errors) {
|
notification.warning({
|
top: 92,
|
message: result.errors,
|
duration: 5
|
})
|
this.setState({ loading: false })
|
return
|
}
|
|
let param = {
|
ID: this.state.primaryId,
|
excel_in: result.lines
|
}
|
|
if (this.props.BID) {
|
param.BID = this.props.BID
|
}
|
|
if (btn.intertype === 'system') { // 系统存储过程
|
param.func = 'sPC_TableData_InUpDe'
|
|
if (sessionStorage.getItem('dataM') === 'true') { // 数据权限
|
result.sql = result.sql.replace(/\$@/ig, '/*')
|
result.sql = result.sql.replace(/@\$/ig, '*/')
|
result.bottom = result.bottom.replace(/\$@/ig, '/*')
|
result.bottom = result.bottom.replace(/@\$/ig, '*/')
|
} else {
|
result.sql = result.sql.replace(/@\$|\$@/ig, '')
|
result.bottom = result.bottom.replace(/@\$|\$@/ig, '')
|
}
|
|
param.excel_in_type = 'true'
|
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')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
if (this.props.menuType === 'HS' && param.timestamp) { // 云端验证
|
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) => {
|
if (res.status) {
|
this.execSuccess(res)
|
} else {
|
this.execError(res)
|
}
|
}, () => {
|
this.execError({})
|
})
|
} else if (btn.intertype === 'inner' && btn.innerFunc) { // 自定义存储过程
|
param.func = btn.innerFunc
|
|
Api.genericInterface(param).then((res) => {
|
if (res.status) {
|
this.execSuccess(res)
|
} else {
|
this.execError(res)
|
}
|
}, () => {
|
this.execError({})
|
})
|
} else if (btn.intertype === 'outer') { // 外部接口
|
let _outParam = null
|
|
new Promise(resolve => {
|
// 内部请求
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
// 存在内部函数时,数据预处理
|
Api.genericInterface(param).then(res => {
|
if (res.status) {
|
delete res.ErrCode
|
delete res.ErrMesg
|
delete res.message
|
delete res.status
|
|
// 使用处理后的数据调用外部接口
|
let keys = Object.keys(res) // 提交外部接口前,添加BID
|
if (this.props.BID && keys.filter(key => key.toLowerCase() === 'bid').length === 0) {
|
res.BID = this.props.BID
|
}
|
resolve(res)
|
} else {
|
this.execError(res)
|
resolve(false)
|
}
|
}, () => {
|
this.execError({})
|
})
|
} else {
|
resolve(param)
|
}
|
}).then(res => {
|
if (!res) return
|
// 外部请求
|
_outParam = fromJS(res).toJS()
|
|
if (this.props.menuType === 'HS') {
|
if (btn.sysInterface === 'true' && options.cloudServiceApi) {
|
param.rduri = options.cloudServiceApi
|
} else if (btn.sysInterface !== 'true') {
|
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') {
|
if (window.GLOB.systemType === 'production' && btn.proInterface) {
|
param.rduri = btn.proInterface
|
} else {
|
param.rduri = btn.interface
|
}
|
}
|
}
|
|
if (btn.outerFunc) {
|
res.func = btn.outerFunc
|
}
|
|
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')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
|
}
|
|
return Api.genericInterface(res)
|
}).then(response => {
|
if (!response) return
|
// 回调请求
|
if (btn.callbackFunc) {
|
// 存在回调函数时,调用
|
delete response.message
|
delete response.status
|
|
response.func = btn.callbackFunc
|
|
let _callbackparam = {..._outParam, ...response}
|
return Api.genericInterface(_callbackparam)
|
} else {
|
if (response.status) {
|
this.execSuccess(response)
|
} else {
|
this.execError(response)
|
}
|
}
|
}).then(res => {
|
if (!res) return
|
|
if (res.status) {
|
this.execSuccess(res)
|
} else {
|
this.execError(res)
|
}
|
})
|
}
|
}
|
|
render() {
|
const { btn, show } = this.props
|
const { loading, disabled, hidden } = this.state
|
|
if (hidden) return null
|
|
if (show === 'actionList') {
|
return <div style={{display: 'inline-block'}} onClick={(e) => e.stopPropagation()}>
|
<Button
|
icon={btn.icon}
|
loading={loading}
|
disabled={disabled}
|
className={'mk-btn mk-' + btn.class}
|
onClick={() => {this.actionTrigger()}}
|
>{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 ? <MkIcon 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
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(ExcelInButton)
|