feat: 刷新页面添加是否登录判断重新获取用户信息

This commit is contained in:
RubyLiu 2023-08-23 11:24:26 +08:00 committed by 刘瑞斌
parent b50791ebd0
commit 1012e9879d
5 changed files with 36 additions and 3 deletions

View File

@ -6,7 +6,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onBeforeMount } from 'vue'; import { computed, onBeforeMount, onMounted } from 'vue';
import enUS from '@arco-design/web-vue/es/locale/lang/en-us'; import enUS from '@arco-design/web-vue/es/locale/lang/en-us';
import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn'; import zhCN from '@arco-design/web-vue/es/locale/lang/zh-cn';
import GlobalSetting from '@/components/pure/global-setting/index.vue'; import GlobalSetting from '@/components/pure/global-setting/index.vue';
@ -17,8 +17,11 @@
import useLicenseStore from '@/store/modules/setting/license'; import useLicenseStore from '@/store/modules/setting/license';
import { watchStyle, watchTheme, setFavicon } from '@/utils/theme'; import { watchStyle, watchTheme, setFavicon } from '@/utils/theme';
import { GetPlatformIconUrl } from '@/api/requrls/setting/config'; import { GetPlatformIconUrl } from '@/api/requrls/setting/config';
import { useUserStore } from '@/store';
import { useRouter } from 'vue-router';
const appStore = useAppStore(); const appStore = useAppStore();
const userStore = useUserStore();
const licenseStore = useLicenseStore(); const licenseStore = useLicenseStore();
const { currentLocale } = useLocale(); const { currentLocale } = useLocale();
@ -61,7 +64,15 @@
setLocalStorage('isInitUrl', 'true'); // url setLocalStorage('isInitUrl', 'true'); // url
} }
} catch (error) { } catch (error) {
// eslint-disable-next-line no-console
console.log(error); console.log(error);
} }
}); });
onMounted(async () => {
const res = await userStore.isLogin();
if (!res) {
const router = useRouter();
router.push({ name: 'login' });
}
});
</script> </script>

View File

@ -2,6 +2,7 @@ export default {
// 消息提醒相关 // 消息提醒相关
'message.errorTip': 'Error Tip', 'message.errorTip': 'Error Tip',
'message.logouting': 'Logging out...', 'message.logouting': 'Logging out...',
'message.loginExpired': 'Login expired, please login again!',
'message.logoutSuccess': 'Logout success', 'message.logoutSuccess': 'Logout success',
'message.loadingDefaultTip': 'Loading...', 'message.loadingDefaultTip': 'Loading...',
'message.menuLoading': 'Loading the menu...', 'message.menuLoading': 'Loading the menu...',

View File

@ -2,6 +2,7 @@ export default {
// 消息提醒相关 // 消息提醒相关
'message.errorTip': '错误提示', 'message.errorTip': '错误提示',
'message.logouting': '正在退出登录...', 'message.logouting': '正在退出登录...',
'message.loginExpired': '登录过期,请重新登录!',
'message.logoutSuccess': '登出成功', 'message.logoutSuccess': '登出成功',
'message.loadingDefaultTip': '加载中...', 'message.loadingDefaultTip': '加载中...',
'message.menuLoading': '菜单加载中...', 'message.menuLoading': '菜单加载中...',

View File

@ -1,5 +1,5 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { login as userLogin, logout as userLogout } from '@/api/modules/user'; import { login as userLogin, logout as userLogout, isLogin as userIsLogin } from '@/api/modules/user';
import { setToken, clearToken } from '@/utils/auth'; import { setToken, clearToken } from '@/utils/auth';
import { removeRouteListener } from '@/utils/route-listener'; import { removeRouteListener } from '@/utils/route-listener';
import useAppStore from '../app'; import useAppStore from '../app';
@ -8,6 +8,7 @@ import { useI18n } from '@/hooks/useI18n';
import type { LoginData } from '@/models/user'; import type { LoginData } from '@/models/user';
import type { UserState } from './types'; import type { UserState } from './types';
import { Message } from '@arco-design/web-vue';
const useUserStore = defineStore('user', { const useUserStore = defineStore('user', {
// 开启数据持久化 // 开启数据持久化
@ -92,6 +93,24 @@ const useUserStore = defineStore('user', {
this.logoutCallBack(); this.logoutCallBack();
} }
}, },
// 是否已经登录
async isLogin() {
try {
const res = await userIsLogin();
const appStore = useAppStore();
setToken(res.sessionId, res.csrfToken);
this.setInfo(res);
if (appStore.currentOrgId === '') {
appStore.setCurrentOrgId(res.lastOrganizationId || '');
}
return true;
} catch (err) {
const { t } = useI18n();
Message.error(t('message.loginExpired'));
this.logoutCallBack();
return false;
}
},
}, },
}); });

View File

@ -37,6 +37,7 @@
:disabled="!isXpack" :disabled="!isXpack"
allow-search allow-search
:options="affiliatedOrgOption" :options="affiliatedOrgOption"
:default-value="isXpack ? '' : 'default_organization'"
:placeholder="t('system.project.affiliatedOrgPlaceholder')" :placeholder="t('system.project.affiliatedOrgPlaceholder')"
:field-names="{ label: 'name', value: 'id' }" :field-names="{ label: 'name', value: 'id' }"
> >
@ -103,7 +104,7 @@
const affiliatedOrgOption = ref<SystemOrgOption[]>([]); const affiliatedOrgOption = ref<SystemOrgOption[]>([]);
const licenseStore = useLicenseStore(); const licenseStore = useLicenseStore();
const moduleOption = [ const moduleOption = [
{ label: 'menu.workplace', value: 'workstation' }, { label: 'menu.workbench', value: 'workstation' },
{ label: 'menu.testPlan', value: 'testPlan' }, { label: 'menu.testPlan', value: 'testPlan' },
{ label: 'menu.bugManagement', value: 'bugManagement' }, { label: 'menu.bugManagement', value: 'bugManagement' },
{ label: 'menu.featureTest', value: 'caseManagement' }, { label: 'menu.featureTest', value: 'caseManagement' },