| | |
| | | import React, {Component} from 'react' |
| | | import PropTypes from 'prop-types' |
| | | import { is, fromJS } from 'immutable' |
| | | import { Form, Row, Col, Button, notification, Modal, Tooltip, Radio, Select } from 'antd' |
| | | import { Form, Row, Col, Button, notification, Tooltip, Select, Switch, Radio } from 'antd' |
| | | import { QuestionCircleOutlined } from '@ant-design/icons' |
| | | import moment from 'moment' |
| | | |
| | | import Api from '@/api' |
| | | import Utils from '@/utils/utils.js' |
| | | import { checkSQL, getSearchFields } from '@/utils/utils-custom.js' |
| | | import CodeMirror from '@/templates/zshare/codemirror' |
| | | import './index.scss' |
| | | import MKEmitter from '@/utils/events.js' |
| | | // import './index.scss' |
| | | |
| | | class CustomForm extends Component { |
| | | static propTpyes = { |
| | | scripts: PropTypes.array, // 自定义脚本列表 |
| | | usefulfields: PropTypes.any, // 可用字段 |
| | | systemScripts: PropTypes.array, // 系统脚本 |
| | | scriptsChange: PropTypes.func // 表单 |
| | | searches: PropTypes.any, // 搜索条件 |
| | | linefields: PropTypes.any, |
| | | scriptsChange: PropTypes.func |
| | | } |
| | | |
| | | state = { |
| | | editItem: null, |
| | | systemScripts: [], |
| | | usefulfields: null, |
| | | loading: false, |
| | | verifySql: '' |
| | | skip: false |
| | | } |
| | | |
| | | UNSAFE_componentWillMount () { |
| | | this.resetfield(this.props.usefulfields) |
| | | this.setState({ |
| | | usefulfields: getSearchFields(this.props.searches) |
| | | }) |
| | | } |
| | | |
| | | UNSAFE_componentWillReceiveProps (nextProps) { |
| | | if (nextProps.usefulfields && !is(fromJS(this.props.usefulfields), fromJS(nextProps.usefulfields))) { |
| | | this.resetfield(nextProps.usefulfields) |
| | | UNSAFE_componentWillReceiveProps(nextProps) { |
| | | if (!is(fromJS(this.props.searches), fromJS(nextProps.searches))) { |
| | | this.setState({ |
| | | usefulfields: getSearchFields(nextProps.searches) |
| | | }) |
| | | } |
| | | } |
| | | |
| | | resetfield = (columns) => { |
| | | columns = columns.filter(item => item.Column !== '$Index') |
| | | let fields = columns.map(item => item.Column) |
| | | componentDidMount () { |
| | | this.getsysScript() |
| | | } |
| | | |
| | | let _sql = `Declare @UserName nvarchar(50),@FullName nvarchar(50),@RoleID nvarchar(512),@mk_departmentcode nvarchar(50),@mk_organization nvarchar(50),@login_city nvarchar(50),@ErrorCode nvarchar(50), @retmsg nvarchar(4000) |
| | | ` |
| | | getsysScript = () => { |
| | | if (sessionStorage.getItem('mk_sys_scripts')) { |
| | | this.setState({ |
| | | systemScripts: JSON.parse(sessionStorage.getItem('mk_sys_scripts')) |
| | | }) |
| | | return |
| | | } |
| | | |
| | | this.setState({ |
| | | verifySql: _sql, |
| | | usefulfields: fields.join(', ') |
| | | let _scriptSql = `Select distinct func+Remark as funcname,longparam, s.Sort from s_custom_script s inner join (select OpenID from sapp where ID=@Appkey@) p on s.openid = case when s.appkey='' then s.openid else p.OpenID end order by s.Sort` |
| | | |
| | | _scriptSql = Utils.formatOptions(_scriptSql, 'x') |
| | | |
| | | let _sParam = { |
| | | func: 'sPC_Get_SelectedList', |
| | | LText: _scriptSql, |
| | | obj_name: 'data', |
| | | arr_field: 'funcname,longparam', |
| | | exec_type: 'x' |
| | | } |
| | | |
| | | _sParam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') |
| | | _sParam.secretkey = Utils.encrypt('', _sParam.timestamp) |
| | | _sParam.open_key = Utils.encryptOpenKey(_sParam.secretkey, _sParam.timestamp) // 云端数据验证 |
| | | |
| | | Api.getCloudConfig(_sParam).then(res => { |
| | | if (res.status) { |
| | | let _scripts = res.data.map(item => { |
| | | return { |
| | | name: item.funcname, |
| | | value: window.decodeURIComponent(window.atob(item.longparam)) |
| | | } |
| | | }) |
| | | |
| | | sessionStorage.setItem('mk_sys_scripts', JSON.stringify(_scripts)) |
| | | |
| | | this.setState({ |
| | | systemScripts: _scripts |
| | | }) |
| | | } else { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: res.message, |
| | | duration: 5 |
| | | }) |
| | | } |
| | | }) |
| | | } |
| | | |
| | |
| | | |
| | | this.props.form.setFieldsValue({ |
| | | sql: record.sql, |
| | | position: record.position || 'back' |
| | | position: record.position || 'front' |
| | | }) |
| | | } |
| | | |
| | | handleConfirm = () => { |
| | | const { skip } = this.state |
| | | // 表单提交时检查输入值是否正确 |
| | | this.props.form.validateFieldsAndScroll((err, values) => { |
| | | if (!err) { |
| | | if (/^[\s\n]+$/.test(values.sql)) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: '请输入sql!', |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | | |
| | | values.uuid = this.state.editItem ? this.state.editItem.uuid : '' |
| | | |
| | | let _quot = values.sql.match(/'{1}/g) |
| | | let _lparen = values.sql.match(/\({1}/g) |
| | | let _rparen = values.sql.match(/\){1}/g) |
| | | let pass = checkSQL(values.sql, 'customscript') |
| | | |
| | | _quot = _quot ? _quot.length : 0 |
| | | _lparen = _lparen ? _lparen.length : 0 |
| | | _rparen = _rparen ? _rparen.length : 0 |
| | | if (!pass) return |
| | | |
| | | if (_quot % 2 !== 0) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: 'sql中\'必须成对出现', |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } else if (_lparen !== _rparen) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: 'sql中()必须成对出现', |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } else if (/--/ig.test(values.sql)) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: '自定义sql语句中,不可出现字符 -- ,注释请用 /*内容*/', |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | | |
| | | let error = Utils.verifySql(values.sql, 'customscript') |
| | | |
| | | if (error) { |
| | | notification.warning({ |
| | | top: 92, |
| | | message: 'sql中不可使用' + error, |
| | | duration: 5 |
| | | }) |
| | | return |
| | | } |
| | | |
| | | let tail = ` |
| | | aaa: |
| | | ` |
| | | |
| | | let _initCustomScript = '' // 初始化脚本 |
| | | let _prevCustomScript = '' // 默认sql前执行脚本 |
| | | let _backCustomScript = '' // 默认sql后执行脚本 |
| | | |
| | | this.props.scripts.forEach(item => { |
| | | if (item.status === 'false') return |
| | | |
| | | if (item.position === 'init') { |
| | | _initCustomScript += ` |
| | | /* 初始化脚本 */ |
| | | ${values.uuid === item.uuid ? values.sql : item.sql} |
| | | ` |
| | | } else if (item.position === 'front') { |
| | | _prevCustomScript += ` |
| | | /* 默认sql前脚本 */ |
| | | ${values.uuid === item.uuid ? values.sql : item.sql} |
| | | ` |
| | | } else { |
| | | _backCustomScript += ` |
| | | /* 默认sql后脚本 */ |
| | | ${values.uuid === item.uuid ? values.sql : item.sql} |
| | | ` |
| | | } |
| | | this.setState({ |
| | | loading: true |
| | | }) |
| | | |
| | | if (!values.uuid) { |
| | | if (values.position === 'init') { |
| | | _initCustomScript += ` |
| | | /* 初始化脚本 */ |
| | | ${values.sql} |
| | | ` |
| | | } else if (values.position === 'front') { |
| | | _prevCustomScript += ` |
| | | /* 默认sql前脚本 */ |
| | | ${values.sql} |
| | | ` |
| | | } else { |
| | | _backCustomScript += ` |
| | | /* 默认sql后脚本 */ |
| | | ${values.sql} |
| | | ` |
| | | } |
| | | } |
| | | |
| | | let param = { |
| | | func: 's_debug_sql', |
| | | exec_type: 'y', |
| | | LText: this.state.verifySql + _initCustomScript + _prevCustomScript + _backCustomScript + tail |
| | | } |
| | | |
| | | param.LText = param.LText.replace(/@\$|\$@/ig, '') |
| | | |
| | | // 外联数据库替换 |
| | | if (window.GLOB.externalDatabase !== null) { |
| | | param.LText = param.LText.replace(/@db@/ig, window.GLOB.externalDatabase) |
| | | } |
| | | |
| | | 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}) |
| | | Api.getLocalConfig(param).then(res => { |
| | | if (res.status) { |
| | | this.props.scriptsChange(values, (status) => { |
| | | if (status) { |
| | | this.setState({ |
| | | skip: false, |
| | | loading: false, |
| | | editItem: null |
| | | }, () => { |
| | | this.props.scriptsChange(values) |
| | | }) |
| | | if (values.uuid) { |
| | | MKEmitter.emit('editLineId', values.uuid) |
| | | } |
| | | this.props.form.setFieldsValue({ |
| | | sql: '' |
| | | sql: ' ' |
| | | }) |
| | | } else { |
| | | this.setState({loading: false}) |
| | | |
| | | Modal.error({ |
| | | title: res.message |
| | | this.setState({ |
| | | loading: false |
| | | }) |
| | | } |
| | | }) |
| | | }, skip) |
| | | } |
| | | }) |
| | | } |
| | |
| | | }) |
| | | |
| | | this.props.form.setFieldsValue({ |
| | | sql: '' |
| | | sql: ' ', |
| | | position: 'front' |
| | | }) |
| | | } |
| | | |
| | | selectScript = (value, option) => { |
| | | const { usefulfields, sheet } = this.props |
| | | |
| | | let _value = '' |
| | | if (value === 'default') { |
| | | let fields = usefulfields.filter(item => item.import !== 'false') |
| | | fields = fields.map(col => col.Column).join(',') |
| | | |
| | | let _sheet = sheet.replace(/(.*)\.(.*)\.|@db@/ig, '') |
| | | |
| | | _value = `Select ${fields} From ${_sheet}` |
| | | } else { |
| | | _value = value |
| | | } |
| | | |
| | | let _value = value |
| | | let _sql = this.props.form.getFieldValue('sql') |
| | | if (/^\s+$/.test(_sql)) { |
| | | _sql = '' |
| | | } |
| | | if (_sql) { |
| | | _sql = _sql + ` |
| | | |
| | |
| | | } |
| | | |
| | | render() { |
| | | const { systemScripts, sheet } = this.props |
| | | const { usefulfields } = this.state |
| | | const { sheet, linefields } = this.props |
| | | const { usefulfields, systemScripts, skip } = this.state |
| | | const { getFieldDecorator } = this.props.form |
| | | const formItemLayout = { |
| | | labelCol: { |
| | |
| | | <Form {...formItemLayout} className="verify-form" id="verify-excelin-custom-scripts"> |
| | | <Row gutter={24}> |
| | | <Col span={8}> |
| | | <Form.Item label={'表名'} style={{whiteSpace: 'nowrap', margin: 0}}> |
| | | <Form.Item label="表名" style={{whiteSpace: 'nowrap', margin: 0}}> |
| | | {sheet} |
| | | </Form.Item> |
| | | </Col> |
| | | <Col span={10}> |
| | | <Form.Item label={'报错字段'} style={{margin: 0, whiteSpace: 'nowrap'}}> |
| | | ErrorCode(增加后缀NT表示数据不回滚,如ENT、NNT、FNT、NMNT), retmsg |
| | | <Form.Item label="报错字段" style={{margin: 0, whiteSpace: 'nowrap'}}> |
| | | errorcode(增加后缀NT表示数据不回滚,如ENT、NNT、FNT、NMNT、CNT、-2NT), retmsg |
| | | </Form.Item> |
| | | </Col> |
| | | {usefulfields ? <Col span={24} className="sqlfield"> |
| | | <Form.Item label={'可用字段'}> |
| | | BID, ID, LoginUID, SessionUid, UserID, Appkey, UserName, FullName, RoleID, mk_departmentcode, mk_organization, login_city, {usefulfields} |
| | | <Col span={24} className="sqlfield"> |
| | | <Form.Item label="可用字段"> |
| | | <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'公共值,请按照@xxx@格式使用。'}><span style={{color: '#1890ff'}}>BID, ID, LoginUID, SessionUid, UserID, Appkey, lang, time_id, typename</span></Tooltip>, |
| | | <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'系统变量,系统会定义变量并赋值。'}><span style={{color: '#fa8c16'}}>UserName, FullName, RoleID, mk_departmentcode, mk_organization, mk_user_type, mk_nation, mk_province, mk_city, mk_district, mk_address</span></Tooltip> |
| | | <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'排序、分页以及搜索条件变量,请按照@xxx@格式使用。'}>, orderBy, pageSize, pageIndex{usefulfields ? ', ' + usefulfields : ''}</Tooltip> |
| | | {linefields ? <Tooltip mouseLeaveDelay={0.3} mouseEnterDelay={0.3} placement="top" title={'表单及行变量,系统会定义变量并赋值。'}>, {linefields}</Tooltip> : null} |
| | | </Form.Item> |
| | | </Col> : null} |
| | | </Col> |
| | | <Col span={8} style={{whiteSpace: 'nowrap'}}> |
| | | <Form.Item style={{marginBottom: 0}} label={ |
| | | <Tooltip placement="bottomLeft" title={'自定义脚本与默认sql位置关系。'}> |
| | | <Tooltip placement="bottomLeft" title={'自定义脚本与数据源位置关系。'}> |
| | | <QuestionCircleOutlined className="mk-form-tip" /> |
| | | 执行位置 |
| | | </Tooltip> |
| | |
| | | initialValue: 'front' |
| | | })( |
| | | <Radio.Group> |
| | | <Radio value="init">初始化</Radio> |
| | | <Radio value="front">sql前</Radio> |
| | | <Radio value="back">sql后</Radio> |
| | | <Radio value="front">前置</Radio> |
| | | <Radio value="back">后置</Radio> |
| | | </Radio.Group> |
| | | )} |
| | | </Form.Item> |
| | | </Col> |
| | | <Col span={10}> |
| | | <Form.Item style={{marginBottom: 0}} label={'快捷添加'}> |
| | | <Col span={8}> |
| | | <Form.Item style={{marginBottom: 0}} label="快捷添加"> |
| | | <Select |
| | | showSearch |
| | | filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0} |
| | | onSelect={this.selectScript} |
| | | getPopupContainer={() => document.getElementById('verify-excelin-custom-scripts')} |
| | | > |
| | | <Select.Option key="default" value={'default'}> |
| | | 默认sql |
| | | </Select.Option> |
| | | <Select.Option key="debugger" value={`z_debug: select @ErrorCode='E',@retmsg='测试断点' goto aaa`}> |
| | | 测试断点 |
| | | </Select.Option> |
| | |
| | | </Select> |
| | | </Form.Item> |
| | | </Col> |
| | | <Col span={6} className="add"> |
| | | <Col span={5} className="add" style={{whiteSpace: 'nowrap'}}> |
| | | <Button onClick={this.handleConfirm} loading={this.state.loading} className="mk-green" style={{marginBottom: 15, marginLeft: 40}}> |
| | | 保存 |
| | | </Button> |
| | |
| | | 取消 |
| | | </Button> |
| | | </Col> |
| | | <Col span={3} style={{paddingTop: '15px', fontSize: '12px', whiteSpace: 'nowrap'}}> |
| | | 强制保存: |
| | | <Switch checked={skip} size="small" onChange={() => this.setState({skip: !skip})}/> |
| | | </Col> |
| | | <Col span={24} className="sql"> |
| | | <Form.Item label={'sql'}> |
| | | <Form.Item label="sql"> |
| | | {getFieldDecorator('sql', { |
| | | initialValue: '', |
| | | rules: [ |