Skip to content

Commit 5671fa2

Browse files
committed
路由拦截
1 parent d744243 commit 5671fa2

File tree

4 files changed

+13
-49
lines changed

4 files changed

+13
-49
lines changed

src/components/menus/Menus.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export default {
4848
return this.$route.path.replace('/', '');
4949
}
5050
},
51-
mounted () {
52-
console.log('当前的路由', this.menuList);
53-
},
5451
components: {
5552
MenuItem,
5653
},

src/router/router.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ const router = new VueRouter({
3737
* @param {type}
3838
* @return:
3939
*/
40-
// eslint-disable-next-line no-unused-vars
4140
const auth = (to, from, next) => {
42-
console.log(to.meta.unauth, Boolean(storage.getItem(authToken)), to.fullPath);
4341
// 如果需要登录的地址及本地不存在authToken的时候就到登录页面
4442
if (!to.meta.unauth && !storage.getItem(authToken)) {
4543
Vue.prototype.$notify({
@@ -50,15 +48,21 @@ const auth = (to, from, next) => {
5048
}
5149
}
5250

51+
/**
52+
* @Author: 水痕
53+
* @Date: 2020-06-02 13:10:09
54+
* @LastEditors: 水痕
55+
* @Description: 根据后端返回的菜单接口权限来拦截本地菜单路由
56+
* @param {type}
57+
* @return:
58+
*/
5359
const authRoutes = async (to, from, next) => {
54-
if (!store.state.hasPermission) {
55-
const routes = await store.dispatch('getAuthRoutes');
56-
// router.options.routes[0].children.push(...routes);
57-
router.addRoutes(routes);
58-
console.log(routes, '////', router.options.routes)
60+
const routes = await store.dispatch('getMenuListApi');
61+
const routesUrlList = routes.map(item => item.index);
62+
if (routesUrlList.includes(to.name)) {
5963
next({ ...to, replace: true });
6064
} else {
61-
next();
65+
next({ name: 'home', replace: true });
6266
}
6367
}
6468
/********************************路由拦截配置 start********************************/

src/store/modules/menus.js

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
11
import MenuService from '@/services/menu';
2-
import { constantRoutes } from '@/router/constant-routes';
3-
4-
/**
5-
* @Author: 水痕
6-
* @Date: 2020-06-02 09:57:15
7-
* @LastEditors: 水痕
8-
* @Description: 根据服务器返回的路由文件过滤本地的路由文件数据
9-
* @param {type}
10-
* @return:
11-
*/
12-
const getRoutes = (authList) => {
13-
const auth = authList.map(item => item.index);
14-
// 对当前本地的路由文件进行过滤
15-
const filterRoutes = (authRoutes) => {
16-
return authRoutes.filter(route => {
17-
if (auth.includes(route.name)) {
18-
// 递归遍历子菜单
19-
if (route.children) {
20-
route.children = filterRoutes(route.children);
21-
}
22-
return route;
23-
}
24-
})
25-
}
26-
return filterRoutes(constantRoutes);
27-
}
282

293
export default {
304
state: {
31-
hasPermission: false, // 判断是否有权限
32-
authMenuList: [], // 授权的菜单列表
5+
authMenuList: [], // 授权的菜单列表(服务器端的)
336
},
347
mutations: {
35-
setPermission (state) {
36-
state.hasPermission = true;
37-
},
388
setAuthMenuList (state, payload) {
399
state.authMenuList = payload;
4010
}
@@ -44,13 +14,7 @@ export default {
4414
async getMenuListApi ({ commit }) {
4515
const { menuList } = await MenuService.getMenuList();
4616
commit('setAuthMenuList', menuList);
47-
commit('setPermission');
4817
return menuList;
4918
},
50-
// 根据获取的菜单,动态添加路由到本地路由文件
51-
async getAuthRoutes ({ dispatch }) {
52-
const authList = await dispatch('getMenuListApi');
53-
return getRoutes(authList);
54-
}
5519
}
5620
}

src/utils/menu.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const getTreeList = (dataList) => {
2525
}
2626
return arr;
2727
}, []);
28-
console.log(formatArray);
2928
return formatArray;
3029
};
3130

0 commit comments

Comments
 (0)