This commit is contained in:
张成
2025-12-22 16:26:59 +08:00
parent aa2d03ee30
commit e17d5610f5
54 changed files with 11735 additions and 3 deletions

230
app/mixins/authMixin.js Normal file
View File

@@ -0,0 +1,230 @@
/**
* 用户认证管理 Mixin
*/
import { getToken } from '../utils/api';
export default {
data() {
return {
phone: '',
password: '',
isLoggedIn: false,
loginButtonText: '登录',
userName: '',
remainingDays: null,
snCode: '',
deviceId: '-',
listenChannel: '-',
userMenuInfo: {
userName: '',
snCode: ''
}
};
},
methods: {
async loadSavedConfig() {
try {
// 从 store 加载保存的手机号
if (this.$store) {
const savedPhone = this.$store.state.config.phone || this.$store.state.auth.phone;
if (savedPhone) {
this.phone = savedPhone;
}
}
// 注意:现在数据都在 store 中,通过持久化插件自动恢复
// 如果需要从主进程同步数据,可以在这里调用,但通常不需要
// 因为登录成功后已经通过 syncUserInfo 同步到主进程了
} catch (error) {
console.error('加载配置失败:', error);
if (this.addLog) {
this.addLog('error', `加载配置失败: ${error.message}`);
}
}
},
// checkActivationStatus 方法已移除
// 现在登录状态由 Vuex Store 管理,通过持久化插件自动恢复
// 不再需要从主进程获取状态
// 用户登录(只调用主进程接口,业务逻辑由主进程处理)
async userLogin(password, rememberMe = true) {
// 基本验证
if (!this.phone) {
if (this.addLog) {
this.addLog('error', '请输入手机号');
}
return { success: false, error: '请输入手机号' };
}
if (!password) {
if (this.addLog) {
this.addLog('error', '请输入密码');
}
return { success: false, error: '请输入密码' };
}
if (!window.electronAPI) {
if (this.addLog) {
this.addLog('error', 'Electron API不可用');
}
return { success: false, error: 'Electron API不可用' };
}
try {
if (this.addLog) {
this.addLog('info', `正在使用手机号 ${this.phone} 登录...`);
}
const result = await window.electronAPI.invoke('auth:login', {
phone: this.phone,
password: password
});
if (result.success && result.data) {
// 登录成功,通过 store 更新状态(业务逻辑由主进程处理)
if (this.$store) {
await this.$store.dispatch('auth/login', {
phone: this.phone,
password: password,
deviceId: result.data.device_id || ''
});
if (rememberMe) {
this.$store.dispatch('config/setRememberMe', true);
this.$store.dispatch('config/setPhone', this.phone);
}
}
// MQTT 连接由主进程自动处理,这里只检查状态
if (this.checkMQTTStatus) {
setTimeout(() => {
this.checkMQTTStatus();
}, 1000);
}
// 开始获取任务状态
if (this.startTaskStatusUpdate) {
this.startTaskStatusUpdate();
}
return { success: true, data: result.data };
} else {
if (this.addLog) {
this.addLog('error', `登录失败: ${result.error || '未知错误'}`);
}
return { success: false, error: result.error || '未知错误' };
}
} catch (error) {
if (this.addLog) {
this.addLog('error', `登录过程中发生错误: ${error.message}`);
}
return { success: false, error: error.message };
}
},
async tryAutoLogin() {
try {
if (!this.$store) {
return false;
}
// 从 store 检查是否有保存的登录信息
const savedPhone = this.$store.state.config.phone || this.$store.state.auth.phone;
const userLoggedOut = this.$store.state.config.userLoggedOut || this.$store.state.auth.userLoggedOut;
// 如果用户手动退出,不自动登录
if (userLoggedOut) {
return false;
}
if (!savedPhone) {
return false;
}
// 检查 store 中是否有有效的登录信息token 和用户信息)
const token = getToken();
const storeSnCode = this.$store ? this.$store.state.auth.snCode : '';
const storeUserName = this.$store ? this.$store.state.auth.userName : '';
// 如果有 token 和用户信息,说明已登录(数据已通过持久化插件恢复)
if (token && (storeSnCode || storeUserName)) {
// 更新登录状态
this.$store.commit('auth/SET_LOGGED_IN', true);
this.$store.commit('auth/SET_LOGIN_BUTTON_TEXT', '注销登录');
if (this.addLog) {
this.addLog('info', '自动登录成功');
}
// 连接MQTT
// MQTT 连接由主进程自动处理,这里只检查状态
if (this.checkMQTTStatus) {
setTimeout(() => {
this.checkMQTTStatus();
}, 1000);
}
return true; // 自动登录成功
}
return false; // 未登录
} catch (error) {
console.error('自动登录失败:', error);
if (this.addLog) {
this.addLog('error', `自动登录失败: ${error.message}`);
}
return false;
}
},
// 注销登录(只调用主进程接口,业务逻辑由主进程处理)
async logoutDevice() {
if (!window.electronAPI) {
if (this.addLog) {
this.addLog('error', 'Electron API不可用');
}
return;
}
try {
if (this.addLog) {
this.addLog('info', '正在注销登录...');
}
await window.electronAPI.invoke('auth:logout');
// 停止任务状态更新
if (this.stopTaskStatusUpdate) {
this.stopTaskStatusUpdate();
}
// 更新 store 状态
if (this.$store) {
this.$store.dispatch('auth/logout');
this.$store.dispatch('config/setUserLoggedOut', true);
}
if (this.addLog) {
this.addLog('success', '注销登录成功');
}
// 触发跳转到登录页面
if (this.$emit) {
this.$emit('logout-success');
}
} catch (error) {
if (this.addLog) {
this.addLog('error', `注销登录异常: ${error.message}`);
}
}
}
},
watch: {
snCode(newVal) {
this.userMenuInfo.snCode = newVal;
}
}
};