前言
权限授权登录
登录
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true;
login(this.loginForm)
.then(res => {
if (res.code === 200) {
// 存放token
sessionStorage.setItem("tokens", res.data.token);
// 触发Vuex 来 加载 获取当前用户的菜单,并解析路由
store.dispatch("setMenuList");
this.$message({
message: "登录成功",
type: "success",
duration: 1000
});
this.$router.replace({ path: "/dashboard" });
}
})
.catch(() => {
this.loading = false;
});
} else {
console.log("error submit!!");
return false;
}
});
}
获取当前用户菜单,解析路由
/*
* @Description:
* @Author: ZhangXin
* @Date: 2021-02-02 16:10:59
* @LastEditTime: 2021-02-23 23:03:30
* @LastEditors: ZhangXin
*/
// getMenu 解析后台路由
import { getMenu } from '../../utils/getMenu'
// 引入路由 和 静态路由
import router, { constantRoutes } from '../../router/index'
const state = {
routerType: '',
// 菜单路由
meunList: []
}
const mutations = {
SET_ROUTER_TYPE(state, type) {
state.routerType = type
},
SET_ROUTER_MENULIST(state, list) {
// 静态路由 + 动态路由 合并 完整路由
const array = constantRoutes.concat(list)
state.meunList = array
router.options.routes = array
router.addRoutes([...array])
}
}
const actions = {
setMenuList({ commit, state }) {
// 接收返回来的 路由数组
return new Promise((resolve, reject) => {
getMenu().then(res => {
commit('SET_ROUTER_TYPE', '')
commit('SET_ROUTER_MENULIST', res)
resolve(res)
})
})
}
}
export default {
state,
mutations,
actions
}
解析后端返回来路由(重点)
/*
* @Description:
* @Author: ZhangXin
* @Date: 2021-02-02 16:03:48
* @LastEditTime: 2021-02-23 23:09:02
* @LastEditors: ZhangXin
*/
import Layout from '@/layout'
import {getUserAuthMenu} from '@/api/user'
/**
* @description: 解析后端返回来的菜单树
* @param {*} data 后端返回来的路由树
* @param {*} arr 菜单
* @return {*}
*/
function tree(data, arr) {
data.forEach((datas, index) => {
arr.push({
path: datas.path,
name: datas.name,
types: datas.types,
hidden: datas.hidden == 'true' ? true : false,
// 当时这块踩坑了
component: datas.component === 'Layout' ? Layout : resolve => require([`@/views/${datas.component}.vue`], resolve),
meta: {
title: datas.meta.title,
icon: datas.meta.icon,
// 用来存放按钮权限
button: datas.meta.button
},
// redirect: datas.redirect,
id: datas.id,
// 子路由
children: []
})
if (datas.children) {
const childArr = tree(datas.children, [])
arr[index].children = childArr
}
})
return arr
}
/**
* @description: 获取当前登录用户的菜单
* @param {*}
* @return {*}
*/
export function getMenu() {
return new Promise(function (resolve, reject) {
getUserAuthMenu().then(res => {
if(res.code === 200){
const datas = res.data
// 调用 tree 来解析后端返回来的树
resolve(tree(datas, []))
}
})
})
}
后端接收路由格式
前端接收到的真实菜单树
页面刷新,路由丢失
如何处理呢?
我这块直接在 App.vue
的 生命周期 created
中 , 再次执行了 Vuex
来进行加载和解析,没有做其它操作。 当然了,具体业务具体对待。
<template>
<div id="app">
<router-view v-if="isRouterAlive" />
</div>
</template>
<script>
import store from "@/store";
export default {
name: "App",
provide() {
return {
reload: this.reload
};
},
data() {
return {
isRouterAlive: true
};
},
methods: {
reload() {
this.isRouterAlive = false;
this.$nextTick(() => (this.isRouterAlive = true));
}
},
created() {
//只要刷新页面,就会重新加载路由树,保证了路由不会丢失数据
store.dispatch("setMenuList");
}
};
</script>