vue3-element-admin 去掉登录

1.src/router/index 去掉登录路由

复制代码
  // {
  //   path: "/login",
  //   component: () => import("@/views/login/index.vue"),
  //   meta: { hidden: true },
  // },

2./src/plugins/permission修改 未登录

复制代码
import type {
  NavigationGuardNext,
  RouteLocationNormalized,
  RouteRecordRaw,
} from "vue-router";

import NProgress from "@/utils/nprogress";
import { isLogin } from "@/utils/auth";

import router from "@/router";
import { usePermissionStore, useUserStore } from "@/store";

export function setupPermission() {
  // 白名单路由
 const whiteList = ["/login"];

  router.beforeEach(async (to, from, next) => {
        NProgress.start();
        
    if (isLogin()) {
      if (to.path === "/login") {
        // 如果已登录,跳转到首页
        next({ path: "/" });
        NProgress.done();
      } else {
        const userStore = useUserStore();
        const hasRoles =
        userStore.user.roles && userStore.user.roles.length > 0;
        if (hasRoles) {
          // 如果未匹配到任何路由,跳转到404页面
          if (to.matched.length === 0) {
            next(from.name ? { name: from.name } : "/404");
          } else {
            // 如果路由参数中有 title,覆盖路由元信息中的 title
            const title =
              (to.params.title as string) || (to.query.title as string);
            if (title) {
              to.meta.title = title;
            }
            next();
          }
        } else {
          const permissionStore = usePermissionStore();
          try {
            await userStore.getUserInfo();
            const dynamicRoutes = await permissionStore.generateRoutes();
            dynamicRoutes.forEach((route: RouteRecordRaw) =>
              router.addRoute(route)
            );
            next({ ...to, replace: true });
          } catch (error) {
            console.error(error);
            // 移除 token 并重定向到登录页,携带当前页面路由作为跳转参数
            await userStore.resetToken();
            redirectToLogin(to, next);
            NProgress.done();
          }
        }
      }
    } else {
      // 未登录
      if (whiteList.includes(to.path)) {
        // 在白名单,直接进入
        next(); 
      } else {
        // 不在白名单,重定向到登录页
        const permissionStore = usePermissionStore();
        const dynamicRoutes = await permissionStore.generateRoutes();
        dynamicRoutes.forEach((route: RouteRecordRaw) =>
          router.addRoute(route)
        );
        next(); 
        //redirectToLogin(to, next);
        NProgress.done();
      }
    }
  });

  router.afterEach(() => {
    NProgress.done();
  });
}

/** 重定向到登录页 */
function redirectToLogin(
  to: RouteLocationNormalized,
  next: NavigationGuardNext
) {
  const params = new URLSearchParams(to.query as Record<string, string>);
  const queryString = params.toString();
  const redirect = queryString ? `${to.path}?${queryString}` : to.path;
  next(`/login?redirect=${encodeURIComponent(redirect)}`);
}

/** 判断是否有权限 */
export function hasAuth(
  value: string | string[],
  type: "button" | "role" = "button"
) {
  const { roles, perms } = useUserStore().user;

  // 超级管理员 拥有所有权限
  if (type === "button" && roles.includes("ROOT")) {
    return true;
  }

  const auths = type === "button" ? perms : roles;
  return typeof value === "string"
    ? auths.includes(value)
    : value.some((perm) => auths.includes(perm));
}

3.src/store/moudles/permission 修改generateRoutes

复制代码
  /**
   * 生成动态路由
   */
  function generateRoutes() {
    return import('@/router').then(({ constantRoutes }) => {
      routes.value  = constantRoutes;
      return constantRoutes;
    });
    // return new Promise<RouteRecordRaw[]>((resolve, reject) => {
    //   MenuAPI.getRoutes()
    //     .then((data) => {
    //       const dynamicRoutes = transformRoutes(data);
    //       routes.value = constantRoutes.concat(dynamicRoutes);
    //       resolve(dynamicRoutes);
    //     })
    //     .catch((error) => {
    //       reject(error);
    //     });
    // });
  }
相关推荐
AI开源软件12 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
程序员大阳1 天前
副队长Vue3教程(43)--Axios基础使用,get/post请求、基础参数
vue3·axios·参数·get·post
RuoyiOffice1 天前
Vue3商城后台实战:一款商品,从规格上架走到部分退款
vue.js·spring boot·vue3·商城系统·商城·企业管理·ruoyi office
htzyl2062 天前
Vue3中watch的deep默认值:reactive与ref为什么相反
前端·vue.js
qq_274475152 天前
第 21 篇:Proxy 代理与 Reflect:数据劫持基础,Vue 响应式底层原理前置
前端·javascript·vue.js
低代码布道师2 天前
第06篇:用户新增——React Hook Form + Zod 搭建可复用的表单体系
前端·react.js·前端框架
北城笑笑2 天前
Vue 115 ,上篇:给智能体装上计费表,行知订阅次数额度与每日额度双规则设计( 需求分析与方案设计,Vue3 + RuoYi 3.9.2 + 主子账号 )
java·前端·javascript·数据库·vue.js·后端·vue
anyup2 天前
2027 年初,uni-app 官方停更,老项目该何去何从?
前端·vue.js·uni-app
徐小夕2 天前
JitKnow 5.0发布:让AI知识库变成可组装的知识中台
前端·vue.js·github
斯内普吖2 天前
(开源)校园资料分享平台实战指南 基于 Node.js + Express + Vue + MySQL
vue.js·mysql·开源·node.js·express