import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, notification, Modal, Spin, Tabs } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import SettingUtils from './utils.jsx'
|
import DataSource from './datasource'
|
import CustomScript from '@/templates/zshare/customscript'
|
import './index.scss'
|
|
const { TabPane } = Tabs
|
|
class SettingForm extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
menu: PropTypes.object, // 菜单信息
|
config: PropTypes.object, // 页面配置信息
|
permFuncField: PropTypes.array, // 存储过程可用字段
|
inputSubmit: PropTypes.any // 回车提交事件
|
}
|
|
state = {
|
loading: false,
|
setting: null,
|
defaultsql: '',
|
activeKey: 'setting',
|
}
|
|
UNSAFE_componentWillMount() {
|
const { config } = this.props
|
|
let _setting = fromJS(config.setting).toJS()
|
let _scripts = _setting.scripts || []
|
|
this.setState({
|
setting: _setting,
|
scripts: _scripts
|
})
|
}
|
|
|
handleConfirm = () => {
|
const { activeKey, setting, scripts } = this.state
|
|
let _scripts = scripts.filter(script => script.status !== 'false')
|
|
if (activeKey === 'setting') {
|
return new Promise((resolve, reject) => {
|
this.settingForm.handleConfirm().then(res => {
|
if (res.interType === 'system' && res.default === 'false' && _scripts.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '不执行默认sql时,请添加自定义脚本!',
|
duration: 5
|
})
|
reject()
|
return
|
}
|
|
this.setState({
|
setting: res
|
}, () => {
|
this.sqlverify(() => { resolve({...res, scripts}) }, reject, 'submit')
|
})
|
}, () => {
|
reject()
|
})
|
})
|
} else {
|
return new Promise((resolve, reject) => {
|
let _loading = false
|
if (this.scriptsForm && this.scriptsForm.state.editItem) {
|
_loading = true
|
} else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) {
|
_loading = true
|
}
|
|
if (_loading) {
|
notification.warning({
|
top: 92,
|
message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
|
duration: 5
|
})
|
reject()
|
} else if (setting.interType === 'system' && setting.default === 'false' && _scripts.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '不执行默认sql时,请添加自定义脚本!',
|
duration: 5
|
})
|
reject()
|
} else {
|
this.sqlverify(() => {
|
resolve({...setting, scripts})
|
}, () => {
|
reject()
|
}, 'submit')
|
}
|
})
|
}
|
}
|
|
sqlverify = (_resolve, _reject, type, uscripts) => {
|
const { setting, scripts } = this.state
|
|
if (setting.interType !== 'system') { // 不使用系统接口时,不需要sql验证
|
_resolve()
|
return
|
}
|
|
let _scripts = []
|
if (uscripts) { // 过滤禁用的脚本
|
_scripts = uscripts.filter(script => script.status !== 'false')
|
} else {
|
_scripts = scripts.filter(script => script.status !== 'false')
|
}
|
|
if (type === 'setting' && setting.default === 'false') {
|
_resolve()
|
} else if (type === 'scripts' && _scripts.length === 0) {
|
_resolve()
|
} else { // type 为 submit 、 verify ,以及其他需要验证的场景
|
let param = {
|
func: 's_debug_sql',
|
LText: SettingUtils.getDebugSql(setting, _scripts)
|
}
|
param.LText = Utils.formatOptions(param.LText)
|
param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
param.secretkey = Utils.encrypt(param.LText, param.timestamp)
|
|
Api.getLocalConfig(param).then(result => {
|
if (result.status) {
|
_resolve()
|
} else {
|
_reject()
|
Modal.error({
|
title: result.message
|
})
|
}
|
})
|
}
|
}
|
|
changeTab = (val) => {
|
const { activeKey } = this.state
|
|
if (activeKey === 'setting') {
|
this.settingForm.handleConfirm().then(res => {
|
if (res.interType !== 'system') {
|
notification.warning({
|
top: 92,
|
message: '使用系统接口时,才可以设置自定义脚本!',
|
duration: 5
|
})
|
return
|
}
|
|
let _defaultSql = ''
|
if (res.dataresource) {
|
let _dataresource = res.dataresource
|
let arr_field = `${res.valueField},${res.labelField},${res.parentField}`
|
|
if (/\s/.test(_dataresource)) {
|
_dataresource = '(' + _dataresource + ') tb'
|
}
|
|
_defaultSql = ` select ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${res.order}) as rows from ${_dataresource}) tmptable order by tmptable.rows `
|
}
|
this.setState({
|
setting: res,
|
defaultsql: _defaultSql
|
}, () => {
|
this.setState({loading: true})
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, activeKey)
|
})
|
})
|
} else {
|
let _loading = false
|
if (this.scriptsForm && this.scriptsForm.state.editItem) {
|
_loading = true
|
} else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) {
|
_loading = true
|
}
|
|
if (_loading) {
|
notification.warning({
|
top: 92,
|
message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
|
duration: 5
|
})
|
return
|
}
|
this.setState({loading: true})
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, activeKey)
|
}
|
}
|
|
// 自定义脚本修改
|
scriptsChange = (scripts) => {
|
return new Promise((resolve, reject) => {
|
this.sqlverify(resolve, reject, 'verify', scripts)
|
})
|
}
|
|
// 自定义脚本更新
|
scriptsUpdate = (scripts) => {
|
this.setState({scripts})
|
}
|
|
render() {
|
const { menu } = this.props
|
const { activeKey, setting, loading, scripts } = this.state
|
|
return (
|
<div className="model-tree-setting-form-box" id="model-setting-form-body">
|
{loading && <Spin size="large" />}
|
<Tabs activeKey={activeKey} className="verify-card-box" onChange={this.changeTab}>
|
<TabPane tab="数据源" key="setting">
|
<DataSource
|
menu={menu}
|
dict={this.props.dict}
|
setting={setting}
|
permFuncField={this.props.permFuncField}
|
inputSubmit={this.props.inputSubmit}
|
wrappedComponentRef={(inst) => this.settingForm = inst}
|
/>
|
</TabPane>
|
<TabPane tab="自定义脚本" key="scripts">
|
<CustomScript
|
dict={this.props.dict}
|
setting={setting}
|
scripts={scripts}
|
defaultSql={this.state.defaultsql}
|
searches={[]}
|
scriptsChange={this.scriptsChange}
|
scriptsUpdate={this.scriptsUpdate}
|
wrappedComponentRef={(inst) => this.scriptsForm = inst}
|
/>
|
</TabPane>
|
</Tabs>
|
</div>
|
)
|
}
|
}
|
|
export default Form.create()(SettingForm)
|