import { defineStore } from 'pinia'; import { ref, computed } from 'vue'; import { getUserInfo, login, getCompanyStatus } from '@/apis/user'; import tokenInfo from '@/stores/modules/token'; import client from '@/stores/modules/client'; import { ElMessage } from 'element-plus'; export const useUserInfo = defineStore('userInfo', () => { // 状态(State) const tokenInfoStore = new tokenInfo() const loginShow = ref(false); // 控制登录弹窗的显示状态 const userInfo = ref({}); // 用户的详细信息 const isExpired = ref(false); // 企业账户是否过期 const expiredMessage = ref(''); // 过期提示消息 const supportCameraCounts = ref(1); // 支持的相机数量 // 多相机模式判断 // support_camera_counts > 1 则为多相机模式 const isMultiCameraMode = computed(() => { return supportCameraCounts.value > 1 }) const cameraMode = computed(() => { return supportCameraCounts.value > 1 ? 'multi' : 'single' }) // HLM用户判断 const isHLM = computed(() => { return userInfo.value?.brand_company_code === '1300' }) // 定时检查相关 let checkInterval: NodeJS.Timeout | null = null; // 定时器 const CHECK_INTERVAL = 10 * 60 * 1000; // 10分钟检查一次 // Actions(操作) /** * 更新用户信息。 * * @param {any} data - 新的用户信息数据。 */ const updateUserInfo = (data: any) => { userInfo.value = data; }; /** * 更新用户的登录令牌。 * * @param {string} data - 新的登录令牌。 */ const updateToken = (data: string) => { tokenInfoStore.updateToken(data) }; /** * 更新登录弹窗的显示状态。 * * @param {boolean} data - 是否显示登录弹窗。 */ const updateLoginShow = (data: boolean) => { loginShow.value = data; }; /** * 执行用户登录操作。 * * @param {any} data - 登录所需的用户凭据。 * @returns {Promise} 登录接口返回的结果。 * @throws {Error} 如果登录失败,抛出错误。 */ const loginAction = async (data: any) => { try { const res = await login(data); // 调用登录接口 await updateToken(res.data.token); // 更新登录令牌 // await getInfo(); // 获取用户信息 return res; } catch (error) { console.error('登录失败:', error); throw error; } }; /** * 执行用户退出登录操作。 * * @param {any} data - 退出登录所需的参数。 * @returns {Promise} 退出登录的结果。 * @throws {Error} 如果退出登录失败,抛出错误。 */ const loginOut = async (data: any) => { try { // 关闭所有子窗口 try { const clientStore = client(); await clientStore.ipc.invoke('controller.utils.closeAllWindows'); console.log('所有子窗口已关闭'); } catch (error) { console.error('关闭子窗口失败:', error); // 关闭窗口失败不影响退出登录流程 } // 停止定期检查 stopPeriodicCheck(); // 清空用户信息 try { tokenInfoStore.clearToken() } catch (e) { await updateToken('') } await updateUserInfo({}); // 跳转到首页 try { // 在Electron环境中使用正确的协议 if (window.location.protocol === 'file:') { window.location.href = window.location.href.split('#')[0] + '#/'; window.location.reload() } else { window.location.href = '/'; } console.log('已跳转到首页'); } catch (error) { console.error('跳转到首页失败:', error); // 跳转失败不影响退出登录流程 } } catch (error) { console.error('退出登录失败:', error); throw error; } }; /** * 检查企业状态并处理跳转 * @returns {Promise} 是否过期 */ const checkCompanyStatus = async (): Promise => { try { const statusRes = await getCompanyStatus({}); const statusData = statusRes.data; if (statusData && statusData.status === 0) { // 已过期,设置过期状态 isExpired.value = true; expiredMessage.value = statusData.message || '企业账户已过期,请联系管理员'; // 触发自定义事件,通知组件进行跳转 if (typeof window !== 'undefined') { window.dispatchEvent(new CustomEvent('companyExpired', { detail: { message: expiredMessage.value } })); } return true; } else { // 未过期,重置状态 isExpired.value = false; expiredMessage.value = ''; // 保存支持的相机数量 if (statusData?.support_camera_counts) { supportCameraCounts.value = statusData.support_camera_counts; } return false; } } catch (statusError) { console.error('获取企业状态失败:', statusError); // 企业状态获取失败不影响正常使用,只记录错误 isExpired.value = false; expiredMessage.value = ''; return false; } }; /** * 启动定期检查企业状态 */ const startPeriodicCheck = () => { // 清除已有的定时器 if (checkInterval) { clearInterval(checkInterval); } // 设置新的定时器,每5分钟检查一次 checkInterval = setInterval(async () => { await checkCompanyStatus(); }, CHECK_INTERVAL); console.log('企业状态定期检查已启动'); }; /** * 停止定期检查企业状态 */ const stopPeriodicCheck = () => { if (checkInterval) { clearInterval(checkInterval); checkInterval = null; console.log('企业状态定期检查已停止'); } }; /** * 获取用户信息并更新状态。 * * @returns {Promise} 用户信息数据。 * @throws {Error} 如果获取用户信息失败,抛出错误。 */ const getInfo = async () => { try { const res = await getUserInfo({ device:'aigc', }); // 调用获取用户信息接口 const { data } = res; if (!data) { try { tokenInfoStore.clearToken() } catch (e) { updateToken('') } throw new Error('请重新登录!'); } updateUserInfo(data); // 更新用户信息 // 获取企业状态,检查是否过期 await checkCompanyStatus(); // 启动定期检查 startPeriodicCheck(); return data; } catch (error) { console.error('获取用户信息失败:', error); throw error; } }; return { loginShow, userInfo, isExpired, expiredMessage, supportCameraCounts, isMultiCameraMode, cameraMode, isHLM, updateUserInfo, updateLoginShow, loginAction, loginOut, getInfo, checkCompanyStatus, startPeriodicCheck, stopPeriodicCheck, }; }); export default useUserInfo;