king
2021-10-18 4b6a4e2f04f492d770573cf48ca52d4e748a086a
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Button } from 'antd'
 
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
class AutoMatic extends Component {
  static propTpyes = {
    autoMatic: PropTypes.object,
    config: PropTypes.object
  }
 
  state = {
    running: false,
    line: 1,
    init: true
  }
 
  timer = null
 
  componentDidMount () {
    MKEmitter.addListener('autoGetData', this.autoGetData)
    MKEmitter.addListener('autoExecOver', this.autoExecOver)
    MKEmitter.addListener('autoMaticOver', this.autoMaticOver)
    MKEmitter.addListener('autoMaticError', this.autoMaticError)
    MKEmitter.addListener('autoTransSelectData', this.autoTransSelectData)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('autoGetData', this.autoGetData)
    MKEmitter.removeListener('autoExecOver', this.autoExecOver)
    MKEmitter.removeListener('autoMaticOver', this.autoMaticOver)
    MKEmitter.removeListener('autoMaticError', this.autoMaticError)
    MKEmitter.removeListener('autoTransSelectData', this.autoTransSelectData)
  }
 
  autoExecOver = (btnId, type) => {
    const { autoMatic, config } = this.props
 
    if (!this.state.running || btnId !== autoMatic.action) return
 
    if (type === 'error') {
      if (autoMatic.onFail === 'next') {
        this.setState({line: this.state.line + 1}, () => {
          setTimeout(() => {
            MKEmitter.emit('autoQueryData', config.MenuID, this.state.line)
          }, 1000)
        })
      } else if (autoMatic.onFail === 'stay') {
        setTimeout(() => {
          MKEmitter.emit('autoQueryData', config.MenuID, this.state.line)
        }, 1000)
      } else {
        this.setState({running: false})
      }
    } else {
      if (autoMatic.onSuccess === 'next') {
        this.setState({line: this.state.line + 1}, () => {
          setTimeout(() => {
            MKEmitter.emit('autoQueryData', config.MenuID, this.state.line)
          }, 1000)
        })
      } else if (autoMatic.onSuccess === 'stay') {
        setTimeout(() => {
          MKEmitter.emit('autoQueryData', config.MenuID, this.state.line)
        }, 1000)
      } else {
        this.setState({running: false})
      }
    }
  }
 
  autoTransSelectData = (MenuID, data) => {
    const { config, autoMatic } = this.props
 
    if (!this.state.running || MenuID !== config.MenuID) return
 
    setTimeout(() => {
      MKEmitter.emit('triggerBtnId', autoMatic.action, [data], 'autoMatic')
      if (['prompt', 'pop'].includes(autoMatic.OpenType)) {
        let delay = this.state.init && autoMatic.OpenType === 'pop' ? 2000 : 200
 
        setTimeout(() => {
          if (autoMatic.OpenType === 'prompt') {
            let node = document.querySelector('.ant-modal-confirm-btns >.ant-btn-primary')
            node && node.click()
          } else if (autoMatic.OpenType === 'pop') {
            MKEmitter.emit('triggerBtnPopSubmit', autoMatic.action)
          }
        }, delay)
      }
    }, 100)
  }
 
  autoMaticError = (MenuID) => {
    const { config } = this.props
 
    if (!this.state.running || MenuID !== config.MenuID) return
 
    this.setState({running: false})
  }
 
  autoGetData = (MenuID) => {
    const { config } = this.props
 
    if (!this.state.running || MenuID !== config.MenuID) return
    setTimeout(() => {
      MKEmitter.emit('autoSelectData', config.MenuID, this.state.line)
    }, 100)
  }
 
  trigger = () => {
    const { config } = this.props
    let running = !this.state.running
 
    MKEmitter.emit('autoQueryData', config.MenuID, 1)
 
    this.setState({running: running, line: 1})
    clearTimeout(this.timer)
  }
 
  autoMaticOver = (MenuID) => {
    const { config, autoMatic } = this.props
 
    if (!this.state.running || MenuID !== config.MenuID) return
 
    this.setState({running: false})
 
    if (autoMatic.onFinish !== 'over') {
      let interval = autoMatic.interval * 1000 || 10
 
      if (autoMatic.restart === 'first') {
        this.setState({line: 1})
      } else {
        this.setState({line: this.state.line + 1})
      }
 
      this.timer = setTimeout(() => {
        this.setState({running: true})
        MKEmitter.emit('autoQueryData', config.MenuID, this.state.line)
      }, interval)
    }
  }
 
  render() {
    const { running } = this.state
 
    return (
      <Button
        icon={running ? 'pause' : 'forward'}
        shape="circle"
        className={'auto-matic ' + (window.GLOB.systemType === 'production' ? 'low' : '')}
        onClick={this.trigger}
      />
    )
  }
}
 
export default AutoMatic