From 4b6a4e2f04f492d770573cf48ca52d4e748a086a Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期一, 18 十月 2021 18:30:50 +0800
Subject: [PATCH] 2021-10-18

---
 src/tabviews/zshare/automatic/index.jsx |  125 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 120 insertions(+), 5 deletions(-)

diff --git a/src/tabviews/zshare/automatic/index.jsx b/src/tabviews/zshare/automatic/index.jsx
index e89a1b0..717528c 100644
--- a/src/tabviews/zshare/automatic/index.jsx
+++ b/src/tabviews/zshare/automatic/index.jsx
@@ -3,7 +3,7 @@
 import { is, fromJS } from 'immutable'
 import { Button } from 'antd'
 
-// import MKEmitter from '@/utils/events.js'
+import MKEmitter from '@/utils/events.js'
 import './index.scss'
 
 class AutoMatic extends Component {
@@ -14,7 +14,18 @@
 
   state = {
     running: false,
-    line: 0
+    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) {
@@ -28,15 +39,119 @@
     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
+    const { config } = this.props
     let running = !this.state.running
 
-    // MKEmitter.emit('autoQueryData', config.MenuID, 0)
+    MKEmitter.emit('autoQueryData', config.MenuID, 1)
 
-    this.setState({running: running})
+    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() {

--
Gitblit v1.8.0