import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import { Button, Modal, notification, message } from 'antd'
|
import MutilForm from '@/tabviews/zshare/mutilform'
|
import Utils from '@/utils/utils.js'
|
import { refCodes } from '../config'
|
import Api from '@/api'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
class MainAction extends Component {
|
static propTpyes = {
|
BID: PropTypes.string, // 主表ID
|
datalist: PropTypes.any, // 列表数据
|
Tab: PropTypes.any, // 如果当前元素为标签时,tab为标签信息
|
MenuID: PropTypes.string, // 菜单ID
|
actions: PropTypes.array, // 按钮组
|
selectedData: PropTypes.array, // 选中数据
|
dict: PropTypes.object, // 字典项
|
setting: PropTypes.any, // 页面通用设置
|
ContainerId: PropTypes.any, // tab页面ID,用于弹窗控制
|
refreshdata: PropTypes.func, // 执行完成后数据刷新
|
}
|
|
state = {
|
visible: false,
|
formdata: null,
|
tabledata: null,
|
confirmLoading: false,
|
execAction: null,
|
configMap: {},
|
loadingNumber: ''
|
}
|
|
/**
|
* @description 触发按钮操作
|
*/
|
actionTrigger = (item) => {
|
const { setting, BID, datalist, selectedData } = this.props
|
|
if (!BID) {
|
notification.warning({
|
top: 92,
|
message: '需要上级主键值!',
|
duration: 5
|
})
|
return
|
}
|
|
let _this = this
|
let data = selectedData || []
|
|
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.sqlType === 'delete') {
|
let sysKeys = data.filter(cell => cell.uuid === 'sso_system' || cell.uuid === 'local_system')
|
if (sysKeys.length > 0) {
|
notification.warning({
|
top: 92,
|
message: '系统密钥不可删除!',
|
duration: 5
|
})
|
return
|
}
|
}
|
|
if ((!datalist || datalist.length === 0) && item.sqlType !== 'revert') {
|
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) => {
|
let datalist = JSON.parse(JSON.stringify(this.props.datalist))
|
|
let cell = {}
|
if (formdata) {
|
formdata.forEach(_cell => {
|
cell[_cell.key] = _cell.value
|
})
|
}
|
|
if (btn.sqlType === 'insert') {
|
let newkey = {
|
uuid: Utils.getuuid(),
|
keyType: 'other',
|
...cell
|
}
|
datalist.push(newkey)
|
} else if (btn.sqlType === 'update') {
|
let newkey = {
|
...data[0],
|
...cell
|
}
|
|
datalist = datalist.map(item => {
|
if (item.uuid === newkey.uuid) {
|
return newkey
|
} else {
|
return item
|
}
|
})
|
} else if (btn.sqlType === 'update') {
|
let keys = data.map(item => item.uuid)
|
|
datalist = datalist.filter(item => !keys.includes(item.uuid))
|
} else if (btn.sqlType === 'revert') {
|
datalist = [
|
{uuid: 'sso_system', public: 'a', private: 'apple', keyType: 'sso'},
|
{uuid: 'local_system', public: 'b', private: 'banana', keyType: 'local'},
|
{uuid: 'other_system1', public: 'c', private: 'cherry', keyType: 'other'},
|
{uuid: 'other_system2', public: 'd', private: 'damson', keyType: 'other'}
|
]
|
}
|
|
let _number = Math.floor(Math.random() * 10)
|
let keyString = window.btoa(window.encodeURIComponent(JSON.stringify(datalist)))
|
let regular = refCodes[_number]
|
keyString = keyString.replace(new RegExp(regular.char, 'g'), regular.md5str)
|
keyString = window.btoa(keyString)
|
keyString = _number + keyString
|
|
let param = {
|
func: 's_app_secret_param_adduptdel',
|
BID: this.props.BID,
|
LText: keyString,
|
timestamp: moment().format('YYYY-MM-DD HH:mm:ss')
|
}
|
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
Api.getLocalConfig(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
|
})
|
}
|
|
this.props.refreshdata(btn.execSuccess)
|
}
|
|
/**
|
* @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)
|
}
|
|
this.props.refreshdata(btn.execError)
|
}
|
|
/**
|
* @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
|
dict={this.props.dict}
|
menuType="HS"
|
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="secret-button-list secret-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
|