import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import { Button, Modal, notification, message } from 'antd'
|
|
import asyncSpinComponent from '@/utils/asyncSpinComponent'
|
import options from '@/store/options.js'
|
import Utils from '@/utils/utils.js'
|
import Api from '@/api'
|
import './index.scss'
|
|
const { confirm } = Modal
|
const MutilForm = asyncSpinComponent(() => import('@/tabviews/zshare/mutilform'))
|
|
class MainAction extends Component {
|
static propTpyes = {
|
BID: PropTypes.string, // 主表ID
|
BData: PropTypes.any, // 主表数据
|
Tab: PropTypes.any, // 如果当前元素为标签时,tab为标签信息
|
MenuID: PropTypes.string, // 菜单ID
|
actions: PropTypes.array, // 按钮组
|
dict: PropTypes.object, // 字典项
|
setting: PropTypes.any, // 页面通用设置
|
ContainerId: PropTypes.any, // tab页面ID,用于弹窗控制
|
refreshdata: PropTypes.func, // 执行完成后数据刷新
|
selectedData: PropTypes.array // 表格选中数据
|
}
|
|
state = {
|
visible: false,
|
formdata: null,
|
tabledata: null,
|
confirmLoading: false,
|
execAction: null,
|
configMap: {}
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (item, record) => {
|
const { setting, selectedData } = this.props
|
|
let _this = this
|
let data = selectedData || []
|
|
if (record) { // 表格中触发按钮
|
data = [record]
|
}
|
|
if (item.Ot !== 'notRequired' && data.length === 0) {
|
// 需要选择行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.confirm.selectline'],
|
duration: 5
|
})
|
return
|
} else if (item.Ot === 'requiredSgl' && data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.confirm.selectSingleLine'],
|
duration: 5
|
})
|
return
|
} else if (item.Ot !== 'notRequired' && !setting.primaryKey) {
|
// 需要选择行时,校验是否设置主键
|
notification.warning({
|
top: 92,
|
message: '未设置主键!',
|
duration: 5
|
})
|
return
|
}
|
|
if (item.OpenType === 'prompt') {
|
confirm({
|
title: this.props.dict['main.action.confirm.tip'],
|
onOk() {
|
return new Promise(resolve => {
|
_this.execSubmit(item, data, resolve)
|
})
|
},
|
onCancel() {}
|
})
|
} else if (item.OpenType === 'pop') {
|
this.setState({
|
visible: true,
|
execAction: item,
|
tabledata: data
|
})
|
}
|
}
|
|
/**
|
* @description 按钮提交执行
|
*/
|
execSubmit = (btn, data, _resolve, formdata) => {
|
const { setting } = this.props
|
let primaryId = data[0] ? data[0][setting.primaryKey] : ''
|
|
if (btn.sqlType === 'insert') {
|
primaryId = ''
|
}
|
|
let values = {}
|
if (formdata) {
|
formdata.forEach(_data => {
|
values[_data.key] = _data.value
|
})
|
} else {
|
values = data[0]
|
}
|
|
let param = { // 自定义脚本添加修改删除
|
func: 's_custom_script_adduptdel',
|
ID: primaryId,
|
func_param: values.func,
|
Remark: values.Remark,
|
Sort: values.Sort,
|
LText: values.LongParam
|
}
|
|
if (btn.sqlType === 'delete') {
|
param.LText = window.GLOB.appkey || ''
|
param.Remark = ''
|
param.Sort = 0
|
param.func_param = ''
|
}
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
if (options.cloudServiceApi) {
|
param.rduri = options.cloudServiceApi
|
}
|
|
Api.genericInterface(param).then((res) => {
|
if (res.status) {
|
this.execSuccess(btn, res)
|
} else {
|
this.execError(res, btn)
|
}
|
_resolve()
|
})
|
}
|
|
/**
|
* @description 操作成功后处理
|
* 1、excel导出,成功后取消导出按钮加载中状态
|
* 2、状态码为 S 时,显示成功信息后系统默认信息
|
* 3、状态码为 -1 时,不显示任何信息
|
* 4、模态框执行成功后是否关闭
|
* 5、通知主列表刷新
|
*/
|
execSuccess = (btn, res) => {
|
if (res && res.ErrCode === 'S') { // 执行成功
|
notification.success({
|
top: 92,
|
message: res.ErrMesg || this.props.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.props.dict['main.action.confirm.success']
|
})
|
} else if (res && res.ErrCode === '-1') { // 完成后不提示
|
|
}
|
|
if (btn.OpenType === 'pop' && btn.setting && btn.setting.finish !== 'unclose') {
|
this.setState({
|
visible: false
|
})
|
}
|
|
if (btn.execSuccess !== 'never') {
|
this.props.refreshdata()
|
}
|
}
|
|
/**
|
* @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.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)
|
}
|
}
|
|
|
/**
|
* @description 模态框(表单),确认
|
*/
|
handleOk = () => {
|
this.formRef.handleConfirm().then(res => {
|
this.setState({
|
confirmLoading: true
|
})
|
|
this.execSubmit(this.state.execAction, this.state.tabledata, () => {
|
this.setState({
|
confirmLoading: false
|
})
|
}, res)
|
}, () => {})
|
}
|
|
/**
|
* @description 模态框(表单),取消
|
*/
|
handleCancel = () => {
|
this.setState({
|
visible: false
|
})
|
}
|
|
/**
|
* @description 显示模态框
|
*/
|
getModels = () => {
|
const { execAction } = this.state
|
|
if (!execAction || !this.state.visible) return
|
|
let title = ''
|
let width = '62vw'
|
let clickouter = false
|
let container = document.body
|
|
if (execAction && execAction.setting) {
|
title = execAction.setting.title
|
width = execAction.setting.width + 'vw'
|
|
if (execAction.setting.container === 'tab' && this.props.ContainerId) {
|
width = execAction.setting.width + '%'
|
container = () => document.getElementById(this.props.ContainerId)
|
}
|
|
if (execAction.setting.clickouter === 'close') {
|
clickouter = true
|
}
|
}
|
|
return (
|
<Modal
|
title={title}
|
maskClosable={clickouter}
|
getContainer={container}
|
wrapClassName='action-modal'
|
visible={this.state.visible}
|
width={width}
|
onOk={this.handleOk}
|
confirmLoading={this.state.confirmLoading}
|
onCancel={this.handleCancel}
|
destroyOnClose
|
>
|
<MutilForm
|
menuType="HS"
|
dict={this.props.dict}
|
action={execAction}
|
inputSubmit={this.handleOk}
|
configMap={this.state.configMap}
|
data={this.state.tabledata[0]}
|
BData={this.props.BData}
|
wrappedComponentRef={(inst) => this.formRef = inst}
|
/>
|
</Modal>
|
)
|
}
|
|
render() {
|
return (
|
<div className="script-button-list script-toolbar-button">
|
{this.props.actions.map((item, index) => {
|
return (
|
<Button
|
className={'mk-btn mk-' + item.class}
|
icon={item.icon}
|
key={'action' + index}
|
onClick={() => {this.actionTrigger(item)}}
|
>{item.label}</Button>
|
)
|
})}
|
{this.getModels()}
|
</div>
|
)
|
}
|
}
|
|
export default MainAction
|