12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import router, { configRoutes } from "@/router";
- import { useUserStore } from "@/store/modules/user";
- import { usePermissionStore } from "@/store/modules/permission";
- import { useDictionaryStore } from "@/store/modules/dictionary";
- import { isNewVersion } from "@/utils/version.js";
- import NProgress from "@/utils/nprogress";
- import { getUserDicts } from "@/api/auth";
- export function setupPermission() {
- // 白名单路由
- const whiteList = [""];
- router.beforeEach(async (to, from, next) => {
- NProgress.start();
- isNewVersion();
- const hasToken = localStorage.getItem("token");
- if (hasToken) {
- if (to.path === "/login") {
- // 如果已登录,跳转首页
- next({ path: "/" });
- NProgress.done();
- } else {
- // 如果已经登录,让菜单加载本地的写死路由的数据
- const userStore = usePermissionStore();
- if (!userStore.routes || userStore.routes.length === 0) {
- userStore.setRoutes(configRoutes);
- }
- const dictStore = useDictionaryStore();
- if (
- !dictStore.dicts.value ||
- JSON.stringify(dictStore.dicts.value) === "{}"
- ) {
- const res = await getUserDicts(dictStore.types);
- if (res.data) {
- dictStore.dicts = res?.data;
- }
- }
- next();
- }
- } else {
- // 未登录可以访问白名单页面
- if (whiteList.indexOf(to.path) !== -1) {
- // const dictStore = useDictionaryStore();
- // dictStore.checkDicts();
- next();
- } else {
- if (to.path === "/login") {
- next();
- } else {
- next(`/login`);
- }
- NProgress.done();
- }
- }
- });
- router.afterEach(() => {
- NProgress.done();
- });
- }
|