king
2022-07-22 0c439ced2c97905cb2b02f5f689a37b19369fb8a
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import moment from 'moment'
import { Button, Modal, notification, message } from 'antd'
import Utils, { getSysDefaultSql } from '@/utils/utils.js'
import options from '@/store/options.js'
import Api from '@/api'
import './index.scss'
 
const { confirm } = Modal
 
class MainAction extends Component {
  static propTpyes = {
    MenuID: PropTypes.string,      // 菜单ID
    primaryId: PropTypes.string,   // 主键
    actions: PropTypes.array,      // 按钮组
    dict: PropTypes.object,        // 字典项
    data: PropTypes.any,           // 数据
    setting: PropTypes.any,        // 页面通用设置
    getFormData: PropTypes.func,   // 获取表单值
    refreshdata: PropTypes.func,   // 执行完成后数据刷新
  }
 
  state = {
    formdata: null,
    loadingUuid: ''
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (item) => {
    const { data } = this.props
    let _this = this
 
    if (item.btnType !== 'cancel') {
      this.props.getFormData().then(res => {
        if (item.OpenType === 'prompt') {
          confirm({
            title: this.props.dict['main.action.confirm.tip'],
            onOk() {
              return new Promise(resolve => {
                _this.execSubmit(item, data, resolve, res)
              })
            },
            onCancel() {}
          })
        } else if (item.OpenType === 'exec') {
          this.setState({loadingUuid: item.uuid})
 
          this.execSubmit(item, data, () => {
            this.setState({loadingUuid: ''})
          }, res)
        }
      })
    } else {
      item.afterExecSuccess = 'close'
      this.props.refreshdata(item, 'success', '')
    }
  }
 
  /**
   * @description 按钮提交执行
   */
  execSubmit = (btn, data, _resolve, formdata) => {
    const { setting, primaryId } = this.props
 
    let _primaryId = primaryId
 
    if (btn.intertype === 'inner') {
      let param = { // 系统存储过程
        func: btn.innerFunc,
        BID: ''
      }
 
      param[setting.primaryKey] = primaryId
 
      formdata.forEach(_data => {
        param[_data.key] = _data.value
      })
 
      if (!param[setting.primaryKey]) {
        param[setting.primaryKey] = Utils.getguid()
      }
 
      _primaryId = param[setting.primaryKey]
 
      Api.genericInterface(param).then((res) => {
        if (res.status) {
          this.execSuccess(btn, res, _primaryId, formdata)
        } else {
          this.execError(res, btn)
        }
        _resolve()
      })
    } else if (btn.intertype === 'system') {
      // 使用系统接口时,数据源不可为空, 使用系统函数时,类型不可为空
      if (!btn.sql || !btn.sqlType) {
        this.actionSettingError()
        _resolve()
        return
      }
 
      // 创建凭证时,需要选择行时
      if (!data && btn.verify && btn.verify.voucher && btn.verify.voucher.enabled) {
        notification.warning({
          top: 92,
          message: '使用创建凭证函数,需要选择行!',
          duration: 5
        })
        return
      }
 
      let param = { // 系统存储过程
        func: 'sPC_TableData_InUpDe',
        exec_type: 'y', // 后台解码
        BID: ''
      }
 
      if (btn.sql && btn.sqlType === 'insert') { // 系统函数添加时,生成uuid
        param.ID = Utils.getguid()
        param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
        param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
        param.secretkey = Utils.encrypt('', param.timestamp)
        param.LText = Utils.formatOptions(param.LText)
 
        _primaryId = param.ID
      } else if (btn.sql && btn.sqlType === 'insertOrUpdate') { // 系统函数添加或修改时
        param.ID = primaryId || Utils.getguid()
        param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
        param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
        param.secretkey = Utils.encrypt('', param.timestamp)
        param.LText = Utils.formatOptions(param.LText)
 
        _primaryId = param.ID
      } else if (btn.sql) {
        param.ID = primaryId
        param.LText = getSysDefaultSql(btn, setting, formdata, param, data, []) // 数据源
        param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
        param.secretkey = Utils.encrypt('', param.timestamp)
        param.LText = Utils.formatOptions(param.LText)
      }
 
      if (window.GLOB.mkHS && param.timestamp) { // 云端验证
        param.open_key = Utils.encryptOpenKey(param.secretkey, param.timestamp)
      }
 
      Api.genericInterface(param).then((res) => {
        if (res.status) {
          this.execSuccess(btn, res, _primaryId, formdata)
        } else {
          this.execError(res, btn)
        }
        _resolve()
      })
    } else if (btn.intertype === 'outer') {
      /** *********************调用外部接口************************* */
 
      if (!btn.interface) { // 接口地址不存在时报错
        this.actionSettingError()
        _resolve()
        return
      }
 
      let param = {
        BID: ''
      }
 
      param[setting.primaryKey] = primaryId
 
      formdata.forEach(_data => {
        param[_data.key] = _data.value
      })
 
      if (!param[setting.primaryKey]) {
        param[setting.primaryKey] = Utils.getguid()
      }
      _primaryId = param[setting.primaryKey]
 
      let _outParam = null
  
      new Promise(resolve => {
        // 内部请求
        if (btn.innerFunc) {
          param.func = btn.innerFunc
          // 存在内部函数时,数据预处理
          Api.genericInterface(param).then(res => {
            if (res.status) {
              delete res.ErrCode
              delete res.ErrMesg
              delete res.message
              delete res.status
  
              // 使用处理后的数据调用外部接口
              resolve(res)
            } else {
              this.execError(res, btn)
              resolve(false)
              _resolve()
            }
          })
        } else {
          resolve(param)
        }
      }).then(res => {
        if (!res) return
        // 外部请求
        _outParam = JSON.parse(JSON.stringify(res))
  
        if (window.GLOB.mkHS) {
          if (btn.sysInterface === 'true' && options.cloudServiceApi) {
            res.rduri = options.cloudServiceApi
          } else if (btn.sysInterface !== 'true') {
            res.rduri = btn.interface
          }
        } else {
          if (btn.sysInterface === 'true' && window.GLOB.mainSystemApi) {
            res.rduri = window.GLOB.mainSystemApi
          } else if (btn.sysInterface !== 'true') {
            res.rduri = btn.interface
          }
        }
 
        if (btn.outerFunc) {
          res.func = btn.outerFunc
        }
  
        return Api.genericInterface(res)
      }).then(response => {
        if (!response) return
        // 回调请求
        if (btn.callbackFunc) {
          // 存在回调函数时,调用
          delete response.message
          delete response.status
  
          response.func = btn.callbackFunc
  
          let _callbackparam = {..._outParam, ...response}
          return Api.genericInterface(_callbackparam)
        } else {
          if (response.status) {
            this.execSuccess(btn, response, _primaryId, formdata)
            _resolve()
          } else {
            this.execError(response, btn)
            _resolve()
          }
        }
      }).then(res => {
        if (!res) return
  
        if (res.status) {
          this.execSuccess(btn, res, _primaryId, formdata)
          _resolve()
        } else {
          this.execError(res, btn)
          _resolve()
        }
      })
      
    } else {
      this.actionSettingError()
      _resolve()
      return
    }
  }
 
  /**
   * @description 操作成功后处理
   * 1、excel导出,成功后取消导出按钮加载中状态
   * 2、状态码为 S 时,显示成功信息后系统默认信息
   * 3、状态码为 -1 时,不显示任何信息
   * 4、模态框执行成功后是否关闭
   * 5、通知主列表刷新
   */
  execSuccess = (btn, res, primaryId, formdata) => {
    if (res && res.ErrCode === 'S') { // 执行成功
      notification.success({
        top: 92,
        message: res.ErrMesg || this.props.dict['main.action.confirm.success'],
        duration: 2
      })
    } else if (res && res.ErrCode === '-1') { // 完成后不提示
 
    }
 
    this.props.refreshdata(btn, 'success', primaryId, formdata)
  }
 
  /**
   * @description 操作失败后处理
   * 1、状态码为 E、N、F、NM 时,显示相应提示信息
   * 2、excel导出,失败后取消导出按钮加载中状态
   * 3、通知主列表刷新
   */
  execError = (res, btn) => {
    if (res.ErrCode === 'E') {
      Modal.error({
        title: res.message || res.ErrMesg,
      })
    } else if (res.ErrCode === 'N') {
      notification.error({
        top: 92,
        message: res.message || res.ErrMesg,
        duration: 10
      })
    } else if (res.ErrCode === 'F') {
      notification.error({
        className: 'notification-custom-error',
        top: 92,
        message: res.message || res.ErrMesg,
        duration: 10
      })
    } else if (res.ErrCode === 'NM') {
      message.error(res.message || res.ErrMesg)
    }
    
    this.props.refreshdata(btn, 'error')
  }
 
  /**
   * @description 按钮配置信息错误提示
   */
  actionSettingError = () => {
    notification.warning({
      top: 92,
      message: this.props.dict['main.action.settingerror'],
      duration: 5
    })
  }
 
  
  render() {
    const { loadingUuid } = this.state
 
    return (
      <div className="button-list formtab-button">
        {this.props.actions.map((item, index) => {
          if (loadingUuid === item.uuid) {
            return (
              <Button
                className={'mk-btn mk-' + item.class}
                icon={item.icon}
                key={'action' + index}
                onClick={() => {this.actionTrigger(item)}}
                loading
              >{item.label}</Button>
            )
          } else {
            return (
              <Button
                className={'mk-btn mk-' + item.class}
                icon={item.icon}
                key={'action' + index}
                onClick={() => {this.actionTrigger(item)}}
              >{item.label}</Button>
            )
          }
        })}
      </div>
    )
  }
}
 
export default MainAction