king
2020-06-16 6c16e43cd6521460c804391c042348dbb14086fc
src/tabviews/formtab/formgroup/index.jsx
@@ -1,10 +1,11 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Form, Row, Col, Input, InputNumber, Select, DatePicker, notification, Collapse } from 'antd'
import moment from 'moment'
import { formRule } from '@/utils/option.js'
import Utils from '@/utils/utils.js'
import FileUpload from '@/tabviews/tableshare/fileupload'
import FileUpload from '@/tabviews/zshare/fileupload'
import './index.scss'
const {MonthPicker} = DatePicker
@@ -28,7 +29,104 @@
    fieldlen: null,
    groups: null,
    formlist: [],
    encrypts: [],    // 加密字段
    intercepts: [],  // 截取字段
    record: {}
  }
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { datatype, encrypts } = this.state
    if (nextProps.data && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
      let _fieldsvalue = {}
      let _record = {}
      Object.keys(nextProps.data).forEach(key => {
        if (this.props.form.getFieldValue(key) !== undefined) {
          if (datatype[key] === 'multiselect') {
            let _val = nextProps.data[key] ? nextProps.data[key].split(',').filter(Boolean) : []
            _fieldsvalue[key] = _val
          } else if (datatype[key] === 'date') {
            let _val = nextProps.data[key] ? nextProps.data[key] : null
            if (_val) {
              _val = moment(_val, 'YYYY-MM-DD')
            }
            _fieldsvalue[key] = _val
          } else if (datatype[key] === 'datemonth') {
            let _val = nextProps.data[key] ? nextProps.data[key] : null
            if (_val) {
              _val = moment(_val, 'YYYY-MM')
            }
            _fieldsvalue[key] = _val
          } else if (datatype[key] === 'datetime') {
            let _val = nextProps.data[key] ? nextProps.data[key] : null
            if (_val) {
              _val = moment(_val, 'YYYY-MM-DD HH:mm:ss')
            }
            _fieldsvalue[key] = _val
          } else if (datatype[key] === 'fileupload') {
            let _val = nextProps.data[key] ? nextProps.data[key] : ''
            if (_val) {
              try {
                _val = _val.split(',').map((url, index) => {
                  return {
                    uid: `${index}`,
                    name: url.slice(url.lastIndexOf('/') + 1),
                    status: 'done',
                    url: url,
                    origin: true
                  }
                })
              } catch {
                _val = []
              }
            } else {
              _val = []
            }
            _fieldsvalue[key] = _val
          } else if (datatype[key] === 'text' || datatype[key] === 'textarea') {
            let _value = nextProps.data[key]
            if (encrypts.includes(key)) { // 加密字段
              try {
                _value = window.btoa(window.encodeURIComponent(_value))
              } catch (e) {
                console.warn(e)
              }
            }
            _fieldsvalue[key] = _value
          } else {
            _fieldsvalue[key] = nextProps.data[key]
          }
        } else {
          let _value = nextProps.data[key]
          if (encrypts.includes(key)) { // 加密字段
            try {
              _value = window.btoa(window.encodeURIComponent(_value))
            } catch (e) {
              console.warn(e)
            }
          }
          _record[key] = _value
        }
      })
      if (Object.keys(_fieldsvalue).length > 0) {
        this.props.form.setFieldsValue(_fieldsvalue)
      }
      this.setState({
        record: _record
      })
    }
  }
  UNSAFE_componentWillMount () {
@@ -39,16 +137,28 @@
    let readin = {}
    let fieldlen = {}
    let _formlist = []
    let encrypts = []
    let intercepts = []
    let _groups = groups.map(group => {
      group.sublist = group.sublist.map(item => {
        let _readin = item.readin !== 'false'
        if (item.type === 'funcvar') {
          _readin = false
          item.initval = '' // 初始化为空
        }
        if (item.type === 'textarea' && item.encryption === 'true') {
          encrypts.push(item.field)
        }
        if (item.interception === 'true') {
          intercepts.push(item.field)
        }
        item.initVal = item.initval ? JSON.parse(JSON.stringify(item.initval)) : ''
        let _fieldlen = item.fieldlength || 50
        if (item.type === 'textarea' || item.type === 'fileupload') {
        if (item.type === 'textarea' || item.type === 'fileupload' || item.type === 'multiselect') {
          _fieldlen = item.fieldlength || 512
        } else if (item.type === 'number') {
          _fieldlen = item.decimal ? item.decimal : 0
@@ -60,7 +170,21 @@
        fieldlen[item.field] = _fieldlen
        if (_readin && !/^date/.test(item.type) && data && data.hasOwnProperty(item.field)) {
          item.initval = data[item.field]
          let newval = data[item.field]
          if (encrypts.includes(item.field) && newval) {
            try {
              newval = window.decodeURIComponent(window.atob(newval))
            } catch (e) {
              console.warn(e)
            }
          }
          item.initval = newval
        }
        if (item.supvalue) {
          item.supvalue = item.supvalue.split(',')
        }
        _formlist.push(item)
@@ -71,19 +195,22 @@
      return group
    })
    let error = false
    _groups = _groups.map(group => {
      group.sublist = group.sublist.map(item => {
        if (item.type === 'link') {
          let supItem = _formlist.filter(form => form.field === item.linkField)[0]
  
          // 关联显示列中的字段值,通过该值过滤下拉选项
          if (!supItem && data && data.hasOwnProperty(item.linkField)) {
            supItem = {initval: data[item.linkField]}
          }
          
          if (!supItem) {
            error = true
            notification.warning({
              top: 92,
              message: '未查询到表单《' + item.label + '》关联字段!',
              duration: 5
            })
          } else {
            item.options = item.oriOptions.filter(option => option.parentId === supItem.initval)
          }
@@ -93,14 +220,6 @@
      return group
    })
    if (error) {
      notification.warning({
        top: 92,
        message: '关联菜单设置错误!',
        duration: 10
      })
    }
    
    this.setState({
      readtype: readtype,
@@ -127,6 +246,7 @@
    
            subfields.push(item)
          }
          return item
        })
        return group
@@ -209,13 +329,15 @@
    let cols = 2
    if (this.props.setting && this.props.setting.cols) {
      cols = parseInt(this.props.setting.cols)
      if (cols > 3 || cols < 1) {
      if (cols > 4 || cols < 1) {
        cols = 2
      }
    }
    formlist.forEach((item, index) => {
      if (item.hidden === 'true') return
      if (item.supField && !item.supvalue.includes(this.props.form.getFieldValue(item.supField))) return
      
      if (item.type === 'text') {
        fields.push(
@@ -250,7 +372,7 @@
                initialValue: _initval,
                rules: [
                  {
                    required: item.required === 'true',
                    required: true,
                    message: this.props.dict['form.required.input'] + item.label + '!'
                  }
                ]
@@ -284,6 +406,7 @@
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  onChange={(value, option) => {this.selectChange(item, value, option)}}
                  disabled={item.readonly === 'true'}
                >
                  {item.options.map(option =>
                    <Select.Option id={option.key} data={hasSubField ? option : ''} title={option.Text} key={option.key} value={option.Value}>{option.Text}</Select.Option>
@@ -311,6 +434,7 @@
                  showSearch
                  mode="multiple"
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  disabled={item.readonly === 'true'}
                >
                  {item.options.map(option =>
                    <Select.Option id={option.key} title={option.Text} key={option.key} value={option.Value}>{option.Text}</Select.Option>
@@ -340,7 +464,7 @@
                  }
                ]
              })(
                <DatePicker />
                <DatePicker disabled={item.readonly === 'true'} />
              )}
            </Form.Item>
          </Col>
@@ -365,13 +489,14 @@
                  }
                ]
              })(
                <MonthPicker />
                <MonthPicker disabled={item.readonly === 'true'} />
              )}
            </Form.Item>
          </Col>
        )
      } else if (item.type === 'datetime') {
        let _initval = this.props.data ? this.props.data[item.field] : ''
        if (_initval && this.state.readin[item.field]) {
          _initval = moment(_initval, 'YYYY-MM-DD HH:mm:ss')
        } else {
@@ -390,7 +515,7 @@
                  }
                ]
              })(
                <DatePicker showTime />
                <DatePicker showTime disabled={item.readonly === 'true'} />
              )}
            </Form.Item>
          </Col>
@@ -433,12 +558,20 @@
          </Col>
        )
      } else if (item.type === 'funcvar') {
        fields.push(
          <Col span={24 / cols} key={index}>
            <Form.Item label={item.label}>
              {getFieldDecorator(item.field, {
                initialValue: '系统自动生成',
              })(<Input placeholder="" autoComplete="off" disabled={true} />)}
            </Form.Item>
          </Col>
        )
      } else if (item.type === 'textarea') {
        let _labelcol = cols !== 3 ? 8 / cols : 3
        let _wrapcol = cols !== 3 ? 16 + (cols - 1) * 4 : 21
        let _style = {}
        if (cols === 2) {
        if (cols === 2 || cols === 4) {
          _style.paddingLeft = '7px'
        }
        fields.push(
@@ -456,7 +589,7 @@
                    message: formRule.textarea.message
                  }
                ]
              })(<TextArea autosize={{ minRows: 2, maxRows: 6 }} disabled={item.readonly === 'true'} />)}
              })(<TextArea autosize={{ minRows: 2, maxRows: item.maxRows || 6 }} disabled={item.readonly === 'true'} />)}
            </Form.Item>
          </Col>
        )
@@ -467,7 +600,7 @@
  }
  handleConfirm = () => {
    const { record } = this.state
    const { record, encrypts, intercepts } = this.state
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
@@ -478,19 +611,26 @@
            if (!item.field) return
            if (item.type === 'funcvar') {
              let _val = item.initval
              if (values.hasOwnProperty(item.field)) {
                _val = values[item.field] === '系统自动生成' ? '' : values[item.field]
              } else if (record.hasOwnProperty(item.field)) {
                _val = record[item.field]
              }
              search.push({
                type: 'funcvar',
                readonly: 'true',
                readin: false,
                fieldlen: this.state.fieldlen[item.field],
                key: item.field,
                value: ''
                value: _val
              })
            } else if (item.hidden === 'true' && item.field !== this.props.setting.primaryKey) {
              let _val = item.initval
              if (record.hasOwnProperty(item.field)) {
                _val = record[item.field]
              }
              search.push({
                type: this.state.datatype[item.field],
                readonly: this.state.readtype[item.field],
@@ -499,10 +639,21 @@
                key: item.field,
                value: _val
              })
            } else if (item.supField && !item.supvalue.includes(this.props.form.getFieldValue(item.supField))) {
              search.push({
                type: this.state.datatype[item.field],
                readonly: this.state.readtype[item.field],
                readin: this.state.readin[item.field],
                fieldlen: this.state.fieldlen[item.field],
                key: item.field,
                value: item.initVal
              })
            }
          })
          Object.keys(values).forEach(key => {
            if (this.state.datatype[key] === 'funcvar')  return
            let _value = ''
            if (this.state.datatype[key] === 'datetime') {
              if (values[key]) {
@@ -536,9 +687,12 @@
              }
              _value = vals.join(',')
            } else if (this.state.datatype[key] === 'text') {
              _value = values[key].replace(/(^\s*|\s*$) | \t* | \v*/ig, '')
            } else if (this.state.datatype[key] === 'text' || this.state.datatype[key] === 'textarea') {
              _value = values[key].replace(/\t*|\v*/g, '') // 去除制表符
              if (intercepts.includes(key)) {              // 去除首尾空格
                _value = _value.replace(/(^\s*|\s*$)/g, '')
              }
            } else {
              _value = values[key]
@@ -553,6 +707,22 @@
              value: _value
            })
          })
          if (encrypts && encrypts.length > 0) {
            search = search.map(item => {
              let _value = item.value
              if (encrypts.includes(item.key)) {
                try {
                  _value = window.btoa(window.encodeURIComponent(_value))
                } catch (e) {
                  console.warn(e)
                }
              }
              item.value = _value
              return item
            })
          }
          resolve(search)
        } else {
          reject(err)
@@ -567,7 +737,8 @@
  }
  render() {
    const { groups } = this.props
    const { setting } = this.props
    const { groups } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
@@ -579,16 +750,19 @@
      }
    }
    let _width = (setting && setting.width) || 100
    let keys = groups.map(group => group.uuid)
    return (
      <Form {...formItemLayout}>
      <Form {...formItemLayout} className="form-tab-form-field">
        <Collapse
          defaultActiveKey={keys}
          expandIconPosition='right'
        >
          {groups.map(group =>
            <Panel header={group.label} key={group.uuid}>
              <Row gutter={24}>{this.getFields(group.sublist)}</Row>
              <Row style={{margin: '0 auto', width: _width + '%'}} gutter={24}>{this.getFields(group.sublist)}</Row>
            </Panel>
          )}
        </Collapse>