king
2024-12-05 4a356e81b1a456f0cb16f61f548c46171c26c1b6
src/templates/zshare/verifycard/customform/index.jsx
@@ -1,24 +1,23 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Row, Col, Input, Select, Button, notification, Modal, Tooltip } from 'antd'
import { Form, Row, Col, Input, Select, Button, notification, Tooltip, Switch } from 'antd'
import Api from '@/api'
import { checkSQL } from '@/utils/utils-custom.js'
import CodeMirror from '@/templates/zshare/codemirror'
// import './index.scss'
class CustomForm extends Component {
  static propTpyes = {
    btn: PropTypes.object,          // 按钮
    btn: PropTypes.object,
    formfields: PropTypes.string,
    colfields: PropTypes.string,
    initsql: PropTypes.string,      // 可用字段
    customChange: PropTypes.func    // 表单
    customChange: PropTypes.func
  }
  state = {
    editItem: null,
    loading: false
    loading: false,
    skip: false
  }
  edit = (record) => {
@@ -40,16 +39,17 @@
    })
    this.props.form.setFieldsValue({
      sql: ' ',
      sql: '',
      errmsg: ''
    })
  }
  handleConfirm = () => {
    // 表单提交时检查输入值是否正确
    const { skip, editItem } = this.state
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (/^[\s\n]+$/.test(values.sql)) {
        if (!values.sql || /^[\s\n]+$/.test(values.sql)) {
          notification.warning({
            top: 92,
            message: '请输入sql!',
@@ -58,55 +58,41 @@
          return
        }
        
        values.uuid = this.state.editItem ? this.state.editItem.uuid : ''
        values.uuid = editItem ? editItem.uuid : ''
        values.resultName = values.result === 'false' ? '不存在' : '存在'
        let pass = checkSQL(values.sql, 'customscript')
        if (!pass) return
        if (!pass && !skip) return
        let sql = `${this.props.initsql}
          /* 自定义验证 */
          select @tbid='', @ErrorCode='',@retmsg=''
          select top 1 @tbid='X' from (${values.sql}) a
          If @tbid = ''
          Begin
            select @ErrorCode='${values.errorCode}',@retmsg='${values.errmsg}'
            goto aaa
          end
          aaa: select @ErrorCode as ErrorCode,@retmsg as retmsg`
        if (skip) {
          this.setState({
            skip: false,
            editItem: null
          })
          this.props.form.setFieldsValue({
            sql: '',
            errmsg: ''
          })
          this.props.customChange(values)
        } else {
          this.setState({loading: true})
        // if (window.GLOB.funcs && window.GLOB.funcs.length > 0) {
        //   window.GLOB.funcs.forEach(item => {
        //     let reg = new RegExp('\\$ex@' + item.func_code + '@ex\\$', 'ig')
        //     sql = sql.replace(reg, `/*$ex@${item.func_code}-begin*/\n${item.key_sql}\n/*@ex$-end*/`)
        //   })
        // }
        // 数据权限
        sql = sql.replace(/@\$|\$@/ig, '')
        this.setState({loading: true})
        Api.sDebug(sql).then(res => {
          if (res.status || res.ErrCode === '-2') {
          this.props.customChange(values, () => {
            this.setState({
              loading: false,
              editItem: null
            }, () => {
              this.props.customChange(values)
            })
            this.props.form.setFieldsValue({
              sql: ' ',
              sql: '',
              errmsg: ''
            })
          } else {
            this.setState({loading: false})
            Modal.error({
              title: res.message
          }, () => {
            this.setState({
              loading: false
            })
          }
        })
          })
        }
      }
    })
  }
@@ -114,6 +100,7 @@
  render() {
    const { formfields, colfields, btn } = this.props
    const { getFieldDecorator } = this.props.form
    const { skip } = this.state
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
@@ -138,15 +125,9 @@
            </Form.Item>
          </Col>
          <Col span={21} className="sql">
            <Form.Item label="sql">
            <Form.Item label="sql" required>
              {getFieldDecorator('sql', {
                initialValue: '',
                rules: [
                  {
                    required: true,
                    message: '请输入sql!'
                  }
                ]
                initialValue: ''
              })(<CodeMirror />)}
            </Form.Item>
          </Col>
@@ -161,9 +142,13 @@
            {btn.sql ? <div style={{marginTop: '31px'}}>
              表名:  <div style={{wordBreak: 'break-all'}}>{btn.sql}</div>
            </div> : null}
            <div style={{paddingTop: '20px', fontSize: '12px'}}>
              强制保存:
              <Switch checked={skip} size="small" onChange={() => this.setState({skip: !skip})}/>
            </div>
          </Col>
          <Col span={7}>
            <Form.Item label={'结果处理'}>
            <Form.Item label="结果处理">
              {getFieldDecorator('result', {
                initialValue: 'true',
                rules: [
@@ -198,7 +183,7 @@
            </Form.Item>
          </Col>
          <Col span={7}>
            <Form.Item label={'报错编码'}>
            <Form.Item label="报错编码">
              {getFieldDecorator('errorCode', {
                initialValue: 'E',
                rules: [
@@ -219,7 +204,6 @@
              )}
            </Form.Item>
          </Col>
        </Row>
      </Form>
    )