refactor(projects): 路由声明重构,添加composables,BaseLayout进行中,文件夹规范

This commit is contained in:
Soybean
2021-11-19 01:33:36 +08:00
parent 1c5fdca596
commit 1e84d13d54
75 changed files with 668 additions and 565 deletions

View File

@ -1,5 +1,5 @@
import { ref, computed } from 'vue';
import useBoolean from '../common/useBoolean';
import { useBoolean } from '../common';
/**
* 倒计时
@ -9,10 +9,10 @@ export default function useCountDown(second: number) {
if (second <= 0 && second % 1 !== 0) {
throw Error('倒计时的时间应该为一个正整数!');
}
const { bool: isComplete, setTrue, setFalse } = useBoolean(false);
const counts = ref(0);
const isCounting = computed(() => Boolean(counts.value));
// 完成一轮倒计时
const { bool: isComplete, setTrue, setFalse } = useBoolean(false);
let intervalId: any;

View File

@ -1,23 +1,5 @@
import useAppTitle from './useAppTitle';
import useContext from './useContext';
import useRouterChange from './useRouterChange';
import useRouteParam from './useRouteParam';
import useRouteQuery from './useRouteQuery';
import useRouteProps from './useRouteProps';
import useBoolean from './useBoolean';
import useLoading from './useLoading';
import useScrollBehavior from './useScrollBehavior';
import useIsMobile from './useIsMobile';
export {
useAppTitle,
useContext,
useRouterChange,
useRouteParam,
useRouteQuery,
useRouteProps,
useBoolean,
useLoading,
useScrollBehavior,
useIsMobile
};
export { useContext, useBoolean, useLoading };

View File

@ -1,6 +0,0 @@
/** 项目名称 */
export default function useAppTitle() {
const title = import.meta.env.VITE_APP_TITLE as string;
return title;
}

View File

@ -1,11 +0,0 @@
import { useBreakpoints, breakpointsTailwind } from '@vueuse/core';
/** 是否是移动端 */
export default function useIsMobile() {
const breakpoints = useBreakpoints(breakpointsTailwind);
const isMobile = breakpoints.smaller('lg');
return {
isMobile
};
}

View File

@ -1,7 +0,0 @@
// import { computed } from 'vue';
// import { useRoute } from 'vue-router';
// import { ROUTE_NAME_MAP } from '@/utils';
export default function useRouteParam() {
// const route = useRoute();
}

View File

@ -1,22 +0,0 @@
import { computed } from 'vue';
import { useRoute } from 'vue-router';
export default function useRouteProps() {
const route = useRoute();
const props = computed(() => {
/** 路由名称 */
const name = route.name as string;
/** 混存页面 */
const keepAlive = Boolean(route.meta?.keepAlive);
/** 视高100% */
const fullPage = Boolean(route.meta?.fullPage);
return {
name,
keepAlive,
fullPage
};
});
return props;
}

View File

@ -1,21 +0,0 @@
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { EnumRoutePath } from '@/enum';
import { ROUTE_NAME_MAP } from '@/utils';
export default function useRouteQuery() {
const route = useRoute();
/** 登录跳转链接 */
const loginRedirectUrl = computed(() => {
let url: EnumRoutePath | undefined;
if (route.name === ROUTE_NAME_MAP.get('login')) {
url = (route.query?.redirectUrl as EnumRoutePath) || '';
}
return url;
});
return {
loginRedirectUrl
};
}

View File

@ -1,69 +0,0 @@
import { useRouter, useRoute } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import { EnumRoutePath } from '@/enum';
import { router as globalRouter } from '@/router';
import type { LoginModuleType } from '@/interface';
/**
* 重定向地址
* - current: 取当前的path作为重定向地址
*/
type LoginRedirect = 'current' | EnumRoutePath;
/**
* 路由跳转
* @param inSetup - 是否在vue页面/组件的setup里面调用
*/
export default function useRouterChange(inSetup: boolean = true) {
const router = inSetup ? useRouter() : globalRouter;
const route = inSetup ? useRoute() : null;
/** 跳转首页 */
function toHome() {
router.push('/');
}
/**
* 跳转登录页面(通过vue路由)
* @param module - 展示的登录模块
* @param redirectUrl - 重定向地址
*/
function toLogin(module: LoginModuleType = 'pwd-login', redirectUrl: LoginRedirect = 'current') {
const routeLocation: RouteLocationRaw = {
path: EnumRoutePath.login,
query: { module }
};
if (redirectUrl) {
let url = redirectUrl;
if (redirectUrl === 'current') {
url = router.currentRoute.value.fullPath as EnumRoutePath;
}
routeLocation.query!.redirectUrl = url;
}
router.push(routeLocation);
}
/**
* 登陆页跳转登陆页
* @param module - 展示的登录模块
* @param query - 查询参数
*/
function toCurrentLogin(module: LoginModuleType) {
if (route) {
const { query } = route;
router.push({ path: EnumRoutePath.login, query: { ...query, module } });
}
}
/** 登录后跳转重定向的地址 */
function toLoginRedirectUrl(path: EnumRoutePath) {
router.push(path);
}
return {
toHome,
toLogin,
toCurrentLogin,
toLoginRedirectUrl
};
}

View File

@ -1,16 +0,0 @@
import { ref } from 'vue';
/** 滚动行为 */
export default function useScrollBehavior() {
const scrollbar = ref<HTMLElement | null>(null);
/** 重置滚动条行为 */
function resetScrollBehavior() {
scrollbar.value?.scrollTo({ left: 0, top: 0 });
}
return {
scrollbar,
resetScrollBehavior
};
}