feat(projects): 迁移登录完成

This commit is contained in:
Soybean
2022-01-05 01:35:32 +08:00
parent f5a36a05cb
commit b93b80cb4b
54 changed files with 1679 additions and 260 deletions

View File

@ -1 +1,3 @@
export * from './system';
export * from './router';
export * from './route';

View File

@ -0,0 +1,32 @@
import { unref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { router } from '@/router';
import { useRouteStore } from '@/store';
/**
* 路由查询参数
* @param inSetup - 是否在vue页面/组件的setup里面调用在axios里面无法使用useRouter和useRoute
*/
export function useRouteQuery(inSetup: boolean = true) {
const { getRouteName } = useRouteStore();
const route = getRouteInstance(inSetup);
/** 登录后跳转的地址 */
const loginRedirect = computed(() => {
let url: string | undefined;
if (route.name === getRouteName('login')) {
url = (route.query?.redirect as string) || '';
}
return url;
});
return {
loginRedirect
};
}
function getRouteInstance(inSetup: boolean = true) {
const route = inSetup ? useRoute() : unref(router.currentRoute);
return route;
}

View File

@ -0,0 +1,90 @@
import { unref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import { router as globalRouter } from '@/router';
import { useRouteStore } from '@/store';
import { LoginModuleKey } from '@/interface';
/**
* 路由跳转
* @param inSetup - 是否在vue页面/组件的setup里面调用在axios里面无法使用useRouter和useRoute
*/
export function useRouterPush(inSetup: boolean = true) {
const { getRouteName } = useRouteStore();
const router = inSetup ? useRouter() : globalRouter;
const route = inSetup ? useRoute() : unref(globalRouter.currentRoute);
/**
* 路由跳转
* @param to - 需要跳转的路由
* @param newTab - 是否在新的浏览器Tab标签打开
*/
function routerPush(to: RouteLocationRaw, newTab = false) {
if (newTab) {
const routerData = router.resolve(to);
window.open(routerData.href, '_blank');
} else {
router.push(to);
}
}
/** 返回上一级路由 */
function routerBack() {
router.go(-1);
}
/**
* 跳转首页
* @param newTab - 在新的浏览器标签打开
*/
function toHome(newTab = false) {
routerPush(getRouteName('root'), newTab);
}
/**
* 跳转登录页面
* @param loginModule - 展示的登录模块
* @param redirectUrl - 重定向地址(登录成功后跳转的地址),默认undefined表示取当前地址为重定向地址
*/
function toLogin(loginModule?: LoginModuleKey, redirectUrl?: string) {
const module: LoginModuleKey = loginModule || 'pwd-login';
const routeLocation: RouteLocationRaw = {
name: getRouteName('login'),
params: { module }
};
const redirect = redirectUrl || route.fullPath;
Object.assign(routeLocation, { query: { redirect } });
routerPush(routeLocation);
}
/**
* 登录页切换其他模块
* @param module - 切换后的登录模块
*/
function toLoginModule(module: LoginModuleKey) {
const { query } = route;
routerPush({ name: getRouteName('login'), params: { module }, query });
}
/**
* 登录成功后跳转重定向的地址
* @param redirect - 重定向地址
*/
function toLoginRedirect(redirect?: string) {
if (redirect) {
routerPush(redirect);
} else {
toHome();
}
}
return {
routerPush,
routerBack,
toHome,
toLogin,
toLoginModule,
toLoginRedirect
};
}