import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import { Button, Modal, notification, message } from 'antd'
|
import Utils from '@/utils/utils.js'
|
import options from '@/store/options.js'
|
import Api from '@/api'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
class MainAction extends Component {
|
static propTpyes = {
|
menuType: PropTypes.any, // 菜单类型,普通菜单或HS
|
MenuID: PropTypes.string, // 菜单ID
|
primaryId: PropTypes.string, // 主键
|
actions: PropTypes.array, // 按钮组
|
logcolumns: PropTypes.array, // 显示列
|
dict: PropTypes.object, // 字典项
|
data: PropTypes.any, // 数据
|
setting: PropTypes.any, // 页面通用设置
|
getFormData: PropTypes.func, // 获取表单值
|
refreshdata: PropTypes.func, // 执行完成后数据刷新
|
}
|
|
state = {
|
formdata: null,
|
tabledata: null,
|
loadingUuid: ''
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (item) => {
|
const { data } = this.props
|
|
let _this = this
|
|
if (item.btnType !== 'cancel') {
|
this.props.getFormData().then(res => {
|
if (item.OpenType === 'prompt') {
|
confirm({
|
title: this.props.dict['main.action.confirm.tip'],
|
onOk() {
|
return new Promise(resolve => {
|
_this.execSubmit(item, data, resolve, res)
|
})
|
},
|
onCancel() {}
|
})
|
} else if (item.OpenType === 'exec') {
|
this.setState({loadingUuid: item.uuid})
|
|
this.execSubmit(item, data, () => {
|
this.setState({loadingUuid: ''})
|
}, res)
|
}
|
})
|
} else {
|
item.afterExecSuccess = 'close'
|
this.props.refreshdata(item, 'success', '')
|
}
|
}
|
|
/**
|
* @description 按钮提交执行
|
*/
|
execSubmit = (btn, data, _resolve, formdata) => {
|
const { setting, logcolumns, primaryId } = this.props
|
|
let _primaryId = primaryId
|
|
if (btn.intertype === 'inner') {
|
// 使用内部接口时,内部函数和数据源不可同时为空, 使用系统函数时,类型不可为空
|
if (!btn.innerFunc && (!btn.sql || (btn.sql && !btn.sqlType))) {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
// 创建凭证时,需要选择行时
|
if (!data && !btn.innerFunc && btn.verify && btn.verify.voucher && btn.verify.voucher.enabled) {
|
notification.warning({
|
top: 92,
|
message: '使用创建凭证函数,需要选择行!',
|
duration: 10
|
})
|
return
|
}
|
|
let param = { // 系统存储过程
|
func: 'sPC_TableData_InUpDe',
|
BID: ''
|
}
|
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
|
param[setting.primaryKey] = primaryId
|
|
formdata.forEach(_data => {
|
param[_data.key] = _data.value
|
})
|
|
if (!param[setting.primaryKey]) {
|
param[setting.primaryKey] = Utils.getguid()
|
}
|
|
_primaryId = param[setting.primaryKey]
|
|
} else if (btn.sql && btn.sqlType === 'insert') { // 系统函数添加时,生成uuid
|
param.ID = Utils.getguid()
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata, param, data, logcolumns)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
_primaryId = param.ID
|
} else if (btn.sql) {
|
param.ID = primaryId
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata, param, data, logcolumns)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
}
|
Api.genericInterface(param).then((res) => {
|
if (res.status) {
|
this.execSuccess(btn, res, _primaryId, formdata)
|
} else {
|
this.execError(res, btn)
|
}
|
_resolve()
|
})
|
} else if (btn.intertype === 'outer') {
|
/** *********************调用外部接口************************* */
|
|
if (!btn.interface) { // 接口地址不存在时报错
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
let param = {
|
BID: ''
|
}
|
|
param[setting.primaryKey] = primaryId
|
|
formdata.forEach(_data => {
|
param[_data.key] = _data.value
|
})
|
|
if (!param[setting.primaryKey]) {
|
param[setting.primaryKey] = Utils.getguid()
|
}
|
_primaryId = param[setting.primaryKey]
|
|
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
|
|
// 使用处理后的数据调用外部接口
|
resolve(res)
|
} else {
|
this.execError(res, btn)
|
resolve(false)
|
_resolve()
|
}
|
})
|
} 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) {
|
res.rduri = options.cloudServiceApi
|
} else if (btn.sysInterface !== 'true') {
|
res.rduri = btn.interface
|
}
|
} else {
|
if (btn.sysInterface === 'true') {
|
res.rduri = window.GLOB.mainSystemApi || window.GLOB.subSystemApi
|
} else {
|
res.rduri = btn.interface
|
}
|
}
|
|
if (btn.outerFunc) {
|
res.func = btn.outerFunc
|
}
|
|
res.appkey = window.GLOB.appkey || '' // 外部请求时,统一添加appkey
|
|
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(btn, response, _primaryId, formdata)
|
_resolve()
|
} else {
|
this.execError(response, btn)
|
_resolve()
|
}
|
}
|
}).then(res => {
|
if (!res) return
|
|
if (res.status) {
|
this.execSuccess(btn, res, _primaryId, formdata)
|
_resolve()
|
} else {
|
this.execError(res, btn)
|
_resolve()
|
}
|
})
|
|
} else {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
}
|
|
/**
|
* @description 操作成功后处理
|
* 1、excel导出,成功后取消导出按钮加载中状态
|
* 2、状态码为 S 时,显示成功信息后系统默认信息
|
* 3、状态码为 -1 时,不显示任何信息
|
* 4、模态框执行成功后是否关闭
|
* 5、通知主列表刷新
|
*/
|
execSuccess = (btn, res, primaryId, formdata) => {
|
if (res && res.ErrCode === 'S') { // 执行成功
|
notification.success({
|
top: 92,
|
message: res.ErrMesg || this.props.dict['main.action.confirm.success'],
|
duration: 2
|
})
|
} else if (res && res.ErrCode === '-1') { // 完成后不提示
|
|
}
|
|
this.props.refreshdata(btn, 'success', primaryId, formdata)
|
}
|
|
/**
|
* @description 操作失败后处理
|
* 1、状态码为 E、N、F、NM 时,显示相应提示信息
|
* 2、excel导出,失败后取消导出按钮加载中状态
|
* 3、通知主列表刷新
|
*/
|
execError = (res, btn) => {
|
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.errorTime || 15
|
})
|
} else if (res.ErrCode === 'F') {
|
notification.error({
|
className: 'notification-custom-error',
|
top: 92,
|
message: res.message || res.ErrMesg,
|
duration: btn.errorTime || 15
|
})
|
} else if (res.ErrCode === 'NM') {
|
message.error(res.message || res.ErrMesg)
|
}
|
|
this.props.refreshdata(btn, 'error')
|
}
|
|
/**
|
* @description 按钮配置信息错误提示
|
*/
|
actionSettingError = () => {
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.settingerror'],
|
duration: 10
|
})
|
}
|
|
|
render() {
|
const { loadingUuid } = this.state
|
|
return (
|
<div className="button-list formtab-button">
|
{this.props.actions.map((item, index) => {
|
if (loadingUuid === item.uuid) {
|
return (
|
<Button
|
className={'mk-btn mk-' + item.class}
|
icon={item.icon}
|
key={'action' + index}
|
onClick={() => {this.actionTrigger(item)}}
|
loading
|
>{item.label}</Button>
|
)
|
} else {
|
return (
|
<Button
|
className={'mk-btn mk-' + item.class}
|
icon={item.icon}
|
key={'action' + index}
|
onClick={() => {this.actionTrigger(item)}}
|
>{item.label}</Button>
|
)
|
}
|
})}
|
</div>
|
)
|
}
|
}
|
|
export default MainAction
|