| | |
| | | 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' |
| | |
| | | |
| | | const { TextArea } = Input |
| | | const { confirm } = Modal |
| | | const { Paragraph } = Typography |
| | | |
| | | class SettingForm extends Component { |
| | | static propTpyes = { |
| | |
| | | { |
| | | title: 'SQL', |
| | | dataIndex: 'sql', |
| | | width: '60%' |
| | | width: '60%', |
| | | render: (text) => ( |
| | | <Paragraph copyable ellipsis={{ rows: 5, expandable: true }}>{text}</Paragraph> |
| | | ) |
| | | }, |
| | | { |
| | | title: '初始化sql', |
| | |
| | | } |
| | | |
| | | handleConfirm = (otype) => { |
| | | const { menu, config, type } = this.props |
| | | const { menu, type } = this.props |
| | | const { view, setting } = this.state |
| | | // 表单提交时检查输入值是否正确 |
| | | |
| | |
| | | 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) { |
| | |
| | | message: '数据源中不可使用' + error, |
| | | duration: 5 |
| | | }) |
| | | reject() |
| | | return |
| | | } |
| | | } |
| | |
| | | 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) |
| | |
| | | }) |
| | | |
| | | _setting.customScript = _customScript |
| | | let _this = this |
| | | |
| | | return new Promise((resolve, reject) => { |
| | | if (_loading) { |
| | |
| | | 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) |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | changeView = () => { |
| | | const { view } = this.state |
| | | let _this = this |
| | | |
| | | if (view === 'normal') { |
| | | this.handleConfirm('change').then(() => { |
| | |
| | | }) |
| | | |
| | | this.scrolltop() |
| | | }, () => { |
| | | this.setState({sqlVerifing: false}) |
| | | }) |
| | | } else { |
| | | let _loading = false |
| | | let _this = this |
| | | |
| | | if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) { |
| | | _loading = true |
| | |
| | | onCancel() {} |
| | | }) |
| | | } else { |
| | | this.setState({ |
| | | _this.setState({ |
| | | view: 'normal' |
| | | }) |
| | | |
| | | this.scrolltop() |
| | | _this.scrolltop() |
| | | } |
| | | } |
| | | } |
| | |
| | | <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> |