king
2020-05-14 eb31b84962c192de57abbb473cb4733a09bf4363
src/templates/sharecomponent/settingcomponent/settingform/index.jsx
@@ -1,7 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, Row, Col, Input, Radio, Select, Tooltip, Icon, notification, InputNumber, Modal, Button, Table, Popconfirm } from 'antd'
import { Form, Row, Col, Input, Radio, Select, Tooltip, Icon, notification, InputNumber, Modal, Table, Popconfirm, Typography } from 'antd'
import moment from 'moment'
import Api from '@/api'
@@ -11,6 +11,7 @@
const { TextArea } = Input
const { confirm } = Modal
const { Paragraph } = Typography
class SettingForm extends Component {
  static propTpyes = {
@@ -34,7 +35,10 @@
      {
        title: 'SQL',
        dataIndex: 'sql',
        width: '60%'
        width: '60%',
        render: (text) => (
          <Paragraph copyable ellipsis={{ rows: 5, expandable: true }}>{text}</Paragraph>
        )
      },
      {
        title: '初始化sql',
@@ -164,7 +168,7 @@
  }
  handleConfirm = (otype) => {
    const { menu, config, type } = this.props
    const { menu, type } = this.props
    const { view, setting } = this.state
    // 表单提交时检查输入值是否正确
@@ -180,15 +184,15 @@
            values = {...setting, ...values}
            // 数据源前端验证
            if (values.interType === 'inner' && !values.innerFunc && !values.dataresource) {
            if (values.interType === 'inner' && !values.innerFunc && values.default !== 'false' && !values.dataresource) {
              notification.warning({
                top: 92,
                message: '请自定义函数或填写数据源!',
                message: '请填写内部函数或数据源!',
                duration: 5
              })
              reject()
              return
            } else {
            } else if (values.interType === 'inner') {
              let error = Utils.verifySql(values.dataresource)
  
              if (error) {
@@ -197,6 +201,7 @@
                  message: '数据源中不可使用' + error,
                  duration: 5
                })
                reject()
                return
              }
            }
@@ -220,85 +225,14 @@
              Api.getLocalConfig(param)
            }
            // 合成自定义script
            let _customScript = ''
            values.scripts.forEach(item => {
              if (item.status === 'false' || item.initsql !== 'true') return
              _customScript += `
                ${item.sql}
              `
            })
            values.scripts.forEach(item => {
              if (item.status === 'false' || item.initsql === 'true') return
              _customScript += `
                ${item.sql}
              `
            })
            values.customScript = _customScript
            // 数据源后台语法验证
            if (values.interType === 'inner' && !values.innerFunc && /\s/.test(values.dataresource)) {
              if (otype === 'change') { // 切换自定义设置
                this.setState({
                  sqlVerifing: true
                })
              }
              let _dataresource = values.dataresource
              if (values.queryType === 'statistics') {
                let allSearch = Utils.initMainSearch(config.search)
                allSearch = Utils.getAllSearchOptions(allSearch)
                let options = allSearch.map(item => {
                  return {
                    reg: new RegExp('@' + item.key + '@', 'ig'),
                    value: item.value
                  }
                })
                options.forEach(item => {
                  _dataresource = _dataresource.replace(item.reg, `'${item.value}'`)
                })
              }
              let param = {
                func: 's_debug_sql',
                LText: _dataresource
              }
              param.LText = Utils.formatOptions(param.LText)
              param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
              param.secretkey = Utils.encrypt(param.LText, param.timestamp)
              Api.getLocalConfig(param).then(result => {
                if (result.status) {
                  if (otype === 'change') {
                    this.setState({
                      sqlVerifing: false,
                      setting: values
                    }, () => {
                      resolve()
                    })
                  } else {
                    resolve(values)
                  }
                } else {
                  this.setState({sqlVerifing: false})
                  Modal.error({
                    title: result.message
                  })
                }
              })
            } else if (otype === 'change') {
            if (otype === 'change') {
              this.setState({
                setting: values,
              }, () => {
                resolve()
              })
            } else {
              resolve(values)
              this.sqlverify(values, resolve, reject)
            }
          } else {
            reject(err)
@@ -328,6 +262,7 @@
      })
      _setting.customScript = _customScript
      let _this = this
      return new Promise((resolve, reject) => {
        if (_loading) {
@@ -336,16 +271,87 @@
            okText: this.props.dict['model.confirm'],
            cancelText: this.props.dict['header.cancel'],
            onOk() {
              resolve(_setting)
              _this.sqlverify(_setting, resolve, reject)
            },
            onCancel() {
              reject()
            }
          })
        } else {
          resolve(_setting)
          this.sqlverify(_setting, resolve, reject)
        }
      })
    }
  }
  sqlverify = (_setting, _resolve, _reject) => {
    const { config } = this.props
    if (_setting.interType === 'inner' && !_setting.innerFunc && _setting.default !== 'false' && /\s/.test(_setting.dataresource)) {
      let _dataresource = _setting.dataresource
      let _customScript = _setting.customScript
      let allSearch = Utils.initMainSearch(config.search)
      allSearch = Utils.getAllSearchOptions(allSearch)
      let regoptions = allSearch.map(item => {
        return {
          reg: new RegExp('@' + item.key + '@', 'ig'),
          value: `'${item.value}'`
        }
      })
      if (_setting.queryType === 'statistics') {
        regoptions.forEach(item => {
          _dataresource = _dataresource.replace(item.reg, item.value)
        })
      }
      if (_customScript) {
        regoptions.push({
          reg: new RegExp('@orderBy@', 'ig'),
          value: _setting.order
        })
        if (_setting.laypage !== 'false') {
          regoptions.push({
            reg: new RegExp('@pageSize@', 'ig'),
            value: 10
          }, {
            reg: new RegExp('@pageIndex@', 'ig'),
            value: 1
          })
        }
        regoptions.forEach(item => {
          _customScript = _customScript.replace(item.reg, item.value)
        })
        _dataresource = `${_customScript}
          ${_dataresource}
        `
      }
      _dataresource = _dataresource.replace(/@\$|\$@/ig, '')
      let param = {
        func: 's_debug_sql',
        LText: _dataresource
      }
      param.LText = Utils.formatOptions(param.LText)
      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
      Api.getLocalConfig(param).then(result => {
        if (result.status) {
          _resolve(_setting)
        } else {
          _reject()
          Modal.error({
            title: result.message
          })
        }
      })
    } else {
      _resolve(_setting)
    }
  }
@@ -445,6 +451,7 @@
  changeView = () => {
    const { view } = this.state
    let _this = this
    if (view === 'normal') {
      this.handleConfirm('change').then(() => {
@@ -509,12 +516,9 @@
        })
        this.scrolltop()
      }, () => {
        this.setState({sqlVerifing: false})
      })
    } else {
      let _loading = false
      let _this = this
      if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) {
        _loading = true
@@ -535,11 +539,11 @@
          onCancel() {}
        })
      } else {
        this.setState({
        _this.setState({
          view: 'normal'
        })
        this.scrolltop()
        _this.scrolltop()
      }
    }
  }
@@ -856,7 +860,7 @@
        <Form {...formItemLayout} className="model-table-setting-form" id="model-table-setting-form">
          {view !=='custom' ? <Row gutter={24}>{this.getFields(formlist)}</Row> : null}
          <Row gutter={24}>
            {view !=='custom' ? <Button loading={this.state.sqlVerifing} onClick={this.changeView} style={{border: 0, boxShadow: 'unset',float: 'right', color: '#1890ff', marginRight: 12, cursor: 'pointer'}}>自定义设置<Icon style={{marginLeft: 5}} type="right" /></Button> : null}
            {view !=='custom' ? <span onClick={this.changeView} style={{float: 'right', color: '#1890ff', marginRight: 12, marginTop: 15, cursor: 'pointer'}}>自定义设置<Icon style={{marginLeft: 5}} type="right" /></span> : null}
            {view ==='custom' ? <span onClick={this.changeView} style={{float: 'left', color: '#1890ff', marginLeft: 12, marginTop: 15, cursor: 'pointer'}}><Icon style={{marginRight: 5}} type="left" />基础设置</span> : null}
          </Row>
        </Form>