vue项目权限管理
首先,权限管理 般需求是两个: 权限和按钮权限。 权限管理 般需求是 权限和按钮权限的管理 具体实现的时候分后端和前端两种 案:
前端 案会把所有路由信息在前端配置,通过路由守卫要求 户登录, 户登录后根据 过滤出路由表。 如我会配置 个 asyncRoutes 数组,需要认证的 在其路由的 meta 中添加 个 roles 字段,等获取 户 之后取两者的交集,若结果不为空则说明可以访问。此过滤过程结束,剩下的路由就是该 户能访问的 ,最后通过 router.addRoutes(accessRoutes) 式动态添加路由即可。
后端 案会把所有 路由信息存在数据库中, 户登录的时候根据其 查询得到其能访问的所有 路由信息返回给前端,前端再通过 addRoutes 动态添加路由信息。
按钮权限的控制通常会实现 个指令,例如 v-permission ,将按钮要求 通过值传给v-permission指令,在指令的 moutned 钩 中可以判断当前 户 和按钮是否存在交集,有则保留按钮, 则移除按钮。 纯前端 案的优点是实现简单,不需要额外权限管理 ,但是维护起来问题 较 ,有新的 和 需求 就要修改前端代码重新打包部署;服务端 案就不存在这个问题,通过专 的 和权限管理 ,配置 和按钮权限信息到数据库,应 每次登陆时获取的都是最新的路由信息,可谓 劳永逸!
路由守卫 permission.js :import router from "./router" import store from "./store" import { Message } from "element-ui" import NProgress from "nprogress" // progress bar import "nprogress/nprogress.css" // progress bar style import { getToken } from "@/utils/auth" // get token from cookie import getPageTitle from "@/utils/get-page-title" NProgress.configure({ showSpinner: false }) // NProgress Configuration const whiteList = ["/login", "/auth-redirect"] // no redirect whitelist router.beforeEach(async(to, from, next) => { // start progress bar NProgress.start() // set page title document.title = getPageTitle(to.meta.title) // determine whether the user has logged in const hasToken = getToken() if (hasToken) { if (to.path === "/login") { // if is logged in, redirect to the home page next({ path: "/" }) NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939 } else { // determine whether the user has obtained his permission roles through getInfo const hasRoles = store.getters.roles && store.getters.roles.length > 0 if (hasRoles) { next() } else { try { // get user info // note: roles must be a object array! such as: ["admin"] or ,["developer","editor"] const { roles } = await store.dispatch("user/getInfo") // generate accessible routes map based on roles const accessRoutes = await store.dispatch("permission/generateRoutes", roles) // dynamically add accessible routes router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record next({ ...to, replace: true }) } catch (error) { // remove token and go to login page to re-login await store.dispatch("user/resetToken") Message.error(error || "Has Error") next(`/login?redirect=${to.path}`) NProgress.done() } } } } else { /* has no token*/ if (whiteList.indexOf(to.path) !== -1) { // in the free login whitelist, go directly next() } else { // other pages that do not have permission to access are redirected to the login page. next(`/login?redirect=${to.path}`) NProgress.done() } } }) router.afterEach(() => { // finish progress bar NProgress.done() }) 复制代码
路由 成## permission.js :import { asyncRoutes, constantRoutes } from "@/router" /** * Use meta.role to determine if the current user has permission * @param roles * @param route */ function hasPermission(roles, route) { if (route.meta && route.meta.roles) { return roles.some(role => route.meta.roles.includes(role)) } else { return true } } /** * Filter asynchronous routing tables by recursion * @param routes asyncRoutes * @param roles */ export function filterAsyncRoutes(routes, roles) { const res = [] routes.forEach(route => { const tmp = { ...route } if (hasPermission(roles, tmp)) { if (tmp.children) { tmp.children = filterAsyncRoutes(tmp.children, roles) } res.push(tmp) } }) return res } const state = { routes: [], addRoutes: [] } const mutations = { SET_ROUTES: (state, routes) => { state.addRoutes = routes state.routes = constantRoutes.concat(routes) } } const actions = { generateRoutes({ commit }, roles) { return new Promise(resolve => { let accessedRoutes if (roles.includes("admin")) { accessedRoutes = asyncRoutes || [] } else { accessedRoutes = filterAsyncRoutes(asyncRoutes, roles) } commit("SET_ROUTES", accessedRoutes) resolve(accessedRoutes) }) } } export default { namespaced: true, state, mutations, actions } 复制代码
动态追加路由## permission.js :import router from "./router" import store from "./store" import { Message } from "element-ui" import NProgress from "nprogress" // progress bar import "nprogress/nprogress.css" // progress bar style import { getToken } from "@/utils/auth" // get token from cookie import getPageTitle from "@/utils/get-page-title" NProgress.configure({ showSpinner: false }) // NProgress Configuration const whiteList = ["/login", "/auth-redirect"] // no redirect whitelist router.beforeEach(async(to, from, next) => { // start progress bar NProgress.start() // set page title document.title = getPageTitle(to.meta.title) // determine whether the user has logged in const hasToken = getToken() if (hasToken) { if (to.path === "/login") { // if is logged in, redirect to the home page next({ path: "/" }) NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939 } else { // determine whether the user has obtained his permission roles through getInfo const hasRoles = store.getters.roles && store.getters.roles.length > 0 if (hasRoles) { next() } else { try { // get user info // note: roles must be a object array! such as: ["admin"] or ,["developer","editor"] const { roles } = await store.dispatch("user/getInfo") // generate accessible routes map based on roles const accessRoutes = await store.dispatch("permission/generateRoutes", roles) // dynamically add accessible routes router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record next({ ...to, replace: true }) } catch (error) { // remove token and go to login page to re-login await store.dispatch("user/resetToken") Message.error(error || "Has Error") next(`/login?redirect=${to.path}`) NProgress.done() } } } } else { /* has no token*/ if (whiteList.indexOf(to.path) !== -1) { // in the free login whitelist, go directly next() } else { // other pages that do not have permission to access are redirected to the login page. next(`/login?redirect=${to.path}`) NProgress.done() } } }) router.afterEach(() => { // finish progress bar NProgress.done() }) 复制代码
服务端返回的路由信息如何添加到路由器中? // 前端组件名和组件映射表 const map = { // xx: require("@/views/xx.vue").default // 同步的 式 xx: () => import("@/views/xx.vue") // 异步的 式 } // 服务端返回的 asyncRoutes const asyncRoutes = [ { path: "/xx", component: "xx", ... } ] // 遍历asyncRoutes,将component替换为map[component] function mapComponent(asyncRoutes) { asyncRoutes.forEach(route => { route.component = map[route.component]; if(route.children) { route.children.map(child => mapComponent(child)) } }) } mapComponent(asyncRoutes)
新能源车提升续航里程,不只有811和刀片这两条路在行业内有一种主流的声音,纯电动车型的续航里程达到500km以上就够用了,再提升续航就是浪费。但是如果你真正开过电动车,你就会对续航有不一样的认识,标称的NEDC续航里程,在常温状
有没有升级成5G网络的手机用户,使用的怎么样?现在好像不需要办理5G套餐,只要换成5G手机就可以使用5G网络了,反正我没有办理5G套餐,电信就给我自动开通了5G功能,同事的移动号码也是自动开通的5G功能,联通那边问了一下基本上
iOS15再曝光,这些功能真的很实用距离WWDC2021全球开发者大会越来越近了,关于iOS15系统的爆料也越来越多。和iPhone一样,iOS系统每年只更新一次大版本。虽然更新频率不高,每次更新所受到的关注度却无人
miniLED是过渡技术?iPadPro的屏幕意味着什么4月底,让不少人翘首以待的科技春晚2021苹果春季新品发布会如期举行。虽然发布会的亮点很多,但对于手机用户来说,最值得关注的可能就是新的12。9英寸iPadPro了。新款12。9英
云南移动携手华为,解决了5G网络中的一大难题都是5G网络,为什么你网速比我的快呢?这涉及到一个非常重要的技术问题。我们来一探究竟!近日,云南移动联合华为在滇池国际会展中心,基于2。6GHz组网,完成国内首个5GDLCoMP(
观看距离4米的话可以买多大的液晶电视?有何科学参考标准?观看距离4米的话可以买多大的液晶电视?有何科学参考标准?电视的观看距离重要的参考标准是电视分辨率和电视的尺寸了。正常情况观看距离越远选择越大尺寸的屏幕的电视观看体验会越好,而电视的
华为麒麟970处理器ai智能在哪里?欢迎在今日头条和悟空问答上点击右上角关注太平洋电脑网,更多精彩资讯等着您。首先你要理解人工智能是什么,才能很好地理解人工智能芯片。人工智能(ArtificialIntelligen
(新华热评)网约车平台请给公众一个解释来源新华网新华社合肥5月6日电题网约车平台请给公众一个解释新华社记者戴威胡锐近日,部分网约车平台隐瞒高额抽成的新闻被曝出,引发网友热议。乘客反映打车越来越贵,司机感叹挣钱越来越难。
网约车平台乘客打车贵司机挣钱难,抽成甚至达50新华社合肥5月6日电题网约车平台请给公众一个解释新华社记者戴威胡锐近日,部分网约车平台隐瞒高额抽成的新闻被曝出,引发网友热议。乘客反映打车越来越贵,司机感叹挣钱越来越难。本该让出行
快评丨网约车平台抽成高不高,讨论不妨深入一些不管如何,让平台抽成比例成为阳光下的计算,这是网约车平台必须完成的一个环节。近日,据新华社报道,一些网约车平台抽成比例能达到25甚至更高,而司机和乘客对每一单的抽成比例计算方式毫不
小鹏汽车4月共交付5147台,同比增长285小鹏P7并未受到特斯拉Model3的冲击。日前,小鹏汽车官方公布了其4月的交付量成绩单,今年4月总交付量为5147台,较去年同期大增285,其中,明星车型小鹏P7交付量2995台,