king
2020-12-04 3659f0773a14b54c18ed0af8b64de4afe8227489
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import moment from 'moment'
import { Button, Modal, notification, message } from 'antd'
import MutilForm from '@/tabviews/zshare/mutilform'
import options from '@/store/options.js'
import Utils from '@/utils/utils.js'
import Api from '@/api'
import './index.scss'
 
const { confirm } = Modal
 
class MainAction extends Component {
  static propTpyes = {
    BID: PropTypes.string,            // 主表ID
    BData: PropTypes.any,             // 主表数据
    Tab: PropTypes.any,               // 如果当前元素为标签时,tab为标签信息
    MenuID: PropTypes.string,         // 菜单ID
    actions: PropTypes.array,         // 按钮组
    dict: PropTypes.object,           // 字典项
    setting: PropTypes.any,           // 页面通用设置
    ContainerId: PropTypes.any,       // tab页面ID,用于弹窗控制
    refreshdata: PropTypes.func,      // 执行完成后数据刷新
    selectedData: PropTypes.array     // 表格选中数据
  }
 
  state = {
    visible: false,
    formdata: null,
    tabledata: null,
    confirmLoading: false,
    execAction: null,
    configMap: {}
  }
 
  refreshdata = (item, type) => {
    this.props.refreshdata(item, type)
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (item, record) => {
    const { setting, selectedData } = this.props
 
    let _this = this
    let data = selectedData || []
    
    if (record) { // 表格中触发按钮
      data = [record]
    }
 
    if (item.Ot !== 'notRequired' && data.length === 0) {
      // 需要选择行时,校验数据
      notification.warning({
        top: 92,
        message: this.props.dict['main.action.confirm.selectline'],
        duration: 5
      })
      return
    } else if (item.Ot === 'requiredSgl' && data.length !== 1) {
      // 需要选择单行时,校验数据
      notification.warning({
        top: 92,
        message: this.props.dict['main.action.confirm.selectSingleLine'],
        duration: 5
      })
      return
    } else if (item.Ot !== 'notRequired' && !setting.primaryKey) {
      // 需要选择行时,校验是否设置主键
      notification.warning({
        top: 92,
        message: '未设置主键!',
        duration: 5
      })
      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 === 'pop') {
      this.setState({
        visible: true,
        execAction: item,
        tabledata: data
      })
    }
  }
 
  /**
   * @description 按钮提交执行
   */
  execSubmit = (btn, data, _resolve, formdata) => {
    const { setting } = this.props
    let primaryId = data[0] ? data[0][setting.primaryKey] : ''
 
    if (btn.sqlType === 'insert') {
      primaryId = ''
    }
 
    let values = {}
    if (formdata) {
      formdata.forEach(_data => {
        values[_data.key] = _data.value
      })
    } else {
      values = data[0]
    }
 
    let param = { // 自定义脚本添加修改删除
      func: 's_custom_script_adduptdel',
      ID: primaryId,
      func_param: values.func,
      Remark: values.Remark,
      Sort: values.Sort,
      LText: values.LongParam
    }
 
    param.LText = window.btoa(window.encodeURIComponent(JSON.stringify(param.LText)))
 
    if (btn.sqlType === 'delete') {
      param.LText = window.GLOB.appkey || ''
      param.Remark = ''
      param.Sort = 0
      param.func_param = ''
    }
    
    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
 
    if (options.cloudServiceApi) {
      param.rduri = options.cloudServiceApi
    }
 
    Api.genericInterface(param).then((res) => {
      if (res.status) {
        this.execSuccess(btn, res)
      } else {
        this.execError(res, btn)
      }
      _resolve()
    })
  }
 
  /**
   * @description 操作成功后处理
   * 1、excel导出,成功后取消导出按钮加载中状态
   * 2、状态码为 S 时,显示成功信息后系统默认信息
   * 3、状态码为 -1 时,不显示任何信息
   * 4、模态框执行成功后是否关闭
   * 5、通知主列表刷新
   */
  execSuccess = (btn, res) => {
    if (res && res.ErrCode === 'S') { // 执行成功
      notification.success({
        top: 92,
        message: res.ErrMesg || this.props.dict['main.action.confirm.success'],
        duration: btn.verify && btn.verify.stime ? btn.verify.stime : 2
      })
    } else if (res && res.ErrCode === 'Y') { // 执行成功
      Modal.success({
        title: res.ErrMesg || this.props.dict['main.action.confirm.success']
      })
    } else if (res && res.ErrCode === '-1') { // 完成后不提示
 
    }
    
    if (btn.OpenType === 'pop' && btn.setting && btn.setting.finish !== 'unclose') {
      this.setState({
        visible: false
      })
    }
 
    this.refreshdata(btn, 'success')
  }
 
  /**
   * @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: btn.verify && btn.verify.ntime ? btn.verify.ntime : 10
      })
    } else if (res.ErrCode === 'F') {
      notification.error({
        className: 'notification-custom-error',
        top: 92,
        message: res.message || res.ErrMesg,
        duration: btn.verify && btn.verify.ftime ? btn.verify.ftime : 10
      })
    } else if (res.ErrCode === 'NM') {
      message.error(res.message || res.ErrMesg)
    }
    
    this.refreshdata(btn, 'error')
  }
 
 
  /**
   * @description 模态框(表单),确认
   */
  handleOk = () => {
    this.formRef.handleConfirm().then(res => {
      this.setState({
        confirmLoading: true
      })
 
      this.execSubmit(this.state.execAction, this.state.tabledata, () => {
        this.setState({
          confirmLoading: false
        })
      }, res)
    }, () => {})
  }
 
  /**
   * @description 模态框(表单),取消
   */
  handleCancel = () => {
    this.setState({
      visible: false
    })
  }
 
  /**
   * @description 显示模态框
   */
  getModels = () => {
    const { execAction } = this.state
 
    if (!execAction || !this.state.visible) return
 
    let title = ''
    let width = '62vw'
    let clickouter = false
    let container = document.body
 
    if (execAction && execAction.setting) {
      title = execAction.setting.title
      width = execAction.setting.width + 'vw'
 
      if (execAction.setting.container === 'tab' && this.props.ContainerId) {
        width = execAction.setting.width + '%'
        container = () => document.getElementById(this.props.ContainerId)
      }
 
      if (execAction.setting.clickouter === 'close') {
        clickouter = true
      }
    }
 
    return (
      <Modal
        title={title}
        maskClosable={clickouter}
        getContainer={container}
        wrapClassName='action-modal'
        visible={this.state.visible}
        width={width}
        onOk={this.handleOk}
        confirmLoading={this.state.confirmLoading}
        onCancel={this.handleCancel}
        destroyOnClose
      >
        <MutilForm
          menuType="HS"
          dict={this.props.dict}
          action={execAction}
          inputSubmit={this.handleOk}
          configMap={this.state.configMap}
          data={this.state.tabledata[0]}
          BData={this.props.BData}
          wrappedComponentRef={(inst) => this.formRef = inst}
        />
      </Modal>
    )
  }
 
  render() {
    return (
      <div className="script-button-list script-toolbar-button">
        {this.props.actions.map((item, index) => {
          return (
            <Button
              className={'mk-btn mk-' + item.class}
              icon={item.icon}
              key={'action' + index}
              onClick={() => {this.actionTrigger(item)}}
            >{item.label}</Button>
          )
        })}
        {this.getModels()}
      </div>
    )
  }
}
 
export default MainAction