Files
autoAiWorkSys/app/mixins/qrCodeMixin.js
张成 e17d5610f5 1
2025-12-22 16:26:59 +08:00

221 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 二维码管理 Mixin
* 二维码刷新逻辑在渲染层处理,主进程只负责执行获取二维码的操作
*/
export default {
data() {
return {
qrCodeAutoRefreshInterval: null, // 二维码自动刷新定时器
qrCodeCountdownInterval: null, // 倒计时定时器
};
},
computed: {
qrCodeUrl() {
return this.$store ? this.$store.state.qrCode.qrCodeUrl : null;
},
qrCodeCountdown() {
return this.$store ? this.$store.state.qrCode.qrCodeCountdown : 0;
},
qrCodeCountdownActive() {
return this.$store ? this.$store.state.qrCode.qrCodeCountdownActive : false;
},
qrCodeExpired() {
return this.$store ? this.$store.state.qrCode.qrCodeExpired : false;
},
qrCodeRefreshCount() {
return this.$store ? this.$store.state.qrCode.qrCodeRefreshCount : 0;
},
isPlatformLoggedIn() {
return this.$store ? this.$store.state.platform.isPlatformLoggedIn : false;
}
},
methods: {
/**
* 获取二维码
*/
async getQrCode() {
try {
if (!window.electronAPI || !window.electronAPI.invoke) {
console.error('[二维码] electronAPI 不可用');
return;
}
const result = await window.electronAPI.invoke('command:execute', {
platform: 'boss',
action: 'get_login_qr_code',
data: { type: 'app' },
source: 'renderer'
});
if (result.success && result.data) {
const qrCodeUrl = result.data.qrCodeUrl || result.data.qr_code_url || result.data.oos_url || result.data.imageData;
const qrCodeOosUrl = result.data.qrCodeOosUrl || result.data.oos_url || null;
if (qrCodeUrl && this.$store) {
this.$store.dispatch('qrCode/setQrCode', { url: qrCodeUrl, oosUrl: qrCodeOosUrl });
if (this.addLog) {
this.addLog('success', '二维码已获取');
}
return true;
}
} else {
console.error('[二维码] 获取失败:', result.error || '未知错误');
if (this.addLog) {
this.addLog('error', `获取二维码失败: ${result.error || '未知错误'}`);
}
}
} catch (error) {
console.error('[二维码] 获取失败:', error);
if (this.addLog) {
this.addLog('error', `获取二维码失败: ${error.message}`);
}
}
return false;
},
/**
* 启动二维码自动刷新
*/
startQrCodeAutoRefresh() {
// 如果定时器已存在,不重复启动
if (this.qrCodeAutoRefreshInterval) {
return;
}
// 如果平台已登录,不启动自动刷新
if (this.isPlatformLoggedIn) {
return;
}
const QR_CODE_REFRESH_INTERVAL = 25000; // 25秒
const MAX_REFRESH_COUNT = 3; // 最大刷新次数
// 重置刷新次数
if (this.$store) {
this.$store.dispatch('qrCode/setQrCodeCountdown', {
countdown: 25,
isActive: true,
refreshCount: 0,
isExpired: false
});
}
// 立即获取一次二维码
this.getQrCode();
// 启动定时器每25秒刷新一次
this.qrCodeAutoRefreshInterval = setInterval(async () => {
// 检查平台登录状态
if (this.isPlatformLoggedIn) {
this.stopQrCodeAutoRefresh();
return;
}
// 检查刷新次数
const refreshCount = this.qrCodeRefreshCount;
if (refreshCount >= MAX_REFRESH_COUNT) {
// 已达到最大次数
if (this.$store) {
this.$store.dispatch('qrCode/setQrCodeCountdown', {
countdown: 0,
isActive: false,
refreshCount: refreshCount,
isExpired: true
});
}
this.stopQrCodeAutoRefresh();
return;
}
// 刷新二维码
const success = await this.getQrCode();
if (success) {
// 更新刷新次数
if (this.$store) {
this.$store.dispatch('qrCode/setQrCodeCountdown', {
countdown: 25,
isActive: true,
refreshCount: refreshCount + 1,
isExpired: false
});
}
}
}, QR_CODE_REFRESH_INTERVAL);
// 启动倒计时
this.startQrCodeCountdown();
},
/**
* 停止二维码自动刷新
*/
stopQrCodeAutoRefresh() {
if (this.qrCodeAutoRefreshInterval) {
clearInterval(this.qrCodeAutoRefreshInterval);
this.qrCodeAutoRefreshInterval = null;
}
this.stopQrCodeCountdown();
if (this.$store) {
this.$store.dispatch('qrCode/setQrCodeCountdown', {
countdown: 0,
isActive: false,
refreshCount: this.qrCodeRefreshCount,
isExpired: this.qrCodeExpired
});
}
},
/**
* 启动二维码倒计时
*/
startQrCodeCountdown() {
// 如果倒计时定时器已存在,不重复启动
if (this.qrCodeCountdownInterval) {
return;
}
// 每秒更新一次倒计时
this.qrCodeCountdownInterval = setInterval(() => {
if (this.qrCodeCountdownActive && this.qrCodeCountdown > 0) {
const newCountdown = this.qrCodeCountdown - 1;
if (this.$store) {
this.$store.dispatch('qrCode/setQrCodeCountdown', {
countdown: newCountdown,
isActive: true,
refreshCount: this.qrCodeRefreshCount,
isExpired: false
});
}
} else {
// 倒计时结束
this.stopQrCodeCountdown();
}
}, 1000);
},
/**
* 停止二维码倒计时
*/
stopQrCodeCountdown() {
if (this.qrCodeCountdownInterval) {
clearInterval(this.qrCodeCountdownInterval);
this.qrCodeCountdownInterval = null;
}
},
},
watch: {
// 监听平台登录状态,如果已登录则停止刷新
isPlatformLoggedIn(newVal) {
if (newVal) {
this.stopQrCodeAutoRefresh();
}
}
},
beforeUnmount() {
// 组件销毁时清理定时器
this.stopQrCodeAutoRefresh();
}
};