feat: 系统首次使用引导标记

This commit is contained in:
TsMask
2024-04-19 19:51:01 +08:00
parent 399bddc635
commit 845084f77f
2 changed files with 36 additions and 26 deletions

View File

@@ -142,16 +142,26 @@ router.afterEach((to, from, failure) => {
}); });
/**无Token可访问页面地址白名单 */ /**无Token可访问页面地址白名单 */
const WHITE_LIST: string[] = ['/login', '/auth-redirect', '/help', '/register', '/quick-build']; const WHITE_LIST: string[] = [
'/login',
'/auth-redirect',
'/help',
'/register',
'/quick-build',
];
/**全局路由-前置守卫 */ /**全局路由-前置守卫 */
router.beforeEach((to, from, next) => { router.beforeEach(async (to, from, next) => {
NProgress.start(); NProgress.start();
const token = getToken(); const token = getToken();
// 获取系统配置信息 // 获取系统配置信息
const appStore = useAppStore(); const appStore = useAppStore();
if (!appStore.loginBackground) { if (!appStore.loginBackground) {
appStore.fnSysConf(); await appStore.fnSysConf();
console.log(to.path);
if (appStore.sysGuide && to.path !== '/quick-build') {
next({ name: 'QuickBuild' });
}
} }
// 没有token // 没有token
@@ -174,31 +184,27 @@ router.beforeEach((to, from, next) => {
// 判断当前用户是否有角色信息 // 判断当前用户是否有角色信息
const user = useUserStore(); const user = useUserStore();
if (user.roles && user.roles.length === 0) { if (user.roles && user.roles.length === 0) {
// 获取用户信息 try {
user // 获取用户信息
.fnGetInfo() await user.fnGetInfo();
.then(() => { // 获取路由信息
return useRouterStore().generateRoutes(); const accessRoutes = await useRouterStore().generateRoutes();
}) // 根据后台配置生成可访问的路由表
.then(accessRoutes => { if (accessRoutes && accessRoutes.length !== 0) {
// 根据后台配置生成可访问的路由表 for (const route of accessRoutes) {
if (accessRoutes && accessRoutes.length !== 0) { // 动态添加可访问路由表http开头会异常
for (const route of accessRoutes) { if (!validHttp(route.path)) {
// 动态添加可访问路由表http开头会异常 router.addRoute(route);
if (!validHttp(route.path)) {
router.addRoute(route);
}
} }
} }
// 刷新替换原先路由确保addRoutes已完成 }
next({ ...to, replace: true }); // 刷新替换原先路由确保addRoutes已完成
}) next({ ...to, replace: true });
.catch(e => { } catch (error: any) {
console.error(`[${to.path}]: ${e.message}`); console.error(`[${to.path}]: ${error.message}`);
user.fnLogOut().finally(() => { await user.fnLogOut();
next({ name: 'Login' }); next({ name: 'Login' });
}); }
});
} else { } else {
next(); next();
} }

View File

@@ -17,6 +17,8 @@ type AppStore = {
/**服务版本 */ /**服务版本 */
version: string; version: string;
buildTime: string; buildTime: string;
/**系统首次使用 */
sysGuide: boolean;
// 序列号 // 序列号
serialNum: string; serialNum: string;
/**应用版权声明 */ /**应用版权声明 */
@@ -48,6 +50,7 @@ const useAppStore = defineStore('app', {
version: `-`, version: `-`,
buildTime: `-`, buildTime: `-`,
sysGuide: false,
serialNum: `-`, serialNum: `-`,
copyright: `Copyright ©2023 For ${import.meta.env.VITE_APP_NAME}`, copyright: `Copyright ©2023 For ${import.meta.env.VITE_APP_NAME}`,
logoType: 'icon', logoType: 'icon',
@@ -80,6 +83,7 @@ const useAppStore = defineStore('app', {
if (res.code === RESULT_CODE_SUCCESS && res.data) { if (res.code === RESULT_CODE_SUCCESS && res.data) {
this.version = res.data.version; this.version = res.data.version;
this.buildTime = res.data.buildTime; this.buildTime = res.data.buildTime;
this.sysGuide = res.data.sysGuide === 'true';
this.serialNum = res.data.serialNum; this.serialNum = res.data.serialNum;
this.appName = res.data.title; this.appName = res.data.title;
this.copyright = res.data.copyright; this.copyright = res.data.copyright;