Blame view

稻城亚丁小程序/admin-web/src/router/index.ts 3.18 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  import { createRouter, createWebHistory } from "vue-router";
  import { getToken } from "@/utils/request";
  import { useUserStore } from "@/stores/user";
  import { fetchMyMenus, fetchProfile } from "@/api/admin";
  import {
    ensureContentBeforeSystem,
    flattenContentMenus,
    flattenMpUserMenu,
    withDownloadAssetsMenu,
    withDashboardMenu,
    withMpUserMenu,
  } from "@/utils/menuExtras";
  
  const router = createRouter({
    history: createWebHistory(),
    routes: [
      {
        path: "/login",
        name: "login",
        component: () => import("@/views/Login.vue"),
        meta: { public: true, title: "登录" },
      },
      {
        path: "/",
        component: () => import("@/layouts/MainLayout.vue"),
        redirect: "/dashboard",
        children: [
          {
            path: "dashboard",
            name: "dashboard",
            component: () => import("@/views/Dashboard.vue"),
            meta: { title: "工作台" },
          },
          {
            path: "content/work",
            name: "work",
            component: () => import("@/views/work/WorkManage.vue"),
            meta: { title: "作品审核" },
          },
          {
            path: "content/banner",
            name: "banner",
            component: () => import("@/views/content/BannerManage.vue"),
            meta: { title: "轮播图" },
          },
          {
            path: "content/download-assets",
            name: "download-assets",
            component: () => import("@/views/content/DownloadAssets.vue"),
            meta: { title: "下载素材" },
          },
          {
            path: "system/admin",
            name: "system-admin",
            component: () => import("@/views/system/AdminUser.vue"),
            meta: { title: "管理员" },
          },
          {
            path: "system/role",
            name: "system-role",
            component: () => import("@/views/system/Role.vue"),
            meta: { title: "角色管理" },
          },
          {
            path: "system/menu",
            name: "system-menu",
            component: () => import("@/views/system/Menu.vue"),
            meta: { title: "菜单管理" },
          },
          {
            path: "system/mp-user",
            name: "system-mp-user",
            component: () => import("@/views/system/MpUser.vue"),
            meta: { title: "用户管理" },
          },
        ],
      },
      { path: "/:pathMatch(.*)*", redirect: "/dashboard" },
    ],
  });
  
  router.beforeEach(async (to, _from, next) => {
    document.title = `${(to.meta.title as string) || "后台"} · 稻城亚丁`;
    if (to.meta.public) {
      next();
      return;
    }
    if (!getToken()) {
      next({ path: "/login", query: { redirect: to.fullPath } });
      return;
    }
    const store = useUserStore();
    if (!store.profile) {
      try {
        const [prof, menus] = await Promise.all([fetchProfile(), fetchMyMenus()]);
        store.setProfile(prof.data);
        store.setMenus(
          ensureContentBeforeSystem(
            withDownloadAssetsMenu(
              withMpUserMenu(
              withDashboardMenu(flattenMpUserMenu(flattenContentMenus(menus.data.list)))
              )
            )
          )
        );
        next();
      } catch {
        store.clearProfile();
        next({ path: "/login", query: { redirect: to.fullPath } });
      }
      return;
    }
    next();
  });
  
  export default router;