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,24 +14,19 @@
    dict: PropTypes.object, // 字典项
    menu: PropTypes.object,
    config: PropTypes.object,
    usefulFields: PropTypes.array
    inputSubmit: PropTypes.any     // 回车提交事件
  }
  state = {
    interType: (this.props.config.setting && this.props.config.setting.interType) || 'inner',
    columns: null,
    currentTabs: null,
    selectTabs: [],
    usefulFields: [],
    interReadonly: false,
    primaryKey: ''
  }
  UNSAFE_componentWillMount() {
    const { config } = this.props
    let _tabs = []
    let _select = []
    let _tabMap = new Map()
    let _columns = []
    let _setting = config.setting
    
@@ -47,53 +45,32 @@
          return true
        }
      })
    } 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 = []
      }
    })
    let primaryKey = _setting.primaryKey
    if (primaryKey) {
      let field = _columns.filter(column => column.field === primaryKey)
      if (field.length !== 1) {
        primaryKey = ''
      }
    }
    if (!primaryKey) {
      _columns.forEach(col => {
        if (col.field.toLowerCase() === 'id') {
          primaryKey = col.field
        }
      })
    } else {
      usefulFields = []
    }
    this.setState({
      currentTabs: _tabs,
      selectTabs: _select,
      columns: _columns,
      setting: _setting,
      interType: _setting.interType || 'inner',
      interReadonly: _setting.sysInterface === 'true',
      datatype: _setting.datatype || 'maintable',
      primaryKey: primaryKey
      usefulFields
    })
  }
@@ -102,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)
@@ -113,7 +101,7 @@
  onSysChange = (e) => {
    if (e.target.value === 'true') {
      this.props.form.setFieldsValue({
        interface: window.GLOB.mainSystemApi || window.GLOB.subSystemApi
        interface: window.GLOB.mainSystemApi || ''
      })
      this.setState({
        interReadonly: true
@@ -134,7 +122,7 @@
      let _type = this.props.form.getFieldValue('sysInterface')
      if (_type === 'true') {
        this.props.form.setFieldsValue({
          interface: window.GLOB.mainSystemApi || window.GLOB.subSystemApi
          interface: window.GLOB.mainSystemApi || ''
        })
        this.setState({
          interReadonly: true
@@ -158,7 +146,7 @@
      let _type = this.props.form.getFieldValue('sysInterface')
      if (_type === 'true') {
        this.props.form.setFieldsValue({
          interface: window.GLOB.mainSystemApi || window.GLOB.subSystemApi
          interface: window.GLOB.mainSystemApi || ''
        })
        this.setState({
          interReadonly: true
@@ -171,10 +159,18 @@
    })
  }
  handleSubmit = (e) => {
    e.preventDefault()
    if (this.props.inputSubmit) {
      this.props.inputSubmit()
    }
  }
  render() {
    const { dict, usefulFields, menu } = this.props
    const { dict, menu } = this.props
    const { getFieldDecorator } = this.props.form
    const { interType, columns, selectTabs, setting, datatype, primaryKey } = this.state
    const { interType, setting, datatype, usefulFields } = this.state
    const formItemLayout = {
      labelCol: {
@@ -203,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}>
@@ -219,6 +215,7 @@
                  <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>
@@ -226,7 +223,7 @@
          <Col span={12}>
            <Form.Item label={dict['header.menu.datasource']}>
              {getFieldDecorator('datatype', {
                initialValue: setting.datatype || 'maintable'
                initialValue: setting.datatype || 'query'
              })(
                <Radio.Group onChange={this.sourceChange}>
                  <Radio value="maintable">{dict['header.menu.maintable']}</Radio>
@@ -235,30 +232,21 @@
              )}
            </Form.Item>
          </Col>
          {datatype === 'query' ? <Col span={12}>
          <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.map((option, index) =>
                    <Select.Option id={option.uuid} title={option.label} key={index} value={option.field}>{option.label}</Select.Option>
                  )}
                </Select>
              )}
                initialValue: setting.primaryKey
              })(<Input placeholder="" autoComplete="off" disabled/>)}
            </Form.Item>
          </Col> : null}
          </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['header.form.interface.inner']}</Radio>
                  <Radio value="outer">{dict['header.form.interface.outer']}</Radio>
                  <Radio value="inner">{dict['model.interface.inner']}</Radio>
                  <Radio value="outer">{dict['model.interface.outer']}</Radio>
                </Radio.Group>
              )}
            </Form.Item>
@@ -269,8 +257,8 @@
                initialValue: setting.sysInterface || 'false'
              })(
                <Radio.Group onChange={this.onSysChange}>
                  <Radio value="true">{dict['header.form.true']}</Radio>
                  <Radio value="false">{dict['header.form.false']}</Radio>
                  <Radio value="true">{dict['model.true']}</Radio>
                  <Radio value="false">{dict['model.false']}</Radio>
                </Radio.Group>
              )}
            </Form.Item>
@@ -278,7 +266,7 @@
          {datatype === 'query' && interType === 'outer' ? <Col span={12}>
            <Form.Item label={dict['header.form.interface']}>
              {getFieldDecorator('interface', {
                initialValue: setting.sysInterface === 'true' ? (window.GLOB.mainSystemApi || window.GLOB.subSystemApi) : (setting.interface || ''),
                initialValue: setting.sysInterface === 'true' ? (window.GLOB.mainSystemApi || '') : (setting.interface || ''),
                rules: [
                  {
                    required: true,
@@ -289,13 +277,13 @@
                    message: formRule.input.message
                  }
                ]
              })(<Input placeholder="" autoComplete="off" disabled={this.state.interReadonly} />)}
              })(<Input placeholder="" autoComplete="off" disabled={this.state.interReadonly} onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col> : null}
          {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>
            }>
@@ -310,13 +298,13 @@
                    message: formRule.func.maxMessage
                  }
                ]
              })(<Input placeholder="" autoComplete="off" />)}
              })(<Input placeholder="" autoComplete="off" onPressEnter={this.handleSubmit} />)}
            </Form.Item>
          </Col> : null}
          {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">
@@ -338,27 +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={
              <Tooltip placement="topLeft" title="主表可选取关联标签,标签关联后,主表数据切换时,下级标签会跟随主表主键值变化。">
                <Icon type="question-circle" />
                {'下级标签'}
              </Tooltip>
            }>
              {getFieldDecorator('subtabs', {
                initialValue: selectTabs
            <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="初始化">
              {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>