import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Form, Tabs, Table, Popconfirm, Icon, notification, Modal, Typography, Spin } from 'antd'
|
import moment from 'moment'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import ColForm from './columnform'
|
import CustomScriptsForm from './customscript'
|
import SettingForm from './settingform'
|
import SettingUtils from './utils'
|
import './index.scss'
|
|
const { TabPane } = Tabs
|
const { Paragraph } = Typography
|
|
const FieldsComponent = asyncComponent(() => import('@/templates/sharecomponent/fieldscomponent'))
|
|
class VerifyCard extends Component {
|
static propTpyes = {
|
dict: PropTypes.object, // 字典项
|
tableFields: PropTypes.any, // 数据源信息
|
permFuncField: PropTypes.any, // 数据源信息
|
config: PropTypes.object, // 数据源信息
|
menuId: PropTypes.string, // 菜单Id
|
searches: PropTypes.array, // 搜索条件
|
}
|
|
state = {
|
columns: [],
|
activeKey: 'setting',
|
loading: false,
|
initsql: '', // sql验证时变量声明及赋值
|
usefulfields: '',
|
defaultsql: '', // 默认Sql
|
systemScripts: [{
|
name: '默认sql',
|
value: ''
|
}],
|
colColumns: [
|
{
|
title: '名称',
|
dataIndex: 'label',
|
width: '25%'
|
},
|
{
|
title: '字段',
|
dataIndex: 'field',
|
width: '25%'
|
},
|
{
|
title: '数据类型',
|
dataIndex: 'datatype',
|
width: '25%',
|
},
|
{
|
title: '操作',
|
align: 'center',
|
width: '25%',
|
dataIndex: 'operation',
|
render: (text, record) =>
|
(<div>
|
<span className="operation-btn" title={this.props.dict['model.edit']} onClick={() => this.handleEdit(record, 'columns')} style={{color: '#1890ff'}}><Icon type="edit" /></span>
|
<Popconfirm
|
overlayClassName="popover-confirm"
|
title={this.props.dict['model.query.delete']}
|
onConfirm={() => this.deleteColumn(record)
|
}>
|
<span className="operation-btn" style={{color: '#ff4d4f'}}><Icon type="delete" /></span>
|
</Popconfirm>
|
</div>)
|
}
|
],
|
scriptsColumns: [
|
{
|
title: 'SQL',
|
dataIndex: 'sql',
|
width: '60%',
|
render: (text) => (
|
<Paragraph copyable ellipsis={{ rows: 5, expandable: true }}>{text}</Paragraph>
|
)
|
},
|
{
|
title: '状态',
|
dataIndex: 'status',
|
width: '20%',
|
render: (text, record) => record.status === 'false' ?
|
(
|
<div>
|
{this.props.dict['model.status.forbidden']}
|
<Icon style={{marginLeft: '5px'}} type="stop" theme="twoTone" twoToneColor="#ff4d4f" />
|
</div>
|
) :
|
(
|
<div>
|
{this.props.dict['model.status.open']}
|
<Icon style={{marginLeft: '5px'}} type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
|
</div>
|
)
|
},
|
{
|
title: '操作',
|
align: 'center',
|
width: '20%',
|
dataIndex: 'operation',
|
render: (text, record) =>
|
(<div>
|
<span className="operation-btn" title={this.props.dict['model.edit']} onClick={() => this.handleEdit(record, 'scripts')} style={{color: '#1890ff'}}><Icon type="edit" /></span>
|
<span className="operation-btn" onClick={() => this.handleUpDown(record, 'up')} style={{color: '#1890ff'}}><Icon type="arrow-up" /></span>
|
<span className="operation-btn" onClick={() => this.handleUpDown(record, 'down')} style={{color: '#ff4d4f'}}><Icon type="arrow-down" /></span>
|
<span className="operation-btn" title={this.props.dict['model.status.change']} onClick={() => this.handleStatus(record)} style={{color: '#8E44AD'}}><Icon type="swap" /></span>
|
<Popconfirm
|
overlayClassName="popover-confirm"
|
title={this.props.dict['model.query.delete']}
|
onConfirm={() => this.deleteScript(record)
|
}>
|
<span className="operation-btn" style={{color: '#ff4d4f'}}><Icon type="delete" /></span>
|
</Popconfirm>
|
</div>)
|
}
|
]
|
}
|
|
UNSAFE_componentWillMount() {
|
const { config } = this.props
|
|
this.setState({
|
columns: fromJS(config.columns).toJS(),
|
setting: fromJS(config.setting).toJS(),
|
scripts: fromJS(config.scripts).toJS()
|
})
|
|
this.getsysScript()
|
}
|
|
getsysScript = () => {
|
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)
|
|
let _sParam = {
|
func: 'sPC_Get_SelectedList',
|
LText: _scriptSql,
|
obj_name: 'data',
|
arr_field: 'funcname,longparam'
|
}
|
|
_sParam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
|
_sParam.secretkey = Utils.encrypt(_sParam.LText, _sParam.timestamp)
|
|
_sParam.open_key = Utils.encrypt(_sParam.secretkey, _sParam.timestamp, true) // 云端数据验证
|
|
Api.getSystemConfig(_sParam).then(res => {
|
if (res.status) {
|
let _scripts = []
|
|
res.data.forEach(item => {
|
let _item = {
|
name: item.funcname,
|
value: Utils.formatOptions(item.longparam, true)
|
}
|
|
_scripts.push(_item)
|
})
|
|
this.setState({
|
systemScripts: [...this.state.systemScripts, ..._scripts]
|
})
|
} else {
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
}
|
})
|
}
|
|
columnChange = (values) => {
|
let columns = fromJS(this.state.columns).toJS()
|
|
if (values.uuid) {
|
columns = columns.map(item => {
|
if (item.uuid === values.uuid) {
|
return values
|
} else {
|
return item
|
}
|
})
|
} else {
|
values.uuid = Utils.getuuid()
|
columns.push(values)
|
}
|
|
this.setState({ columns })
|
}
|
|
deleteColumn = (record) => {
|
this.setState({ columns: this.state.columns.filter(item => item.uuid !== record.uuid) })
|
}
|
|
deleteScript = (record) => {
|
this.setState({ scripts: this.state.scripts.filter(item => item.uuid !== record.uuid) })
|
}
|
|
handleEdit = (record, type) => {
|
if (type === 'scripts') {
|
this.scriptsForm.edit(record)
|
} else if (type === 'columns') {
|
this.contrastForm.edit(record)
|
}
|
|
let node = document.getElementById('model-verify-card-box-tab').parentNode
|
|
if (node && node.scrollTop) {
|
let inter = Math.ceil(node.scrollTop / 10)
|
|
let timer = setInterval(() => {
|
if (node.scrollTop - inter > 0) {
|
node.scrollTop = node.scrollTop - inter
|
} else {
|
node.scrollTop = 0
|
clearInterval(timer)
|
}
|
}, 10)
|
}
|
}
|
|
handleStatus = (record) => {
|
let scripts = fromJS(this.state.scripts).toJS()
|
record.status = record.status === 'false' ? 'true' : 'false'
|
|
scripts = scripts.map(item => {
|
if (item.uuid === record.uuid) {
|
return record
|
} else {
|
return item
|
}
|
})
|
|
this.setState({ scripts })
|
}
|
|
handleUpDown = (record, direction) => {
|
let scripts = fromJS(this.state.scripts).toJS()
|
let index = 0
|
|
scripts = scripts.filter((item, i) => {
|
if (item.uuid === record.uuid) {
|
index = i
|
}
|
|
return item.uuid !== record.uuid
|
})
|
if ((index === 0 && direction === 'up') || (index === scripts.length && direction === 'down')) {
|
return
|
}
|
|
if (direction === 'up') {
|
scripts.splice(index - 1, 0, record)
|
} else {
|
scripts.splice(index + 1, 0, record)
|
}
|
|
this.setState({ scripts })
|
}
|
|
scriptsChange = (values) => {
|
let scripts = fromJS(this.state.scripts).toJS()
|
|
if (values.uuid) {
|
scripts = scripts.map(item => {
|
if (item.uuid === values.uuid) {
|
return values
|
} else {
|
return item
|
}
|
})
|
} else {
|
scripts.push(values)
|
}
|
|
return new Promise((resolve, reject) => {
|
this.sqlverify(resolve, reject, false, scripts)
|
})
|
}
|
|
scriptSubmit = (values) => {
|
let scripts = fromJS(this.state.scripts).toJS()
|
|
if (values.uuid) {
|
scripts = scripts.map(item => {
|
if (item.uuid === values.uuid) {
|
return values
|
} else {
|
return item
|
}
|
})
|
} else {
|
values.uuid = Utils.getuuid()
|
scripts.push(values)
|
}
|
|
this.setState({ scripts })
|
}
|
|
changeTab = (val) => {
|
const { activeKey } = this.state
|
|
this.setState({loading: true})
|
if (activeKey === 'setting') {
|
this.settingForm.handleConfirm().then(res => {
|
this.setState({
|
setting: res
|
}, () => {
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
loading: false
|
})
|
}, true)
|
})
|
}, () => {
|
this.setState({loading: false})
|
})
|
} else if (activeKey === 'columns') {
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
loading: false
|
})
|
}, true)
|
} else if (activeKey === 'scripts') {
|
let _loading = false
|
if (this.scriptsForm && this.scriptsForm.state.editItem) {
|
_loading = true
|
} else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) {
|
_loading = true
|
}
|
|
if (_loading) {
|
notification.warning({
|
top: 92,
|
message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
|
duration: 5
|
})
|
this.setState({
|
loading: false
|
})
|
return
|
}
|
|
this.sqlverify(() => { // 验证成功
|
this.setState({
|
activeKey: val,
|
loading: false
|
})
|
}, () => { // 验证失败
|
this.setState({
|
loading: false
|
})
|
}, true)
|
}
|
}
|
|
submitDataSource = () => {
|
const { activeKey, setting, columns, scripts } = this.state
|
|
return new Promise((resolve, reject) => {
|
if (activeKey === 'setting') {
|
this.settingForm.handleConfirm().then(res => {
|
this.setState({
|
setting: res
|
}, () => {
|
this.sqlverify(() => { resolve({setting: res, columns, scripts }) }, reject, false)
|
})
|
}, () => {
|
reject()
|
})
|
} else if (activeKey === 'columns') {
|
this.sqlverify(() => { resolve({setting, columns, scripts }) }, reject, false)
|
} else if (activeKey === 'scripts') {
|
let _loading = false
|
if (this.scriptsForm && this.scriptsForm.state.editItem) {
|
_loading = true
|
} else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql')) {
|
_loading = true
|
}
|
|
if (_loading) {
|
notification.warning({
|
top: 92,
|
message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
|
duration: 5
|
})
|
reject()
|
return
|
}
|
|
this.sqlverify(() => { resolve({setting, columns, scripts }) }, reject, false)
|
}
|
})
|
}
|
|
sqlverify = (resolve, reject, change = false, testScripts) => {
|
const { searches } = this.props
|
const { columns, setting, scripts } = this.state
|
|
let _scripts = scripts.filter(item => item.status !== 'false')
|
|
if (testScripts) {
|
_scripts = testScripts.filter(item => item.status !== 'false')
|
}
|
if (!change && setting.interType === 'inner' && !setting.innerFunc && setting.execute === 'false' && _scripts.length === 0) {
|
notification.warning({
|
top: 92,
|
message: '不执行默认sql时,请添加自定义脚本!',
|
duration: 5
|
})
|
reject()
|
return
|
}
|
|
if ((setting.interType === 'inner' && !setting.innerFunc && setting.execute !== 'false') || _scripts.length > 0) {
|
let param = {
|
func: 's_debug_sql',
|
LText: SettingUtils.getDebugSql(setting, _scripts, columns, searches)
|
}
|
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()
|
} else {
|
reject()
|
Modal.error({
|
title: result.message
|
})
|
}
|
})
|
} else {
|
resolve()
|
}
|
}
|
|
updatefields = (columns) => {
|
this.setState({
|
columns: columns
|
})
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
}
|
|
render() {
|
const { columns, setting, scripts, colColumns, scriptsColumns, activeKey, loading } = this.state
|
|
return (
|
<div id="model-verify-card-box-tab">
|
{loading && <Spin size="large" />}
|
<Tabs activeKey={activeKey} className="verify-card-box" onChange={this.changeTab}>
|
<TabPane tab="数据源" key="setting">
|
<SettingForm
|
menuId={this.props.menuId}
|
dict={this.props.dict}
|
permFuncField={this.props.permFuncField}
|
columns={columns}
|
setting={setting}
|
scripts={scripts}
|
wrappedComponentRef={(inst) => this.settingForm = inst}
|
/>
|
</TabPane>
|
<TabPane tab="字段集" key="columns">
|
<ColForm
|
dict={this.props.dict}
|
columnChange={this.columnChange}
|
wrappedComponentRef={(inst) => this.contrastForm = inst}
|
/>
|
<FieldsComponent
|
config={{...this.props.config, columns}}
|
type="fields"
|
tableFields={this.props.tableFields}
|
updatefield={this.updatefields}
|
/>
|
<Table
|
bordered
|
rowKey="uuid"
|
className="custom-table"
|
dataSource={columns}
|
columns={colColumns}
|
pagination={false}
|
/>
|
</TabPane>
|
<TabPane tab="自定义脚本" key="scripts">
|
<CustomScriptsForm
|
setting={setting}
|
searches={this.props.searches}
|
initsql={this.state.initsql}
|
dict={this.props.dict}
|
customScripts={scripts}
|
systemScripts={this.state.systemScripts}
|
scriptsChange={this.scriptsChange}
|
scriptSubmit={this.scriptSubmit}
|
wrappedComponentRef={(inst) => this.scriptsForm = inst}
|
/>
|
<Table
|
bordered
|
rowKey="uuid"
|
className="custom-table"
|
dataSource={scripts}
|
columns={scriptsColumns}
|
pagination={false}
|
/>
|
</TabPane>
|
</Tabs>
|
</div>
|
)
|
}
|
}
|
|
export default Form.create()(VerifyCard)
|