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 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'
|
|
class ExcelInButton extends Component {
|
static propTpyes = {
|
BID: PropTypes.string, // 主表ID
|
BData: PropTypes.any, // 主表数据
|
selectedData: PropTypes.any, // 子表中选择数据
|
Tab: PropTypes.any, // 如果当前元素为标签时,tab为标签信息
|
MenuID: PropTypes.string, // 菜单ID
|
btn: PropTypes.object, // 按钮
|
columns: PropTypes.array, // 字段列
|
setting: PropTypes.any, // 页面通用设置
|
ContainerId: PropTypes.any, // tab页面ID,用于弹窗控制
|
updateStatus: PropTypes.func, // 按钮状态更新
|
triggerBtn: PropTypes.any,
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
loading: 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)
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
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')
|
}
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (record) => {
|
const { setting, Tab, BID, btn, selectedData } = this.props
|
|
if (Tab && Tab.supMenu && !BID) {
|
notification.warning({
|
top: 92,
|
message: '需要上级主键值!',
|
duration: 5
|
})
|
return
|
}
|
|
let data = []
|
|
if (record) { // 表格中触发按钮
|
data = [record]
|
} else {
|
data = selectedData || []
|
}
|
|
if (btn.Ot === 'requiredSgl' && data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.state.dict['main.action.confirm.selectSingleLine'],
|
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.updateStatus('start')
|
this.refs.excelIn.exceltrigger()
|
})
|
} else {
|
notification.warning({
|
top: 92,
|
message: 'excel导入验证信息未设置!',
|
duration: 5
|
})
|
}
|
}
|
|
/**
|
* @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
|
})
|
|
this.props.updateStatus('refresh', btn.execSuccess)
|
}
|
|
/**
|
* @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
|
})
|
|
this.props.updateStatus('refresh', btn.execError)
|
}
|
|
/**
|
* @description Excel 导入
|
*/
|
getexceldata = (data, errors) => {
|
const { btn } = this.props
|
|
if (errors) {
|
if (errors === 'notexit') {
|
notification.warning({
|
top: 92,
|
message: '工作表《' + btn.verify.sheet + '》不存在!',
|
duration: 5
|
})
|
} else if (errors === 'empty') {
|
notification.warning({
|
top: 92,
|
message: '工作表《' + btn.verify.sheet + '》为空!',
|
duration: 5
|
})
|
} else if (errors === 'headerError') {
|
notification.warning({
|
top: 92,
|
message: '工作表《' + btn.verify.sheet + '》表头设置错误!',
|
duration: 5
|
})
|
}
|
|
this.updateStatus('over')
|
return
|
}
|
|
if (!data || data.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '未获取到工作表《' + btn.verify.sheet + '》数据!',
|
duration: 5
|
})
|
this.updateStatus('over')
|
return
|
}
|
|
let result = Utils.getExcelInSql(btn, data, this.state.dict)
|
if (result.errors) {
|
notification.warning({
|
top: 92,
|
message: result.errors,
|
duration: 5
|
})
|
this.updateStatus('over')
|
return
|
}
|
|
let param = {
|
ID: this.state.primaryId
|
}
|
|
if (this.props.BID) {
|
param.BID = this.props.BID
|
}
|
|
if (this.props.dataManager) { // 数据权限
|
result.sql = result.sql.replace(/\$@/ig, '/*')
|
result.sql = result.sql.replace(/@\$/ig, '*/')
|
} else {
|
result.sql = result.sql.replace(/@\$|\$@/ig, '')
|
}
|
|
param.LText = Utils.formatOptions(result.sql)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
if (btn.intertype === 'inner' && !btn.innerFunc) { // 系统存储过程
|
param.func = 'sPC_TableData_InUpDe'
|
|
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 = JSON.parse(JSON.stringify(res))
|
|
if (this.props.menuType === 'HS') {
|
if (btn.sysInterface === 'true' && options.cloudServiceApi) {
|
param.rduri = options.cloudServiceApi
|
} else if (btn.sysInterface !== 'true') {
|
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 (btn.outerFunc) {
|
res.func = btn.outerFunc
|
}
|
|
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 } = this.props
|
const { loading } = this.state
|
|
return (
|
<div className="mk-btn-wrap">
|
<Button
|
className={'mk-btn mk-' + btn.class}
|
icon={btn.icon}
|
onClick={() => {this.actionTrigger()}}
|
loading={loading}
|
>{btn.label}</Button>
|
<ExcelIn MenuID={this.props.MenuID} btn={btn} returndata={this.getexceldata} ref="excelIn" />
|
</div>
|
)
|
}
|
}
|
|
const mapStateToProps = (state) => {
|
return {
|
menuType: state.editLevel,
|
dataManager: state.dataManager
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(ExcelInButton)
|