import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Tabs, Table, Popconfirm, Icon, notification, Modal, Spin } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import ColForm from './columnform'
|
import CustomScript from '@/templates/zshare/customscript'
|
import SettingForm from './settingform'
|
import SettingUtils from './utils'
|
import './index.scss'
|
|
const { TabPane } = Tabs
|
|
const FieldsComponent = asyncComponent(() => import('@/templates/sharecomponent/fieldscomponent'))
|
|
class VerifyCard extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
tableFields: PropTypes.any, // 数据源信息
|
permFuncField: PropTypes.any, // 数据源信息
|
config: PropTypes.object, // 数据源信息
|
menuId: PropTypes.string, // 菜单Id
|
searches: PropTypes.array, // 搜索条件
|
}
|
|
state = {
|
columns: [],
|
activeKey: 'setting',
|
loading: false,
|
usefulfields: '',
|
defaultsql: '', // 默认Sql
|
colColumns: [
|
{
|
title: '名称',
|
dataIndex: 'label',
|
width: '25%'
|
},
|
{
|
title: '字段',
|
dataIndex: 'field',
|
width: '25%'
|
},
|
{
|
title: '数据类型',
|
dataIndex: 'datatype',
|
width: '25%',
|
},
|
{
|
title: '操作',
|
align: 'center',
|
width: '25%',
|
dataIndex: 'operation',
|
render: (text, record) =>
|
(<div>
|
<span className="operation-btn" title={this.props.dict['model.edit']} onClick={() => this.handleEdit(record)} style={{color: '#1890ff'}}><Icon type="edit" /></span>
|
<Popconfirm
|
overlayClassName="popover-confirm"
|
title={this.props.dict['model.query.delete']}
|
onConfirm={() => this.deleteColumn(record)
|
}>
|
<span className="operation-btn" style={{color: '#ff4d4f'}}><Icon type="delete" /></span>
|
</Popconfirm>
|
</div>)
|
}
|
]
|
}
|
|
UNSAFE_componentWillMount() {
|
const { config } = this.props
|
|
this.setState({
|
setting: fromJS(config.setting).toJS(),
|
columns: fromJS(config.columns).toJS(),
|
scripts: fromJS(config.scripts).toJS()
|
})
|
}
|
|
columnChange = (values) => {
|
let columns = fromJS(this.state.columns).toJS()
|
|
if (values.uuid) {
|
columns = columns.map(item => {
|
if (item.uuid === values.uuid) {
|
return values
|
} else {
|
return item
|
}
|
})
|
} else {
|
values.uuid = Utils.getuuid()
|
columns.push(values)
|
}
|
|
this.setState({ columns })
|
}
|
|
deleteColumn = (record) => {
|
this.setState({ columns: this.state.columns.filter(item => item.uuid !== record.uuid) })
|
}
|
|
handleEdit = (record) => {
|
this.contrastForm.edit(record)
|
|
let node = document.getElementById('model-setting-form-body').parentNode
|
|
if (node && node.scrollTop) {
|
let inter = Math.ceil(node.scrollTop / 10)
|
|
let timer = setInterval(() => {
|
if (node.scrollTop - inter > 0) {
|
node.scrollTop = node.scrollTop - inter
|
} else {
|
node.scrollTop = 0
|
clearInterval(timer)
|
}
|
}, 10)
|
}
|
}
|
|
changeTab = (val) => {
|
const { activeKey, setting } = this.state
|
|
if (activeKey === 'setting') {
|
this.settingForm.handleConfirm().then(res => {
|
if (res.interType !== 'system' && val === 'scripts') {
|
notification.warning({
|
top: 92,
|
message: '使用系统接口时,才可以设置自定义脚本!',
|
duration: 5
|
})
|
return
|
}
|
|
this.setState({
|
setting: res
|
}, () => {
|
if (res.interType === 'system') { // 系统接口,sql验证
|
this.setState({loading: true})
|
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, activeKey)
|
} else {
|
this.setState({
|
activeKey: val
|
})
|
}
|
})
|
})
|
} else if (activeKey === 'columns') {
|
if (setting.interType !== 'system' && val === 'scripts') {
|
notification.warning({
|
top: 92,
|
message: '使用系统接口时,才可以设置自定义脚本!',
|
duration: 5
|
})
|
return
|
}
|
|
this.setState({
|
activeKey: val
|
})
|
} else if (activeKey === 'scripts') {
|
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)
|
}
|
}
|
|
submitDataSource = () => {
|
const { activeKey, setting, columns, scripts } = this.state
|
|
return new Promise((resolve, reject) => {
|
if (activeKey === 'setting') {
|
this.settingForm.handleConfirm().then(res => {
|
this.setState({
|
setting: res
|
}, () => {
|
this.sqlverify(() => { resolve({setting: res, columns, scripts }) }, reject, 'submit')
|
})
|
}, () => {
|
reject()
|
})
|
} else if (activeKey === 'columns') {
|
this.sqlverify(() => { resolve({setting, columns, scripts }) }, reject, 'submit')
|
} else if (activeKey === 'scripts') {
|
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()
|
return
|
}
|
|
this.sqlverify(() => { resolve({setting, columns, scripts }) }, reject, 'submit')
|
}
|
})
|
}
|
|
sqlverify = (resolve, reject, type, testScripts) => {
|
const { searches } = this.props
|
const { columns, setting, scripts } = this.state
|
|
if (setting.interType !== 'system') { // 不使用系统接口时,不需要sql验证
|
resolve()
|
return
|
}
|
|
let _scripts = []
|
|
if (testScripts) {
|
_scripts = testScripts.filter(item => item.status !== 'false')
|
} else {
|
_scripts = scripts.filter(item => item.status !== 'false')
|
}
|
|
if (type === 'submit' && setting.execute === 'false' && _scripts.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '不执行默认sql时,请添加自定义脚本!',
|
duration: 5
|
})
|
reject()
|
return
|
}
|
|
// 不使用默认sql切换
|
if (type === 'setting' && setting.execute === 'false') {
|
resolve()
|
} else if (type === 'scripts' && _scripts.length === 0) {
|
resolve()
|
} else if (setting.execute !== 'false' || _scripts.length > 0) {
|
let param = {
|
func: 's_debug_sql',
|
LText: SettingUtils.getDebugSql(setting, _scripts, columns, searches)
|
}
|
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
|
})
|
}
|
})
|
} else {
|
resolve()
|
}
|
}
|
|
updatefields = (columns) => {
|
this.setState({
|
columns: columns
|
})
|
}
|
|
// 自定义脚本修改
|
scriptsChange = (scripts) => {
|
return new Promise((resolve, reject) => {
|
this.sqlverify(resolve, reject, 'verify', scripts)
|
})
|
}
|
|
// 自定义脚本更新
|
scriptsUpdate = (scripts) => {
|
this.setState({scripts})
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
render() {
|
const { columns, setting, scripts, colColumns, activeKey, loading } = this.state
|
|
return (
|
<div className="model-verify-card-box-tab" id="model-setting-form-body">
|
{loading && <Spin size="large" />}
|
<Tabs activeKey={activeKey} className="verify-card-box" onChange={this.changeTab}>
|
<TabPane tab="数据源" key="setting">
|
<SettingForm
|
menuId={this.props.menuId}
|
dict={this.props.dict}
|
permFuncField={this.props.permFuncField}
|
columns={columns}
|
setting={setting}
|
scripts={scripts}
|
wrappedComponentRef={(inst) => this.settingForm = inst}
|
/>
|
</TabPane>
|
<TabPane tab="字段集" key="columns">
|
<ColForm
|
dict={this.props.dict}
|
columnChange={this.columnChange}
|
wrappedComponentRef={(inst) => this.contrastForm = inst}
|
/>
|
<FieldsComponent
|
config={{...this.props.config, columns}}
|
type="fields"
|
tableFields={this.props.tableFields}
|
updatefield={this.updatefields}
|
/>
|
<Table
|
bordered
|
rowKey="uuid"
|
className="custom-table"
|
dataSource={columns}
|
columns={colColumns}
|
pagination={false}
|
/>
|
</TabPane>
|
<TabPane tab={
|
<span>
|
自定义脚本
|
{scripts.length ? <span className="count-tip">{scripts.length}</span> : null}
|
</span>
|
} key="scripts">
|
<CustomScript
|
dict={this.props.dict}
|
setting={setting}
|
scripts={scripts}
|
defaultSql={this.state.defaultsql}
|
searches={this.props.searches}
|
scriptsChange={this.scriptsChange}
|
scriptsUpdate={this.scriptsUpdate}
|
wrappedComponentRef={(inst) => this.scriptsForm = inst}
|
/>
|
</TabPane>
|
</Tabs>
|
</div>
|
)
|
}
|
}
|
|
export default Form.create()(VerifyCard)
|