king
2023-06-14 08cce3334a2dc81d690b518136b0aaea64e48b0b
src/menu/components/table/edit-table/columns/editColumn/index.jsx
@@ -1,9 +1,12 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Modal, notification } from 'antd'
import { Form, Row, Col, Input, Select, InputNumber, Radio, Tooltip, Modal, notification, Popover } from 'antd'
import { QuestionCircleOutlined } from '@ant-design/icons'
import moment from 'moment'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import { getColumnForm } from './formconfig'
import { formRule } from '@/utils/option.js'
import CodeMirror from '@/templates/zshare/codemirror'
@@ -16,7 +19,7 @@
  text: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'prefix', 'postfix', 'textFormat', 'editable', 'initval', 'blacklist'],
  number: ['label', 'field', 'type', 'Align', 'Hide', 'IsSort', 'Width', 'decimal', 'format', 'prefix', 'postfix', 'editable', 'initval', 'sum', 'blacklist'],
  textarea: ['label', 'field', 'type', 'Align', 'Hide', 'Width', 'prefix', 'initval', 'postfix', 'blacklist'],
  custom: ['label', 'type', 'Align', 'Hide', 'Width', 'blacklist'],
  custom: ['label', 'type', 'Align', 'Width', 'blacklist'],
  colspan: ['label', 'type', 'Align', 'Hide', 'blacklist'],
  action: ['label', 'type', 'Align', 'Width'],
  formula: ['label', 'type', 'Align', 'Hide', 'Width', 'prefix', 'postfix', 'eval', 'formula', 'blacklist'],
@@ -25,7 +28,6 @@
class EdiTableColumn extends Component {
  static propTpyes = {
    visible: PropTypes.bool,
    column: PropTypes.object,
    columns: PropTypes.array,
    fields: PropTypes.array,
@@ -35,11 +37,12 @@
  state = {
    visible: false,
    loading: false,
    formlist: null,
    transfield: {}
  }
  column = null
  record = null
  UNSAFE_componentWillMount() {
    let transfield = {}
@@ -57,18 +60,22 @@
  }
  getOptions = () => {
    let _options = fromJS(columnTypeOptions[this.column.type]).toJS()
    let _options = fromJS(columnTypeOptions[this.record.type]).toJS()
    if (this.column.editable === 'true') {
      if (this.column.type === 'text') {
    if (['number', 'text'].includes(this.record.type) && this.record.editable === 'true') {
      _options.push('ctrlField')
      if (this.record.ctrlField) {
        _options.push('ctrlValue')
      }
      if (this.record.type === 'text') {
        _options.push('editType')
        if (this.column.editType === 'switch') {
        if (this.record.editType === 'switch') {
          _options.push('enter', 'openVal', 'closeVal', 'openText', 'closeText', 'editField')
        } else if (this.column.editType === 'select') {
        } else if (this.record.editType === 'select') {
          _options.push('required', 'enter', 'resourceType', 'linkSubField', 'editField', 'dropdown')
          if (this.column.resourceType === '0') {
          if (this.record.resourceType === '0') {
            _options.push('options')
          } else {
            _options.push('dataSource', 'valueField', 'valueText', 'orderBy', 'orderType', 'disableField', 'database')
@@ -76,9 +83,15 @@
        } else {
          _options.push('required', 'enter')
        }
      } else if (this.column.type === 'number') {
      } else if (this.record.type === 'number') {
        _options.push('max', 'min', 'enter')
      }
    }
    if (this.record.type === 'formula' && this.record.eval === 'true') {
      _options.push('decimal')
    }
    if (['number', 'formula'].includes(this.record.type) && this.record.Hide !== 'true') {
      _options.push('noValue')
    }
    return _options
@@ -91,10 +104,11 @@
    })
    let formlist = getColumnForm(column, fields, this.props.columns)
    this.record = {}
    this.column = fromJS(column).toJS()
    this.column.editType = this.column.editType || 'text'
    this.column.resourceType = this.column.resourceType || '0'
    formlist.forEach(item => {
      this.record[item.key] = item.initVal
    })
    
    let _options = this.getOptions()
@@ -102,6 +116,11 @@
      visible: true,
      formlist: formlist.map(item => {
        item.hidden = !_options.includes(item.key)
        if (item.key === 'formula') {
          item.fields = this.props.fields.map(col => col.field)
          item.fields = item.fields.join(', ')
        }
        return item
      })
@@ -119,21 +138,34 @@
  }
  typeChange = (key, value, option) => {
    this.column[key] = value
    this.record[key] = value
    if (key === 'type') {
      let _options = this.getOptions()
      let _field = ''
      if (value === 'formula') {
        _field = this.props.form.getFieldValue('field') || ''
      }
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.column[item.key] || item.initVal
          if (item.key === 'decimal' && value === 'formula') {
            this.record.decimal = ''
          }
          item.initVal = this.record[item.key]
          item.hidden = !_options.includes(item.key)
          return item
        })
      }, () => {
        if (value === 'action' || value === 'colspan') {
        if (value === 'colspan') {
          this.props.form.setFieldsValue({Align: 'center'})
        } else if (value === 'formula' && _field) {
          this.props.form.setFieldsValue({formula: '@' + _field + '@'})
        } else if (value === 'index') {
          this.props.form.setFieldsValue({label: '序号'})
        }
      })
    } else if (key === 'field') {
@@ -149,15 +181,15 @@
        values.type = 'text'
      }
      let _type = this.column.type
      this.column.type = values.type
      let _type = this.record.type
      this.record.type = values.type
      if (values.type !== _type) {
        let _options = this.getOptions()
        this.setState({
          formlist: this.state.formlist.map(item => {
            item.initVal = this.column[item.key] || item.initVal
            item.initVal = this.record[item.key]
            item.hidden = !_options.includes(item.key)
            return item
@@ -170,12 +202,12 @@
      }
    } else if (key === 'format' && value === 'percent') {
      this.props.form.setFieldsValue({postfix: '%'})
    } else if (key === 'editable' || key === 'editType' || key === 'resourceType') {
    } else if (['editable', 'editType', 'resourceType', 'ctrlField', 'eval', 'Hide'].includes(key)) {
      let _options = this.getOptions()
      this.setState({
        formlist: this.state.formlist.map(item => {
          item.initVal = this.column[item.key] || item.initVal
          item.initVal = this.record[item.key]
          item.hidden = !_options.includes(item.key)
          return item
@@ -187,7 +219,7 @@
  multiselectChange = (key, value) => {
    if (key !== 'linkSubField') return
    this.column[key] = value
    this.record[key] = value
  }
  handleEmpty = () => {
@@ -225,7 +257,7 @@
  }
  changeOptions = (data) => {
    this.column.options = data || []
    this.record.options = data || []
  }
  getFields() {
@@ -310,6 +342,28 @@
          { required: item.required, message: '请输入' + item.label + '!' }
        ]
        if (item.key === 'formula') {
          fields.push(
            <Col span={span} key={index}>
              <Form.Item className={className} extra={extra} label={item.tooltip ?
                <Tooltip placement="topLeft" title={item.tooltip}>
                  <QuestionCircleOutlined className="mk-form-tip" />
                  {item.label}
                </Tooltip> : item.label
              }>
                {getFieldDecorator(item.key, {
                  initialValue: initVal,
                  rules: rules
                })(<TextArea rows={item.rows || 2}/>)}
              </Form.Item>
              <Popover overlayClassName="formula-fields" placement="topLeft" title="" content={<div>{item.fields}</div>} trigger="click">
                <span className="formula-icon">字段集</span>
              </Popover>
            </Col>
          )
          return
        }
        content = <TextArea rows={item.rows || 2}/>
      } else if (item.type === 'codemirror') {
        rules = [
@@ -319,13 +373,16 @@
        className = 'text-area'
        extra = <span className="add-resource-empty" onClick={this.handleEmpty}>空</span>
        if (item.placeholder) {
          extra = <><span className="resource-public-var">{item.placeholder}</span>{extra}</>
        }
        content = <CodeMirror />
      } else if (item.type === 'options') {
        span = 24
        className = 'text-area'
        let linkSubFields = this.column.linkSubField || []
        let linkSubFields = this.record.linkSubField || []
      
        content = <EditTable type={'select'} module="form" transfield={transfield} linkSubFields={linkSubFields} onChange={this.changeOptions}/>
      }
@@ -350,34 +407,96 @@
  }
  handleSubmit = () => {
    // const { columns } = this.props
    const { fields } = this.props
    // 表单提交时检查输入值是否正确
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        // if (values.field && columns.filter(col => col.field && col.uuid !== values.uuid && col.field === values.field).length > 0) {
        //   notification.warning({
        //     top: 92,
        //     message: '字段已添加!',
        //     duration: 5
        //   })
        //   return
        // }
        this.setState({visible: false, formlist: null})
        this.props.submitCol(values)
        if (values.type === 'number' && values.editable === 'true') {
          if (typeof(values.max) === 'number' && typeof(values.min) === 'number' && values.max < values.min) {
            notification.warning({
              top: 92,
              message: '最大值不可小于最小值!',
              duration: 5
            })
            return
          }
          // eslint-disable-next-line
        } else if (values.type === 'formula' && values.eval !== 'false' && /^[\u4E00-\u9FA50-9a-zA-Z_\s@\+\-\*\/]*$/ig.test(values.formula) && /[\+\-\*\/]/ig.test(values.formula)) {
          let cols = []
          fields.forEach(col => {
            if (/^(Int|Decimal)/ig.test(col.datatype)) {
              cols.push({reg: new RegExp('@' + col.field + '@', 'ig'), value: `(@${col.field}@)`})
            }
          })
        this.column = null
          cols.forEach(col => {
            values.formula = values.formula.replace(col.reg, col.value)
          })
        }
        if (values.dataSource && /\s/.test(values.dataSource)) {
          let error = Utils.verifySql(values.dataSource)
          if (error) {
            notification.warning({
              top: 92,
              message: '数据源中不可使用' + error,
              duration: 5
            })
            return
          }
          this.setState({
            loading: true
          })
          let param = {
            func: 's_debug_sql',
            exec_type: 'y',
            LText: `declare @mk_departmentcode nvarchar(512),@mk_organization nvarchar(512),@mk_user_type nvarchar(20)
              ${values.dataSource}`
          }
          param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
          param.LText = param.LText.replace(/@\$|\$@/ig, '').replace(/@(BID|ID|LoginUID|SessionUid|UserID|Appkey|time_id)@/ig, `'${param.timestamp}'`)
          param.LText = param.LText.replace(/\n/g, ' ')
          param.LText = Utils.formatOptions(param.LText)
          param.secretkey = Utils.encrypt('', param.timestamp)
          if (window.GLOB.mainSystemApi && values.database === 'sso') {
            param.rduri = window.GLOB.mainSystemApi
          }
          Api.genericInterface(param).then(result => {
            if (result.status) {
              this.setState({visible: false, loading: false, formlist: null})
              this.props.submitCol(values)
              this.record = null
            } else {
              this.setState({loading: false})
              Modal.error({
                title: result.message
              })
            }
          })
        } else {
          this.setState({visible: false, formlist: null})
          this.props.submitCol(values)
          this.record = null
        }
      }
    })
  }
  editModalCancel = () => {
    this.setState({visible: false, formlist: null})
    this.setState({visible: false, loading: false, formlist: null})
    this.props.cancelCol()
  }
  render() {
    const { visible } = this.state
    const { visible, loading } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
@@ -398,6 +517,7 @@
          maskClosable={false}
          onOk={this.handleSubmit}
          onCancel={this.editModalCancel}
          confirmLoading={loading}
          destroyOnClose
        >
          <Form {...formItemLayout} className="commontable-column-form" id="edit-table-column-winter">