Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Dec 25, 2017
1 parent 96e49e7 commit 6a1d22b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Mock.setup({
// 登录相关
// Mock.mock(/\/login\/login/, 'post', loginAPI.loginByUsername)
Mock.mock(/\/login\/logout/, 'post', loginAPI.logout)
Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getUserInfo)
// Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getUserInfo)

// 文章相关
Mock.mock(/\/article\/list/, 'get', articleAPI.getList)
Expand Down
63 changes: 36 additions & 27 deletions src/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,45 @@ const whiteList = ['/login', '/authredirect']// 不重定向白名单
router.beforeEach((to, from, next) => {
NProgress.start() // 开启Progress
if (getToken()) { // 判断是否有token
if (to.path === '/login') {
next({ path: '/' })
} else {
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
store.dispatch('GetUserInfo').then(res => { // 拉取user_info
const roles = res.data.role
store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
next({ ...to }) // hack方法 确保addRoutes已完成
})
}).catch(() => {
store.dispatch('FedLogOut').then(() => {
next({ path: '/login' })
})
})
} else {
// 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
if (hasPermission(store.getters.roles, to.meta.role)) {
next()//
} else {
next({ path: '/401', query: { noGoBack: true }})
}
// 可删 ↑
}
}
} else {


// if (to.path === '/login') {
// next({ path: '/' })
// }

// else if (from.path ==='/login'){

// next({ path: '/' })

// }
// else {
// if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
// store.dispatch('GetUserInfo').then(res => { // 拉取user_info
// const roles = res.data.role
// store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
// router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
// next({ ...to }) // hack方法 确保addRoutes已完成
// })
// }).catch(() => {
// store.dispatch('FedLogOut').then(() => {
// next({ path: '/login' })
// })
// })
// }
// }
next({ path: '/' })

}
else {
console.log(3);

if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
console.log(to.path)
next()
} else {
}
else {
next('/login') // 否则全部重定向到登录页
console.log(2);
NProgress.done() // 在hash模式下 改变手动改变hash 重定向回来 不会触发afterEach 暂时hack方案 ps:history模式下无问题,可删除该行!
}
}
Expand Down
15 changes: 1 addition & 14 deletions src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const user = {
if (data.Success == true) {

console.log(response.data.Token);
// setToken(response.data.Token)
setToken(response.data.Token)
commit('SET_TOKEN', data.Token)

resolve()
Expand Down Expand Up @@ -100,19 +100,6 @@ const user = {
})
},

// 第三方验证登录
// LoginByThirdparty({ commit, state }, code) {
// return new Promise((resolve, reject) => {
// commit('SET_CODE', code)
// loginByThirdparty(state.status, state.email, state.code).then(response => {
// commit('SET_TOKEN', response.data.token)
// setToken(response.data.token)
// resolve()
// }).catch(error => {
// reject(error)
// })
// })
// },

// 登出
LogOut({
Expand Down

0 comments on commit 6a1d22b

Please sign in to comment.