|
@@ -1,9 +1,9 @@
|
|
|
import { defineStore } from 'pinia';
|
|
import { defineStore } from 'pinia';
|
|
|
import { ref, computed } from 'vue';
|
|
import { ref, computed } from 'vue';
|
|
|
-import { getUserInfo, login } from '@/apis/user';
|
|
|
|
|
|
|
+import { getUserInfo, login, getCompanyStatus } from '@/apis/user';
|
|
|
import tokenInfo from '@/stores/modules/token';
|
|
import tokenInfo from '@/stores/modules/token';
|
|
|
import client from '@/stores/modules/client';
|
|
import client from '@/stores/modules/client';
|
|
|
-import { useRouter } from 'vue-router';
|
|
|
|
|
|
|
+import { ElMessage } from 'element-plus';
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useUserInfo = defineStore('userInfo', () => {
|
|
export const useUserInfo = defineStore('userInfo', () => {
|
|
@@ -13,6 +13,12 @@ export const useUserInfo = defineStore('userInfo', () => {
|
|
|
|
|
|
|
|
const loginShow = ref(false); // 控制登录弹窗的显示状态
|
|
const loginShow = ref(false); // 控制登录弹窗的显示状态
|
|
|
const userInfo = ref({}); // 用户的详细信息
|
|
const userInfo = ref({}); // 用户的详细信息
|
|
|
|
|
+ const isExpired = ref(false); // 企业账户是否过期
|
|
|
|
|
+ const expiredMessage = ref(''); // 过期提示消息
|
|
|
|
|
+
|
|
|
|
|
+ // 定时检查相关
|
|
|
|
|
+ let checkInterval: NodeJS.Timeout | null = null; // 定时器
|
|
|
|
|
+ const CHECK_INTERVAL = 10 * 60 * 1000; // 10分钟检查一次
|
|
|
|
|
|
|
|
// Actions(操作)
|
|
// Actions(操作)
|
|
|
|
|
|
|
@@ -83,6 +89,9 @@ export const useUserInfo = defineStore('userInfo', () => {
|
|
|
// 关闭窗口失败不影响退出登录流程
|
|
// 关闭窗口失败不影响退出登录流程
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 停止定期检查
|
|
|
|
|
+ stopPeriodicCheck();
|
|
|
|
|
+
|
|
|
// 清空用户信息
|
|
// 清空用户信息
|
|
|
try {
|
|
try {
|
|
|
tokenInfoStore.clearToken()
|
|
tokenInfoStore.clearToken()
|
|
@@ -112,6 +121,70 @@ export const useUserInfo = defineStore('userInfo', () => {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查企业状态并处理跳转
|
|
|
|
|
+ * @returns {Promise<boolean>} 是否过期
|
|
|
|
|
+ */
|
|
|
|
|
+ const checkCompanyStatus = async (): Promise<boolean> => {
|
|
|
|
|
+ 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 = '';
|
|
|
|
|
+ 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<any>} 用户信息数据。
|
|
* @returns {Promise<any>} 用户信息数据。
|
|
@@ -132,6 +205,13 @@ export const useUserInfo = defineStore('userInfo', () => {
|
|
|
throw new Error('请重新登录!');
|
|
throw new Error('请重新登录!');
|
|
|
}
|
|
}
|
|
|
updateUserInfo(data); // 更新用户信息
|
|
updateUserInfo(data); // 更新用户信息
|
|
|
|
|
+
|
|
|
|
|
+ // 获取企业状态,检查是否过期
|
|
|
|
|
+ await checkCompanyStatus();
|
|
|
|
|
+
|
|
|
|
|
+ // 启动定期检查
|
|
|
|
|
+ startPeriodicCheck();
|
|
|
|
|
+
|
|
|
return data;
|
|
return data;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
console.error('获取用户信息失败:', error);
|
|
console.error('获取用户信息失败:', error);
|
|
@@ -142,11 +222,16 @@ export const useUserInfo = defineStore('userInfo', () => {
|
|
|
return {
|
|
return {
|
|
|
loginShow,
|
|
loginShow,
|
|
|
userInfo,
|
|
userInfo,
|
|
|
|
|
+ isExpired,
|
|
|
|
|
+ expiredMessage,
|
|
|
updateUserInfo,
|
|
updateUserInfo,
|
|
|
updateLoginShow,
|
|
updateLoginShow,
|
|
|
loginAction,
|
|
loginAction,
|
|
|
loginOut,
|
|
loginOut,
|
|
|
getInfo,
|
|
getInfo,
|
|
|
|
|
+ checkCompanyStatus,
|
|
|
|
|
+ startPeriodicCheck,
|
|
|
|
|
+ stopPeriodicCheck,
|
|
|
};
|
|
};
|
|
|
});
|
|
});
|
|
|
|
|
|