- ruoyi-ui\src\permission.js修改router.beforeEach代码
router.beforeEach((to, from, next) => {
NProgress.start()
if (getToken()) {
to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
if (store.getters.roles.length === 0) {
store.dispatch('GetInfo').then(res => {
const roles = res.roles
store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
router.addRoutes(accessRoutes)
if (to.fullPath == '/') {
var heardPath=accessRoutes[0].path.split("/")[1]==''?"":accessRoutes[0].path;
let pathIndex = ''
pathIndex = heardPath + '/' + accessRoutes[0].children[0].path
next({ path: pathIndex, replace: true })
} else {
next({ ...to, replace: true })
}
})
}).catch(err => {
store.dispatch('LogOut').then(() => {
Message.error(err)
location.href = '/login';
})
})
} else {
next()
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
NProgress.done()
}
}
})
- ruoyi-ui/src/store/modules/permission.js修改代码
const permission = {
state: {
routes: [],
addRoutes: [],
defaultRoutes: [],
topbarRouters: [],
sidebarRouters: [],
//修改默认页面
indexPage:''
},
mutations: {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
},
SET_DEFAULT_ROUTES: (state, routes) => {
state.defaultRoutes = constantRoutes.concat(routes)
},
SET_TOPBAR_ROUTES: (state, routes) => {
state.topbarRouters = routes
},
SET_SIDEBAR_ROUTERS: (state, routes) => {
state.sidebarRouters = constantRoutes.concat(routes);
},
SET_INDEX_PAGE:(state,indexPage)=>{
state.indexPage = indexPage;
}
},
actions: {
// 生成路由
GenerateRoutes({ commit }) {
return new Promise(resolve => {
// 向后端请求路由数据
getRouters().then(res => {
const sdata = JSON.parse(JSON.stringify(res.data))
const rdata = JSON.parse(JSON.stringify(res.data))
//修改默认首页
const indexdata = res.data[0].path+'/'+res.data[0].children[0].path
const sidebarRoutes = filterAsyncRouter(sdata)
const rewriteRoutes = filterAsyncRouter(rdata, false, true)
const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
router.addRoutes(asyncRoutes);
commit('SET_ROUTES', rewriteRoutes)
commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
commit('SET_DEFAULT_ROUTES', sidebarRoutes)
commit('SET_TOPBAR_ROUTES', sidebarRoutes)
//修改默认首页
commit('SET_INDEX_PAGE',indexdata)
resolve(rewriteRoutes)
})
})
}
}
}
- ruoyi-ui/src/router/index.js注释代码
{
path: '',
component: Layout,
redirect: 'index',
children: [
{
path: 'index',
component: () => import('@/views/index'),
name: 'Index',
meta: { title: '首页', icon: 'dashboard', affix: true }
}
]
},
- ruoyi/src/utils/request.js 和 ruoyi/src/layout/components/Navbar.vue 修改一处代码
store.dispatch('LogOut').then(() => {
location.href = '/login';
})
5.ruoyi-ui/src/views/login.vue修改代码
// this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
this.$store.dispatch('GetInfo').then(res => {
const roles = res.roles
this.$store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
var heardPath=accessRoutes[0].path.split("/")[1]==''?"":accessRoutes[0].path;
this.$router.addRoutes(accessRoutes)
let pathIndex = ''
pathIndex = heardPath + '/' + accessRoutes[0].children[0].path
if (pathIndex != '') {
this.$router.push({ path: pathIndex }).catch(() => {})
}
})
})
- ruoyi-ui/src/components/Breadcrumb/index.vue注释代码
if (!this.isDashboard(first)) {
matched = [{ path: '/index', meta: { title: '首页' }}].concat(matched)
}
7.点击左侧菜单栏上的系统名称后跳转404页面
需要修改ruoyi-ui/src/router/index.js 中 component: () => import(‘@/views/error/404’),
{
path: '/404',
//component: () => import('@/views/error/404'),
component: () => import('@/views/system/user/profile/index'),
hidden: true
},