import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Row, Col, Input, Select, Icon, Radio, notification, Tooltip, InputNumber, Cascader } from 'antd'
|
import { btnIcons, btnCustomClasses, formRule } from '@/utils/option.js'
|
|
import './index.scss'
|
|
const { TextArea } = Input
|
const actionTypeOptions = {
|
pop: ['label', 'OpenType', 'intertype', 'Ot', 'show', 'icon', 'class', 'execSuccess', 'execError', 'syncComponent', 'width'],
|
prompt: ['label', 'OpenType', 'intertype', 'Ot', 'show', 'icon', 'class', 'execSuccess', 'execError', 'syncComponent', 'width'],
|
exec: ['label', 'OpenType', 'intertype', 'Ot', 'show', 'icon', 'class', 'execSuccess', 'execError', 'syncComponent', 'width'],
|
excelIn: ['label', 'Ot', 'OpenType', 'intertype', 'show', 'icon', 'class', 'sheet', 'execSuccess', 'execError', 'syncComponent', 'width'],
|
excelOut: ['label', 'OpenType', 'intertype', 'show', 'icon', 'class', 'execSuccess', 'execError', 'pagination', 'search', 'width'],
|
popview: ['label', 'Ot', 'OpenType', 'show', 'icon', 'class', 'popClose', 'width'],
|
tab: ['label', 'Ot', 'OpenType', 'show', 'icon', 'class', 'linkmenu', 'width'],
|
innerpage: ['label', 'Ot', 'OpenType', 'pageTemplate', 'show', 'icon', 'class', 'width'],
|
funcbutton: ['label', 'OpenType', 'funcType', 'show', 'icon', 'class', 'width']
|
}
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
type: PropTypes.any, // type为"card"时,只可选单行或不选行
|
setting: PropTypes.object, // 页面设置
|
formlist: PropTypes.any, // 表单信息
|
card: PropTypes.any, // 按钮信息
|
inputSubmit: PropTypes.any // 回车提交事件
|
}
|
|
state = {
|
formlist: null, // 表单信息
|
openType: null, // 打开方式
|
interType: null, // 接口类型:内部、外部
|
funcType: null, // 功能类型
|
requireOptions: [{
|
value: 'notRequired',
|
text: this.props.dict['header.form.notRequired']
|
}, {
|
value: 'requiredSgl',
|
text: this.props.dict['header.form.requiredSgl']
|
}, {
|
value: 'required',
|
text: this.props.dict['header.form.required']
|
}, {
|
value: 'requiredOnce',
|
text: this.props.dict['header.form.requiredOnce']
|
}],
|
insertUpdateOptions: [{
|
value: 'insert',
|
text: this.props.dict['header.form.action.insert']
|
}, {
|
value: 'update',
|
text: this.props.dict['header.form.action.update']
|
}, {
|
value: 'audit',
|
text: this.props.dict['header.form.action.audit']
|
}],
|
deleteOptions: [{
|
value: 'LogicDelete',
|
text: this.props.dict['header.form.action.LogicDelete']
|
}, {
|
value: 'delete',
|
text: this.props.dict['header.form.action.delete']
|
}, {
|
value: 'custom',
|
text: this.props.dict['header.form.custom']
|
}]
|
}
|
|
|
UNSAFE_componentWillMount () {
|
const { card, type } = this.props
|
|
let _opentype = card.OpenType // 打开方式
|
let _intertype = card.intertype || 'system' // 接口类型
|
let _funcType = card.funcType || 'print' // 功能按钮默认类型
|
|
if (card.execMode) { // 转换打印时打开方式
|
_opentype = 'funcbutton'
|
}
|
|
let _options = this.getOptions(_opentype, _intertype, _funcType, card.pageTemplate)
|
|
this.setState({
|
openType: _opentype,
|
interType: _intertype,
|
funcType: _funcType,
|
formlist: this.props.formlist.map(item => {
|
if (item.key === 'class') {
|
item.options = btnCustomClasses
|
} else if (item.key === 'icon') {
|
item.options = btnIcons
|
} else if (item.key === 'Ot') {
|
if (type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else if (card.pageTemplate === 'pay') { // 行级按钮、支付按钮,只能选单行
|
item.options = this.state.requireOptions.filter(op => ['requiredSgl'].includes(op.value))
|
} else if (['innerpage', 'blank', 'tab', 'popview', 'excelIn'].includes(_opentype)) {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else if (card.sqlType === 'insert') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl', 'required'].includes(op.value))
|
} else {
|
item.options = this.state.requireOptions
|
}
|
} else if (item.key === 'sqlType') {
|
if (['prompt', 'exec'].includes(_opentype)) {
|
item.options = this.state.deleteOptions
|
} else {
|
item.options = this.state.insertUpdateOptions
|
}
|
} else if (item.key === 'OpenType') {
|
item.initVal = _opentype
|
}
|
|
item.hidden = !_options.includes(item.key)
|
return item
|
})
|
})
|
}
|
|
getOptions = (_opentype, _intertype, _funcType, _pageTemplate) => {
|
let _options = fromJS(actionTypeOptions[_opentype]).toJS() // 选项列表
|
|
if (_opentype === 'innerpage') { // 新页面,可选模板(自定义时,可填入外部链接)
|
if (_pageTemplate === 'custom') {
|
_options.push('url', 'joint')
|
}
|
} else if (_opentype === 'excelOut') { // 导入导出
|
if (_intertype === 'outer') {
|
_options.push('innerFunc', 'sysInterface', 'interface', 'outerFunc')
|
} else if (_intertype === 'inner') {
|
_options.push('innerFunc')
|
}
|
} else if (_opentype === 'excelIn') { // 导入导出
|
if (_intertype === 'outer') {
|
_options.push('innerFunc', 'sysInterface', 'interface', 'outerFunc', 'callbackFunc')
|
} else if (_intertype === 'inner') {
|
_options.push('innerFunc')
|
}
|
} else if (_opentype === 'funcbutton') {
|
if (_funcType === 'print') {
|
if (_intertype === 'outer') {
|
_options.push('execMode', 'intertype', 'innerFunc', 'sysInterface', 'interface', 'outerFunc', 'callbackFunc', 'Ot', 'execSuccess', 'execError')
|
} else if (_intertype === 'inner') {
|
_options.push('execMode', 'intertype', 'innerFunc', 'Ot', 'execSuccess', 'execError')
|
}
|
}
|
} else if (_opentype !== 'popview' && _opentype !== 'tab') {
|
if (_intertype === 'outer') {
|
_options.push('innerFunc', 'sysInterface', 'interface', 'outerFunc', 'callbackFunc')
|
} else if (_intertype === 'inner') {
|
_options.push('innerFunc')
|
} else {
|
_options.push('sql', 'sqlType')
|
}
|
}
|
|
return _options
|
}
|
|
componentDidMount () {
|
const { card } = this.props
|
|
if (card.focus) {
|
try {
|
let _form = document.getElementById('label')
|
_form && _form.select()
|
} catch {
|
console.warn('表单focus失败!')
|
}
|
}
|
}
|
|
/**
|
* @description 下拉切换
|
* 1、打开方式切换,重置可见表单和表单值
|
* 2、显示位置切换,重置选择行
|
* 3、切换标签类型,重置可选标签
|
*/
|
openTypeChange = (key, value) => {
|
const { card, type } = this.props
|
|
if (key === 'OpenType') {
|
let _options = this.getOptions(value, this.state.interType, this.state.funcType, card.pageTemplate)
|
|
let _fieldval = {}
|
|
let _formlist = this.state.formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
|
if (item.hidden) return item
|
|
if (item.key === 'intertype') {
|
_fieldval.intertype = this.state.interType
|
} else if (item.key === 'Ot') {
|
if (type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else if (['innerpage', 'blank', 'tab', 'popview'].includes(value)) {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
_fieldval.Ot = 'requiredSgl'
|
} else if (value === 'excelIn') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
_fieldval.Ot = 'notRequired'
|
} else {
|
item.options = this.state.requireOptions
|
}
|
} else if (item.key === 'sqlType') {
|
if (['prompt', 'exec'].includes(value)) {
|
item.options = this.state.deleteOptions
|
} else {
|
item.options = this.state.insertUpdateOptions
|
}
|
_fieldval.sqlType = ''
|
}
|
|
return item
|
})
|
|
this.setState({
|
openType: value,
|
formlist: _formlist
|
}, () => {
|
if (value === 'excelIn') {
|
_fieldval.label = this.props.dict['model.form.excelIn']
|
_fieldval.class = 'border-dgreen'
|
} else if (value === 'excelOut') {
|
_fieldval.label = this.props.dict['model.form.excelOut']
|
_fieldval.class = 'dgreen'
|
}
|
|
this.props.form.setFieldsValue(_fieldval)
|
})
|
} else if (key === 'funcType') {
|
let _options = this.getOptions(this.state.openType, this.state.interType, value, card.pageTemplate)
|
let _fieldval = {}
|
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
|
if (item.hidden) return item
|
|
if (item.key === 'intertype') {
|
_fieldval.intertype = this.state.interType
|
} else if (item.key === 'Ot' && value === 'print') {
|
if (type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else {
|
item.options = this.state.requireOptions
|
}
|
}
|
|
return item
|
})
|
}, () => {
|
this.props.form.setFieldsValue(_fieldval)
|
})
|
} else if (key === 'sqlType') {
|
let _fieldval = {}
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
if (item.key === 'Ot' && type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else if (item.key === 'Ot' && value === 'insert') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl', 'required'].includes(op.value))
|
} else if (item.key === 'Ot') {
|
item.options = this.state.requireOptions
|
}
|
return item
|
})
|
}, () => {
|
if (value === 'insert') {
|
_fieldval.label = '添加'
|
_fieldval.class = 'green'
|
_fieldval.Ot = 'notRequired'
|
} else if (value === 'update') {
|
_fieldval.label = '修改'
|
_fieldval.class = 'purple'
|
_fieldval.Ot = 'requiredSgl'
|
} else if (value === 'audit') {
|
_fieldval.label = '审核'
|
_fieldval.class = 'purple'
|
_fieldval.Ot = 'requiredSgl'
|
} else if (value === 'LogicDelete' || value === 'delete') {
|
_fieldval.label = '删除'
|
_fieldval.class = 'danger'
|
_fieldval.Ot = 'requiredSgl'
|
}
|
|
if (card.$type === 'tableButton') { // 表格按钮
|
delete _fieldval.Ot
|
}
|
|
this.props.form.setFieldsValue(_fieldval)
|
})
|
} else if (key === 'pageTemplate') {
|
let _fieldval = {}
|
let _options = this.getOptions(this.state.openType, this.state.interType, this.state.funcType, value)
|
|
this.setState({
|
openType: value,
|
formlist: this.state.formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
|
if (item.key === 'Ot') {
|
if (value === 'pay') {
|
item.options = this.state.requireOptions.filter(op => ['requiredSgl'].includes(op.value))
|
_fieldval.Ot = 'requiredSgl'
|
} else if (type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
}
|
}
|
|
return item
|
})
|
}, () => {
|
this.props.form.setFieldsValue(_fieldval)
|
})
|
}
|
}
|
|
onChange = (e, key) => {
|
const { type } = this.props
|
const { openType } = this.state
|
let value = e.target.value
|
|
if (key === 'intertype') {
|
let _options = this.getOptions(openType, value, this.state.funcType)
|
|
this.setState({
|
interType: value,
|
formlist: this.state.formlist.map(item => {
|
item.hidden = !_options.includes(item.key)
|
|
if (item.key === 'interface') {
|
item.readonly = false
|
} else if (item.key === 'sysInterface') {
|
item.initVal = 'false'
|
} else if (item.key === 'Ot') {
|
if (type === 'card') {
|
item.options = this.state.requireOptions.filter(op => ['notRequired', 'requiredSgl'].includes(op.value))
|
} else {
|
item.options = this.state.requireOptions
|
}
|
}
|
return item
|
})
|
}, () => {
|
if (this.props.form.getFieldValue('sqlType') !== undefined) {
|
this.props.form.setFieldsValue({sqlType: ''})
|
}
|
})
|
} else if (key === 'sysInterface') {
|
if (value === 'true') {
|
this.props.form.setFieldsValue({
|
interface: window.GLOB.mainSystemApi || ''
|
})
|
}
|
this.setState({
|
formlist: this.state.formlist.map(item => {
|
if (item.key === 'interface' && value === 'true') {
|
item.readonly = true
|
} else if (item.key === 'interface') {
|
item.readonly = false
|
}
|
|
return item
|
})
|
})
|
}
|
}
|
|
handleSubmit = (e) => {
|
e.preventDefault()
|
|
if (this.props.inputSubmit) {
|
this.props.inputSubmit()
|
}
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
const fields = []
|
|
this.state.formlist.forEach((item, index) => {
|
if (item.hidden || item.forbid) return
|
|
if (item.type === 'text') { // 文本搜索
|
let _rules = []
|
if (item.key === 'innerFunc') {
|
let str = '^(' + item.fields.join('|') + ')'
|
let _patten = new RegExp(str + formRule.func.innerPattern + '$', 'g')
|
_rules = [{
|
pattern: _patten,
|
message: formRule.func.innerMessage
|
}, {
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
}]
|
} else if (item.key === 'outerFunc' || item.key === 'callbackFunc') {
|
_rules = [{
|
pattern: formRule.func.pattern,
|
message: formRule.func.message
|
}, {
|
max: formRule.func.max,
|
message: formRule.func.maxMessage
|
}]
|
} else {
|
_rules = [{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}]
|
}
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<Icon type="question-circle" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: item.readonly ? false : !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
},
|
..._rules
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly} onPressEnter={this.handleSubmit} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'number') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<Icon type="question-circle" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: item.readonly ? false : !!item.required,
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(<InputNumber min={0} max={10000} precision={0} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.tooltip ?
|
<Tooltip placement="topLeft" overlayClassName={item.tooltipClass} title={item.tooltip}>
|
<Icon type="question-circle" />
|
{item.label}
|
</Tooltip> : item.label
|
}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || '',
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
filterOption={(input, option) => option.props.children[2].toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
onChange={(value) => {this.openTypeChange(item.key, value)}}
|
getPopupContainer={() => document.getElementById('winter')}
|
>
|
{item.options.map((option, index) =>
|
<Select.Option id={`${index}`} title={option.text} key={`${index}`} value={option.value}>
|
{item.key === 'icon' && option.value && <Icon type={option.value} />} {option.text}
|
</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'radio') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal,
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Radio.Group onChange={(e) => {this.onChange(e, item.key)}} disabled={item.readonly}>
|
{
|
item.options.map(option => {
|
return (
|
<Radio key={option.value} value={option.value}>{option.text}</Radio>
|
)
|
})
|
}
|
</Radio.Group>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'textarea') {
|
fields.push(
|
<Col span={24} key={index}>
|
<Form.Item label={item.label} className="textarea">
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal
|
})(<TextArea rows={4} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'cascader') { // 多选
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal || [],
|
rules: [
|
{
|
required: !!item.required,
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Cascader options={item.options || []} placeholder=""/>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'mcascader') {
|
fields.push(
|
<Col span={12} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.key, {
|
initialValue: item.initVal
|
})(
|
<Cascader options={item.options} expandTrigger="hover" placeholder="" />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
return fields
|
}
|
|
handleConfirm = () => {
|
const { setting } = this.props
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
values.uuid = this.props.card.uuid
|
values.verify = this.props.card.verify || null
|
|
if (values.show === 'icon' && !values.icon) {
|
notification.warning({
|
top: 92,
|
message: '请选择图标!',
|
duration: 5
|
})
|
return
|
} else if (values.OpenType === 'excelOut') {
|
if (values.intertype === 'system' && setting.interType !== 'system') {
|
notification.warning({
|
top: 92,
|
message: '表格数据查询未使用数据源,导出Excel使用内部接口时,需自定义内部函数!',
|
duration: 5
|
})
|
return
|
}
|
|
values.Ot = 'notRequired'
|
} else if (values.OpenType === 'popview') {
|
values.createTab = true // 用于标记按钮复制时,是否复制原有标签
|
} else if (values.OpenType === 'funcbutton') { // 转换打印时打开方式
|
if (values.funcType === 'print') {
|
values.OpenType = values.execMode
|
}
|
} else if (['pop', 'prompt', 'exec'].includes(values.OpenType) && values.verify) {
|
if ((values.Ot === 'requiredOnce' || this.props.card.Ot === 'requiredOnce') && this.props.card.Ot !== values.Ot) {
|
values.verify.uniques = []
|
}
|
}
|
|
resolve(values)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 7 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 17 }
|
}
|
}
|
return (
|
<Form {...formItemLayout} className="ant-advanced-search-form commontable-action-form" id="winter">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|