import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import moment from 'moment'
|
import { Button, Affix, Modal, notification, Spin } from 'antd'
|
import MutilForm from '../mutilform'
|
import Utils from '@/utils/utils.js'
|
import Api from '@/api'
|
import './index.scss'
|
|
const { confirm } = Modal
|
|
class MainAction extends Component {
|
static propTpyes = {
|
MenuID: PropTypes.string,
|
actions: PropTypes.array, // 搜索条件列表
|
dict: PropTypes.object, // 字典项
|
setting: PropTypes.any
|
}
|
|
state = {
|
visible: false,
|
formdata: null,
|
tabledata: null,
|
confirmLoading: false,
|
execAction: null,
|
loadingUuid: '',
|
btnloading: false,
|
configMap: {}
|
}
|
|
refreshdata = (item, type) => {
|
this.props.refreshdata(item, type)
|
}
|
actionTrigger = (item) => {
|
const { setting } = this.props
|
|
let _this = this
|
let data = this.props.gettableselected() || []
|
|
if (item.Ot !== 'notRequired' && data.length === 0) {
|
// 需要选择行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.confirm.selectline'],
|
duration: 10
|
})
|
return
|
} else if (item.Ot === 'requiredSgl' && data.length !== 1) {
|
// 需要选择单行时,校验数据
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.confirm.selectSingleLine'],
|
duration: 10
|
})
|
return
|
} else if (item.Ot !== 'notRequired' && !setting.primaryKey) {
|
// 需要选择行时,校验是否设置主键
|
notification.warning({
|
top: 92,
|
message: '未设置主键!',
|
duration: 10
|
})
|
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 === 'exec') {
|
this.setState({loadingUuid: item.uuid})
|
this.execSubmit(item, data, () => {
|
this.setState({loadingUuid: ''})
|
})
|
} else if (item.OpenType === 'pop') {
|
this.setState({
|
execAction: item,
|
tabledata: data,
|
btnloading: true
|
}, () => {
|
this.improveAction(item)
|
})
|
} else {
|
notification.warning({
|
top: 92,
|
message: '完善中。。。',
|
duration: 10
|
})
|
}
|
}
|
|
execSubmit = (btn, data, _resolve, formdata) => {
|
const { setting } = this.props
|
if (btn.intertype === 'inner') {
|
// 使用内部接口时,内部函数和数据源不可同时为空, 使用系统函数时,类型不可为空
|
if (!btn.innerFunc && (!btn.sql || (btn.sql && !btn.sqlType))) {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
if (
|
btn.Ot === 'notRequired' ||
|
btn.Ot === 'requiredSgl' ||
|
(btn.Ot === 'requiredOnce' && btn.OpenType !== 'pop') ||
|
(btn.OpenType === 'pop' && !btn.innerFunc && btn.sql && btn.sqlType === 'insert')
|
) {
|
let param = { // 系统存储过程
|
func: 'sPC_TableData_InUpDe'
|
}
|
|
if (btn.OpenType === 'prompt' || btn.OpenType === 'exec') { // 是否弹框或直接执行
|
let ID = ''
|
if (btn.Ot === 'notRequired') {
|
|
} else if (btn.Ot === 'requiredSgl') {
|
ID = data[0][setting.primaryKey]
|
} else if (btn.Ot === 'requiredOnce') {
|
let ids = data.map(d => { return d[setting.primaryKey]})
|
ID = ids.join(',')
|
}
|
|
param.ID = ID
|
param.BID = ''
|
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
} else if (btn.sql) {
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
}
|
} else if (btn.OpenType === 'pop') { // 表单
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
|
formdata.forEach(_data => {
|
param[_data.key] = _data.value
|
})
|
if (setting.primaryKey) {
|
if (!param.hasOwnProperty(setting.primaryKey) && data[0] && data[0][setting.primaryKey]) {
|
param[setting.primaryKey] = data[0][setting.primaryKey]
|
}
|
}
|
if (!param.hasOwnProperty('ID') && setting.primaryKey && data[0] && data[0][setting.primaryKey]) {
|
param.ID = data[0][setting.primaryKey]
|
}
|
if (!param.hasOwnProperty('BID')) {
|
param.BID = ''
|
}
|
} else if (btn.sql && btn.sqlType === 'insert') {
|
param.ID = Utils.getguid()
|
param.BID = ''
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
} else if (btn.sql) {
|
param.ID = data[0][setting.primaryKey]
|
param.BID = ''
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata)) // 数据源
|
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)
|
} else {
|
this.execError(res, btn)
|
}
|
_resolve()
|
})
|
} else if (btn.Ot === 'required' || (btn.Ot === 'requiredOnce' && btn.OpenType === 'pop')) {
|
let deffers = data.map(cell => {
|
let param = {
|
func: 'sPC_TableData_InUpDe'
|
}
|
|
if (btn.OpenType === 'prompt' || btn.OpenType === 'exec') { // 是否弹框或直接执行
|
param.ID = cell[setting.primaryKey]
|
param.BID = ''
|
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
} else if (btn.sql) {
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
}
|
} else if (btn.OpenType === 'pop') { // 表单
|
if (btn.innerFunc) {
|
param.func = btn.innerFunc
|
|
formdata.forEach(_data => {
|
param[_data.key] = _data.value
|
})
|
if (!param.hasOwnProperty(setting.primaryKey) && cell[setting.primaryKey]) {
|
param[setting.primaryKey] = cell[setting.primaryKey]
|
}
|
if (!param.hasOwnProperty('ID') && cell[setting.primaryKey]) {
|
param.ID = cell[setting.primaryKey]
|
}
|
} else if (btn.sql) {
|
param.ID = cell[setting.primaryKey]
|
param.BID = ''
|
param.LText = Utils.formatOptions(Utils.getSysDefaultSql(btn, setting, formdata)) // 数据源
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
}
|
}
|
|
return new Promise(resolve => {
|
Api.genericInterface(param).then(res => {
|
resolve(res)
|
})
|
})
|
})
|
Promise.all(deffers).then(result => {
|
let iserror = false
|
let errorMsg = ''
|
result.forEach(res => {
|
if (res.status) {
|
|
} else {
|
iserror = true
|
errorMsg = res.message
|
}
|
})
|
if (!iserror) {
|
this.execSuccess(btn)
|
} else {
|
notification.error({
|
top: 92,
|
message: errorMsg,
|
duration: 15
|
})
|
this.refreshdata(btn, 'error')
|
}
|
_resolve()
|
})
|
} else {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
} else if (btn.intertype === 'outer') {
|
/** *********************调用外部接口************************* */
|
let param = {
|
ID: '',
|
BID: ''
|
}
|
|
if (!btn.interface) { // 接口地址不存在时报错
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
if (btn.Ot === 'notRequired' || btn.Ot === 'requiredSgl' || btn.Ot === 'requiredOnce') {
|
// 获取id
|
if (btn.Ot === 'notRequired') {
|
|
} else if (btn.Ot === 'requiredSgl') {
|
param.ID = data[0][setting.primaryKey]
|
} else if (btn.Ot === 'requiredOnce') {
|
let ids = data.map(d => { return d[setting.primaryKey]})
|
param.ID = ids.join(',')
|
}
|
|
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
|
|
res.rduri = btn.interface
|
// res.method = btn.method
|
if (btn.outerFunc) {
|
res.func = btn.outerFunc
|
}
|
// 使用处理后的数据调用外部接口
|
resolve(res)
|
} else {
|
this.execError(res, btn)
|
_resolve()
|
}
|
})
|
} else {
|
// 不存在内部函数时,生成外部请求参数
|
param.rduri = btn.interface
|
// param.method = btn.method
|
if (btn.outerFunc) {
|
param.func = btn.outerFunc
|
}
|
resolve(param)
|
}
|
}).then(res => {
|
if (!res) return
|
// 外部请求
|
return Api.genericInterface(res)
|
}).then(response => {
|
// 回调请求
|
if (response.status) {
|
if (btn.callbackFunc) {
|
// 存在回调函数时,调用
|
delete response.ErrCode
|
delete response.ErrMesg
|
delete response.message
|
delete response.status
|
|
response.func = btn.callbackFunc
|
return Api.genericInterface(response)
|
} else {
|
this.execSuccess(btn)
|
_resolve()
|
}
|
} else {
|
this.execError(response, btn)
|
_resolve()
|
}
|
}).then(res => {
|
if (!res) return
|
|
if (res.status) {
|
this.execSuccess(btn)
|
} else {
|
this.execError(res, btn)
|
}
|
_resolve()
|
})
|
|
} else if (btn.Ot === 'required') {
|
// 选择多行,循环调用
|
|
new Promise(resolve => {
|
// 内部请求
|
if (btn.innerFunc) {
|
let deffers = data.map(cell => {
|
let _param = {
|
BID: '',
|
func: btn.innerFunc
|
}
|
_param.ID = cell[setting.primaryKey]
|
return new Promise(resolve => {
|
Api.genericInterface(_param).then(res => {
|
resolve(res)
|
})
|
})
|
})
|
Promise.all(deffers).then(result => {
|
let iserror = false
|
let errorMsg = ''
|
result.forEach(res => {
|
if (!res.status) {
|
iserror = true
|
errorMsg = res.message
|
}
|
})
|
if (!iserror) {
|
resolve(result)
|
} else {
|
notification.error({
|
top: 92,
|
message: errorMsg,
|
duration: 15
|
})
|
this.refreshdata(btn, 'error')
|
_resolve()
|
}
|
})
|
} else {
|
let params = data.map(cell => {
|
return {
|
BID: '',
|
ID: cell[setting.primaryKey]
|
}
|
})
|
resolve(params)
|
}
|
}).then(result => {
|
// 外部请求
|
if (!result) return
|
|
let deffers = result.map(res => {
|
delete res.ErrCode
|
delete res.ErrMesg
|
delete res.message
|
delete res.status
|
|
res.rduri = btn.interface
|
// res.method = btn.method
|
if (btn.outerFunc) {
|
res.func = btn.outerFunc
|
}
|
return new Promise(resolve => {
|
Api.genericInterface(res).then(response => {
|
resolve(response)
|
})
|
})
|
})
|
return Promise.all(deffers)
|
|
}).then(result => {
|
// 回调请求
|
let iserror = false
|
let errorMsg = ''
|
result.forEach(res => {
|
if (!res.status) {
|
iserror = true
|
errorMsg = res.message
|
}
|
})
|
if (iserror) {
|
notification.error({
|
top: 92,
|
message: errorMsg,
|
duration: 15
|
})
|
this.refreshdata(btn, 'error')
|
_resolve()
|
return
|
}
|
|
if (btn.callbackFunc) {
|
// 存在回调函数时,调用
|
let deffers = result.map(res => {
|
delete res.ErrCode
|
delete res.ErrMesg
|
delete res.message
|
delete res.status
|
|
res.func = btn.callbackFunc
|
return new Promise(resolve => {
|
Api.genericInterface(res).then(response => {
|
resolve(response)
|
})
|
})
|
})
|
return Promise.all(deffers)
|
} else {
|
_resolve()
|
this.execSuccess(btn)
|
}
|
}).then(result => {
|
if (!result) return
|
|
let iserror = false
|
let errorMsg = ''
|
result.forEach(res => {
|
if (!res.status) {
|
iserror = true
|
errorMsg = res.message
|
}
|
})
|
if (iserror) {
|
notification.error({
|
top: 92,
|
message: errorMsg,
|
duration: 15
|
})
|
this.refreshdata(btn, 'error')
|
return
|
} else {
|
this.execSuccess(btn)
|
}
|
_resolve()
|
})
|
|
} else {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
|
} else {
|
this.actionSettingError()
|
_resolve()
|
return
|
}
|
}
|
|
execSuccess = (btn) => {
|
notification.success({
|
top: 92,
|
message: this.props.dict['main.action.confirm.success'],
|
duration: 2
|
})
|
if (btn.OpenType === 'pop') {
|
this.setState({
|
visible: false
|
})
|
}
|
this.refreshdata(btn, 'success')
|
}
|
|
execError = (res, btn) => {
|
notification.error({
|
top: 92,
|
message: res.message,
|
duration: 15
|
})
|
this.refreshdata(btn, 'error')
|
}
|
|
actionSettingError = () => {
|
notification.warning({
|
top: 92,
|
message: this.props.dict['main.action.settingerror'],
|
duration: 10
|
})
|
}
|
|
improveAction = (action) => {
|
const { configMap, execAction } = this.state
|
|
let _config = configMap[action.uuid]
|
|
if (_config) {
|
this.setState({
|
execAction: {..._config, ...execAction}
|
}, () => {
|
this.improveActionForm()
|
})
|
} else {
|
Api.getSystemCacheConfig({
|
func: 'sPC_Get_LongParam',
|
MenuID: action.uuid
|
}).then(res => {
|
let _LongParam = ''
|
|
if (res.status && res.LongParam) {
|
_LongParam = window.decodeURIComponent(window.atob(res.LongParam))
|
try {
|
_LongParam = JSON.parse(_LongParam)
|
} catch (e) {
|
_LongParam = ''
|
}
|
}
|
|
if (!res.status) {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 10
|
})
|
this.setState({
|
execAction: null,
|
tabledata: null,
|
btnloading: false
|
})
|
} else if (!_LongParam || (action.OpenType === 'pop' && _LongParam.type !== 'Modal')) {
|
notification.warning({
|
top: 92,
|
message: '未获取到按钮配置信息!',
|
duration: 10
|
})
|
this.setState({
|
execAction: null,
|
tabledata: null,
|
btnloading: false
|
})
|
} else {
|
this.setState({
|
configMap: {...configMap, [action.uuid]: _LongParam},
|
execAction: {..._LongParam, ...execAction}
|
}, () => {
|
this.improveActionForm()
|
})
|
}
|
})
|
}
|
}
|
|
improveActionForm = () => {
|
const { configMap, execAction } = this.state
|
let subfields = []
|
|
if (execAction.groups.length > 0) {
|
execAction.groups.forEach(group => {
|
group.sublist.forEach(field => {
|
if ((field.type === 'select' || field.type === 'link') && field.resourceType === '1') {
|
subfields.push(field)
|
}
|
})
|
})
|
} else {
|
execAction.fields.forEach(field => {
|
if ((field.type === 'select' || field.type === 'link') && field.resourceType === '1') {
|
subfields.push(field)
|
}
|
})
|
}
|
|
if (subfields.length === 0) {
|
this.setState({
|
visible: true,
|
btnloading: false
|
})
|
return
|
}
|
|
let deffers = subfields.map(item => {
|
let arrfield = item.valueField + ',' + item.valueText
|
|
if (item.type === 'link') {
|
arrfield = arrfield + ',' + item.linkField
|
}
|
|
let param = {
|
func: 'sPC_Get_SelectedList',
|
LText: item.dataSourceSql,
|
obj_name: 'data',
|
arr_field: arrfield
|
}
|
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
return new Promise(resolve => {
|
Api.getSystemCacheConfig(param).then(res => {
|
res.search = item
|
resolve(res)
|
})
|
})
|
})
|
|
let _field = {}
|
let error = ''
|
Promise.all(deffers).then(result => {
|
result.forEach(res => {
|
if (res.status) {
|
let options = res.data.map(cell => {
|
let item = {
|
key: Utils.getuuid(),
|
Value: cell[res.search.valueField],
|
Text: cell[res.search.valueText]
|
}
|
|
if (res.search.type === 'link') {
|
item.parentId = cell[res.search.linkField]
|
}
|
|
return item
|
})
|
|
_field[res.search.uuid] = options
|
} else {
|
error = res
|
}
|
})
|
|
if (error) {
|
notification.warning({
|
top: 92,
|
message: error.message,
|
duration: 10
|
})
|
}
|
|
this.setState({
|
configMap: {...configMap, ..._field}
|
}, () => {
|
this.setState({
|
visible: true,
|
btnloading: false
|
})
|
})
|
})
|
}
|
|
handleOk = () => {
|
this.formRef.handleConfirm().then(res => {
|
this.setState({
|
confirmLoading: true
|
})
|
this.execSubmit(this.state.execAction, this.state.tabledata, () => {
|
this.setState({
|
confirmLoading: false
|
})
|
}, res)
|
}, () => {})
|
}
|
|
handleCancel = () => {
|
this.setState({
|
visible: false
|
})
|
}
|
|
getModels = () => {
|
const { execAction } = this.state
|
|
if (!execAction || !this.state.visible) return
|
|
let title = ''
|
let width = '62vw'
|
let container = document.body
|
if (execAction && execAction.setting) {
|
title = execAction.setting.title
|
width = execAction.setting.width + 'vw'
|
|
if (execAction.setting.container === 'tab') {
|
width = execAction.setting.width + '%'
|
container = () => document.getElementById('commontable' + this.props.MenuID)
|
}
|
}
|
|
return (
|
<Modal
|
getContainer={container}
|
wrapClassName='action-modal'
|
title={title}
|
visible={this.state.visible}
|
width={width}
|
onOk={this.handleOk}
|
confirmLoading={this.state.confirmLoading}
|
onCancel={this.handleCancel}
|
destroyOnClose
|
>
|
<MutilForm
|
dict={this.props.dict}
|
action={execAction}
|
configMap={this.state.configMap}
|
data={this.state.tabledata[0]}
|
wrappedComponentRef={(inst) => this.formRef = inst}
|
/>
|
</Modal>
|
)
|
}
|
|
render() {
|
const { loadingUuid, btnloading } = this.state
|
|
if (this.props.setting.actionfixed) { // 按钮是否固定在头部
|
return (
|
<Affix offsetTop={48}>
|
<div className="button-list" id={this.props.MenuID + 'mainaction'}>
|
{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>
|
)
|
}
|
})}
|
{this.getModels()}
|
{btnloading && <Spin size="large" />}
|
</div>
|
</Affix>
|
)
|
} else {
|
return (
|
<div className="button-list">
|
{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>
|
)
|
}
|
})}
|
{this.getModels()}
|
{btnloading && <Spin size="large" />}
|
</div>
|
)
|
}
|
}
|
}
|
|
export default MainAction
|