-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Pr@main/xpack login #2441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pr@main/xpack login #2441
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { hasPermission } from '@/utils/permission/index' | ||
| import NProgress from 'nprogress' | ||
| import { | ||
| createRouter, | ||
| createWebHistory, | ||
|
|
@@ -9,6 +10,7 @@ import { | |
| } from 'vue-router' | ||
| import useStore from '@/stores' | ||
| import { routes } from '@/router/routes' | ||
| NProgress.configure({ showSpinner: false, speed: 500, minimum: 0.3 }) | ||
| const router = createRouter({ | ||
| history: createWebHistory(import.meta.env.BASE_URL), | ||
| routes: routes | ||
|
|
@@ -17,6 +19,7 @@ const router = createRouter({ | |
| // 路由前置拦截器 | ||
| router.beforeEach( | ||
| async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => { | ||
| NProgress.start() | ||
| if (to.name === '404') { | ||
| next() | ||
| return | ||
|
|
@@ -48,6 +51,9 @@ router.beforeEach( | |
| } | ||
| } | ||
| ) | ||
| router.afterEach(() => { | ||
| NProgress.done() | ||
| }) | ||
|
|
||
| export const getChildRouteListByPathAndName = (path: any, name?: RouteRecordName | any) => { | ||
| return getChildRouteList(routes, path, name) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No significant irregularities, potential issues, or immediate optimizations were identified in the provided code snippet. The code is well-structured and follows Vue Router conventions. Here are a few minor improvements that can be made:
Here's an optimized version with these considerations: // Import necessary modules
import { hasPermission } from '@/utils/permission/index';
import NProgress from 'nprogress';
NProgress.configure({ showSpinner: false, speed: 500, minimum: 0.3 });
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: routes
});
// Define route guards
router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
try {
// Start loading progress bar
NProgress.start();
if (to.name === '404') {
next();
return;
}
const userState = await useStore().getters['user/getUserState'];
if (!hasPermission(userState.permission)) {
if (from.path !== '/login' && from.path.indexOf('/admin/') !== 0){
// Redirect to login page when unauthorized non-admin users access restricted areas
next(`/home?r=403&redirect=${to.fullPath}`);
} else {
// For admin redirect them directly to their dashboard upon redirection failure
next('dashboard');
}
return;
}
next(); // Proceed to destination if allowed permissions match the required role
} catch (error) {
console.error("Failed to authenticate user:", error);
next('/'); // Redirect to home on authentication failure
}
});
router.afterEach(() => {
// Complete/loading process after navigation completes
NProgress.done();
});
export function getChildRouteListByPathAndName(path: string, name?: RouteRecordName): RouteRecordRaw[] | null {
return getChildRouteList(routes, path, name);
}These changes include adding consistent whitespace around operators and expressions, and organizing related functions under one |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| <template> | ||
| <login-layout v-if="user.isEnterprise() ? user.themeInfo : true" v-loading="loading"> | ||
| <login-layout v-if="!loading" v-loading="loading"> | ||
| <LoginContainer :subTitle="user.themeInfo?.slogan || $t('views.system.theme.defaultSlogan')"> | ||
| <h2 class="mb-24" v-if="!showQrCodeTab">{{ loginMode || $t('views.login.title') }}</h2> | ||
| <div v-if="!showQrCodeTab"> | ||
|
|
@@ -225,10 +225,10 @@ const login = () => { | |
| }) | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| onBeforeMount(() => { | ||
| loading.value = true | ||
| user.asyncGetProfile().then((res) => { | ||
| if (user.isEnterprise()) { | ||
| loading.value = true | ||
| user | ||
| .getAuthType() | ||
| .then((res) => { | ||
|
|
@@ -261,6 +261,8 @@ onMounted(() => { | |
| } | ||
| }) | ||
| .finally(() => (loading.value = false)) | ||
| } else { | ||
| loading.value = false | ||
| } | ||
| }) | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code snippet has several improvements and corrections: Improvements:
Here is the revised code: <template>
<login-layout v-if="!loading" v-loading="loading">
<LoginContainer :subTitle="user.themeInfo?.slogan || $t('views.system.theme.defaultSlogan')">
<h2 class="mb-24">{{ !showQrCodeTab ? loginMode || $t('views.login.title') : '' }}</h2>
<!-- QR Code Tab should be conditionally rendered here -->
</LoginContainer>
</login-layout>
</template>
<script setup lang="ts">
import { computed, ref, watchEffect } from 'vue';
// Import necessary components
const loading = ref(true);
const showQrCodeTab = ref(false); // Optional: Define this flag if needed
// Functionality defined earlier...
onBeforeMount(async () => {
try {
await user.asyncGetProfile(); // Remove duplicate call in mounted
if (user.isEnterprise()) {
await user.getAuthType();
// Handle enterprise-specific logic here
}
} catch (error) {
console.error('Error fetching user profile:', error);
} finally {
loading.value = false;
}
});
// Other methods...
</script>This version ensures that the component only mounts when it needs to perform data fetching and handles errors appropriately without unnecessarily repeating calls. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code you provided has some issues that need to be addressed:
Issues Identified:
Inconsistent Template Syntax: The use of
v-ifin both<template>and directly within an SVG element might be confusing and redundant because Vue components should not have both template sections (<template>...</template>and standalone elements).Redundant Condition Check: The condition checks for
loginLogoand its type are redundant since they will always evaluate whether it exists.Simplified Logic: You can simplify the logic for determining the image source and size using computed properties instead of multiple conditional statements.
Potential Bug with Computed Properties: Ensure that
user.themeInfo.loginLogodoes exist before checking its type; otherwise,undefinedmay lead to runtime errors.Optimal Suggestions:
Here's a revised version of your code with suggestions for addressing these points:
Explanation:
<img>tag.setupTheme()to handle dynamic updates to the theme and logos. This simplifies management of different themes.updateLogo()to manage the actual source of the logos, ensuring correct functionality even when no images are found.