Commit 1e3c7ff8 authored by chenjiahao's avatar chenjiahao

将前台所有页面加入白名单;修改登录逻辑

parent 582db30f
export enum PageEnum {
// basic login path
BASE_LOGIN = '/login',
// 前台登录页面
FONT_LOGIN = '/aaaFont/Login',
// 前台注册页面
FONT_REGISTER = '/aaaFont/register',
// 首页
HOME = '/aaaFont/font',
// font path
FONT = '/aaaFont',
// basic home path
BASE_HOME = '/dashboard',
// error page path
......
......@@ -6,14 +6,47 @@ import { PageEnum } from '@/enums/pageEnum';
import { useUserStoreWithOut } from '@/store/modules/user';
import { PAGE_NOT_FOUND_ROUTE } from '@/router/routes/basic';
import { PCFontRoute } from '@/router/routes';
// import { RootRoute } from '@/router/routes';
const LOGIN_PATH = PageEnum.BASE_LOGIN;
const FONT = PageEnum.FONT;
const FONT_LOGIN = PageEnum.FONT_LOGIN;
const FONT_REGISTER = PageEnum.FONT_REGISTER;
const HOME = PageEnum.HOME;
function collectChildPaths(routes, prefix) {
let paths = [];
function traverseRoutes(routeConfig) {
if (routeConfig.path.startsWith(prefix)) {
paths.push(routeConfig.path);
// 如果有children属性,则递归遍历
if (Array.isArray(routeConfig.children)) {
routeConfig.children.forEach(traverseRoutes);
}
}
}
routes.forEach(traverseRoutes);
return paths;
}
// 所有前台页面
const allSubPaths = collectChildPaths([PCFontRoute], '/aaaFont');
// const ROOT_PATH = RootRoute.path;
const whitePathList: PageEnum[] = [LOGIN_PATH];
const whitePathList: PageEnum[] = [
LOGIN_PATH,
FONT,
FONT_LOGIN,
FONT_REGISTER,
HOME,
...allSubPaths,
];
export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut();
......@@ -22,9 +55,9 @@ export function createPermissionGuard(router: Router) {
router.beforeEach(async (to, from, next) => {
if (
from.path === ROOT_PATH &&
to.path === PageEnum.BASE_HOME &&
to.path === PageEnum.HOME &&
userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
userStore.getUserInfo.homePath !== PageEnum.HOME
) {
next(userStore.getUserInfo.homePath);
return;
......@@ -59,15 +92,15 @@ export function createPermissionGuard(router: Router) {
// redirect login page
const redirectData: { path: string; replace: boolean; query?: Recordable<string> } = {
path: LOGIN_PATH,
path: HOME,
replace: true,
};
if (to.fullPath) {
redirectData.query = {
...redirectData.query,
redirect: to.fullPath,
};
}
// if (to.fullPath) {
// redirectData.query = {
// ...redirectData.query,
// redirect: to.fullPath,
// };
// }
next(redirectData);
return;
}
......
......@@ -86,25 +86,26 @@ export const useUserStore = defineStore({
async login(
params: LoginParams & {
goHome?: boolean;
goFont?: boolean;
mode?: ErrorMessageMode;
},
): Promise<any | null> {
try {
const { goHome = true, mode, ...loginParams } = params;
const { goHome = true, goFont = false, mode, ...loginParams } = params;
// 1、调用登录接口
const data = await loginApi(loginParams, mode);
const token = data.data;
// 2、设置 token,并存储本地缓存。
this.setToken(token);
return this.afterLoginAction(goHome);
return this.afterLoginAction(goHome, goFont);
} catch (error) {
return Promise.reject(error);
}
},
async afterLoginAction(goHome?: boolean): Promise<any | null> {
async afterLoginAction(goHome?: boolean, goFont?: boolean): Promise<any | null> {
if (!this.getToken) return null;
// 3、获取用户信息
const userInfo = await this.getUserInfoAction();
const userInfo = await this.getUserInfoAction(goFont);
console.log('userInfo', userInfo);
const sessionTimeout = this.sessionTimeout;
if (sessionTimeout) {
......@@ -122,11 +123,11 @@ export const useUserStore = defineStore({
permissionStore.setDynamicAddedRoute(true);
}
goHome && (await router.replace(userInfo?.homePath || PageEnum.BASE_HOME));
goHome && (await router.replace(userInfo?.homePath || PageEnum.HOME));
}
return userInfo;
},
async getUserInfoAction(): Promise<UserInfo | null> {
async getUserInfoAction(goFont = false): Promise<UserInfo | null> {
if (!this.getToken) return null;
let userInfo;
let type = localStorage.getItem('type');
......@@ -211,7 +212,11 @@ export const useUserStore = defineStore({
const firstChild = await this.getFirstChild(userInfo.data.menus[0]);
console.log('firstChild', firstChild);
// 登陆后进入的第一个页面
userInfo.data.homePath = firstChild;
if (goFont) {
userInfo.data.homePath = PageEnum.HOME;
} else {
userInfo.data.homePath = firstChild;
}
localStorage.setItem('firstChild', firstChild);
// 设置用户信息,并存储本地缓存
this.setUserInfo(userInfo.data);
......@@ -240,8 +245,10 @@ export const useUserStore = defineStore({
this.setSessionTimeout(false);
this.setUserInfo(null);
if (goLogin) {
// 直接回登陆页
router.replace(PageEnum.BASE_LOGIN);
// // 直接回登陆页
// router.replace(PageEnum.BASE_LOGIN);
// 直接回首页
router.replace(PageEnum.HOME);
} else {
// 回登陆页带上当前路由地址
router.replace({
......
......@@ -168,6 +168,7 @@
const userInfo = await userStore.login({
password: data.password,
username: data.account,
goFont: true,
mode: 'none', //不要默认的错误提示
});
console.log('userInfo', userInfo);
......
......@@ -37,7 +37,7 @@
</el-menu>
</div>
<div class="right-div">
<div v-if="isLogin"
<div v-if="!isLogin"
><el-button plain class="long_btn" @click="login">登录</el-button>
<el-button
class="long_btn"
......@@ -147,9 +147,7 @@
},
mounted() {
console.log(getToken());
if (getToken()) {
this.isLogin = false;
}
this.isLogin = !!getToken();
// window.addEventListener('.el-main', this.handleScroll)
},
methods: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment