import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Radio, Tooltip, Icon } from 'antd'
|
|
import { formRule } from '@/utils/option.js'
|
import './index.scss'
|
|
const { TextArea } = Input
|
|
class SettingForm extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
setting: PropTypes.object, // 数据源配置
|
updateStatus: PropTypes.func, // 状态更新
|
}
|
|
state = {
|
procMode: 'script',
|
funcTooltip: '',
|
funcRules: []
|
}
|
|
UNSAFE_componentWillMount () {
|
const { setting } = this.props
|
|
let usefulFields = sessionStorage.getItem('permFuncField')
|
if (usefulFields) {
|
try {
|
usefulFields = JSON.parse(usefulFields)
|
} catch (e) {
|
usefulFields = []
|
}
|
} else {
|
usefulFields = []
|
}
|
|
let tooltip = null
|
let rules = []
|
|
if (usefulFields.length > 0) {
|
tooltip = '开头可用字符:' + usefulFields.join(', ')
|
let str = '^(' + usefulFields.join('|') + ')'
|
let _patten = new RegExp(str + formRule.func.innerPattern + '$', 'g')
|
|
rules.push({
|
pattern: _patten,
|
message: formRule.func.innerMessage
|
})
|
}
|
|
this.setState({
|
procMode: setting.procMode || 'script',
|
funcTooltip: tooltip,
|
funcRules: rules
|
})
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
onRadioChange = (e, key) => {
|
let value = e.target.value
|
|
if (key === 'procMode') {
|
this.setState({
|
procMode: value
|
})
|
}
|
this.props.updateStatus({[key]: value})
|
}
|
|
render() {
|
const { setting, dict } = this.props
|
const { getFieldDecorator } = this.props.form
|
const { funcRules, funcTooltip, procMode } = this.state
|
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
|
return (
|
<div className="model-table-datasource-setting-form-box">
|
<Form {...formItemLayout} className="model-setting-form">
|
<Row gutter={24}>
|
<Col span={12}>
|
<Form.Item label="接口名">
|
{getFieldDecorator('name', {
|
initialValue: setting.name || '',
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.input'] + '接口名!'
|
},
|
]
|
})(<Input placeholder={''} autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label="状态">
|
{getFieldDecorator('status', {
|
initialValue: setting.status || 'true'
|
})(
|
<Radio.Group>
|
<Radio value="true">启用</Radio>
|
<Radio value="false">禁用</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label="参数处理">
|
{getFieldDecorator('procMode', {
|
initialValue: procMode,
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.select'] + '参数处理方式!'
|
},
|
]
|
})(
|
<Radio.Group style={{whiteSpace: 'nowrap'}} onChange={(e) => {this.onRadioChange(e, 'procMode')}}>
|
<Radio value="script">前置脚本</Radio>
|
<Radio value="inner">前置函数</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
{procMode === 'inner' ? <Col span={12}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={funcTooltip}>
|
<Icon type="question-circle" />
|
前置函数
|
</Tooltip>
|
}>
|
{getFieldDecorator('prevFunc', {
|
initialValue: setting.prevFunc || '',
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.input'] + '前置函数!'
|
},
|
{
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
},
|
...funcRules
|
]
|
})(<Input placeholder={''} autoComplete="off" />)}
|
</Form.Item>
|
</Col> : null}
|
<Col className="data-source" span={24}>
|
<Form.Item label="测试地址">
|
{getFieldDecorator('interface', {
|
initialValue: setting.interface || '',
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.input'] + '测试地址!'
|
},
|
]
|
})(<TextArea rows={2} />)}
|
</Form.Item>
|
</Col>
|
<Col className="data-source" span={24}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title="正式系统所使用的的接口地址。">
|
<Icon type="question-circle" />
|
正式地址
|
</Tooltip>
|
}>
|
{getFieldDecorator('proInterface', {
|
initialValue: setting.proInterface || ''
|
})(<TextArea rows={2} />)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label="请求方式">
|
{getFieldDecorator('method', {
|
initialValue: setting.method || 'post',
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.select'] + '请求方式!'
|
},
|
]
|
})(
|
<Radio.Group>
|
<Radio value="get">GET</Radio>
|
<Radio value="post">POST</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={'如果自定义接口不支持跨域请求,会通过当前系统转发。'}>
|
<Icon type="question-circle" />
|
跨域请求
|
</Tooltip>
|
}>
|
{getFieldDecorator('cross', {
|
initialValue: setting.cross || 'true'
|
})(
|
<Radio.Group>
|
<Radio value="true">支持</Radio>
|
<Radio value="false">不支持</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label="回调方式">
|
{getFieldDecorator('callbackType', {
|
initialValue: setting.callbackType || 'script'
|
})(
|
<Radio.Group onChange={(e) => {this.onRadioChange(e, 'callbackType')}}>
|
<Radio value="default">默认脚本</Radio>
|
<Radio value="script">自定义脚本</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
<Col span={12}>
|
<Form.Item label="回调表名">
|
{getFieldDecorator('cbTable', {
|
initialValue: setting.cbTable || '',
|
rules: [
|
{
|
required: true,
|
message: dict['form.required.input'] + '回调表名!'
|
},
|
{
|
max: 50,
|
message: '表名最长为50个字符!'
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
</Row>
|
</Form>
|
</div>
|
)
|
}
|
}
|
|
export default Form.create()(SettingForm)
|