king
2022-05-20 652b0129ec60098ae1ad22fb8413eba009fb3aaf
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button, notification, Modal } from 'antd'
 
import Api from '@/api'
import zhCN from '@/locales/zh-CN/main.js'
import enUS from '@/locales/en-US/main.js'
import MKEmitter from '@/utils/events.js'
import MkIcon from '@/components/mk-icon'
// import './index.scss'
 
const { confirm } = Modal
 
class NewPageButton extends Component {
  static propTpyes = {
    show: PropTypes.any,              // 按钮显示样式控制
    MenuID: PropTypes.any,            // 菜单ID
    BData: PropTypes.any,             // 主表数据
    BID: PropTypes.string,            // 主表ID
    btn: PropTypes.object,            // 按钮
    selectedData: PropTypes.any,      // 子表中选择数据
    setting: PropTypes.any,           // 页面通用设置
    disabled: PropTypes.any,          // 行按钮禁用
  }
 
  state = {
    dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    disabled: false,
    hidden: false,
    loading: false
  }
 
  UNSAFE_componentWillMount () {
    const { btn, selectedData, BData } = this.props
    let disabled = false
 
    if (btn.controlField && selectedData && selectedData.length > 0) { // 表格中按钮隐藏控制
      selectedData.forEach(item => {
        let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          disabled = true
        }
      })
      this.setState({hidden: disabled && btn.control === 'hidden'})
    } else if (btn.control === 'parent') {
      if (!BData || !BData.hasOwnProperty(btn.controlField)) {
        this.setState({hidden: true})
      } else {
        let s = BData[btn.controlField] + ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          this.setState({hidden: true})
        } else {
          this.setState({hidden: false})
        }
      }
    }
 
    if (this.props.disabled || disabled) {
      this.setState({disabled: true})
    }
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentDidMount () {
    MKEmitter.addListener('triggerBtnId', this.actionTrigger)
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { btn, selectedData, BData } = this.props
 
    let disabled = false
    if (btn.controlField && !is(fromJS(nextProps.selectedData || []), fromJS(selectedData || []))) {
      if (nextProps.selectedData && nextProps.selectedData.length > 0) { // 表格中按钮隐藏控制
        nextProps.selectedData.forEach(item => {
          let s = item[btn.controlField] !== undefined ? item[btn.controlField] + '' : ''
          if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
            disabled = true
          }
        })
      }
      this.setState({disabled, hidden: disabled && btn.control === 'hidden'})
    } else if (btn.control === 'parent' && !is(fromJS(nextProps.BData || {}), fromJS(BData || {}))) {
      if (!nextProps.BData || !nextProps.BData.hasOwnProperty(btn.controlField)) {
        this.setState({hidden: true})
      } else {
        let s = nextProps.BData[btn.controlField] + ''
        if (s === btn.controlVal || (btn.controlVal && btn.controlVal.split(',').includes(s))) {
          this.setState({hidden: true})
        } else {
          this.setState({hidden: false})
        }
      }
    }
 
    if (nextProps.disabled || disabled) {
      this.setState({disabled: true})
    } else {
      this.setState({disabled: false})
    }
  }
 
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('triggerBtnId', this.actionTrigger)
  }
  
  /**
   * @description 触发按钮操作
   */
  actionTrigger = (triggerId, record, type) => {
    const { setting, selectedData, btn, MenuID } = this.props
    const { loading, disabled } = this.state
    
    if (loading || disabled) return
    if (triggerId && btn.uuid !== triggerId) return
 
    if (btn.funcType === 'closetab') {
      MKEmitter.emit('closeTabView', MenuID || btn.$MenuID)
 
      if (btn.refreshTab && btn.refreshTab.length > 0) {
        MKEmitter.emit('reloadMenuView', btn.refreshTab[btn.refreshTab.length - 1], 'table')
      }
      return
    } else if (type === 'linkbtn' && !btn.$toolbtn && !is(fromJS(selectedData || []), fromJS(record))) {
      return
    }
    
    let data = record || selectedData || []
 
    if (data.length !== 1) {
      // 需要选择单行时,校验数据
      notification.warning({
        top: 92,
        message: this.state.dict['main.action.confirm.selectSingleLine'],
        duration: 5
      })
      return
    } else if (!setting.primaryKey) {
      // 需要选择行时,校验是否设置主键
      notification.warning({
        top: 92,
        message: '未设置主键!',
        duration: 5
      })
      return
    }
 
    let primaryId = data[0][setting.primaryKey] || ''
 
    this.setState({loading: true})
    this.changeUser(primaryId)
  }
 
  /**
   * @description 切换用户
   */
  changeUser = (primaryId) => {
    const { setting, btn } = this.props
    let _this = this
 
    let param = {
      func: 'webapi_ChangeUser'
    }
 
    if (this.props.BID) {
      param.BID = this.props.BID
    }
 
    if (window.GLOB.mainSystemApi) {
      param.rduri = window.GLOB.mainSystemApi
    }
 
    param[setting.primaryKey] = primaryId
 
    confirm({
      title: this.state.dict['main.action.confirm.tip'],
      onOk() {
        return new Promise(resolve => {
          Api.genericInterface(param).then(res => {
            resolve()
            if (res.status) {
              sessionStorage.setItem('UserID', res.UserID)
              sessionStorage.setItem('LoginUID', res.LoginUID)
              sessionStorage.setItem('User_Name', res.UserName)
              sessionStorage.setItem('Full_Name', res.FullName)
              sessionStorage.setItem('avatar', res.icon || '')
              sessionStorage.setItem('dataM', res.dataM ? 'true' : '')
              sessionStorage.setItem('localDataM', res.dataM ? 'true' : '')
              sessionStorage.setItem('debug', res.debug || '')
              sessionStorage.setItem('role_id', res.role_id || '')
              sessionStorage.setItem('departmentcode', res.departmentcode || '')
              sessionStorage.setItem('organization', res.organization || '')
              sessionStorage.setItem('localRole_id', res.role_id || '')
              
              sessionStorage.removeItem('CloudAvatar')
              sessionStorage.removeItem('cloudDataM')
              sessionStorage.removeItem('CloudUserID')
              sessionStorage.removeItem('CloudUserName')
              sessionStorage.removeItem('CloudLoginUID')
              sessionStorage.removeItem('CloudFullName')
              
              window.location.reload()
            } else {
              notification.error({
                top: 92,
                message: res.message || res.ErrMesg,
                duration: btn.verify && btn.verify.ntime ? btn.verify.ntime : 10
              })
              _this.setState({loading: false})
            }
          }, () => {
            resolve()
            _this.setState({loading: false})
          })
        })
      },
      onCancel() {
        _this.setState({loading: false})
      }
    })
  }
 
  render() {
    const { btn, show } = this.props
    const { loading, disabled, hidden } = this.state
 
    if (hidden) return null
 
    if (show === 'actionList') {
      return (
        <Button
          icon={btn.icon}
          loading={loading}
          disabled={disabled}
          title={disabled ? (btn.reason || '') : ''}
          className={'mk-btn mk-' + btn.class}
          onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
        >{btn.label}</Button>
      )
    } else { // icon、text、 all 卡片
      let label = ''
      let icon = ''
 
      if (show === 'button') {
        label = btn.label
        icon = btn.icon || ''
      } else if (show === 'link') {
        label = <span>{btn.label}{btn.icon ? <MkIcon style={{marginLeft: '8px'}} type={btn.icon}/> : ''}</span>
        icon = ''
      } else if (show === 'icon') {
        icon = btn.icon || ''
      // } else if (show === 'text') {
      } else {
        label = btn.label
      }
 
      return (
        <Button
          type="link"
          title={disabled ? (btn.reason || '') : (show === 'icon' ? btn.label : '')}
          loading={loading}
          disabled={disabled}
          style={btn.style}
          icon={icon}
          onClick={(e) => {e.stopPropagation(); this.actionTrigger()}}
        >{label}</Button>
      )
    }
  }
}
 
export default NewPageButton