import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Row, Col, Button, notification, Tooltip, Select, Switch } from 'antd'
|
|
import Utils from '@/utils/utils.js'
|
import { checkSQL } from '@/utils/utils-custom.js'
|
import CodeMirror from '@/templates/zshare/codemirror'
|
// import './index.scss'
|
|
class CustomForm extends Component {
|
static propTpyes = {
|
scripts: PropTypes.array,
|
columns: PropTypes.any,
|
systemScripts: PropTypes.array,
|
scriptsChange: PropTypes.func
|
}
|
|
state = {
|
editItem: null,
|
usefulfields: null,
|
loading: false,
|
skip: false
|
}
|
|
UNSAFE_componentWillMount () {
|
this.resetfield()
|
}
|
|
resetfield = () => {
|
const { columns } = this.props
|
let fields = columns.map(item => item.field)
|
|
this.setState({
|
usefulfields: fields.join(', ')
|
})
|
}
|
|
edit = (record) => {
|
this.setState({
|
editItem: record
|
})
|
|
this.props.form.setFieldsValue({
|
sql: record.sql
|
})
|
}
|
|
handleConfirm = () => {
|
const { type, scripts } = this.props
|
const { editItem, skip } = this.state
|
|
// 表单提交时检查输入值是否正确
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (err) return
|
|
if (!values.sql || /^[\s\n]+$/.test(values.sql)) {
|
notification.warning({
|
top: 92,
|
message: '请输入sql!',
|
duration: 5
|
})
|
return
|
}
|
|
values.uuid = editItem ? editItem.uuid : ''
|
|
if (type === 'fullscreen' && editItem) {
|
values.status = editItem.status || 'true'
|
}
|
|
let pass = checkSQL(values.sql, 'customscript')
|
|
if (!pass) return
|
|
let _scripts = fromJS(scripts).toJS()
|
|
if (values.uuid) {
|
_scripts = _scripts.map(item => {
|
if (values.uuid === item.uuid) {
|
return values
|
}
|
return item
|
})
|
} else {
|
values.uuid = Utils.getuuid()
|
|
_scripts.push(values)
|
}
|
|
if (skip) {
|
this.setState({
|
skip: false,
|
editItem: null
|
})
|
this.props.scriptsChange(_scripts, null, null, values)
|
this.props.form.setFieldsValue({
|
sql: ''
|
})
|
} else {
|
this.setState({loading: true})
|
this.props.scriptsChange(_scripts, () => {
|
this.setState({
|
loading: false,
|
editItem: null
|
})
|
|
this.props.form.setFieldsValue({
|
sql: ''
|
})
|
}, () => {
|
this.setState({loading: false})
|
}, values)
|
}
|
})
|
}
|
|
handleCancel = () => {
|
this.setState({
|
editItem: null
|
})
|
|
this.props.form.setFieldsValue({
|
sql: ''
|
})
|
}
|
|
selectScript = (value, option) => {
|
let _sql = this.props.form.getFieldValue('sql')
|
if (/^\s+$/.test(_sql)) {
|
_sql = ''
|
}
|
if (_sql) {
|
_sql = _sql + `
|
|
`
|
}
|
|
_sql = _sql.replace(/\s{6}$/, '')
|
_sql = _sql + `/*${option.props.children}*/
|
`
|
_sql = _sql.replace(/\s{4}$/, '')
|
_sql = _sql + value
|
|
this.props.form.setFieldsValue({
|
sql: _sql
|
})
|
}
|
|
render() {
|
const { systemScripts, type } = this.props
|
const { usefulfields, editItem, skip } = this.state
|
const { getFieldDecorator } = this.props.form
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
|
return (
|
<Form {...formItemLayout} className="verify-form" id="verify-excelin-custom-scripts">
|
<Row gutter={24}>
|
{!type ? <Col span={8}>
|
<Form.Item label="报错字段" style={{margin: 0, whiteSpace: 'nowrap'}}>
|
errorcode, retmsg
|
</Form.Item>
|
</Col> : null}
|
{!type ? <Col span={24} className="sqlfield">
|
<Form.Item label="可用字段">
|
<Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'公共值,请按照@xxx@格式使用。'}><span style={{color: '#1890ff'}}>BID, ID, LoginUID, SessionUid, UserID, Appkey, lang, time_id, typename</span></Tooltip>,
|
<Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'系统变量,系统会定义变量并赋值。'}><span style={{color: '#fa8c16'}}>UserName, FullName, RoleID, mk_departmentcode, mk_organization, mk_user_type, mk_nation, mk_province, mk_city, mk_district, mk_address</span></Tooltip>,
|
{usefulfields}
|
</Form.Item>
|
</Col> : null}
|
{!type ? <Col span={8}>
|
<Form.Item style={{marginBottom: 0}} label="快捷添加">
|
<Select
|
showSearch
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onSelect={this.selectScript}
|
getPopupContainer={() => document.getElementById('verify-excelin-custom-scripts')}
|
>
|
<Select.Option key="debugger" value={`z_debug: select @ErrorCode='E',@retmsg='测试断点' goto aaa`}>
|
测试断点
|
</Select.Option>
|
{systemScripts.map((option, i) =>
|
<Select.Option key={i} value={option.value}>
|
{option.name}
|
</Select.Option>
|
)}
|
</Select>
|
</Form.Item>
|
</Col> : null}
|
<Col span={5} className="add" style={{paddingTop: '2px', whiteSpace: 'nowrap'}}>
|
<Button onClick={this.handleConfirm} loading={this.state.loading} className="mk-green" style={{marginBottom: 15, marginLeft: 40}}>
|
{type === 'fullscreen' && !editItem ? '添加' : '保存'}
|
</Button>
|
<Button onClick={this.handleCancel} style={{marginBottom: 15, marginLeft: 10}}>
|
取消
|
</Button>
|
</Col>
|
<Col span={3} className="forced" style={{paddingTop: '12px', fontSize: '12px', whiteSpace: 'nowrap'}}>
|
强制保存:
|
<Switch checked={skip} size="small" onChange={() => this.setState({skip: !skip})}/>
|
</Col>
|
<Col span={24} className="sql">
|
<Form.Item label="sql" required>
|
{getFieldDecorator('sql', {
|
initialValue: ''
|
})(<CodeMirror />)}
|
</Form.Item>
|
</Col>
|
</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(CustomForm)
|