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) } }