一、用户权限
1. 修改router/index.js文件 去掉多余的路由,只留下这两个路由。/404、{ path: “*”, redirect: “/404”, hidden: true }都注释掉。
2. 修改store/modules/user.js文件
import { adminMenus, signUserMenus } from "@/router/menus";
const getDefaultState = () => {
return {
menus: "",
};
};
const mutations = {
SET_MEUN: (state, menus) => {
state.menus = menus;
},
}
getRouter({ commit, state }) {
return new Promise((resolve, reject) => {
let role = sessionStorage.getItem("name");
let menus;
if (role == "admin") {
menus = adminMenus;
} else if (role == "urgent") {
menus = urgentMenus;
} else {
menus = signUserMenus;
}
menus.push(
{
path: "/404",
component: "404",
hidden: true,
},
{
path: "*",
redirect: "/404",
hidden: true,
}
);
commit("SET_MEUN", menus);
resolve();
});
},
3. 修改store/getters.js文件
const getters = {
menus: (state) => state.user.menus,
};
4. 自定义路由结构文件 因为路径在前端就定死了,所以就在前端写了个路由文件menus.js。
export const adminMenus = [
{
component: "Layout",
hidden: false,
path: "/operation",
children: [
{
path: "index",
component: "operation/index",
name: "operation",
meta: { title: "许可", icon: "operation" },
},
],
},
{
alwaysShow: true,
component: "Layout",
hidden: false,
path: "/system",
meta: {
title: "系统管理",
icon: "system",
},
children: [
{
hidden: false,
title: "用户管理",
path: "user",
component: "user/index",
name: "user",
meta: {
title: "用户管理",
icon: "el-icon-user",
},
}
],
},
5、新建两个路由加载文件 _import_development.js
module.exports = file => require('@/views/' + file + '.vue').default
_import_production.js
module.exports = file => () => import('@/views/' + file + '.vue')
7、修改文件 8、修改permission.js文件
import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { getToken } from "@/utils/auth";
import getPageTitle from "@/utils/get-page-title";
const _import = require("./router/_import_" + process.env.NODE_ENV);
import Layout from "@/layout";
NProgress.configure({ showSpinner: false });
const whiteList = ["/login"];
router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = getPageTitle(to.meta.title);
const hasToken = getToken();
let token = sessionStorage.getItem("token");
if (token) {
if (to.path === "/login") {
next({ path: "/" });
NProgress.done();
} else {
const hasGetUserInfo = store.getters.name;
if (hasGetUserInfo) {
next();
} else {
try {
await store.dispatch("user/getInfo");
await store.dispatch("user/getRouter");
if (store.getters.menus.length < 1) {
global.antRouter = [];
next();
}
const menus = filterAsyncRouter(store.getters.menus);
router.addRoutes(menus);
global.antRouter = menus;
next({ ...to, replace: true });
} catch (error) {
await store.dispatch("user/resetToken");
Message.error(error || "Has Error");
next(`/login?redirect=${to.path}`);
NProgress.done();
}
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next();
} else {
next(`/login?redirect=${to.path}`);
NProgress.done();
}
}
});
router.afterEach(() => {
NProgress.done();
});
function filterAsyncRouter(asyncRouterMap) {
const accessedRouters = asyncRouterMap.filter((route) => {
if (route.component) {
if (route.component === "Layout") {
route.component = Layout;
} else {
try {
route.component = _import(route.component);
} catch (error) {
}
}
}
if (route.children && route.children.length) {
route.children = filterAsyncRouter(route.children);
}
return true;
});
return accessedRouters;
}
二、按钮权限 main.js
import '@/permission'
Vue.directive('permissions', {
inserted(el, binding, node) {
const {
value
} = binding
const roles = JSON.parse(sessionStorage.getItem('authorities'));
if (value && value instanceof Array && value.length > 0) {
const permissionRoles = value
const hasPermission = roles.some(role => {
return permissionRoles.includes(role)
})
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(`使用方式: v-permission="['admin','editor']"`)
}
}
})
index.vue
<el-button
type="primary"
@click="pageQuery('cx')"
v-permissions="findOrgPage"
>查询</el-button
>
data(){
retunr{
findOrgPage: ["findOrgPage"],
}
}
|