king
2019-11-30 95f2f60ba9eb343c2605e1ae68c221443d75f704
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import React, {Component} from 'react'
import PropTypes from 'prop-types'
// import { is, fromJS } from 'immutable'
import { Button, Affix, Modal, notification } from 'antd'
import MutilForm from '@/components/mutilform'
import Api from '@/api'
import './index.scss'
 
const { confirm } = Modal
 
class MainAction extends Component {
  static propTpyes = {
    MenuID: PropTypes.string,
    actions: PropTypes.array, // 搜索条件列表
    dict: PropTypes.object, // 字典项
    setting: PropTypes.any
  }
 
  state = {
    visible: false,
    formdata: null,
    tabledata: null,
    confirmLoading: false,
    execAction: null,
    loadingUuid: ''
  }
  
  refreshdata = (item, type) => {
    this.props.refreshdata(item, type)
  }
  actionTrigger = (item) => {
    const { setting } = this.props
    let _this = this
    let data = this.props.gettableselected() || []
 
    if (item.Ot !== 'notRequired' && data.length === 0) {
      // 需要选择行时,校验数据
      notification.warning({
        top: 92,
        message: this.props.dict['main.action.confirm.selectline'],
        duration: 10
      })
      return
    } else if (item.Ot === 'requiredSgl' && data.length !== 1) {
      // 需要选择单行时,校验数据
      notification.warning({
        top: 92,
        message: this.props.dict['main.action.confirm.selectSingleLine'],
        duration: 10
      })
      return
    } else if (item.Ot !== 'notRequired' && !setting.primaryKey) {
      // 需要选择行时,校验是否设置主键
      notification.warning({
        top: 92,
        message: '未设置主键!',
        duration: 10
      })
      return
    }
 
    if (item.OpenType === 'prompt') {
      confirm({
        title: this.props.dict['main.action.confirm.tip'],
        onOk() {
          return new Promise(resolve => {
            _this.execSubmit(item, data, resolve)
          })
        },
        onCancel() {}
      })
    } else if (item.OpenType === 'exec') {
      this.setState({loadingUuid: item.uuid})
      this.execSubmit(item, data, () => {
        this.setState({loadingUuid: ''})
      })
    }
  }
 
  execSubmit = (btn, data, _resolve) => {
    const { setting } = this.props
    if (btn.intertype === 'inner') {
      // 使用内部接口时,内部函数和数据源不可同时为空
      if (!btn.innerFunc && !btn.sql) {
        this.actionSettingError()
        _resolve()
        return
      }
 
      if (btn.Ot === 'notRequired' || btn.Ot === 'requiredSgl' || btn.Ot === 'requiredOnce') {
        // 获取id
        let param = {
          func: 'sPC_TableData_InUpDe'
        }
        let ID = ''
        if (btn.Ot === 'notRequired') {
          
        } else if (btn.Ot === 'requiredSgl') {
          ID = data[0][setting.primaryKey]
        } else if (btn.Ot === 'requiredOnce') {
          let ids = data.map(d => { return d[setting.primaryKey]})
          ID = ids.join(',')
        }
 
        if (btn.innerFunc) {
          param.func = btn.innerFunc
          param.ID = ID
          param.BID = ''
        } else if (btn.sql) {
          param.LText = btn.sql // 数据源
        }
 
        Api.genericInterface(param).then((res) => {
          if (res.status) {
            this.execSuccess(btn)
          } else {
            this.execError(res, btn)
          }
          _resolve()
        })
      } else if (btn.Ot === 'required') {
        let deffers = data.map(cell => {
          let param = {
            func: 'sPC_TableData_InUpDe'
          }
          let ID = cell[setting.primaryKey]
 
          if (btn.innerFunc) {
            param.func = btn.innerFunc
            param.ID = ID
            param.BID = ''
          } else if (btn.sql) {
            param.LText = btn.sql // 数据源
          }
 
          return new Promise(resolve => {
            Api.genericInterface(param).then(res => {
              resolve(res)
            })
          })
        })
        Promise.all(deffers).then(result => {
          let iserror = false
          let errorMsg = ''
          result.forEach(res => {
            if (res.status) {
              
            } else {
              iserror = true
              errorMsg = res.message
            }
          })
          if (!iserror) {
            this.execSuccess(btn)
          } else {
            notification.error({
              top: 92,
              message: errorMsg,
              duration: 15
            })
            this.refreshdata(btn, 'error')
          }
          _resolve()
        })
      } else {
        this.actionSettingError()
        _resolve()
        return
      }
    } else if (btn.intertype === 'outer') {
      /** *********************调用外部接口************************* */
      let param = {
        ID: '',
        BID: ''
      }
 
      if (!btn.interface) { // 接口地址不存在时报错
        this.actionSettingError()
        _resolve()
        return
      }
 
      if (btn.Ot === 'notRequired' || btn.Ot === 'requiredSgl' || btn.Ot === 'requiredOnce') {
        // 获取id
        if (btn.Ot === 'notRequired') {
          
        } else if (btn.Ot === 'requiredSgl') {
          param.ID = data[0][setting.primaryKey]
        } else if (btn.Ot === 'requiredOnce') {
          let ids = data.map(d => { return d[setting.primaryKey]})
          param.ID = ids.join(',')
        }
 
        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
  
                res.rduri = btn.interface
                // res.method = btn.method
                if (btn.outerFunc) {
                  res.func = btn.outerFunc
                }
                // 使用处理后的数据调用外部接口
                resolve(res)
              } else {
                this.execError(res, btn)
                _resolve()
              }
            })
          } else {
            // 不存在内部函数时,生成外部请求参数
            param.rduri = btn.interface
            // param.method = btn.method
            if (btn.outerFunc) {
              param.func = btn.outerFunc
            }
            resolve(param)
          }
        }).then(res => {
          if (!res) return
          // 外部请求
          console.log(res)
          return Api.genericInterface(res)
        }).then(response => {
          // 回调请求
          if (response.status) {
            if (btn.callbackFunc) {
              // 存在回调函数时,调用
              delete response.ErrCode
              delete response.ErrMesg
              delete response.message
              delete response.status
  
              response.func = btn.callbackFunc
              return Api.genericInterface(response)
            } else {
              this.execSuccess(btn)
              _resolve()
            }
          } else {
            this.execError(response, btn)
            _resolve()
          }
        }).then(res => {
          if (!res) return
  
          if (res.status) {
            this.execSuccess(btn)
          } else {
            this.execError(res, btn)
          }
          _resolve()
        })
 
      } else if (btn.Ot === 'required') {
        // 选择多行,循环调用
 
        new Promise(resolve => {
          // 内部请求
          if (btn.innerFunc) {
            let deffers = data.map(cell => {
              let _param = {
                BID: '',
                func: btn.innerFunc
              }
              _param.ID = cell[setting.primaryKey]
              return new Promise(resolve => {
                Api.genericInterface(_param).then(res => {
                  resolve(res)
                })
              })
            })
            Promise.all(deffers).then(result => {
              let iserror = false
              let errorMsg = ''
              result.forEach(res => {
                if (!res.status) {
                  iserror = true
                  errorMsg = res.message
                }
              })
              if (!iserror) {
                resolve(result)
              } else {
                notification.error({
                  top: 92,
                  message: errorMsg,
                  duration: 15
                })
                this.refreshdata(btn, 'error')
                _resolve()
              }
            })
          } else {
            let params = data.map(cell => {
              return {
                BID: '',
                ID: cell[setting.primaryKey]
              }
            })
            resolve(params)
          }
        }).then(result => {
          // 外部请求
          if (!result) return
 
          let deffers = result.map(res => {
            delete res.ErrCode
            delete res.ErrMesg
            delete res.message
            delete res.status
 
            res.rduri = btn.interface
            // res.method = btn.method
            if (btn.outerFunc) {
              res.func = btn.outerFunc
            }
            return new Promise(resolve => {
              Api.genericInterface(res).then(response => {
                resolve(response)
              })
            })
          })
          return Promise.all(deffers)
 
        }).then(result => {
          // 回调请求
          let iserror = false
          let errorMsg = ''
          result.forEach(res => {
            if (!res.status) {
              iserror = true
              errorMsg = res.message
            }
          })
          if (iserror) {
            notification.error({
              top: 92,
              message: errorMsg,
              duration: 15
            })
            this.refreshdata(btn, 'error')
            _resolve()
            return
          }
          
          if (btn.callbackFunc) {
            // 存在回调函数时,调用
            let deffers = result.map(res => {
              delete res.ErrCode
              delete res.ErrMesg
              delete res.message
              delete res.status
  
              res.func = btn.callbackFunc
              return new Promise(resolve => {
                Api.genericInterface(res).then(response => {
                  resolve(response)
                })
              })
            })
            return Promise.all(deffers)
          } else {
            _resolve()
            this.execSuccess(btn)
          }
        }).then(result => {
          if (!result) return
 
          let iserror = false
          let errorMsg = ''
          result.forEach(res => {
            if (!res.status) {
              iserror = true
              errorMsg = res.message
            }
          })
          if (iserror) {
            notification.error({
              top: 92,
              message: errorMsg,
              duration: 15
            })
            this.refreshdata(btn, 'error')
            return
          } else {
            this.execSuccess(btn)
          }
          _resolve()
        })
 
      } else {
        this.actionSettingError()
        _resolve()
        return
      }
      
    } else {
      this.actionSettingError()
      _resolve()
      return
    }
  }
 
  execSuccess = (btn) => {
    notification.success({
      top: 92,
      message: this.props.dict['main.action.confirm.success'],
      duration: 5
    })
    this.refreshdata(btn, 'success')
  }
 
  execError = (res, btn) => {
    notification.error({
      top: 92,
      message: res.message,
      duration: 15
    })
    this.refreshdata(btn, 'error')
  }
 
  actionSettingError = () => {
    notification.warning({
      top: 92,
      message: this.props.dict['main.action.settingerror'],
      duration: 10
    })
  }
 
  getModels = () => {
    return (
      <Modal
        wrapClassName='action-modal'
        title={(this.state.execAction && this.state.execAction.MenuName) || ''}
        visible={this.state.visible}
        width={(this.state.execAction && +this.state.execAction.PopWidth) || 520}
        onOk={this.handleOk}
        confirmLoading={this.state.confirmLoading}
        onCancel={this.handleCancel}
      >
        {this.state.formdata &&
          <MutilForm
            dict={this.props.dict}
            formlist={this.state.formdata}
            data={this.state.tabledata}
            wrappedComponentRef={(inst) => this.formRef = inst}
          />}
      </Modal>
    )
  }
 
  handleOk = () => {
    this.formRef.handleConfirm().then(res => {
      this.setState({
        confirmLoading: true
      })
      console.log(res)
      Api.setActionSubmit({
        func: 'SetActionSubmitSuccess'
      }).then((res) => {
        if (res.status) {
          notification.success({
            top: 92,
            message: this.props.dict['main.action.confirm.success']
          })
          this.setState({
            confirmLoading: false,
            visible: false
          })
        } else {
          notification.error({
            top: 92,
            message: res.message
          })
        }
      })
    }, () => {})
  }
 
  handleCancel = () => {
    this.setState({
      visible: false
    })
    this.formRef.handleReset()
  }
 
  render() {
    const { loadingUuid } = this.state
 
    if (this.props.setting.actionfixed) { // 按钮是否固定在头部
      return (
        <Affix offsetTop={48}>
          <div className="button-list" id={this.props.MenuID + 'mainaction'}>
            {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>
                )
              }
            })}
            {this.getModels()}
          </div>
        </Affix>
      )
    } else {
      return (
        <div className="button-list">
          {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