Skip to content

Commit

Permalink
refactor(projects): 登录模块由query变更为动态路由params
Browse files Browse the repository at this point in the history
  • Loading branch information
Soybean committed Nov 29, 2021
1 parent f29106e commit 225c4fe
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/composables/common/route.ts
Expand Up @@ -36,7 +36,7 @@ export function useRouteQuery() {
const loginRedirectUrl = computed(() => {
let url: string | undefined;
if (route.name === routeName('login')) {
url = (route.query?.redirectUrl as string) || '';
url = (route.query?.redirect as string) || '';
}
return url;
});
Expand Down
31 changes: 14 additions & 17 deletions src/composables/common/router.ts
@@ -1,6 +1,7 @@
import { unref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter, routePath } from '@/router';
import { router as globalRouter, routeName } from '@/router';
import type { LoginModuleType } from '@/interface';

/**
Expand All @@ -9,7 +10,7 @@ import type { LoginModuleType } from '@/interface';
*/
export function useRouterPush(inSetup: boolean = true) {
const router = inSetup ? useRouter() : globalRouter;
const route = inSetup ? useRoute() : null;
const route = inSetup ? useRoute() : unref(globalRouter.currentRoute);

/** 跳转首页 */
function toHome() {
Expand All @@ -24,35 +25,31 @@ export function useRouterPush(inSetup: boolean = true) {
/**
* 跳转登录页面(通过vue路由)
* @param module - 展示的登录模块
* @param redirectUrl - 重定向地址
* @param redirect - 重定向地址(登录成功后跳转的地址)
*/
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl: LoginRedirect = 'current') {
function toLogin(module: LoginModuleType = 'pwd-login', redirect: LoginRedirect = 'current') {
const routeLocation: RouteLocationRaw = {
path: routePath('login'),
query: { module }
name: routeName('login'),
params: { module }
};
if (redirectUrl) {
let url = redirectUrl;
if (redirectUrl === 'current') {
if (redirect) {
let url = redirect;
if (redirect === 'current') {
url = router.currentRoute.value.fullPath;
}
routeLocation.query!.redirectUrl = url;
Object.assign(routeLocation, { query: { redirect: url } });
}
router.push(routeLocation);
}

/**
* 登陆页跳转登陆页
* 登陆页跳转登陆页(登录模块切换)
* @param module - 展示的登录模块
* @param query - 查询参数
*/
function toCurrentLogin(module: LoginModuleType) {
if (route) {
const { query } = route;
router.push({ path: routePath('login'), query: { ...query, module } });
} else {
throw Error('该函数必须在setup里面调用!');
}
const { query } = route;
router.push({ name: routeName('login'), params: { module }, query: { ...query } });
}

/** 登录后跳转重定向的地址 */
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/common/GlobalHeader/components/UserAvatar.vue
Expand Up @@ -46,7 +46,7 @@ function handleDropdown(optionKey: string) {
negativeText: '取消',
onPositiveClick: () => {
resetAuthStorage();
toLogin('pwd-login', 'current');
toLogin();
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/router/guard/permission.ts
@@ -1,6 +1,6 @@
import type { RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
import { router, routeName } from '@/router';
import { getToken, getLoginRedirectUrl } from '@/utils';
import { routeName } from '@/router';
import { getToken } from '@/utils';

type RouterAction = [boolean, () => void];

Expand Down Expand Up @@ -33,8 +33,8 @@ export function handlePagePermission(
[
!isLogin && needLogin,
() => {
const redirectUrl = getLoginRedirectUrl(router);
next({ name: routeName('login'), query: { redirectUrl } });
const redirect = to.fullPath;
next({ name: routeName('login'), query: { redirect } });
}
],
// 登录状态进入需要登录权限的页面,直接通行
Expand Down
5 changes: 3 additions & 2 deletions src/router/routes/constant-routes.ts
Expand Up @@ -2,6 +2,7 @@ import type { RouteRecordRaw } from 'vue-router';
import { BlankLayout } from '@/layouts';
import type { LoginModuleType } from '@/interface';
import { Login, NoPermission, NotFound, ServiceError } from '@/views';
import { getLoginModuleRegExp } from '@/utils';
import { routeName, routePath, routeTitle } from '../constant';
import { ROUTE_HOME_NAME } from './route-home';

Expand Down Expand Up @@ -29,10 +30,10 @@ const constantRoutes: RouteRecordRaw[] = [
// 登录
{
name: routeName('login'),
path: routePath('login'),
path: `${routePath('login')}/:module(${getLoginModuleRegExp()})?`,
component: Login,
props: route => {
const moduleType: LoginModuleType = (route.query?.module as LoginModuleType) || 'pwd-login';
const moduleType = (route.params.module as LoginModuleType) || 'pwd-login';
return {
module: moduleType
};
Expand Down
8 changes: 7 additions & 1 deletion src/utils/router/helpers.ts
@@ -1,6 +1,6 @@
import type { Component } from 'vue';
import type { Router, RouteRecordRaw, RouteMeta } from 'vue-router';
import type { ImportedRouteModules } from '@/interface';
import type { ImportedRouteModules, LoginModuleType } from '@/interface';

interface SingleRouteConfig {
/** 路由 */
Expand Down Expand Up @@ -107,3 +107,9 @@ export function getLoginRedirectUrl(router: Router) {
const redirectUrl = path === '/' ? undefined : path;
return redirectUrl;
}

/** 获取登录模块的正则字符串 */
export function getLoginModuleRegExp() {
const modules: LoginModuleType[] = ['pwd-login', 'code-login', 'register', 'reset-pwd', 'bind-wechat'];
return modules.join('|');
}
4 changes: 2 additions & 2 deletions src/views/component/button/index.vue
Expand Up @@ -2,8 +2,8 @@
<div>
<n-card title="按钮" class="h-full shadow-sm rounded-16px">
<n-grid cols="s:1 m:2" responsive="screen" :x-gap="16" :y-gap="16">
<n-grid-item v-for="item in buttonExample" :key="item.id" class="h-180px">
<n-card :title="item.label" class="h-full">
<n-grid-item v-for="item in buttonExample" :key="item.id">
<n-card :title="item.label" class="min-h-180px">
<p v-if="item.desc" class="pb-16px">{{ item.desc }}</p>
<n-space>
<n-button
Expand Down
21 changes: 13 additions & 8 deletions src/views/system/login/index.vue
Expand Up @@ -9,10 +9,8 @@
<n-gradient-text type="primary" :size="28">{{ title }}</n-gradient-text>
</header>
<main class="pt-24px">
<div v-for="item in modules" v-show="module === item.key" :key="item.key">
<h3 class="text-18px text-primary font-medium">{{ item.label }}</h3>
<component :is="item.component" />
</div>
<h3 class="text-18px text-primary font-medium">{{ activeModule.label }}</h3>
<component :is="activeModule.component" />
</main>
</div>
</n-card>
Expand All @@ -34,7 +32,7 @@ import { useThemeStore } from '@/store';
interface Props {
/** 登录模块分类 */
module?: LoginModuleType;
module: LoginModuleType;
}
interface LoginModule {
Expand All @@ -43,9 +41,7 @@ interface LoginModule {
component: Component;
}
withDefaults(defineProps<Props>(), {
module: 'pwd-login'
});
const props = defineProps<Props>();
const theme = useThemeStore();
const title = useAppTitle();
Expand All @@ -58,6 +54,15 @@ const modules: LoginModule[] = [
{ key: 'bind-wechat', label: EnumLoginModule['bind-wechat'], component: BindWechat }
];
const activeModule = computed(() => {
const active: LoginModule = { ...modules[0] };
const findItem = modules.find(item => item.key === props.module);
if (findItem) {
Object.assign(active, findItem);
}
return active;
});
const bgColor = computed(() => {
const COLOR_WHITE = '#ffffff';
const ratio = theme.darkMode ? 0.6 : 0.3;
Expand Down

1 comment on commit 225c4fe

@vercel
Copy link

@vercel vercel bot commented on 225c4fe Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.