/** * 用户认证状态管理 */ import { login as apiLogin, setToken, clearToken, getToken } from '../../utils/api'; export default { namespaced: true, state: { email: '', password: '', isLoggedIn: false, loginButtonText: '登录', userName: '', remainingDays: null, snCode: '', deviceId: null, platformType: null, userId: null, listenChannel: '-', userLoggedOut: false, rememberMe: false, userMenuInfo: { userName: '', snCode: '' } }, mutations: { SET_EMAIL(state, email) { state.email = email; }, SET_PASSWORD(state, password) { state.password = password; }, SET_LOGGED_IN(state, isLoggedIn) { state.isLoggedIn = isLoggedIn; }, SET_LOGIN_BUTTON_TEXT(state, text) { state.loginButtonText = text; }, SET_USER_NAME(state, userName) { state.userName = userName; state.userMenuInfo.userName = userName; }, SET_REMAINING_DAYS(state, days) { state.remainingDays = days; }, SET_SN_CODE(state, snCode) { state.snCode = snCode; state.userMenuInfo.snCode = snCode; state.listenChannel = snCode ? `request_${snCode}` : '-'; }, SET_DEVICE_ID(state, deviceId) { state.deviceId = deviceId; }, SET_PLATFORM_TYPE(state, platformType) { state.platformType = platformType; }, SET_USER_ID(state, userId) { state.userId = userId; }, SET_USER_LOGGED_OUT(state, value) { state.userLoggedOut = value; }, SET_REMEMBER_ME(state, value) { state.rememberMe = value; }, SET_USER_MENU_INFO(state, info) { state.userMenuInfo = { ...state.userMenuInfo, ...info }; }, CLEAR_AUTH(state) { state.isLoggedIn = false; state.loginButtonText = '登录'; state.listenChannel = '-'; state.snCode = ''; state.deviceId = null; state.platformType = null; state.userId = null; state.userName = ''; state.remainingDays = null; state.userMenuInfo = { userName: '', snCode: '' }; state.password = ''; state.userLoggedOut = true; } }, actions: { /** * 从 store 恢复登录状态 * 状态已通过持久化插件从 localStorage 恢复 */ async restoreLoginStatus({ commit, state }) { try { const token = getToken(); console.log('[Auth Store] 尝试恢复登录状态:', { hasToken: !!token, userLoggedOut: state.userLoggedOut, snCode: state.snCode, userName: state.userName, isLoggedIn: state.isLoggedIn, persistedState: { email: state.email, platformType: state.platformType, userId: state.userId, deviceId: state.deviceId } }); // 如果有 token 且用户没有手动退出,且有用户信息,则恢复登录状态 if (token && !state.userLoggedOut && (state.snCode || state.userName)) { console.log('[Auth Store] 满足恢复登录条件,开始恢复...'); commit('SET_LOGGED_IN', true); commit('SET_LOGIN_BUTTON_TEXT', '注销登录'); // 恢复登录状态后,同步用户信息到主进程(确保 platform_type 等数据同步) if (window.electronAPI && window.electronAPI.invoke) { try { await window.electronAPI.invoke('auth:sync-user-info', { platform_type: state.platformType || null, sn_code: state.snCode || '', user_id: state.userId || null, user_name: state.userName || '', device_id: state.deviceId || null }); console.log('[Auth Store] 用户信息已同步到主进程'); // 恢复登录状态后,手动连接 MQTT if (state.snCode) { try { const mqttResult = await window.electronAPI.invoke('mqtt:connect', state.snCode); console.log('[Auth Store] 恢复登录后 MQTT 连接结果:', mqttResult); // 如果连接成功,立即更新MQTT状态到store if (mqttResult && mqttResult.success && mqttResult.isConnected) { commit('mqtt/SET_CONNECTED', true, { root: true }); commit('mqtt/SET_STATUS', '已连接', { root: true }); console.log('[Auth Store] 恢复登录后 MQTT 状态已更新到 store'); } } catch (mqttError) { console.warn('[Auth Store] 恢复登录后 MQTT 连接失败:', mqttError); } } } catch (error) { console.warn('[Auth Store] 恢复登录状态时同步用户信息到主进程失败:', error); } } else { console.warn('[Auth Store] electronAPI 不可用,无法同步用户信息到主进程'); } console.log('[Auth Store] 登录状态恢复完成'); } else { console.log('[Auth Store] 不满足恢复登录条件,跳过恢复'); } } catch (error) { console.error('[Auth Store] 恢复登录状态失败:', error); } }, /** * 用户登录 * @param {Object} context Vuex context * @param {Object} payload 登录参数 * @param {string} payload.email 邮箱 * @param {string} payload.password 密码 * @param {string} payload.deviceId 设备ID(可选) * @returns {Promise} 登录结果 */ async login({ commit }, { email, password, deviceId = null }) { try { // 调用登录接口 const response = await apiLogin(email, password, deviceId); if (response.code === 0 && response.data) { // 登录成功,更新状态 const { token, user, device_id } = response.data; // token 已在 apiLogin 中设置,这里不需要重复设置 commit('SET_EMAIL', email); commit('SET_PASSWORD', password); commit('SET_LOGGED_IN', true); commit('SET_LOGIN_BUTTON_TEXT', '注销登录'); commit('SET_USER_NAME', user?.name || email); commit('SET_REMAINING_DAYS', user?.remaining_days || null); commit('SET_SN_CODE', user?.sn_code || ''); commit('SET_DEVICE_ID', device_id || deviceId); commit('SET_PLATFORM_TYPE', user?.platform_type || null); commit('SET_USER_ID', user?.id || null); commit('SET_USER_LOGGED_OUT', false); // 登录成功后,同步更新 platform store 的显示名称 if (user?.platform_type) { const platformNames = { 'boss': 'BOSS直聘', 'liepin': '猎聘', 'zhilian': '智联招聘', '1': 'BOSS直聘' }; const displayName = platformNames[user.platform_type] || user.platform_type; commit('platform/SET_CURRENT_PLATFORM', displayName, { root: true }); // 初始化平台登录状态为未登录(需要用户通过平台登录) commit('platform/SET_PLATFORM_LOGIN_STATUS', { status: '未登录', color: '#FF9800', isLoggedIn: false }, { root: true }); console.log('[Auth Store] 平台信息已初始化'); } // 同步用户信息到主进程的 authService(确保主进程也能获取到 platform_type 和 token) if (window.electronAPI && window.electronAPI.invoke) { try { await window.electronAPI.invoke('auth:sync-user-info', { token: token, // 同步 token 到主进程 platform_type: user?.platform_type || null, sn_code: user?.sn_code || '', user_id: user?.id || null, user_name: user?.name || email, // 修复:使用 email 而不是 phone device_id: device_id || deviceId }); // 登录成功后,手动连接 MQTT if (user?.sn_code) { try { const mqttResult = await window.electronAPI.invoke('mqtt:connect', user.sn_code); console.log('[Auth Store] MQTT 连接结果:', mqttResult); // 如果连接成功,立即更新MQTT状态到store if (mqttResult && mqttResult.success && mqttResult.isConnected) { commit('mqtt/SET_CONNECTED', true, { root: true }); commit('mqtt/SET_STATUS', '已连接', { root: true }); console.log('[Auth Store] MQTT 状态已更新到 store'); } } catch (mqttError) { console.warn('[Auth Store] MQTT 连接失败:', mqttError); } } } catch (error) { console.warn('[Auth Store] 同步用户信息到主进程失败:', error); } } return { success: true, data: response.data }; } else { // 登录失败 return { success: false, error: response.message || '登录失败' }; } } catch (error) { console.error('[Auth Store] 登录失败:', error); return { success: false, error: error.message || '登录过程中发生错误' }; } }, logout({ commit }) { commit('CLEAR_AUTH'); clearToken(); }, updateUserInfo({ commit }, userInfo) { if (userInfo.name) commit('SET_USER_NAME', userInfo.name); if (userInfo.sn_code) commit('SET_SN_CODE', userInfo.sn_code); if (userInfo.device_id) commit('SET_DEVICE_ID', userInfo.device_id); if (userInfo.remaining_days !== undefined) { commit('SET_REMAINING_DAYS', userInfo.remaining_days); } } }, getters: { isLoggedIn: state => state.isLoggedIn, userInfo: state => ({ email: state.email, userName: state.userName, snCode: state.snCode, deviceId: state.deviceId, remainingDays: state.remainingDays }) } };