import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, Radio, Tooltip, notification, InputNumber } from 'antd'
|
import { QuestionCircleOutlined } from '@ant-design/icons'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import { formRule } from '@/utils/option.js'
|
import Utils from '@/utils/utils.js'
|
import CodeMirror from '@/templates/zshare/codemirror'
|
// import './index.scss'
|
|
const { TextArea } = Input
|
|
class SettingForm extends Component {
|
static propTpyes = {
|
menu: PropTypes.object, // 菜单信息
|
setting: PropTypes.object, // 数据源配置
|
inputSubmit: PropTypes.func // 触发提交
|
}
|
|
state = {
|
interType: 'system',
|
funcTooltip: '',
|
funcRules: []
|
}
|
|
UNSAFE_componentWillMount () {
|
const { setting } = this.props
|
|
let usefulFields = sessionStorage.getItem('permFuncField')
|
let tooltip = null
|
let rules = []
|
|
if (usefulFields) {
|
try {
|
usefulFields = JSON.parse(usefulFields)
|
} catch (e) {
|
usefulFields = []
|
}
|
} else {
|
usefulFields = []
|
}
|
|
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({
|
interType: setting.interType || 'system',
|
funcTooltip: tooltip,
|
funcRules: rules
|
})
|
}
|
|
handleConfirm = () => {
|
const { setting } = this.props
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
// 数据源前端验证
|
if (values.interType === 'system' && values.default !== 'false' && !values.dataresource) {
|
notification.warning({
|
top: 92,
|
message: '请填写数据源!',
|
duration: 5
|
})
|
reject()
|
return
|
} else if (values.interType === 'system' && values.default !== 'false' && values.dataresource) {
|
let _quot = values.dataresource.match(/'{1}/g)
|
let _lparen = values.dataresource.match(/\({1}/g)
|
let _rparen = values.dataresource.match(/\){1}/g)
|
|
_quot = _quot ? _quot.length : 0
|
_lparen = _lparen ? _lparen.length : 0
|
_rparen = _rparen ? _rparen.length : 0
|
|
if (_quot % 2 !== 0) {
|
notification.warning({
|
top: 92,
|
message: '数据源中\'必须成对出现',
|
duration: 5
|
})
|
reject()
|
return
|
} else if (_lparen !== _rparen) {
|
notification.warning({
|
top: 92,
|
message: '数据源中()必须成对出现',
|
duration: 5
|
})
|
reject()
|
return
|
} else if (/--/ig.test(values.dataresource)) {
|
notification.warning({
|
top: 92,
|
message: '数据源中,不可出现字符 -- ,注释请用 /*内容*/',
|
duration: 5
|
})
|
reject()
|
return
|
}
|
|
let error = Utils.verifySql(values.dataresource)
|
|
if (error) {
|
notification.warning({
|
top: 92,
|
message: '数据源中不可使用' + error,
|
duration: 5
|
})
|
reject()
|
return
|
}
|
}
|
|
// 数据源保存
|
if (
|
values.interType === 'system' && values.default !== 'false' &&
|
/[^\s]+\s+[^\s]+/ig.test(values.dataresource) && setting.dataresource !== values.dataresource
|
) {
|
let param = {
|
func: 's_DataSrc_Save',
|
LText: values.dataresource,
|
MenuID: this.props.menu.MenuID
|
}
|
|
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.genericInterface(param)
|
}
|
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
onRadioChange = (e, key) => {
|
let value = e.target.value
|
|
if (key === 'interType') {
|
this.setState({
|
interType: value
|
})
|
} else if (key === 'sysInterface') {
|
if (value === 'true') {
|
this.props.form.setFieldsValue({
|
interface: window.GLOB.mainSystemApi || ''
|
})
|
}
|
}
|
}
|
|
handleSubmit = (e) => {
|
e.preventDefault()
|
|
if (this.props.inputSubmit) {
|
this.props.inputSubmit()
|
}
|
}
|
|
render() {
|
const { setting, menu } = this.props
|
const { getFieldDecorator } = this.props.form
|
const { interType, funcRules, funcTooltip } = this.state
|
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
|
return (
|
<Form {...formItemLayout}>
|
<Row gutter={24}>
|
<Col span={8}>
|
<Form.Item label="表名">
|
{getFieldDecorator('tableName', {
|
initialValue: setting.tableName || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入表名!'
|
},
|
{
|
max: 50,
|
message: '表名最长为50个字符!'
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit}/>)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label="标题">
|
{getFieldDecorator('title', {
|
initialValue: setting.title || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入标题!'
|
},
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label="接口类型">
|
{getFieldDecorator('interType', {
|
initialValue: interType,
|
rules: [
|
{
|
required: true,
|
message: '请选择接口类型!'
|
}
|
]
|
})(
|
<Radio.Group onChange={(e) => {this.onRadioChange(e, 'interType')}}>
|
<Radio value="system">系统</Radio>
|
<Radio value="inner">内部</Radio>
|
<Radio value="outer">外部</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
{interType === 'outer' ? <Col span={8}>
|
<Form.Item label="系统接口">
|
{getFieldDecorator('sysInterface', {
|
initialValue: setting.sysInterface || 'false',
|
rules: [
|
{
|
required: true,
|
message: '请选择系统接口!'
|
},
|
]
|
})(
|
<Radio.Group onChange={(e) => {this.onRadioChange(e, 'sysInterface')}}>
|
<Radio value="true">是</Radio>
|
<Radio value="false">否</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col> : null}
|
{interType === 'inner' ? <Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={funcTooltip}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
内部函数
|
</Tooltip>
|
}>
|
{getFieldDecorator('innerFunc', {
|
initialValue: setting.innerFunc || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入内部函数!'
|
},
|
{
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
},
|
...funcRules
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col> : null}
|
{interType === 'outer' ? <Col span={24} className="mk-through-line3">
|
<Form.Item label="接口地址">
|
{getFieldDecorator('interface', {
|
initialValue: setting.interface || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入接口地址!'
|
}
|
]
|
})(<TextArea rows={2}/>)}
|
</Form.Item>
|
</Col> : null}
|
{interType === 'outer' ? <Col span={8}>
|
<Form.Item label="外部函数">
|
{getFieldDecorator('outerFunc', {
|
initialValue: setting.outerFunc || '',
|
rules: [
|
{
|
pattern: formRule.func.pattern,
|
message: formRule.func.message
|
}, {
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col> : null}
|
{interType === 'system' ? <Col span={24} className="mk-through-line3">
|
<Form.Item help={'数据ID:' + menu.MenuID} labelCol={{xs: { span: 24 }, sm: { span: 4 }}} wrapperCol={ {xs: { span: 24 }, sm: { span: 20 }} } label={
|
<Tooltip placement="topLeft" title={'使用系统函数时,需填写数据源。注:数据权限替换符 $@ -> /* 或 \'\'、 @$ -> */ 或 \'\''}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
数据源
|
</Tooltip>
|
}>
|
{getFieldDecorator('dataresource', {
|
initialValue: setting.dataresource || ''
|
})(<CodeMirror />)}
|
</Form.Item>
|
</Col> : null}
|
<Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title="数据值字段。">
|
<QuestionCircleOutlined className="mk-form-tip" />
|
Value
|
</Tooltip>
|
}>
|
{getFieldDecorator('valueField', {
|
initialValue: setting.valueField || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入Value!'
|
},
|
{
|
pattern: formRule.field.pattern,
|
message: formRule.field.message
|
}, {
|
max: formRule.field.max,
|
message: formRule.field.maxMessage
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={'显示文字字段。'}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
Label
|
</Tooltip>
|
}>
|
{getFieldDecorator('labelField', {
|
initialValue: setting.labelField || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入Label!'
|
},
|
{
|
pattern: formRule.field.pattern,
|
message: formRule.field.message
|
}, {
|
max: formRule.field.max,
|
message: formRule.field.maxMessage
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title="父级字段。">
|
<QuestionCircleOutlined className="mk-form-tip" />
|
Parent
|
</Tooltip>
|
}>
|
{getFieldDecorator('parentField', {
|
initialValue: setting.parentField || '',
|
rules: [
|
{
|
required: true,
|
message: '请输入Label!'
|
},
|
{
|
pattern: formRule.field.pattern,
|
message: formRule.field.message
|
}, {
|
max: formRule.field.max,
|
message: formRule.field.maxMessage
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label="排序">
|
{getFieldDecorator('order', {
|
initialValue: setting.order || 'ID desc',
|
rules: [
|
{
|
required: true,
|
message: '请输入排序!'
|
},
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
})(<Input placeholder={'ID asc, UID desc'} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={'父级字段值与顶级标识相同时,视为顶级节点。'}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
顶级标识
|
</Tooltip>
|
}>
|
{getFieldDecorator('mark', {
|
initialValue: setting.mark || '',
|
rules: [
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
})(<Input placeholder={''} autoComplete="off" onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label={
|
<Tooltip placement="topLeft" title={'栅格布局,每行等分为24列,树形比例可设置为2-12(最大50%)'}>
|
<QuestionCircleOutlined className="mk-form-tip" />
|
宽度
|
</Tooltip>
|
}>
|
{getFieldDecorator('width', {
|
initialValue: setting.width || 5,
|
rules: [
|
{
|
required: true,
|
message: '请输入宽度!'
|
}
|
]
|
})(<InputNumber min={2} max={12} precision={0} />)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label="搜索">
|
{getFieldDecorator('searchable', {
|
initialValue: setting.searchable || 'true'
|
})(
|
<Radio.Group>
|
<Radio value="true">显示</Radio>
|
<Radio value="false">隐藏</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
{interType === 'system' ? <Col span={8}>
|
<Form.Item label="默认sql">
|
{getFieldDecorator('default', {
|
initialValue: setting.default || 'true'
|
})(
|
<Radio.Group>
|
<Radio value="true">执行</Radio>
|
<Radio value="false">不执行</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col> : null}
|
<Col span={8}>
|
<Form.Item label="显示图标">
|
{getFieldDecorator('showIcon', {
|
initialValue: setting.showIcon || 'false'
|
})(
|
<Radio.Group>
|
<Radio value="true">是</Radio>
|
<Radio value="false">否</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
<Col span={8}>
|
<Form.Item label="显示分割线">
|
{getFieldDecorator('showLine', {
|
initialValue: setting.showLine || 'false'
|
})(
|
<Radio.Group>
|
<Radio value="true">是</Radio>
|
<Radio value="false">否</Radio>
|
</Radio.Group>)}
|
</Form.Item>
|
</Col>
|
</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(SettingForm)
|