1. 程式人生 > 程式設計 >vue動態設定路由許可權的主要思路

vue動態設定路由許可權的主要思路

之前看到網上有些動態設定路由的,但是跟目前的專案不是很匹配,就自己動手實現了一種。主要思路就是:

1.配置路由的時候繫結好id,可後端開發完成後,與後端同步id就行,這id唯一不變,根據此id可找到路由地址及icon。

const routerArr = [
 {
 path: '',name: '',component: () => import( /* webpackChunkName: "strategiesMaintain" */ '@/components/Layout/Index'),meta: {
 requireAuth: true,id: 1,icon: 'iconzhanghuguanli',title: '路由1'
 },children: [{ 
 path: '/verificationLog',name: 'VerificationLog',component: () => import( /* webpackChunkName: "verificationLog" */ '@/views/auditManage/verificationLog'),id: 101,icon: 'icon-disanfangyanzhengrizhi',title: '路由11'
 }
 },{
 path: '/systemLog',name: 'SystemLog',component: () => import( /* webpackChunkName: "systemLog" */ '@/views/auditManage/systemLog'),id: 102,icon: 'icon-xitongcaozuorizhi',title: '路由12'
 }
 }]
 }
];

export default routerArr;

2.設定本地路由與後端傳來的路由的聯絡,主要是根據id繫結路由地址及iconClass

import routerModules from "@/router/modules";
import {http} from '@/utils/http'
import store from '@/store';
import { Message } from 'element-ui'

const formateResData = (val) =>{ // 格式化路由資料
 const obj = {};
 const fn = (arr)=>{
  for(let i = 0,item;item = arr[i++];){
  obj[item['meta']['id']] = {
   path: item['path'],iconClass: item['meta']['icon']
  };
  if(item.children && item.children.length > 0){
   fn(item.children);
  }
  }
 }
 fn(val);
 return obj;
};

const MAPOBJ = formateResData(routerModules);
const dealWithData = (navData) => { // 處理選單資料
 let firstLink = "";
 const navIdArr = [];
 const fn = (arr) => {
  for (let i = 0,item;item = arr[i++];) {
  item['iconClass'] = MAPOBJ[item.id].iconClass;
  item['linkAction'] = MAPOBJ[item.id].path;
  navIdArr.push(item.id);
  if (!firstLink && !item.subMenu) { // 設定預設跳轉
   firstLink = item['linkAction'];
  }
  if (item.subMenu && item.subMenu.length > 0) {
   fn(item.subMenu);
  }
  }
 }
 fn(navData);
 return {navData,navIdArr,firstLink};
};

let navIds = [];

const getNav = async (to={},from={},next=()=>{})=>{ // 獲取導航資料
 const {code,data} = await http("/menu/list",{},"GET"); // 獲取選單資料
 // const data = require("@/mock/api/menuData"); // 使用mock資料
 const {navData,firstLink} = dealWithData(data);
 store.commit('setNavData',navData);
 navIds = navIdArr;
 if(to.fullPath == '/index'){ // 從登入過來 或者是回首頁
 next(firstLink);
 }else { // 重新整理
 if(navIds.indexOf(to.meta.id) == -1){ // 後端沒有返回該選單
  Message.error('選單不存在或者沒有許可權');
  return;
 }
 next();
 }
}

export const setGuard = (to={},next=()=>{}) =>{ // 設定許可權
 if(navIds.length === 0){ // 還沒有獲取選單資料
 getNav(to,from,next);
 }else { // 獲取到選單資料
 if(navIds.indexOf(to.meta.id) == -1){ // 後端沒有返回該選單
  Message.error('選單不存在或者沒有許可權');
  return;
 }
 next();
 }
}

3.在mainjs中引入配置

router.beforeEach((to,next) => {
 let token = wlhStorage.get("authorization");
 if (to.path == "/login") {
 storage.clear();// 清空快取
 next();
 } else {
 if (to.meta.requireAuth && token) { // 登陸
  setGuard(to,next);
 } else { // 沒有登入
  next("/login");
 }
 }
})

總結

到此這篇關於vue動態設定路由許可權的文章就介紹到這了,更多相關vue動態設定路由許可權內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!