Commit 1e3c7ff8 authored by chenjiahao's avatar chenjiahao

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

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