king
2022-10-08 809987a63ced6f35a7d37623af33bdba31ca6c93
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Form, notification, Modal, Spin, Tabs } from 'antd'
import moment from 'moment'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
import SettingUtils from './utils.jsx'
import DataSource from './datasource'
import asyncComponent from '@/utils/asyncComponent'
import './index.scss'
 
const { TabPane } = Tabs
const CustomScript = asyncComponent(() => import('@/templates/zshare/customscript'))
 
class SettingForm extends Component {
  static propTpyes = {
    dict: PropTypes.object,         // 字典项
    menu: PropTypes.object,         // 菜单信息
    config: PropTypes.object,       // 页面配置信息
    inputSubmit: PropTypes.any      // 回车提交事件
  }
 
  state = {
    loading: false,
    setting: null,
    defaultsql: '',
    activeKey: 'setting',
  }
 
  UNSAFE_componentWillMount() {
    const { config } = this.props
 
    let _setting = fromJS(config.setting).toJS()
    let _scripts = _setting.scripts || []
 
    if (window.GLOB.funcs && window.GLOB.funcs.length > 0) {
      window.GLOB.funcs.forEach(m => {
        let reg = new RegExp(`\\/\\*\\$ex@${m.func_code}-begin\\*\\/[\\s\\S]+\\/\\*@ex\\$-end\\*\\/`, 'ig')
        _scripts.forEach(item => {
          item.sql = item.sql.replace(reg, `$ex@${m.func_code}@ex$`)
        })
        if (_setting.dataresource) {
          _setting.dataresource = _setting.dataresource.replace(reg, `$ex@${m.func_code}@ex$`)
        }
      })
    }
 
    this.setState({
      setting: _setting,
      scripts: _scripts
    })
  }
 
 
  handleConfirm = () => {
    const { activeKey, setting, scripts } = this.state
    
    let _scripts = scripts.filter(script => script.status !== 'false')
 
    if (activeKey === 'setting') {
      return new Promise((resolve, reject) => {
        this.settingForm.handleConfirm().then(res => {
          if (res.interType === 'system' && res.default === 'false' && _scripts.length === 0) {
            notification.warning({
              top: 92,
              message: '不执行默认sql时,请添加自定义脚本!',
              duration: 5
            })
            reject()
            return
          }
 
          this.setState({
            setting: res
          }, () => {
            this.sqlverify(() => { resolve({...res, scripts}) }, reject, 'submit')
          })
        }, () => {
          reject()
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        let _loading = false
        if (this.scriptsForm && this.scriptsForm.state.editItem) {
          _loading = true
        } else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.scriptsForm.props.form.getFieldValue('sql'))) {
          _loading = true
        }
  
        if (_loading) {
          notification.warning({
            top: 92,
            message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
            duration: 5
          })
          reject()
        } else if (setting.interType === 'system' && setting.default === 'false' && _scripts.length === 0) {
          notification.warning({
            top: 92,
            message: '不执行默认sql时,请添加自定义脚本!',
            duration: 5
          })
          reject()
        } else {
          this.sqlverify(() => {
            resolve({...setting, scripts})
          }, () => {
            reject()
          }, 'submit')
        }
      })
    }
  }
 
  sqlverify = (_resolve, _reject, type, uscripts) => {
    const { setting, scripts } = this.state
 
    if (setting.interType !== 'system') { // 不使用系统接口时,不需要sql验证
      _resolve()
      return
    }
 
    let _scripts = []
    if (uscripts) { // 过滤禁用的脚本
      _scripts = uscripts.filter(script => script.status !== 'false')
    } else {
      _scripts = scripts.filter(script => script.status !== 'false')
    }
 
    if (type === 'setting' && setting.default === 'false') {
      _resolve()
    } else if (type === 'scripts' && _scripts.length === 0) {
      _resolve()
    } else { // type 为 submit 、 verify ,以及其他需要验证的场景
      let timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      let param = {
        func: 's_debug_sql',
        exec_type: 'y',
        LText: SettingUtils.getDebugSql(setting, _scripts, timestamp)
      }
      param.LText = Utils.formatOptions(param.LText)
      param.timestamp = timestamp
      param.secretkey = Utils.encrypt('', timestamp)
      
      Api.genericInterface(param).then(result => {
        if (result.status) {
          _resolve()
        } else {
          _reject()
          Modal.error({
            title: result.message
          })
        }
      })
    }
  }
 
  changeTab = (val) => {
    const { activeKey } = this.state
 
    if (activeKey === 'setting') {
      this.settingForm.handleConfirm().then(res => {
        if (res.interType !== 'system') {
          notification.warning({
            top: 92,
            message: '使用系统接口时,才可以设置自定义脚本!',
            duration: 5
          })
          return
        }
 
        let _defaultSql = ''
        if (res.dataresource) {
          let _dataresource = res.dataresource
          let arr_field = `${res.valueField},${res.labelField},${res.parentField}`
 
          if (/\s/.test(_dataresource)) {
            _dataresource = '(' + _dataresource + ') tb'
          }
 
          _defaultSql = ` select ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${res.order}) as rows from ${_dataresource}) tmptable order by tmptable.rows `
        }
        this.setState({
          setting: res,
          defaultsql: _defaultSql
        }, () => {
          this.setState({loading: true})
          this.sqlverify(() => { // 验证成功
            this.setState({
              activeKey: val,
              loading: false
            })
          }, () => {             // 验证失败
            this.setState({
              activeKey: val,
              loading: false
            })
          }, activeKey)
        })
      })
    } else {
      let _loading = false
      if (this.scriptsForm && this.scriptsForm.state.editItem) {
        _loading = true
      } else if (this.scriptsForm && this.scriptsForm.props.form.getFieldValue('sql') && !/^\s+$/.test(this.scriptsForm.props.form.getFieldValue('sql'))) {
        _loading = true
      }
 
      if (_loading) {
        notification.warning({
          top: 92,
          message: '存在未保存脚本,请点击确定保存,或点击取消放弃修改!',
          duration: 5
        })
        return
      }
      this.setState({loading: true})
      this.sqlverify(() => { // 验证成功
        this.setState({
          activeKey: val,
          loading: false
        })
      }, () => {             // 验证失败
        this.setState({
          activeKey: val,
          loading: false
        })
      }, activeKey)
    }
  }
 
  // 自定义脚本修改
  scriptsChange = (scripts) => {
    return new Promise((resolve, reject) => {
      this.sqlverify(resolve, reject, 'verify', scripts)
    })
  }
 
  // 自定义脚本更新
  scriptsUpdate = (scripts) => {
    this.setState({scripts})
  }
 
  render() {
    const { menu } = this.props
    const { activeKey, setting, loading, scripts } = this.state
 
    return (
      <div className="model-tree-setting-form-box">
        {loading && <Spin size="large" />}
        <Tabs activeKey={activeKey} onChange={this.changeTab}>
          <TabPane tab="数据源" key="setting">
            <DataSource
              menu={menu}
              dict={this.props.dict}
              setting={setting}
              inputSubmit={this.props.inputSubmit}
              wrappedComponentRef={(inst) => this.settingForm = inst}
            />
          </TabPane>
          <TabPane tab={
            <span>
              自定义脚本
              {scripts.length ? <span className="count-tip">{scripts.length}</span> : null}
            </span>
          } key="scripts" id="mk-setting-scripts">
            <CustomScript
              dict={this.props.dict}
              setting={setting}
              scripts={scripts}
              defaultSql={this.state.defaultsql}
              searches={[]}
              scriptsChange={this.scriptsChange}
              scriptsUpdate={this.scriptsUpdate}
              wrappedComponentRef={(inst) => this.scriptsForm = inst}
            />
          </TabPane>
        </Tabs>
      </div>
    )
  }
}
 
export default Form.create()(SettingForm)