king
2022-04-26 5046d0d13dc6a8563b8e54e31913bc44cfa1072f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, notification, Tabs } from 'antd'
 
import asyncComponent from '@/utils/asyncComponent'
import BaseForm from './baseform'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import './index.scss'
 
const { TabPane } = Tabs
const SimpleScript = asyncComponent(() => import('./simplescript'))
 
class SettingForm extends Component {
  static propTpyes = {
    config: PropTypes.object,       // 页面配置信息
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    formlist: [],
    btnloading: false,
    activeKey: 'setting',
    setting: null,
    defaultSql: '',
    status: {}
  }
 
  UNSAFE_componentWillMount() {
    const { config } = this.props
 
    let _setting = fromJS(config).toJS()
    let _preScripts = _setting.preScripts || []
    let _cbScripts = _setting.cbScripts || []
 
    this.setState({
      setting: _setting,
      preScripts: _preScripts,
      cbScripts: _cbScripts,
      status: fromJS(_setting).toJS()
    })
  }
 
 
  handleConfirm = () => {
    const { activeKey, setting, preScripts, cbScripts } = this.state
 
    let _loading = false
    if (this.preScriptsForm && this.preScriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.preScriptsForm.props.form.getFieldValue('sql'))) {
      _loading = true
    } else if (this.cbScriptsForm && this.cbScriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.cbScriptsForm.props.form.getFieldValue('sql'))) {
      _loading = true
    }
 
    if (_loading) {
      notification.warning({
        top: 92,
        message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
        duration: 5
      })
      return Promise.reject()
    }
 
    // 表单提交时检查输入值是否正确
    if (activeKey === 'setting') {
      return new Promise((resolve, reject) => {
        this.settingForm.handleConfirm().then(res => {
          resolve({...res, preScripts, cbScripts})
        }, () => {
          reject()
        })
      })
    } else {
      return new Promise((resolve) => {
        resolve({...setting, preScripts, cbScripts})
      })
    }
  }
 
  // 标签切换
  changeTab = (val) => {
    const { activeKey } = this.state
 
    let _loading = false
    if (this.preScriptsForm && this.preScriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.preScriptsForm.props.form.getFieldValue('sql'))) {
      _loading = true
    } else if (this.cbScriptsForm && this.cbScriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.cbScriptsForm.props.form.getFieldValue('sql'))) {
      _loading = true
    }
 
    if (_loading) {
      notification.warning({
        top: 92,
        message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
        duration: 5
      })
      return
    }
 
    if (activeKey === 'setting') {
      this.settingForm.handleConfirm().then(res => {
        this.setState({
          setting: res,
          activeKey: val
        })
      })
    } else {
      this.setState({
        activeKey: val
      })
    }
  }
  
  // 前置脚本更新
  preScriptsUpdate = (preScripts) => {
    this.setState({preScripts})
  }
  
  // 后置脚本更新
  cbScriptsUpdate = (cbScripts) => {
    this.setState({cbScripts})
  }
 
  updateStatus = (status) => {
    this.setState({status: {...this.state.status, ...status}})
  }
 
  render() {
    const { dict, activeKey, setting, preScripts, cbScripts, status } = this.state
 
    return (
      <div className="model-interface-form-box" id="model-interface-form-body">
        <Tabs activeKey={activeKey} onChange={this.changeTab}>
          <TabPane tab="接口设置" key="setting">
            <BaseForm
              dict={dict}
              setting={setting}
              updateStatus={this.updateStatus}
              wrappedComponentRef={(inst) => this.settingForm = inst}
            />
          </TabPane>
          <TabPane tab={
            <span>
              前置脚本
              {preScripts.length ? <span className="count-tip">{preScripts.length}</span> : null}
            </span>
          } disabled={status.procMode !== 'script'} key="prescripts">
            <SimpleScript
              dict={dict}
              type="front"
              setting={setting}
              scripts={preScripts}
              scriptsUpdate={this.preScriptsUpdate}
              wrappedComponentRef={(inst) => this.preScriptsForm = inst}
            />
          </TabPane>
          <TabPane tab={
            <span>
              回调脚本
              {cbScripts.length ? <span className="count-tip">{cbScripts.length}</span> : null}
            </span>
          } disabled={status.callbackType !== 'script'} key="cbscripts">
            <SimpleScript
              dict={dict}
              type="back"
              setting={setting}
              scripts={cbScripts}
              scriptsUpdate={this.cbScriptsUpdate}
              wrappedComponentRef={(inst) => this.cbScriptsForm = inst}
            />
          </TabPane>
        </Tabs>
      </div>
    )
  }
}
 
export default Form.create()(SettingForm)