Commit 67b1f906 by xinla

设置首页重定向

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