云
8 小时以前 fb4db4ae9b6a7f113ba049f6789e6916c0dcc276
src/store/auth.ts
@@ -21,6 +21,7 @@
  socialLogin,
} from '#/api';
import { $t } from '#/locales';
import { resetTerminal, safeDestination, terminalHome, terminalType } from '#/mobile/terminal';
export const useAuthStore = defineStore('auth', () => {
  const accessStore = useAccessStore();
@@ -28,6 +29,7 @@
  const router = useRouter();
  const loginLoading = ref(false);
  const loadedTerminal = ref<string>();
  /**
   * 异步处理登录操作
@@ -67,6 +69,7 @@
      // 如果成功获取到 accessToken
      if (accessToken) {
        accessStore.setIsAccessChecked(false);
        accessStore.setAccessToken(accessToken);
        accessStore.setRefreshToken(refreshToken);
@@ -87,11 +90,12 @@
          onSuccess
            ? await onSuccess?.()
            : await router.push(
                userInfo.homePath || preferences.app.defaultHomePath,
                safeDestination(router.currentRoute.value.query.redirect,
                  terminalHome() || userInfo.homePath || preferences.app.defaultHomePath),
              );
        }
        if (userInfo?.nickname) {
        if (userInfo?.nickname && terminalType.value === 'pc') {
          notification.success({
            description: `${$t('authentication.loginSuccessDesc')}:${userInfo?.nickname}`,
            duration: 3,
@@ -108,7 +112,7 @@
    };
  }
  async function logout(redirect: boolean = true) {
  async function logout(redirect: boolean = true, preserveTerminal: boolean = false) {
    try {
      const accessToken = accessStore.accessToken as string;
      if (accessToken) {
@@ -118,6 +122,8 @@
      // 不做任何处理
    }
    resetAllStores();
    if (!preserveTerminal) resetTerminal();
    loadedTerminal.value = undefined;
    accessStore.setLoginExpired(false);
    // 回登录页带上当前路由地址
@@ -135,18 +141,20 @@
    // 加载
    // eslint-disable-next-line no-useless-assignment
    let authPermissionInfo: AuthPermissionInfo | null = null;
    authPermissionInfo = await getAuthPermissionInfoApi();
    authPermissionInfo = await getAuthPermissionInfoApi(terminalType.value);
    // userStore
    userStore.setUserInfo(authPermissionInfo.user);
    userStore.setUserRoles(authPermissionInfo.roles);
    // accessStore
    accessStore.setAccessMenus(authPermissionInfo.menus);
    accessStore.setAccessCodes(authPermissionInfo.permissions);
    loadedTerminal.value = terminalType.value;
    return authPermissionInfo;
  }
  function $reset() {
    loginLoading.value = false;
    loadedTerminal.value = undefined;
  }
  return {
@@ -154,6 +162,7 @@
    authLogin,
    fetchUserInfo,
    loginLoading,
    loadedTerminal,
    logout,
  };
});