king
2024-12-12 116dcaad9eb1d4d3021076e0454083c19b8e1c79
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
import { notification } from 'antd'
import moment from 'moment'
 
import Api from '@/api'
import Utils from '@/utils/utils.js'
 
export default class timerTask {
  constructor() {
    this.timer = null
    this.interval = null
    this.repeats = 0
  }
 
  init(uuid, interval, repeats, callback) {
    const _change = { '2s': 2000, '5s': 5000, '15s': 15000, '30s': 30000, '1min': 60000, '5min': 300000, '10min': 600000, '15min': 900000, '30min': 1800000, '1hour': 3600000 }
 
    this.interval = _change[interval]
    this.repeats = repeats || 0
 
    if (!this.interval) return
 
    if (this.repeats > 0 && this.repeats <=3) {
      this.timer = setTimeout(() => {
        this.timerTask(this.repeats, callback)
      }, this.interval)
    } else {
      let _param = {
        func: 's_get_timers_role',
        LText: `select '${window.GLOB.appkey || ''}','${uuid}'`, // 只用做密钥验证,已无效
        timer_type: interval,
        component_id: uuid
      }
 
      if (window.GLOB.execType === 'x') {
        _param.exec_type = 'x'
      }
      
      _param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
      _param.LText = Utils.formatOptions(_param.LText, _param.exec_type)
      _param.secretkey = Utils.encrypt(window.GLOB.execType === 'x' ? '' : _param.LText, _param.timestamp)
  
      Api.getSystemConfig(_param).then(result => {
        if (!result.status) {
          notification.warning({
            top: 92,
            message: result.message,
            duration: 5
          })
        } else if (result.run_type) {
          this.timer = setTimeout(() => {
            this.timerTask(this.repeats, callback)
          }, this.interval)
        }
      })
    }
  }
 
  timerTask(times, callback) {
    callback()
    if (this.repeats) {
      times = times - 1
      if (times <= 0) {
        clearTimeout(this.timer)
        return
      }
    }
    this.timer = setTimeout(() => {
      this.timerTask(times, callback)
    }, this.interval)
  }
 
  stop() {
    clearTimeout(this.timer)
  }
}