基於vuex, vue-router,vuex的權限控制教程,完整代碼地址見 https://github.com/linrunzhen... javascript
接下來讓咱們模擬一個普通用戶打開網站的過程,一步一步的走完整個流程。前端
首先從打開本地的服務localhost:8080開始,咱們知道打開後會進入login頁面,那麼判斷的依據是什麼。
首先是token。
沒有登錄的用戶是獲取不到token的,而登錄後的角色咱們會將token存到local或者seesionStorage 所以,根據當前有沒有token便可知道是否登錄。vue
/* state.js */ export default { get UserToken() { return localStorage.getItem('token') }, set UserToken(value) { localStorage.setItem('token', value) } } /* mutation.js */ export default { LOGIN_IN(state, token) { state.UserToken = token }, LOGIN_OUT(state) { state.UserToken = '' } }
router.beforeEach((to, from, next) => { if (!store.state.UserToken) { if ( to.matched.length > 0 && !to.matched.some(record => record.meta.requiresAuth) ) { next() } else { next({ path: '/login' }) } } })
好了,此時用戶打開localhost:8080,默認匹配的是''路徑,此時咱們並無掛載路由,也沒有token,因此來到了login。java
輸入用戶名密碼後,有token了,經過store觸發 commit('LOGIN_IN') 來設置token。ios
可是仍是沒有路由,目前最開始只有login路由git
/* 初始路由 */ export default new Router({ routes: [ { path: '/login', component: Login } ] }) /* 準備動態添加的路由 */ export const DynamicRoutes = [ { path: '', component: Layout, name: 'container', redirect: 'home', meta: { requiresAuth: true, name: '首頁' }, children: [ { path: 'home', component: Home, name: 'home', meta: { name: '首頁' } } ] }, { path: '/403', component: Forbidden }, { path: '*', component: NotFound } ]
因爲權限這塊邏輯還挺多,因此在vuex添加了一個permission模塊來處理權限。github
爲了判斷是已有路由列表,須要在vuex的permission模塊存一個state狀態permissionList用來判斷,假如permissionList不爲null,即已經有路由,若是不存在,就須要咱們幹活了。vue-router
router.beforeEach((to, from, next) => { if (!store.state.UserToken) { ... } else { /* 如今有token了 */ if (!store.state.permission.permissionList) { /* 若是沒有permissionList,真正的工做開始了 */ store.dispatch('permission/FETCH_PERMISSION').then(() => { next({ path: to.path }) }) } else { if (to.path !== '/login') { next() } else { next(from.fullPath) } } } })
來看一下 store.dispatch('permission/FETCH_PERMISSION') 都幹了什麼vuex
actions: { async FETCH_PERMISSION({ commit, state }) { /* 獲取後臺給的權限數組 */ let permissionList = await fetchPermission() /* 根據後臺權限跟咱們定義好的權限對比,篩選出對應的路由並加入到path=''的children */ let routes = recursionRouter(permissionList, dynamicRouter) let MainContainer = DynamicRoutes.find(v => v.path === '') let children = MainContainer.children children.push(...routes) /* 生成左側導航菜單 */ commit('SET_MENU', children) setDefaultRoute([MainContainer]) /* 初始路由 */ let initialRoutes = router.options.routes /* 動態添加路由 */ router.addRoutes(DynamicRoutes) /* 完整的路由表 */ commit('SET_PERMISSION', [...initialRoutes, ...DynamicRoutes]) } }
{ "code": 0, "message": "獲取權限成功", "data": [ { "name": "訂單管理", "children": [ { "name": "訂單列表" }, { "name": "生產管理", "children": [ { "name": "生產列表" } ] }, { "name": "退貨管理" } ] } ] }
/* 這裏是咱們寫好的須要權限判斷的路由 */ const dynamicRoutes = [ { path: '/order', component: Order, name: 'order-manage', meta: { name: '訂單管理' }, children: [ { path: 'list', name: 'order-list', component: OrderList, meta: { name: '訂單列表' } }, { path: 'product', name: 'product-manage', component: ProductManage, meta: { name: '生產管理' }, children: [ { path: 'list', name: 'product-list', component: ProductionList, meta: { name: '生產列表' } }, { path: 'review', name: 'review-manage', component: ReviewManage, meta: { name: '審覈管理' } } ] }, { path: 'returnGoods', name: 'return-goods', component: ReturnGoods, meta: { name: '退貨管理' } } ] } ] export default dynamicRoutes
爲了對比,我寫好了一個遞歸函數,用name和meta.name進行對比 ,根據這個函數就能夠獲得咱們想要的結果json
/** * * @param {Array} userRouter 後臺返回的用戶權限json * @param {Array} allRouter 前端配置好的全部動態路由的集合 * @return {Array} realRoutes 過濾後的路由 */ export function recursionRouter(userRouter = [], allRouter = []) { var realRoutes = [] allRouter.forEach((v, i) => { userRouter.forEach((item, index) => { if (item.name === v.meta.name) { if (item.children && item.children.length > 0) { v.children = recursionRouter(item.children, v.children) } realRoutes.push(v) } }) }) return realRoutes }
獲得過濾後的數組後,加入到path爲''的children下面
{ path: '', component: Layout, name: 'container', redirect: 'home', meta: { requiresAuth: true, name: '首頁' }, children: [ { path: 'home', component: Home, name: 'home', meta: { name: '首頁' } }, <!-- 將上面獲得的東西加入到這裏 --> ... ] }
/* 動態添加路由 */ router.addRoutes(DynamicRoutes) /* 初始路由 */ let initialRoutes = router.options.routes /* 合併起來,就是完整的路由了 */ commit('SET_PERMISSION', [...initialRoutes, ...DynamicRoutes])
路由添加完了,也就是action操做完畢了,便可在action.then裏面調用 next({ path: to.path })進去路由,這裏要注意, next裏面要傳參數即要進入的頁面的路由信息,由於next傳參數後,當前要進入的路由會被廢止,轉而進入參數對應的路由,雖然是同一個路由,這麼作主要是爲了確保addRoutes生效了。
<template> <div class="menu-container"> <template v-for="v in menuList"> <el-submenu :index="v.name" v-if="v.children&&v.children.length>0" :key="v.name"> <template slot="title"> <i class="iconfont icon-home"></i> <span>{{v.meta.name}}</span> </template> <el-menu-item-group> <my-nav :menuList="v.children"></my-nav> </el-menu-item-group> </el-submenu> <el-menu-item :key="v.name" :index="v.name" @click="gotoRoute(v.name)" v-else> <i class="iconfont icon-home"></i> <span slot="title">{{v.meta.name}}</span> </el-menu-item> </template> </div> </template> <script> export default { name: 'my-nav', props: { menuList: { type: Array, default: function() { return [] } } }, methods: { gotoRoute(name) { this.$router.push({ name }) } } } </script>
if (!store.state.permission.permissionList) { store.dispatch('permission/FETCH_PERMISSION').then(() => { next({ path: to.path }) }) } ... router.afterEach((to, from, next) => { var routerList = to.matched store.commit('setCrumbList', routerList) store.commit('permission/SET_CURRENT_MENU', to.name) })
最後還有一點,每次請求得帶上token, 能夠對axios封裝一下來處理
var instance = axios.create({ timeout: 30000, baseURL }) // 添加請求攔截器 instance.interceptors.request.use( function(config) { // 請求頭添加token if (store.state.UserToken) { config.headers.Authorization = store.state.UserToken } return config }, function(error) { return Promise.reject(error) } ) /* axios請求二次封裝 */ instance.get = function(url, data, options) { return new Promise((resolve, reject) => { axios .get(url, data, options) .then( res => { var response = res.data if (response.code === 0) { resolve(response.data) } else { Message.warning(response.message) /* reject(response.message) */ } }, error => { if (error.response.status === 401) { Message.warning({ message: '登錄超時,請從新登陸' }) store.commit('LOGIN_OUT') window.location.reload() } else { Message.error({ message: '系統異常' }) } reject(error) } ) .catch(e => { console.log(e) }) }) } export default instance