import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import { Button, Modal, notification, message } from 'antd'
|
import Utils, { getSysDefaultSql } 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 = {
|
MenuID: PropTypes.string, // 菜单ID
|
primaryId: PropTypes.string, // 主键
|
actions: PropTypes.array, // 按钮组
|
dict: PropTypes.object, // 字典项
|
data: PropTypes.any, // 数据
|
setting: PropTypes.any, // 页面通用设置
|
getFormData: PropTypes.func, // 获取表单值
|
refreshdata: PropTypes.func, // 执行完成后数据刷新
|
}
|
|
state = {
|
formdata: 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, primaryId } = this.props
|
|
let _primaryId = primaryId
|
|
if (btn.intertype === 'inner') {
|
let param = { // 系统存储过程
|
func: btn.innerFunc,
|
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]
|
|
Api.genericInterface(param).then((res) => {
|
if (res.status) {
|
this.execSuccess(btn, res, _primaryId, formdata)
|
} else {
|
this.execError(res, btn)
|
}
|
_resolve()
|
})
|
} else if (btn.intertype === 'system') {
|
// 使用系统接口时,数据源不可为空, 使用系统函数时,类型不可为空
|
if (!btn.sql || !btn.sqlType) {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
// 创建凭证时,需要选择行时
|
if (!data && btn.verify && btn.verify.voucher && btn.verify.voucher.enabled) {
|
notification.warning({
|
top: 92,
|
message: '使用创建凭证函数,需要选择行!',
|
duration: 5
|
})
|
return
|
}
|
|
let param = { // 系统存储过程
|
func: 'sPC_TableData_InUpDe',
|
exec_type: 'y', // 后台解码
|
BID: ''
|
}
|
|
if (btn.sql && btn.sqlType === 'insert') { // 系统函数添加时,生成uuid
|
param.ID = Utils.getguid()
|
param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
param.LText = Utils.formatOptions(param.LText)
|
|
_primaryId = param.ID
|
} else if (btn.sql && btn.sqlType === 'insertOrUpdate') { // 系统函数添加或修改时
|
param.ID = primaryId || Utils.getguid()
|
param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
param.LText = Utils.formatOptions(param.LText)
|
|
_primaryId = param.ID
|
} else if (btn.sql) {
|
param.ID = primaryId
|
param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt('', param.timestamp)
|
param.LText = Utils.formatOptions(param.LText)
|
}
|
|
if (window.GLOB.mkHS && param.timestamp) { // 云端验证
|
param.open_key = Utils.encryptOpenKey(param.secretkey, 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 (window.GLOB.mkHS) {
|
if (btn.sysInterface === 'true' && options.cloudServiceApi) {
|
res.rduri = options.cloudServiceApi
|
res.userid = sessionStorage.getItem('CloudUserID') || ''
|
res.LoginUID = sessionStorage.getItem('CloudLoginUID') || ''
|
} else if (btn.sysInterface !== 'true') {
|
res.rduri = btn.interface
|
}
|
} else {
|
if (btn.sysInterface === 'true' && window.GLOB.mainSystemApi) {
|
res.rduri = window.GLOB.mainSystemApi
|
} else if (btn.sysInterface !== 'true') {
|
res.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(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: 10
|
})
|
} else if (res.ErrCode === 'F') {
|
notification.error({
|
className: 'notification-custom-error',
|
top: 92,
|
message: res.message || res.ErrMesg,
|
duration: 10
|
})
|
} else if (res.ErrCode === 'NM') {
|
message.error(res.message || res.ErrMesg)
|
}
|
|
this.props.refreshdata(btn, 'error')
|
}
|
|
/**
|
* @description 按钮配置信息错误提示
|
*/
|
actionSettingError = () => {
|
notification.warning({
|
top: 92,
|
message: '按钮设置错误!',
|
duration: 5
|
})
|
}
|
|
|
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
|