xiaodai2017 2 anos atrás
pai
commit
6d1d6ecf84

+ 2 - 2
src/api/routes.ts

@@ -1,5 +1,5 @@
 import { http } from "../utils/http";
 
-export const getAsyncRoutes = (params?: object) => {
-  return http.request("get", "/getAsyncRoutes", { params });
+export const getAsyncRoutes = (data: object) => {
+  return http.request("post", "/Admin/menu", { data });
 };

+ 1 - 1
src/router/index.ts

@@ -131,7 +131,7 @@ router.beforeEach((to: toRouteType, _from, next) => {
     } else {
       // 刷新
       if (usePermissionStoreHook().wholeMenus.length === 0)
-        initRouter(name.username).then((router: Router) => {
+        initRouter().then((router: Router) => {
           if (!useMultiTagsStoreHook().getMultiTagsCache) {
             const handTag = (
               path: string,

+ 269 - 69
src/router/utils.ts

@@ -18,7 +18,7 @@ const IFrame = () => import("/@/layout/frameView.vue");
 const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}");
 
 // 动态路由
-// import { getAsyncRoutes } from "/@/api/routes";
+import { getAsyncRoutes } from "/@/api/routes";
 
 // 按照路由中meta下的rank等级升序来排序路由
 function ascending(arr: any[]) {
@@ -113,80 +113,280 @@ function resetRouter(): void {
   });
 }
 
+// 添加不同按钮权限到/permission/button页面中
+function setDifAuthority(authority, routes) {
+  routes.children[1].meta.authority = [authority];
+  return routes;
+}
 // 初始化路由
-function initRouter(arr: any[]) {
+function initRouter() {
   return new Promise(resolve => {
-    // getAsyncRoutes({ name }).then(({ info }) => {
-    if (arr.length === 0) {
-      usePermissionStoreHook().changeSetting(arr);
-    } else {
-      formatFlatteningRoutes(addAsyncRoutes(arr)).map((v: RouteRecordRaw) => {
-        // 防止重复添加路由
-        if (
-          router.options.routes[0].children.findIndex(
-            value => value.path === v.path
-          ) !== -1
-        ) {
-          return;
-        } else {
-          // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
-          router.options.routes[0].children.push(v);
-          // 最终路由进行升序
-          ascending(router.options.routes[0].children);
-          if (!router.hasRoute(v?.name)) router.addRoute(v);
-          const flattenRouters = router.getRoutes().find(n => n.path === "/");
-          router.addRoute(flattenRouters);
-        }
-        resolve(router);
+    getAsyncRoutes({}).then(({ code, data, message }) => {
+      // console.log(code, data, message);
+      // const systemRouter = {
+      //   path: "/system",
+      //   redirect: "/system/menuOperator/index",
+      //   meta: {
+      //     icon: "setting",
+      //     title: "系统管理",
+      //     rank: 11
+      //   },
+      //   children: [
+      //     {
+      //       path: "/system/menuOperator/index",
+      //       name: "menuOperator",
+      //       meta: {
+      //         icon: "flUser",
+      //         title: "用户管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/system/oplog/index",
+      //       name: "oplog",
+      //       meta: {
+      //         icon: "role",
+      //         title: "角色管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/system/updates/index",
+      //       name: "updates",
+      //       meta: {
+      //         icon: "dept",
+      //         title: "部门管理"
+      //       }
+      //     }
+      //   ]
+      // };
+      // const interestRouter = {
+      //   path: "/interest",
+      //   redirect: "/interest/account/index",
+      //   meta: {
+      //     icon: "setting",
+      //     title: "权限管理",
+      //     rank: 11
+      //   },
+      //   children: [
+      //     {
+      //       path: "/interest/account/index",
+      //       name: "account",
+      //       meta: {
+      //         icon: "flUser",
+      //         title: "用户管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/interest/role/index",
+      //       name: "role",
+      //       meta: {
+      //         icon: "role",
+      //         title: "角色管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/interest/action/index",
+      //       name: "action",
+      //       meta: {
+      //         icon: "dept",
+      //         title: "部门管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/interest/dataShare/index",
+      //       name: "dataShare",
+      //       meta: {
+      //         icon: "dict",
+      //         title: "字典管理"
+      //       }
+      //     },
+      //     {
+      //       path: "/interest/group/index",
+      //       name: "group",
+      //       meta: {
+      //         icon: "dict",
+      //         title: "字典管理"
+      //       }
+      //     }
+      //   ]
+      // };
+      const permissionRouter = {
+        path: "/permission",
+        redirect: "/permission/page/index",
+        meta: {
+          title: "menus.permission",
+          icon: "lollipop",
+          rank: 7
+        },
+        children: [
+          {
+            path: "/permission/page/index",
+            name: "PermissionPage",
+            meta: {
+              title: "menus.permissionPage"
+            }
+          },
+          {
+            path: "/permission/button/index",
+            name: "PermissionButton",
+            meta: {
+              title: "menus.permissionButton",
+              authority: []
+            }
+          }
+        ]
+      };
+      const systemRouter = {
+        path: "/system",
+        redirect: "/system/user/index",
+        meta: {
+          icon: "setting",
+          title: "menus.hssysManagement",
+          rank: 11
+        },
+        children: [
+          {
+            path: "/system/user/index",
+            name: "User",
+            meta: {
+              icon: "flUser",
+              title: "menus.hsUser"
+            }
+          },
+          {
+            path: "/system/role/index",
+            name: "Role",
+            meta: {
+              icon: "role",
+              title: "menus.hsRole"
+            }
+          },
+          {
+            path: "/system/dept/index",
+            name: "Dept",
+            meta: {
+              icon: "dept",
+              title: "menus.hsDept"
+            }
+          },
+          {
+            path: "/system/dict",
+            component: "/system/dict/index",
+            name: "Dict",
+            meta: {
+              icon: "dict",
+              title: "menus.hsDict",
+              keepAlive: true
+            }
+          }
+        ]
+      };
+
+      const frameRouter = {
+        path: "/iframe",
+        redirect: "/iframe/pure",
+        meta: {
+          icon: "monitor",
+          title: "menus.hsExternalPage",
+          rank: 10
+        },
+        children: [
+          {
+            path: "/iframe/pure",
+            name: "FramePure",
+            meta: {
+              title: "menus.hsPureDocument",
+              frameSrc: "https://pure-admin-doc.vercel.app"
+            }
+          },
+          {
+            path: "/external",
+            name: "https://pure-admin-doc.vercel.app",
+            meta: {
+              title: "menus.externalLink"
+            }
+          },
+          {
+            path: "/iframe/ep",
+            name: "FrameEp",
+            meta: {
+              title: "menus.hsEpDocument",
+              frameSrc: "https://element-plus.org/zh-CN/"
+            }
+          }
+        ]
+      };
+
+      const tabsRouter = {
+        path: "/tabs",
+        redirect: "/tabs/index",
+        meta: {
+          icon: "IF-team-icontabs",
+          title: "menus.hstabs",
+          rank: 13
+        },
+        children: [
+          {
+            path: "/tabs/index",
+            name: "Tabs",
+            meta: {
+              title: "menus.hstabs"
+            }
+          },
+          {
+            path: "/tabs/detail",
+            name: "TabDetail",
+            meta: {
+              title: "",
+              showLink: false,
+              dynamicLevel: 3,
+              refreshRedirect: "/tabs/index"
+            }
+          }
+        ]
+      };
+      const arrag = [
+        tabsRouter,
+        frameRouter,
+        systemRouter,
+        setDifAuthority("v-admin", permissionRouter)
+      ];
+      console.log(arrag);
+      if (arrag.length === 0) {
+        usePermissionStoreHook().changeSetting(arrag);
+      } else {
+        formatFlatteningRoutes(addAsyncRoutes(arrag)).map(
+          (v: RouteRecordRaw) => {
+            // 防止重复添加路由
+            if (
+              router.options.routes[0].children.findIndex(
+                value => value.path === v.path
+              ) !== -1
+            ) {
+              return;
+            } else {
+              // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
+              router.options.routes[0].children.push(v);
+              // 最终路由进行升序
+              ascending(router.options.routes[0].children);
+              if (!router.hasRoute(v?.name)) router.addRoute(v);
+              const flattenRouters = router
+                .getRoutes()
+                .find(n => n.path === "/");
+              router.addRoute(flattenRouters);
+            }
+            resolve(router);
+          }
+        );
+        usePermissionStoreHook().changeSetting(arrag);
+      }
+      router.addRoute({
+        path: "/:pathMatch(.*)",
+        redirect: "/error/404"
       });
-      usePermissionStoreHook().changeSetting(arr);
-    }
-    router.addRoute({
-      path: "/:pathMatch(.*)",
-      redirect: "/error/404"
+      resolve(code);
     });
-    // });
   });
 }
-// // 初始化路由
-// function initRouter(name: string) {
-//   return new Promise(resolve => {
-//     getAsyncRoutes({ name }).then(({ info }) => {
-//       if (info.length === 0) {
-//         usePermissionStoreHook().changeSetting(info);
-//       } else {
-//         formatFlatteningRoutes(addAsyncRoutes(info)).map(
-//           (v: RouteRecordRaw) => {
-//             // 防止重复添加路由
-//             if (
-//               router.options.routes[0].children.findIndex(
-//                 value => value.path === v.path
-//               ) !== -1
-//             ) {
-//               return;
-//             } else {
-//               // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
-//               router.options.routes[0].children.push(v);
-//               // 最终路由进行升序
-//               ascending(router.options.routes[0].children);
-//               if (!router.hasRoute(v?.name)) router.addRoute(v);
-//               const flattenRouters = router
-//                 .getRoutes()
-//                 .find(n => n.path === "/");
-//               router.addRoute(flattenRouters);
-//             }
-//             resolve(router);
-//           }
-//         );
-//         usePermissionStoreHook().changeSetting(info);
-//       }
-//       router.addRoute({
-//         path: "/:pathMatch(.*)",
-//         redirect: "/error/404"
-//       });
-//     });
-//   });
-// }
 
 /**
  * 将多级嵌套路由处理成一维数组

+ 7 - 4
src/store/modules/permission.ts

@@ -22,19 +22,22 @@ export const usePermissionStore = defineStore({
   actions: {
     // 获取异步路由菜单
     asyncActionRoutes(routes) {
-      console.log(this.wholeMenus.length);
-      if (this.wholeMenus.length > 0) return;
+      // console.log(this.wholeMenus.length);
+      // if (this.wholeMenus.length > 0) return;
       this.wholeMenus = filterTree(
         ascending(this.constantMenus.concat(routes))
       );
-
+      // console.log(this.wholeMenus);
       this.menusTree = cloneDeep(
         filterTree(ascending(this.constantMenus.concat(routes)))
       );
-
+      // console.log(this.menusTree);
       const getButtonAuth = (arrRoutes: Array<RouteConfigs>) => {
+        // console.log(arrRoutes);
+        // console.log(arrRoutes.length);
         if (!arrRoutes || !arrRoutes.length) return;
         arrRoutes.forEach((v: RouteConfigs) => {
+          // console.log(v.meta.authority);
           if (v.meta && v.meta.authority) {
             this.buttonAuth.push(...v.meta.authority);
           }

+ 2 - 2
src/store/modules/user.ts

@@ -191,9 +191,9 @@ export const useUserStore = defineStore({
                 ]
               };
               const arrag = [interestRouter, systemRouter];
-              initRouter(arrag).then(() => {});
-              // console.log(arrag);
+
               resolve(res);
+              // console.log(arrag);
             } else {
               resolve(res);
             }

+ 11 - 50
src/views/login/index.vue

@@ -18,7 +18,7 @@ import { resType } from "/@/store/modules/types";
 import { bg, avatar, currentWeek } from "./utils/static";
 import { ReImageVerify } from "/@/components/ReImageVerify";
 import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
-
+import { initRouter } from "/@/router/utils";
 defineOptions({
   name: "Login"
 });
@@ -63,68 +63,29 @@ const onLogin = async (formEl: FormInstance | undefined) => {
           const { code, message: msg } = res;
 
           if (code === 0) {
-            console.log(res);
-            getMenu();
-            // storageSession.setItem("info", {
-            //   username: "admin",
-            //   accessToken: "eyJhbGciOiJIUzUxMiJ9.test"
-            // });
-
-            // message.success("登录成功");
-            // // ElMessage.success("登录成功");
-            // router.push("/");
+            initRouter().then(co => {
+              console.log(co);
+              loading.value = false;
+              ElMessage.success("登录成功");
+              router.push("/");
+            });
           } else if (code > 100 && code < 200) {
             loading.value = false;
-            console.log(res);
           } else {
             loading.value = false;
             ElMessage.success(msg);
           }
         });
-
-      // // 模拟请求,需根据实际开发进行修改
-      // setTimeout(() => {
-      //   loading.value = false;
-      //   storageSession.setItem("info", {
-      //     username: "admin",
-      //     accessToken: "eyJhbGciOiJIUzUxMiJ9.test"
-      //   });
-      //   initRouter("admin").then(() => {});
-      //   message.success("登录成功");
-      //   router.push("/");
-      // }, 2000);
     } else {
       loading.value = false;
       return fields;
     }
   });
 };
-function getMenu() {
-  useUserStoreHook()
-    .postMenuList({})
-    .then((res: resType) => {
-      const { code, message: msg } = res;
-      loading.value = false;
-      if (code === 0) {
-        console.log(res);
-        // storageSession.setItem("info", {
-        //   username: "admin",
-        //   accessToken: "eyJhbGciOiJIUzUxMiJ9.test"
-        // });
-        // initRouter("admin").then(() => {});
-        // message.success("登录成功");
-        // // ElMessage.success("登录成功");
-        // router.push("/");
-      } else if (code > 100 && code < 200) {
-        console.log(res);
-      } else {
-        ElMessage.success(msg);
-      }
-    });
-}
-function onHandle(value) {
-  useUserStoreHook().SET_CURRENTPAGE(value);
-}
+
+// function onHandle(value) {
+//   useUserStoreHook().SET_CURRENTPAGE(value);
+// }
 
 watch(imgCode, value => {
   useUserStoreHook().SET_VERIFYCODE(value);