Commit d888a81a6c7334c3b4beab7d440e3f0ba72a5ff6
1 parent
9287779a
提交权限控制
Showing
4 changed files
with
65 additions
and
55 deletions
src/permission.js
| 1 | 1 | import router from './router' |
| 2 | 2 | import store from './store' |
| 3 | -import { Message } from 'element-ui' | |
| 3 | +import {Message} from 'element-ui' | |
| 4 | 4 | import NProgress from 'nprogress' // progress bar |
| 5 | 5 | import 'nprogress/nprogress.css' // progress bar style |
| 6 | -import { getToken } from '@/utils/auth' // get token from cookie | |
| 6 | +import {getToken} from '@/utils/auth' // get token from cookie | |
| 7 | 7 | import getPageTitle from '@/utils/get-page-title' |
| 8 | 8 | |
| 9 | -NProgress.configure({ showSpinner: false }) // NProgress Configuration | |
| 9 | +NProgress.configure({showSpinner: false}) // NProgress Configuration | |
| 10 | 10 | |
| 11 | 11 | const whiteList = ['/login'] // no redirect whitelist |
| 12 | 12 | |
| 13 | -router.beforeEach(async(to, from, next) => { | |
| 13 | +router.beforeEach(async (to, from, next) => { | |
| 14 | + debugger | |
| 14 | 15 | // start progress bar |
| 15 | 16 | NProgress.start() |
| 17 | + if (to.path.indexOf("service") == -1) { | |
| 18 | + next() | |
| 19 | + NProgress.done() | |
| 20 | + } else { | |
| 21 | + // set page title | |
| 22 | + document.title = getPageTitle(to.meta.title) | |
| 16 | 23 | |
| 17 | - // set page title | |
| 18 | - document.title = getPageTitle(to.meta.title) | |
| 19 | - | |
| 20 | - // determine whether the user has logged in | |
| 21 | - const hasToken = getToken() | |
| 24 | + // determine whether the user has logged in | |
| 25 | + const hasToken = getToken() | |
| 22 | 26 | |
| 23 | - if (hasToken) { | |
| 24 | - if (to.path === '/login') { | |
| 25 | - // if is logged in, redirect to the home page | |
| 26 | - next({ path: '/' }) | |
| 27 | - NProgress.done() | |
| 28 | - } else { | |
| 29 | - const hasGetUserInfo = store.getters.name | |
| 30 | - if (hasGetUserInfo) { | |
| 31 | - next() | |
| 27 | + if (hasToken) { | |
| 28 | + if (to.path === '/login') { | |
| 29 | + // if is logged in, redirect to the home page | |
| 30 | + next({path: '/'}) | |
| 31 | + NProgress.done() | |
| 32 | 32 | } else { |
| 33 | - try { | |
| 34 | - // get user info | |
| 35 | - await store.dispatch('user/getInfo') | |
| 36 | - | |
| 33 | + const hasGetUserInfo = store.getters.name | |
| 34 | + if (hasGetUserInfo) { | |
| 37 | 35 | next() |
| 38 | - } catch (error) { | |
| 39 | - // remove token and go to login page to re-login | |
| 40 | - await store.dispatch('user/resetToken') | |
| 41 | - Message.error(error || 'Has Error') | |
| 42 | - next(`/login?redirect=${to.path}`) | |
| 43 | - NProgress.done() | |
| 36 | + } else { | |
| 37 | + try { | |
| 38 | + // get user info | |
| 39 | + await store.dispatch('user/getInfo') | |
| 40 | + | |
| 41 | + next() | |
| 42 | + } catch (error) { | |
| 43 | + // remove token and go to login page to re-login | |
| 44 | + await store.dispatch('user/resetToken') | |
| 45 | + Message.error(error || 'Has Error') | |
| 46 | + next(`/login?redirect=${to.path}`) | |
| 47 | + NProgress.done() | |
| 48 | + } | |
| 44 | 49 | } |
| 45 | 50 | } |
| 46 | - } | |
| 47 | - } else { | |
| 48 | - /* has no token*/ | |
| 49 | - | |
| 50 | - if (whiteList.indexOf(to.path) !== -1) { | |
| 51 | - // in the free login whitelist, go directly | |
| 52 | - next() | |
| 53 | 51 | } else { |
| 54 | - // other pages that do not have permission to access are redirected to the login page. | |
| 55 | - next(`/login?redirect=${to.path}`) | |
| 56 | - NProgress.done() | |
| 52 | + /* has no token*/ | |
| 53 | + | |
| 54 | + if (whiteList.indexOf(to.path) !== -1) { | |
| 55 | + // in the free login whitelist, go directly | |
| 56 | + next() | |
| 57 | + } else { | |
| 58 | + // other pages that do not have permission to access are redirected to the login page. | |
| 59 | + next(`/login?redirect=${to.path}`) | |
| 60 | + NProgress.done() | |
| 61 | + } | |
| 57 | 62 | } |
| 58 | 63 | } |
| 59 | 64 | }) | ... | ... |
src/router/index.js
| ... | ... | @@ -55,7 +55,7 @@ export const constantRoutes = [ |
| 55 | 55 | }, |
| 56 | 56 | { |
| 57 | 57 | path: '/service', |
| 58 | - redirect:'/dashboard', | |
| 58 | + redirect:'/service/dashboard', | |
| 59 | 59 | component: () => import('@/views/service'), |
| 60 | 60 | meta: { |
| 61 | 61 | title: '停车服务' |
| ... | ... | @@ -63,47 +63,47 @@ export const constantRoutes = [ |
| 63 | 63 | }, |
| 64 | 64 | children:[ |
| 65 | 65 | { |
| 66 | - path: '/dashboard', | |
| 66 | + path: '/service/dashboard', | |
| 67 | 67 | component: () => import('@/views/dashboard/index'), |
| 68 | 68 | meta: { title: '服务首页', icon: 'dashboard' } |
| 69 | 69 | }, |
| 70 | 70 | |
| 71 | 71 | { |
| 72 | - path: '/account', | |
| 72 | + path: '/service/account', | |
| 73 | 73 | component: () => import('@/views/account/index'), |
| 74 | 74 | meta: { title: '我的账户',icon: 'money' } |
| 75 | 75 | |
| 76 | 76 | }, |
| 77 | 77 | { |
| 78 | - path: '/cardticket', | |
| 78 | + path: '/service/cardticket', | |
| 79 | 79 | component: () => import('@/views/cardticket/index'), |
| 80 | 80 | meta: { title: '我的卡券', icon: 'card' } |
| 81 | 81 | }, |
| 82 | 82 | |
| 83 | 83 | { |
| 84 | - path: '/order', | |
| 84 | + path: '/service/order', | |
| 85 | 85 | component: () => import('@/views/order/index'), |
| 86 | 86 | meta: { title: '我的订单', icon: 'form' } |
| 87 | 87 | }, |
| 88 | 88 | { |
| 89 | - path: '/payback', | |
| 89 | + path: '/service/payback', | |
| 90 | 90 | component: () => import('@/views/payback/index'), |
| 91 | 91 | meta: { title: '欠费补缴', icon: 'nested' } |
| 92 | 92 | }, |
| 93 | 93 | |
| 94 | 94 | { |
| 95 | - path: '/mycar', | |
| 95 | + path: '/service/mycar', | |
| 96 | 96 | component: () => import('@/views/mycar/index'), |
| 97 | 97 | meta: { title: '我的车辆', icon: 'car' } |
| 98 | 98 | |
| 99 | 99 | }, |
| 100 | 100 | { |
| 101 | - path: '/information', | |
| 101 | + path: '/service/information', | |
| 102 | 102 | component: () => import('@/views/information/index'), |
| 103 | 103 | meta: { title: '个人资料' ,icon: 'people'} |
| 104 | 104 | }, |
| 105 | 105 | { |
| 106 | - path: '/myevaluate', | |
| 106 | + path: '/service/myevaluate', | |
| 107 | 107 | component: () => import('@/views/myevaluate/index'), |
| 108 | 108 | meta: { title: '我的评价',icon: 'skill' } |
| 109 | 109 | } | ... | ... |
src/views/login/index.vue
src/views/service.vue
| ... | ... | @@ -12,7 +12,7 @@ |
| 12 | 12 | background-color="#545c64" |
| 13 | 13 | text-color="#fff" |
| 14 | 14 | active-text-color="#409eff" router> |
| 15 | - <el-menu-item index="/dashboard" class="is-active"> | |
| 15 | + <el-menu-item index="/service/dashboard" class="is-active"> | |
| 16 | 16 | <i class="el-icon-menu "></i> |
| 17 | 17 | <span slot="title">服务首页</span> |
| 18 | 18 | </el-menu-item> |
| ... | ... | @@ -21,18 +21,18 @@ |
| 21 | 21 | <i class="el-icon-bank-card"></i> |
| 22 | 22 | <span>我的钱包</span> |
| 23 | 23 | </template> |
| 24 | - <el-menu-item index="/account">我的账户</el-menu-item> | |
| 25 | - <el-menu-item index="/cardticket">我的卡券</el-menu-item> | |
| 24 | + <el-menu-item index="/service/account">我的账户</el-menu-item> | |
| 25 | + <el-menu-item index="/service/cardticket">我的卡券</el-menu-item> | |
| 26 | 26 | </el-submenu> |
| 27 | - <el-menu-item index="/order"> | |
| 27 | + <el-menu-item index="/service/order"> | |
| 28 | 28 | <i class="el-icon-tickets"></i> |
| 29 | 29 | <span slot="title">我的订单</span> |
| 30 | 30 | </el-menu-item> |
| 31 | - <el-menu-item index="/payback"> | |
| 31 | + <el-menu-item index="/service/payback"> | |
| 32 | 32 | <i class="el-icon-finished"></i> |
| 33 | 33 | <span slot="title">欠费补缴</span> |
| 34 | 34 | </el-menu-item> |
| 35 | - <el-menu-item index="/mycar"> | |
| 35 | + <el-menu-item index="/service/mycar"> | |
| 36 | 36 | <i class="el-icon-truck"></i> |
| 37 | 37 | <span slot="title">我的车辆</span> |
| 38 | 38 | </el-menu-item> |
| ... | ... | @@ -41,8 +41,8 @@ |
| 41 | 41 | <i class="el-icon-user-solid"></i> |
| 42 | 42 | <span>个人中心</span> |
| 43 | 43 | </template> |
| 44 | - <el-menu-item index="/information">个人资料</el-menu-item> | |
| 45 | - <el-menu-item index="/myevaluate">我的评价</el-menu-item> | |
| 44 | + <el-menu-item index="/service/information">个人资料</el-menu-item> | |
| 45 | + <el-menu-item index="/service/myevaluate">我的评价</el-menu-item> | |
| 46 | 46 | </el-submenu> |
| 47 | 47 | </el-menu> |
| 48 | 48 | </el-aside> | ... | ... |