Commit 67b1f906 by xinla

设置首页重定向

1 parent 63e6d7f1
import router from './router';
import store from './store';
import { Message } from 'element-ui';
import NProgress from 'nprogress';
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth';
import { getToken } from '@/utils/auth'
// NProgress.configure({ showSpinner: false })
const whiteList = ['/login', '/authCallback', '/bind', '/register', '/test'];
const whiteList = ['/login', '/authCallback', '/bind', '/register', '/test']
router.beforeEach((to, from, next) => {
// NProgress.start()
if (getToken()) {
/* has token*/
if (to.path === '/login') {
next({ path: '/' });
next({ path: '/' })
// NProgress.done()
} else {
if (store.getters.roles.length === 0) {
......@@ -23,40 +23,39 @@ router.beforeEach((to, from, next) => {
.dispatch('GetInfo')
.then((res) => {
// 拉取user_info
const roles = res.roles;
const roles = res.roles
store.dispatch('GenerateRoutes', { roles }).then((accessRoutes) => {
// 测试 默认静态页面
// store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
// 根据roles权限生成可访问的路由表
router.addRoutes(accessRoutes); // 动态添加可访问路由表
// 判断是否有设置企业微信id
if (res.user && res.user.validCropId) {
// 设置了企业微信,正常跳转
next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
} else {
// 没有设置企业微信的
let isEnterpriseWechat = accessRoutes.findIndex((element) => {
element.path === '/_enterpriseWechat';
});
element.path === '/_enterpriseWechat'
})
// 若有设置企业微信的页面权限,默认跳转到设置企业微信页面
if (isEnterpriseWechat > -1) {
next({ path: '/enterpriseWechat', replace: true });
next({ path: '/enterpriseWechat', replace: true })
} else {
// 没有权限的给予提示,并退回登录页面
Message.error('没有设置企业微信的权限,请联系系统管理员');
next({ path: '/login', replace: true });
Message.error('没有设置企业微信的权限,请联系系统管理员')
next({ path: '/login', replace: true })
}
}
});
})
})
.catch((err) => {
store.dispatch('FedLogOut').then(() => {
Message.error(err);
next({ path: '/' });
});
});
Message.error(err)
next({ path: '/' })
})
})
} else {
next();
next()
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
// if (hasPermission(store.getters.roles, to.meta.roles)) {
// next()
......@@ -70,14 +69,14 @@ router.beforeEach((to, from, next) => {
// 没有token
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next();
next()
} else {
next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
// NProgress.done()
}
}
});
})
router.afterEach(() => {
// NProgress.done()
});
})
......@@ -26,6 +26,27 @@ import Layout from '@/layout'
// 公共路由
export const constantRoutes = [
// {
// path: '/index',
// hidden: true,
// name: '运营中心',
// meta: { title: '运营中心' }
// children: [
// {
// path: '/index',
// component: (resolve) => require(['@/views/index'], resolve),
// name: '运营中心',
// hidden: true,
// meta: {
// title: '运营中心',
// icon: 'dashboard',
// breadcrumb: false,
// noCache: true,
// affix: true
// }
// }
// ]
// },
{
path: '/redirect',
component: Layout,
......@@ -43,28 +64,6 @@ export const constantRoutes = [
hidden: true
},
{
path: '/index',
redirect: '/operateCenter/index/dataAnalysis',
hidden: true
// name: '运营中心',
// meta: { title: '运营中心' }
// children: [
// {
// path: '/index',
// component: (resolve) => require(['@/views/index'], resolve),
// name: '运营中心',
// hidden: true,
// meta: {
// title: '运营中心',
// icon: 'dashboard',
// breadcrumb: false,
// noCache: true,
// affix: true
// }
// }
// ]
},
{
path: '/login',
component: (resolve) => require(['@/views/login'], resolve),
hidden: true
......
import { constantRoutes } from '@/router'
import router, { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
import View from '@/layout/components/View'
......@@ -12,6 +12,8 @@ const permission = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
// 根据roles权限生成可访问的路由表
router.addRoutes(routes)
}
},
actions: {
......@@ -21,6 +23,9 @@ const permission = {
// 向后端请求路由数据
getRouters().then((res) => {
const accessedRoutes = filterAsyncRouter(res.data)
// 设置首页重定向
accessedRoutes.unshift({ path: '/index', redirect: accessedRoutes[0].path, hidden: true })
// 设置为匹配路由重定向404
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!