TodaySummaryView.vue 3.27 KB
<template>
	<div class="summary-page">
		<div class="content-item">
			<div class="task-stats">
				<div class="stat-item">
					<span class="stat-label">任务总数</span>
					<span class="stat-value success">{{ summary.totalTask }}</span>
				</div>
				<div class="stat-item">
					<span class="stat-label">进行中任务数</span>
					<span class="stat-value success">{{ summary.uncompletedTask }}</span>
				</div>
				<div class="stat-item">
					<span class="stat-label">完成任务数</span>
					<span class="stat-value success">{{ summary.completedTask }}</span>
				</div>
				<div class="stat-item">
					<span class="stat-label">平均耗时</span>
					<span class="stat-value success">{{ summary.averageTaskDuration }}</span>
				</div>
				<div class="stat-item">
					<span class="stat-label">异常任务数</span>
					<span class="stat-value error">{{ summary.exceptionTask }}</span>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import { CMC_OFFLINE_STATUS_API } from '@/api/cmcApi'

export default {
	name: 'TodaySummaryView',
	props: {
		userName: {
			type: String,
			default: '',
		},
	},
	watch: {
		userName: {
			immediate: true,
			handler(newVal) {
				if (newVal) {
					// this.getData()
				}
			},
		},
	},
	data() {
		return {
			baseUrlOff: CMC_OFFLINE_STATUS_API,
			baseUrlOnLine: window.appConfig.baseUrlintThree,
			sysData: {},
			timer: null,
			deviceStatus: [],
			summary: {},
			drawer: false,
			direction: 'rtl',
			device: {},
			temp: 0,

			// 新增:定时器控制字段
			refreshTimer: null, // 全局刷新定时器ID
		}
	},
	methods: {
		getData() {
			const opt = {
				urlSuffix: window.baseOnLineOrOff ? `${this.baseUrlOnLine}?zoneCode=${this.userName}` : `${this.baseUrlOnLine}?zoneCode=${this.userName}`,
				logTitle: '任务耗时',
				isUrlALL: true,
				headers: window.baseOnLineOrOff,
				header: window.baseOnLineOrOff,
				type: 'post',
			}
			const callBackFn = (res) => {
				if (!this.ajaxSuccessDataBefore(res, opt.logTitle)) return
				this.summary = res.data.result
			}
			''.ajax(this, opt, callBackFn)
		},

		ajaxSuccessDataBefore(res, title) {
			if (!res || !res.data || res.data.result == null || res.data.result.length === 0) {
				this.sysData = []
				''.Log(`${title}无数据`, 'getData')
				return false
			}
			return true
		},

		// 修改:定时器启动方法
		intInterval: function() {
			// 清理现有定时器
			if (this.refreshTimer) {
				clearInterval(this.refreshTimer)
			}

			// 启动新的定时器
			this.refreshTimer = setInterval(() => {
				this.getData()
			}, 10000)

		},

		// 新增:暂停接口请求
		pauseApiRequest() {
			// 停止全局刷新定时器
			if (this.refreshTimer) {
				clearInterval(this.refreshTimer)
				this.refreshTimer = null
			}
		},

		// 新增:恢复接口请求
		resumeApiRequest() {
			// 重启全局刷新
			if (!this.refreshTimer) {
				this.intInterval()
			}
		},
	},
	mounted() {
		this.getData()
		this.intInterval()
	},

	// 新增:组件销毁时停止定时器
	beforeDestroy() {
		this.pauseApiRequest()
	},

	// 新增:激活组件时恢复请求
	activated() {
		this.resumeApiRequest()
	},

	// 新增:停用组件时暂停请求
	deactivated() {
		this.pauseApiRequest()
	},
}
</script>

<style src="../../styles/today-summary.css"></style>