king
2021-11-12 0c84df247914f893ef5e41d57a422e10a2dc814c
src/templates/formtabconfig/settingform/index.jsx
@@ -1,7 +1,10 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Row, Col, Input, Radio, Select, Tooltip, Icon, notification } from 'antd'
import { Form, Row, Col, Input, Radio, Select, Tooltip, notification, InputNumber } from 'antd'
import { QuestionCircleOutlined } from '@ant-design/icons'
import { formRule } from '@/utils/option.js'
import Utils from '@/utils/utils.js'
import './index.scss'
const { TextArea } = Input
@@ -11,35 +14,26 @@
    dict: PropTypes.object, // 字典项
    menu: PropTypes.object,
    config: PropTypes.object,
    usefulFields: PropTypes.array
    inputSubmit: PropTypes.any     // 回车提交事件
  }
  state = {
    interType: null,
    interType: (this.props.config.setting && this.props.config.setting.interType) || 'inner',
    columns: null,
    currentTabs: null,
    selectTabs: []
    usefulFields: [],
    interReadonly: false,
  }
  UNSAFE_componentWillMount() {
    const { config, menu } = this.props
    const { config } = this.props
    let _tabs = []
    let _select = []
    let _tabMap = new Map()
    let _columns = []
    let _interType = 'inner'
    let _setting = config.setting
    try {
      _columns = menu.LongParam.columns.filter(item => item.field && item.type !== 'colspan')
      config.groups.forEach(group => {
        if (group.isDefault) {
          let list = group.sublist.filter(item => !item.origin)
          _columns = [..._columns, ...list]
        } else {
          _columns = [..._columns, ...group.sublist]
        }
        let list = group.sublist.filter(item => item.field)
        _columns = [..._columns, ...list]
      })
      let _colMap = new Map()
@@ -51,36 +45,32 @@
          return true
        }
      })
      _interType = menu.LongParam.setting.interType
    } catch {
    } catch (e) {
      notification.warning({
        top: 92,
        message: '菜单信息错误!',
        duration: 10
        duration: 5
      })
    }
    config.tabgroups.forEach(groupname => {
      config[groupname].forEach(tab => {
        if (tab.origin) return
        _tabs.push(tab)
        _tabMap.set(tab.uuid, true)
      })
    })
    _setting.subtabs && _setting.subtabs.forEach(tabId => {
      if (_tabMap.has(tabId)) {
        _select.push(tabId)
    let usefulFields = sessionStorage.getItem('permFuncField')
    if (usefulFields) {
      try {
        usefulFields = JSON.parse(usefulFields)
      } catch (e) {
        usefulFields = []
      }
    })
    } else {
      usefulFields = []
    }
    this.setState({
      currentTabs: _tabs,
      selectTabs: _select,
      columns: _columns,
      interType: _interType,
      setting: _setting
      setting: _setting,
      interType: _setting.interType || 'inner',
      interReadonly: _setting.sysInterface === 'true',
      datatype: _setting.datatype || 'maintable',
      usefulFields
    })
  }
@@ -89,6 +79,17 @@
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          let error = Utils.verifySql(values.dataresource)
          if (error) {
            notification.warning({
              top: 92,
              message: '数据源中不可使用' + error,
              duration: 5
            })
            return
          }
          resolve(values)
        } else {
          reject(err)
@@ -97,16 +98,79 @@
    })
  }
  onSysChange = (e) => {
    if (e.target.value === 'true') {
      this.props.form.setFieldsValue({
        interface: window.GLOB.mainSystemApi || ''
      })
      this.setState({
        interReadonly: true
      })
    } else {
      this.setState({
        interReadonly: false
      })
    }
  }
  onChange = (e) => {
    this.setState({
      interType: e.target.value
    }, () => {
      if (e.target.value === 'inner') return
      let _type = this.props.form.getFieldValue('sysInterface')
      if (_type === 'true') {
        this.props.form.setFieldsValue({
          interface: window.GLOB.mainSystemApi || ''
        })
        this.setState({
          interReadonly: true
        })
      } else {
        this.setState({
          interReadonly: false
        })
      }
    })
  }
  sourceChange = (e) => {
    const { interType } = this.state
    this.setState({
      datatype: e.target.value
    }, () => {
      if (interType === 'inner') return
      let _type = this.props.form.getFieldValue('sysInterface')
      if (_type === 'true') {
        this.props.form.setFieldsValue({
          interface: window.GLOB.mainSystemApi || ''
        })
        this.setState({
          interReadonly: true
        })
      } else {
        this.setState({
          interReadonly: false
        })
      }
    })
  }
  handleSubmit = (e) => {
    e.preventDefault()
    if (this.props.inputSubmit) {
      this.props.inputSubmit()
    }
  }
  render() {
    const { dict, menu, usefulFields } = this.props
    const { dict, menu } = this.props
    const { getFieldDecorator } = this.props.form
    const { interType, columns, selectTabs, setting } = this.state
    const { interType, setting, datatype, usefulFields } = this.state
    const formItemLayout = {
      labelCol: {
@@ -117,17 +181,6 @@
        xs: { span: 24 },
        sm: { span: 16 }
      }
    }
    let primaryKey = setting.primaryKey
    if (primaryKey) {
      let field = columns.filter(column => column.field === primaryKey)
      if (field.length !== 1) {
        primaryKey = ''
      }
    }
    if (!primaryKey && columns.length === 0) {
      primaryKey = 'ID'
    }
    let str = '^(' + usefulFields.join('|') + ')'
@@ -146,11 +199,11 @@
                    message: dict['form.required.input'] + '表名!'
                  },
                  {
                    max: formRule.input.max,
                    message: formRule.input.message
                    max: 50,
                    message: '表名最长为50个字符!'
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
              })(<Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col>
          <Col span={12}>
@@ -162,26 +215,58 @@
                  <Select.Option value="1">1列</Select.Option>
                  <Select.Option value="2">2列</Select.Option>
                  <Select.Option value="3">3列</Select.Option>
                  <Select.Option value="4">4列</Select.Option>
                </Select>
              )}
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label={dict['header.form.intertype']}>
              {getFieldDecorator('interType', {
                initialValue: setting.interType || 'inner'
            <Form.Item label={dict['header.menu.datasource']}>
              {getFieldDecorator('datatype', {
                initialValue: setting.datatype || 'query'
              })(
                <Radio.Group onChange={this.onChange}>
                  <Radio value="inner">{dict['header.form.interface.inner']}</Radio>
                  <Radio value="outer">{dict['header.form.interface.outer']}</Radio>
                <Radio.Group onChange={this.sourceChange}>
                  <Radio value="maintable">{dict['header.menu.maintable']}</Radio>
                  <Radio value="query">{dict['header.menu.query']}</Radio>
                </Radio.Group>
              )}
            </Form.Item>
          </Col>
          {interType === 'outer' ? <Col span={12}>
          <Col span={12}>
            <Form.Item label="主键">
              {getFieldDecorator('primaryKey', {
                initialValue: setting.primaryKey
              })(<Input placeholder="" autoComplete="off" disabled/>)}
            </Form.Item>
          </Col>
          {datatype === 'query' ? <Col span={12}>
            <Form.Item label={dict['header.form.intertype']}>
              {getFieldDecorator('interType', {
                initialValue: interType
              })(
                <Radio.Group onChange={this.onChange}>
                  <Radio value="inner">{dict['model.interface.inner']}</Radio>
                  <Radio value="outer">{dict['model.interface.outer']}</Radio>
                </Radio.Group>
              )}
            </Form.Item>
          </Col> : null}
          {datatype === 'query' && interType === 'outer' ? <Col span={12}>
            <Form.Item label={dict['header.form.sysInterface']}>
              {getFieldDecorator('sysInterface', {
                initialValue: setting.sysInterface || 'false'
              })(
                <Radio.Group onChange={this.onSysChange}>
                  <Radio value="true">{dict['model.true']}</Radio>
                  <Radio value="false">{dict['model.false']}</Radio>
                </Radio.Group>
              )}
            </Form.Item>
          </Col> : null}
          {datatype === 'query' && interType === 'outer' ? <Col span={12}>
            <Form.Item label={dict['header.form.interface']}>
              {getFieldDecorator('interface', {
                initialValue: setting.interface || '',
                initialValue: setting.sysInterface === 'true' ? (window.GLOB.mainSystemApi || '') : (setting.interface || ''),
                rules: [
                  {
                    required: true,
@@ -192,13 +277,13 @@
                    message: formRule.input.message
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
              })(<Input placeholder="" autoComplete="off" disabled={this.state.interReadonly} onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col> : null}
          {interType !== 'outer' ? <Col span={12}>
          {datatype === 'query' && interType !== 'outer' ? <Col span={12}>
            <Form.Item label={
              <Tooltip placement="topLeft" overlayClassName="middle" title={`可自定义数据处理函数,函数名称需以${usefulFields.join(', ')}等字符开始;未设置时会调用系统函数,使用系统函数需完善数据源。`}>
                <Icon type="question-circle" />
                <QuestionCircleOutlined className="mk-form-tip" />
                {dict['header.form.innerFunc']}
              </Tooltip>
            }>
@@ -213,13 +298,13 @@
                    message: formRule.func.maxMessage
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
              })(<Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col> : null}
          {interType !== 'outer' ? <Col span={24}>
          {datatype === 'query' && interType !== 'outer' ? <Col span={24}>
            <Form.Item help={'数据ID:' + menu.MenuID} label={
              <Tooltip placement="topLeft" title="使用系统函数时,需填写数据源,自定义函数时,可忽略。">
                <Icon type="question-circle" />
                <QuestionCircleOutlined className="mk-form-tip" />
                {'数据源'}
              </Tooltip>
            } className="textarea">
@@ -228,7 +313,7 @@
              })(<TextArea rows={4} />)}
            </Form.Item>
          </Col> : null}
          {interType === 'outer' ? <Col span={12}>
          {datatype === 'query' && interType === 'outer' ? <Col span={12}>
            <Form.Item label={dict['header.form.outerFunc']}>
              {getFieldDecorator('outerFunc', {
                initialValue: setting.outerFunc || '',
@@ -241,47 +326,24 @@
                    message: formRule.func.maxMessage
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
              })(<Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col> : null}
          <Col span={12}>
            <Form.Item label="主键">
              {getFieldDecorator('primaryKey', {
                initialValue: primaryKey
              })(
                <Select
                  getPopupContainer={() => document.getElementById('commontable-setting-form')}
                >
                  <Select.Option key='unset' value="">不设置</Select.Option>
                  {columns.length === 0 ?
                    <Select.Option key='id' value="ID">ID</Select.Option> : null
                  }
                  {columns.map((option, index) =>
                    <Select.Option id={option.uuid} title={option.label} key={index} value={option.field}>{option.label}</Select.Option>
                  )}
                </Select>
              )}
            <Form.Item label="宽度">
              {getFieldDecorator('width', {
                initialValue: setting.width || 100
              })(<InputNumber min={10} max={100} precision={0} />)}
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item label={
              <Tooltip placement="topLeft" title="主表可选取关联标签,标签关联后,主表数据切换时,下级标签会跟随主表主键值变化。">
                <Icon type="question-circle" />
                {'下级标签'}
              </Tooltip>
            }>
              {getFieldDecorator('subtabs', {
                initialValue: selectTabs
            <Form.Item label="初始化">
              {getFieldDecorator('onload', {
                initialValue: setting.onload || 'true'
              })(
                <Select
                  mode="multiple"
                  style={{ width: '100%' }}
                  placeholder="Please select"
                >
                  {this.state.currentTabs.map((option, index) =>
                    <Select.Option id={option.uuid} title={option.label} key={index} value={option.uuid}>{option.label}</Select.Option>
                  )}
                <Select>
                  <Select.Option value="true">加载数据</Select.Option>
                  <Select.Option value="false">不加载数据</Select.Option>
                </Select>
              )}
            </Form.Item>