haoxuan
2024-02-07 26e7722e50eac82de8a004e95058b44faca0d88f
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import Vue from "vue"
import Router from "vue-router"
// import type from "@/router/deployCode"
 
import clientRouter from "./client/index.js"
import salesRouter from "./sales/index.js"
import serviceRouter from "./service/index.js"
import {getMenuTreeByRole} from "@/api/menus/index"
import store from '@/store/index.js'; 
// import backgroundConfigRouter from "./backgroundConfig/index.js"
 
Vue.use(Router)
const login = (resolve) => require(["@/views/other/login/index"], resolve)
const client = (resolve) => require(["@/views/client/index"], resolve)
const sales = (resolve) => require(["@/views/sales/index"], resolve)
const service = (resolve) => require(["@/views/service/index"], resolve)
const noData = (resolve) => require(["@/views/NoData/index"], resolve)
// const background = (resolve) => require(["@/views/backgroundConfig/index"], resolve)
 
export const routes = [
  // 无数据页面
  {
    path: "noData",
    name: "noData",
    meta: {
      title: "",
      auth: true,
    },
    component: noData,
  },
  {
    path: "client", // 客户管理
    name: "client",
    component: client,
    children: clientRouter,
    meta: {
      title: "客户管理",
      isAllways: true
    }
  },
  {
    path: "sales", // 销售管理
    name: "sales",
    component: sales,
    children: salesRouter,
    meta: {
      title: "销售管理",
      isAllways: true
    }
  },
  {
    path: "service", // 服务管理
    name: "service",
    component: service,
    children: serviceRouter,
    meta: {
      title: "服务管理",
      isAllways: true
    }
  }
  // {
  //   path: "background", // 后台设置
  //   name: "background",
  //   component: background,
  //   children: backgroundConfigRouter,
  //   meta: {
  //     title: "后台设置",
  //     isAllways: true
  //   }
  // }
]
export const constantRoutes = [
  {
    path: "/",
    component: () => import("@/components/layout/index"),
    name: "Index",
    meta: {
      title: "首页",
      isAllways: true,
      insIndex: true
    },
    redirect: {
      name: "salesLead"
    },
    children: routes
  },
  {
    path: "/login",
    component: login,
    meta: {
      isLogin: true,
      title: "登录"
    }
  },
  {
    path: "*",
    component: () => import("@/views/other/error/404"),
    meta: {
      title: "404"
    }
  }
]
// 导出路由 在 main.js 里使用
const createRouter = () =>
  new Router({
    mode: "history",
    // base: window.getServerJson.context,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })
 
const router = createRouter()
 
let isSkip = false;
async function hasPermission(routePath) {
  isSkip = false;
  try {
    const res = await getMenuTreeByRole();
    const newPath = {
      path: "/noData"
    };
    const foundObject = res.data.list.find(obj => obj.systemType === 2);
    if (foundObject) {
      store.commit('setMenus', foundObject.menus); 
      foundObject.menus.forEach(item => {
        const nextPath = item.children.find(obj => obj.path === routePath);
        if (nextPath) {
          newPath.path = nextPath.path;
          isSkip = true;
        }
      });
    } else {
      newPath.path = '/noData';
    }
    return newPath; 
  } catch (error) {
    return { path: "/noData" }; 
  }
}
router.beforeEach(async (to, from, next) => {
  try {
    const result = await hasPermission(to.path);
    console.log(result,"result")
    next();
    // if (!isSkip) {
    //   next('/noData')
    // }
  } catch (error) {
    console.error('Error in navigation guard:', error);
    next(); 
  }
});
 
// router.beforeEach((to, from, next) => {
// must call `next`
// console.log(to, from)
// if (to.path === "/custom/salesLead") {
//   next()
// } else {
// if (to.meta.requireAuth) {
//   next({
//     path: "/login",
//     query: { redirect: to.fullPath }
//   })
// } else {
//   next()
// }
// }
// })
 
export default router