refactor: router guard
This commit is contained in:
@@ -40,15 +40,6 @@ export const userClient = new Request({
|
|||||||
|
|
||||||
export const ndmClient = new Request({
|
export const ndmClient = new Request({
|
||||||
requestInterceptor: async (config) => {
|
requestInterceptor: async (config) => {
|
||||||
// 当OCC的token失效时,虽然不会影响车站请求,但是需要重新登录,所以在车站请求之前需要校验OCC的登录状态
|
|
||||||
const [err] = await userClient.post<void>(`/api/ndm/ndmKeepAlive/verify`, {}, { timeout: 5000 });
|
|
||||||
if (err) {
|
|
||||||
window.$message.error('登录超时,请重新登录');
|
|
||||||
const userStore = useUserStore();
|
|
||||||
userStore.resetStore();
|
|
||||||
router.push('/login');
|
|
||||||
return Promise.reject(err);
|
|
||||||
}
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
|
const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
|
||||||
const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
|
const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { userClient } from '@/apis/client';
|
||||||
import type { Station } from '@/apis/domains';
|
import type { Station } from '@/apis/domains';
|
||||||
import { ndmVerify } from '@/apis/requests';
|
import { ndmVerify } from '@/apis/requests';
|
||||||
import { STATION_LIST_QUERY_KEY } from '@/constants';
|
import { STATION_LIST_QUERY_KEY } from '@/constants';
|
||||||
@@ -22,6 +23,12 @@ export function useStationListQuery() {
|
|||||||
enabled: computed(() => pollingEnabled.value),
|
enabled: computed(() => pollingEnabled.value),
|
||||||
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
refetchInterval: getAppEnvConfig().requestInterval * 1000,
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
|
// 主动登录校验
|
||||||
|
const [err] = await userClient.post<void>(`/api/ndm/ndmKeepAlive/verify`, {}, { timeout: 5000 });
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`);
|
const { data: ndmStationList } = await axios.get<{ code: string; name: string }[]>(`/minio/ndm/ndm-stations.json?_t=${dayjs().unix()}`);
|
||||||
|
|
||||||
let stations = ndmStationList.map<Station>((station) => ({
|
let stations = ndmStationList.map<Station>((station) => ({
|
||||||
|
|||||||
@@ -49,22 +49,34 @@ const router = createRouter({
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const whiteList = ['/login', '/debug'];
|
const whiteList = ['/debug'];
|
||||||
|
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
if (userStore.userLoginResult?.token) {
|
const isAuthed = !!userStore.userLoginResult?.token;
|
||||||
|
|
||||||
|
// 放行白名单
|
||||||
|
const inWhiteList = whiteList.some((path) => to.path.startsWith(path));
|
||||||
|
if (inWhiteList) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 已登录用户不允许进入登录页(手动访问 /login 会重定向到首页)
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
|
if (isAuthed) {
|
||||||
next({ path: '/' });
|
next({ path: '/' });
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
} else {
|
return;
|
||||||
if (whiteList.includes(to.path)) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
next('/login');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 其它路由按登录态控制
|
||||||
|
if (!isAuthed) {
|
||||||
|
next('/login');
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user