king
2021-06-02 e543372cc70a19ff2630c79d8421c2c593e54e5f
src/templates/sharecomponent/settingcomponent/settingform/simplescript/index.jsx
@@ -1,11 +1,12 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, Row, Col, Icon, Button, notification, Select, Popconfirm, Typography } from 'antd'
import { Form, Row, Col, Icon, Button, notification, Select, Popconfirm, Typography, Modal, Radio } from 'antd'
import moment from 'moment'
import Utils from '@/utils/utils.js'
import Api from '@/api'
import SettingUtils from '../utils'
import CodeMirror from '@/templates/zshare/codemirror'
import asyncComponent from '@/utils/asyncComponent'
import './index.scss'
@@ -19,7 +20,8 @@
    setting: PropTypes.object,      // 设置
    scripts: PropTypes.array,       // 自定义脚本列表
    searches: PropTypes.array,      // 搜索条件
    defaultSql: PropTypes.string,   // 默认sql
    urlFields: PropTypes.any,       // url变量
    regoptions: PropTypes.any,      // 正则替换
    scriptsChange: PropTypes.func,  // 自定义脚本切换时验证
    scriptsUpdate: PropTypes.func   // 表单
  }
@@ -33,7 +35,7 @@
      {
        title: 'SQL',
        dataIndex: 'sql',
        width: '73%',
        width: '60%',
        render: (text) => {
          let title = text.match(/^\s*\/\*.+\*\//)
          title = title && title[0] ? title[0] : ''
@@ -45,6 +47,18 @@
              <Paragraph copyable ellipsis={{ rows: 4, expandable: true }}>{text}</Paragraph>
            </div>
          )
        }
      },
      {
        title: '执行位置',
        dataIndex: 'position',
        width: '13%',
        render: (text, record) => {
          if (record.position === 'front') {
            return 'sql前'
          } else {
            return 'sql后'
          }
        }
      },
      {
@@ -87,30 +101,49 @@
  }
  UNSAFE_componentWillMount() {
    const { searches, scripts } = this.props
    const { searches, scripts, urlFields } = this.props
    let _usefulFields = []
    searches.forEach(item => {
      if (!item.field) return
      if (item.type === 'group') {
        if (item.transfer === 'true') {
    let scriptsColumns = fromJS(this.state.scriptsColumns).toJS()
    if (searches) {
      searches.forEach(item => {
        if (!item.field) return
        if (item.type === 'group') {
          if (item.transfer === 'true') {
            _usefulFields.push(item.field)
          }
          _usefulFields.push(item.datefield)
          _usefulFields.push(item.datefield + '1')
        } else if (['dateweek', 'datemonth', 'daterange'].includes(item.type)) {
          _usefulFields.push(item.field)
          _usefulFields.push(item.field + '1')
        } else if (_usefulFields.includes(item.field)) {
          _usefulFields.push(item.field + '1')
        } else {
          _usefulFields.push(item.field)
        }
        _usefulFields.push(item.datefield)
        _usefulFields.push(item.datefield + '1')
      } else if (['dateweek', 'datemonth', 'daterange'].includes(item.type)) {
        _usefulFields.push(item.field)
        _usefulFields.push(item.field + '1')
      } else if (_usefulFields.includes(item.field)) {
        _usefulFields.push(item.field + '1')
      } else {
        _usefulFields.push(item.field)
      })
      if (urlFields) {
        _usefulFields.push(...urlFields)
      }
    })
      _usefulFields = _usefulFields.join(', ')
      scriptsColumns = scriptsColumns.filter(item => {
        if (item.dataIndex === 'sql') {
          item.width = '70%'
        }
        return item.dataIndex !== 'position'
      })
    } else {
      _usefulFields = null
    }
    this.setState({
      usefulFields: _usefulFields.join(', '),
      scripts: fromJS(scripts).toJS()
      usefulFields: _usefulFields,
      scripts: fromJS(scripts).toJS(),
      scriptsColumns
    })
  }
@@ -140,7 +173,6 @@
          let _item = {
            name: item.funcname,
            value: window.decodeURIComponent(window.atob(item.longparam))
            // value: Utils.UnformatOptions(item.longparam)
          }
          return _item
        })
@@ -190,7 +222,11 @@
    let values = {
      uuid: editItem && editItem.uuid ? editItem.uuid : Utils.getuuid(),
      sql: _sql
      sql: _sql,
    }
    if (this.props.form.getFieldValue('position')) {
      values.position = this.props.form.getFieldValue('position')
    }
    let _quot = values.sql.match(/'{1}/g)
@@ -249,24 +285,39 @@
      _scripts.push(values)
    }
    let param = {
      func: 's_debug_sql',
      exec_type: 'y',
      LText: SettingUtils.getCustomDebugSql(_scripts, this.props.regoptions)
    }
    param.LText = Utils.formatOptions(param.LText)
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt('', param.timestamp)
    this.setState({loading: true})
    this.props.scriptsChange(_scripts).then(res => {
      this.setState({
        loading: false,
        scripts: _scripts,
        editItem: null
      })
      this.props.scriptsUpdate(_scripts)
      this.props.form.setFieldsValue({
        sql: ''
      })
    }, () => {
      this.setState({loading: false})
    Api.getLocalConfig(param).then(result => {
      if (result.status) {
        this.setState({
          loading: false,
          scripts: _scripts,
          editItem: null
        })
        this.props.scriptsUpdate(_scripts)
        this.props.form.setFieldsValue({
          sql: ''
        })
      } else {
        this.setState({loading: false})
        Modal.error({
          title: result.message
        })
      }
    })
  }
  selectScript = (value, option) => {
    if (!value || !option) return
    let _sql = this.props.form.getFieldValue('sql')
    if (_sql) {
      _sql = _sql + ` 
@@ -286,13 +337,21 @@
  }
  handleEdit = (record) => {
    const { usefulFields } = this.state
    this.setState({
      editItem: record
    })
    this.props.form.setFieldsValue({
      sql: record.sql
    })
    if (usefulFields) {
      this.props.form.setFieldsValue({
        sql: record.sql
      })
    } else {
      this.props.form.setFieldsValue({
        sql: record.sql,
        position: record.position || 'back'
      })
    }
    this.scrolltop()
  }
@@ -344,7 +403,7 @@
  }
  render() {
    const { setting, defaultSql, scripts } = this.props
    const { setting, scripts } = this.props
    const { getFieldDecorator } = this.props.form
    const { usefulFields, scriptsColumns, systemScripts } = this.state
    const formItemLayout = {
@@ -362,29 +421,47 @@
      <div className="modal-menu-setting-script">
        <Form {...formItemLayout}>
          <Row gutter={24}>
            {setting.tableName ? <Col span={8}>
              <Form.Item label={'表名'} style={{whiteSpace: 'nowrap', margin: 0}}>
                {setting.tableName}
            <Col span={4}>
              <Form.Item labelCol={{span: 17}} wrapperCol={{span: 7}} label={'回调表名'} style={{whiteSpace: 'nowrap', margin: 0}}>
                {setting.cbTable}
              </Form.Item>
            </Col>
            <Col span={20}>
              <Form.Item labelCol={{span: 4}} wrapperCol={{span: 20}} label={'报错字段'} style={{margin: 0}}>
                ErrorCode(增加后缀NT表示数据不回滚,如ENT、NNT、FNT、NMNT), retmsg
              </Form.Item>
            </Col>
            {usefulFields ? <Col span={24} className="sqlfield">
              <Form.Item label={'可用字段'}>
                id, bid, loginuid, sessionuid, userid, username, fullname, login_city, appkey, time_id{usefulFields ? ', ' + usefulFields : ''}
              </Form.Item>
            </Col> : null}
            <Col span={16}>
              <Form.Item label={'报错字段'} style={{margin: 0}}>
                ErrorCode, retmsg
              </Form.Item>
            </Col>
            <Col span={24} className="sqlfield">
            {!usefulFields ? <Col span={24} className="sqlfield">
              <Form.Item label={'可用字段'}>
                id, bid, loginuid, sessionuid, userid, username, fullname, appkey, time_id, orderBy{setting.laypage === 'true' ? ', pageSize, pageIndex': ''}{usefulFields ? ', ' + usefulFields : ''}
                id, bid, loginuid, sessionuid, userid, username, fullname, login_city, appkey, time_id
              </Form.Item>
            </Col>
            </Col> : null}
            {!usefulFields ? <Col span={8} style={{whiteSpace: 'nowrap'}}>
              <Form.Item style={{marginBottom: 0}} label="执行位置">
                {getFieldDecorator('position', {
                  initialValue: 'front'
                })(
                  <Radio.Group>
                    <Radio value="front">sql前</Radio>
                    <Radio value="back">sql后</Radio>
                  </Radio.Group>
                )}
              </Form.Item>
            </Col> : null}
            <Col span={10} className="quick-add">
              <Form.Item label={'快捷添加'} style={{marginBottom: 0}}>
                <Select
                  allowClear
                  showSearch
                  filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                  onChange={this.selectScript}
                >
                  <Select.Option key="default" value={defaultSql}>默认sql</Select.Option>
                  {!usefulFields ? <Select.Option key="default" value={`declare @${setting.cbTable} table (mk_api_key nvarchar(100),mk_level nvarchar(10),mk_id nvarchar(50),mk_bid nvarchar(50))\n/*@${setting.cbTable}_data table (mk_level nvarchar(10),mk_id nvarchar(50),mk_bid nvarchar(50))*/`}>默认sql</Select.Option> : null}
                  {systemScripts.map((option, i) =>
                    <Select.Option style={{whiteSpace: 'normal'}} key={i} value={option.value}>{option.name}</Select.Option>
                  )}