king
2024-01-15 9d3c77a83bc4e7febbfb8fd05de2f90100c5af6c
src/tabviews/zshare/mutilform/index.jsx
@@ -28,6 +28,7 @@
const MkFormula = asyncComponent(() => import('./mkFormula'))
const MkCascader = asyncComponent(() => import('./mkCascader'))
const MkVercode = asyncComponent(() => import('./mkVercode'))
const MKPopSelect = asyncComponent(() => import('./mkPopSelect'))
const MKEditor = asyncComponent(() => import('@/components/editor'))
class MutilFormComponent extends Component {
@@ -45,10 +46,13 @@
    ID: '',
    send_type: '',
    timestamp: '',
    n_id: ''
    n_id: '',
    focusId: '',
    reFocusId: ''
  }
  record = {}
  submitId = ''
  componentDidMount () {
    const { action, unload } = this.props
@@ -125,7 +129,7 @@
        item.precision = 'second'
      }
      if (!item.field || !['text', 'number', 'switch', 'check', 'rate', 'select', 'link', 'cascader', 'linkMain', 'funcvar', 'date', 'datemonth', 'radio', 'checkbox', 'checkcard', 'fileupload', 'textarea', 'multiselect', 'brafteditor', 'color', 'vercode'].includes(item.type)) return false
      if (!item.field || !['text', 'number', 'switch', 'check', 'rate', 'select', 'popSelect', 'link', 'cascader', 'linkMain', 'funcvar', 'date', 'datemonth', 'radio', 'checkbox', 'checkcard', 'fileupload', 'textarea', 'multiselect', 'brafteditor', 'color', 'vercode'].includes(item.type)) return false
      if (/^\s+$/.test(item.label)) {
        item.style = item.style || {}
@@ -267,7 +271,7 @@
      }
      if (item.type === 'number') {
        if (isNaN(item.initval)) {
        if (isNaN(item.initval) || item.initval === '') {
          item.initval = 0
        }
      } else if (['select', 'link', 'radio', 'checkbox', 'checkcard', 'multiselect', 'cascader'].includes(item.type) && item.resourceType === '1') {
@@ -292,26 +296,31 @@
        }
      }
      item.rules = []
      if (item.type === 'text') {
        item.initval = item.initval + ''
        let _rules = [{
          pattern: /^[^']*$/ig,
          message: '不可使用英文状态的单引号!'
        }, {
        if (item.required === 'true') {
          item.rules.push({
            required: true,
            message: item.label + '不可为空!'
          })
        }
        item.rules.push({
          validator: (rule, value, callback) => {
            if (/--/ig.test(value)) {
              callback('不可使用 -- !')
            if (/'/.test(value)) {
              callback('不可使用英文状态的单引号!')
            // } else if (/--/.test(value)) {
            //   callback('不可使用 -- !')
            } else {
              callback()
            }
          }
        }, {
          required: item.required === 'true',
          message: item.label + '不可为空!'
        }]
        })
        if (!item.lenControl || item.lenControl === 'limit') {
          _rules.push({
          item.rules.push({
            max: item.fieldlength,
            message: formRule.input.formMessage.replace('@max', item.fieldlength)
          })
@@ -323,7 +332,7 @@
            if (item.regularExtra) {
              reg = new RegExp('^[0-9.-' + item.regularExtra.replace(/\.|-/g, '') + ']*$')
            }
            _rules.push({
            item.rules.push({
              pattern: reg,
              message: item.regularText || formRule.input.numbermsg
            })
@@ -332,7 +341,7 @@
            if (item.regularExtra) {
              reg = new RegExp('^[a-zA-Z' + item.regularExtra + ']*$')
            }
            _rules.push({
            item.rules.push({
              pattern: reg,
              message: item.regularText || formRule.input.lettermsg
            })
@@ -341,7 +350,7 @@
            if (item.regularExtra) {
              reg = new RegExp('^[a-zA-Z0-9' + item.regularExtra + ']*$')
            }
            _rules.push({
            item.rules.push({
              pattern: reg,
              message: item.regularText || '请输入数字或字母'
            })
@@ -350,80 +359,113 @@
            if (item.regularExtra) {
              reg = new RegExp('^[a-zA-Z0-9@_.' + item.regularExtra.replace(/\.|_|@/g, '') + ']*$')
            }
            _rules.push({
            item.rules.push({
              pattern: reg,
              message: item.regularText || '请输入数字、字母以及@_.'
            })
          } else if (item.regular === 'phone') {
            _rules.push({
            item.rules.push({
              pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
              message: item.regularText || '请正确输入手机号'
            })
          } else if (item.regular === 'email') {
            _rules.push({
            item.rules.push({
              pattern: /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/,
              message: item.regularText || '请正确输入邮箱地址'
            })
          }
        }
        item.rules = _rules
      } else if (item.type === 'number') {
        item.rules = [{
          required: true,
          message: item.label + '不可为空!'
        }, {
          validator: (rule, value, callback) => this.handleConfirmPassword(rule, value, callback, item)
        }]
      } else if (item.type === 'textarea') {
        let _rules = [
          {
            required: item.required === 'true',
            message: item.label + '不可为空!'
          },
          {
            max: item.fieldlength,
            message: formRule.input.formMessage.replace('@max', item.fieldlength)
          },
          {
            pattern: /^[^']*$/ig,
            message: '不可使用英文状态的单引号!'
          }, {
        if (typeof(item.min) === 'number' || typeof(item.max) === 'number') {
          item.rules.push({
            validator: (rule, value, callback) => {
              if (/--/ig.test(value)) {
                callback('不可使用 -- !')
              if (isNaN(value)) {
                callback()
              } else if (typeof(item.min) === 'number' && value < item.min) {
                if (item.min < 1e-6) {
                  if (item.min === 1e-6) {
                    callback(item.label + '最小值为 0.000001')
                  } else if (item.min === 1e-7) {
                    callback(item.label + '最小值为 0.0000001')
                  } else if (item.min === 1e-8) {
                    callback(item.label + '最小值为 0.00000001')
                  } else if (item.min === 1e-9) {
                    callback(item.label + '最小值为 0.000000001')
                  } else if (item.min === 1e-10) {
                    callback(item.label + '最小值为 0.0000000001')
                  } else if (item.min === 1e-11) {
                    callback(item.label + '最小值为 0.00000000001')
                  } else {
                    callback(item.label + '最小值为 ' + item.min)
                  }
                } else {
                  callback(item.label + '最小值为 ' + item.min)
                }
              } else if (typeof(item.max) === 'number' && value > item.max) {
                callback(item.label + '最大值为 ' + item.max)
              } else {
                callback()
              }
            }
          })
        }
      } else if (item.type === 'textarea') {
        if (item.required === 'true') {
          item.rules.push({
            required: true,
            message: item.label + '不可为空!'
          })
        }
        item.rules.push({
          validator: (rule, value, callback) => {
            if (/'/.test(value)) {
              callback('不可使用英文状态的单引号!')
            // } else if (/--/.test(value)) {
            //   callback('不可使用 -- !')
            } else {
              callback()
            }
          }
        ]
        item.rules = _rules
        }, {
          max: item.fieldlength,
          message: formRule.input.formMessage.replace('@max', item.fieldlength)
        })
      } else if (item.type === 'brafteditor') {
        item.rules = [
          {
            required: item.required === 'true',
        if (item.required === 'true') {
          item.rules.push({
            required: true,
            message: item.label + '不可为空!'
          },
          {
            max: item.fieldlength,
            message: formRule.input.formMessage.replace('@max', item.fieldlength)
          }
        ]
          })
        }
        item.rules.push({
          max: item.fieldlength,
          message: formRule.input.formMessage.replace('@max', item.fieldlength)
        })
      } else if (item.type === 'linkMain' || item.type === 'vercode') {
        item.rules = [
          {
            required: item.required === 'true',
        if (item.required === 'true') {
          item.rules.push({
            required: true,
            message: item.label + '不可为空!'
          }
        ]
          })
        }
      } else {
        item.rules = [
          {
            required: item.required === 'true',
        if (item.required === 'true') {
          item.rules.push({
            required: true,
            message: '请选择' + item.label + '!'
          }
        ]
          })
        }
      }
      if (item.rules.length === 0) {
        item.rules = null
      }
      fieldMap.set(item.field, item)
@@ -541,6 +583,18 @@
              })
            }
          })
        } else if (item.type === 'popSelect') {
          item.options = []
          item.subFields = []
          item.linkSubField.forEach(m => {
            let n = fieldMap.get(m)
            if (n && ['text', 'number', 'textarea'].includes(n.type)) {
              item.subFields.push({
                uuid: n.uuid,
                field: m
              })
            }
          })
        }
        item.linkSubField = null
@@ -572,17 +626,19 @@
      }
      record[item.field] = item.initval
      item.orgval = item.initval
      if (linkFields[item.field]) {
        item.linkFields = linkFields[item.field]
      }
      
      if (item.enter === 'tab' || item.enter === 'sub') {
        if (fieldMap.has(item.tabField)) {
        item.tabUuid = ''
        if (item.tabField && fieldMap.has(item.tabField)) {
          item.tabUuid = fieldMap.get(item.tabField).uuid
        } else if (item.enter === 'tab') {
          item.enter = 'false'
        } else if (item.enter === 'sub') {
        } else if (item.enter === 'sub' && ['text', 'number'].includes(item.type)) {
          item.tabUuid = item.uuid
        }
      }
@@ -619,17 +675,30 @@
    this.record = record
    let ID = this.props.data ? this.props.data.$$uuid || '' : ''
    let focusItem = null
    this.setState({ formlist, ID }, () => {
    if (action.setting.focus && fieldMap.has(action.setting.focus)) {
      focusItem = fieldMap.get(action.setting.focus)
    }
    let reFocusItem = null
    if (action.setting.refocus && fieldMap.has(action.setting.refocus)) {
      reFocusItem = fieldMap.get(action.setting.refocus)
    }
    this.setState({
      formlist,
      ID,
      focusId: focusItem ? focusItem.uuid : '',
      reFocusId: reFocusItem ? reFocusItem.uuid : ''
    }, () => {
      if (unload) return
      
      if (action.setting && action.setting.focus && fieldMap.has(action.setting.focus)) {
        let focusItem = fieldMap.get(action.setting.focus)
      if (focusItem) {
        if (focusItem.type === 'text' || focusItem.type === 'number') {
          setTimeout(() => {
            MKEmitter.emit('mkFC', 'focus', focusItem.uuid)
          }, 50)
          }, 20)
        } else {
          setTimeout(() => {
            MKEmitter.emit('mkFC', 'focus', focusItem.uuid)
@@ -651,6 +720,68 @@
        }
      }
    })
    if (action.subButton && action.subButton.resetForms) {
      MKEmitter.addListener('resetForms', this.resetForms)
    }
  }
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('resetForms', this.resetForms)
  }
  resetForms = (id, data) => {
    const { action } = this.props
    const { focusId, reFocusId } = this.state
    if (id !== action.uuid) return
    let formlist = fromJS(this.state.formlist).toJS()
    let resetForms = action.subButton.resetForms || []
    formlist = formlist.map(item => {
      if (item.type !== 'text' && item.type !== 'number') return item
      if (resetForms.includes(item.field)) {
        let key = item.field.toLowerCase()
        if (typeof(data[key]) !== 'undefined') {
          item.initval = data[key]
          this.record[item.field] = data[key]
        } else {
          item.initval = item.orgval
          this.record[item.field] = item.orgval
        }
      }
      return item
    })
    let _list = fromJS(formlist).toJS().map(item => {
      if (item.type !== 'text' && item.type !== 'number') return item
      if (resetForms.includes(item.field) && !item.hidden) {
        item.hidden = true
      }
      return item
    })
    this.setState({
      formlist: _list
    }, () => {
      this.setState({
        formlist
      })
    })
    if (reFocusId || focusId || this.submitId) {
      setTimeout(() => {
        MKEmitter.emit('mkFC', 'focus', this.submitId || reFocusId || focusId)
      }, 20)
    }
  }
  getFormData = (deForms) => {
@@ -1002,22 +1133,6 @@
    })
  }
  handleConfirmPassword = (rule, value, callback, item) => {
    let val = parseFloat(value)
    if (!isNaN(val)) {
      if (typeof(item.min) === 'number' && val < item.min) {
        callback(item.label + '最小值为 ' + item.min)
      } else if (typeof(item.max) === 'number' && val > item.max) {
        callback(item.label + '最大值为 ' + item.max)
      } else {
        callback()
      }
    } else {
      callback()
    }
  }
  recordChange = (values, item) => {
    this.record = {...this.record, ...values}
@@ -1124,6 +1239,8 @@
          content = (<MKNumberInput config={item} onChange={(val, defer) => !defer && this.recordChange({[item.field]: val})} onSubmit={this.props.inputSubmit} />)
        } else if (item.type === 'select' || item.type === 'link' || item.type === 'multiselect') {
          content = (<MKSelect config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)} onSubmit={this.props.inputSubmit} />)
        } else if (item.type === 'popSelect') {
          content = (<MKPopSelect config={item} BID={this.props.BID} ID={this.state.ID} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)} onSubmit={this.props.inputSubmit} />)
        } else if (item.type === 'cascader') {
          content = (<MkCascader config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)}/>)
        } else if (item.type === 'color') {
@@ -1132,9 +1249,9 @@
          className = 'checkcard'
          content = (<MKCheckCard config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)}/>)
        } else if (item.type === 'switch') {
          content = (<MKSwitch config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)}/>)
          content = (<MKSwitch config={item} onChange={(val, other) => this.recordChange({[item.field]: val, ...other}, item)} onSubmit={this.props.inputSubmit}/>)
        } else if (item.type === 'check') {
          content = (<MKCheck config={item} onChange={(val) => this.recordChange({[item.field]: val}, item)}/>)
          content = (<MKCheck config={item} onChange={(val) => this.recordChange({[item.field]: val}, item)} onSubmit={this.props.inputSubmit}/>)
        } else if (item.type === 'checkbox') {
          content = (<MKCheckbox config={item} onChange={(val) => this.recordChange({[item.field]: val})}/>)
        } else if (item.type === 'radio') {
@@ -1144,7 +1261,7 @@
        } else if (item.type === 'fileupload') {
          className = item.readonly ? 'readonly' : ''
          className += item.fileType === 'picture-card' ? ' file-upload' : ''
          content = (<MKFileUpload config={item} onChange={(val, other = {}) => this.recordChange({[item.field]: val, ...other})} />)
          content = (<MKFileUpload config={item} data={this.record} onChange={(val, other = {}) => this.recordChange({[item.field]: val, ...other})} />)
        } else if (item.type === 'textarea') {
          content = (<MKTextArea config={item} onChange={(val, defer) => !defer && this.recordChange({[item.field]: val})}/>)
        } else if (item.type === 'rate') {
@@ -1181,7 +1298,7 @@
    return fields
  }
  handleConfirm = () => {
  handleConfirm = (formId) => {
    const { formlist, send_type, timestamp, n_id } = this.state
    // 表单提交时检查输入值是否正确
@@ -1279,6 +1396,8 @@
          forms.push(_item)
        })
        this.submitId = formId || ''
        resolve(forms)
      })
    })