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
|
}
|
|
_param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') // 时间戳
|
_param.LText = Utils.formatOptions(_param.LText) // 关键字符替换,base64加密
|
_param.secretkey = Utils.encrypt(_param.LText, _param.timestamp) // md5密钥
|
|
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)
|
}
|
}
|