Skip to content

Commit

Permalink
权限管理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljhhhhhh committed Oct 16, 2019
1 parent d11261a commit 68e52f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let routes = [
name: 'home',
component: Home,
meta: {
title: '首页',
keepAlive: true
}
}
Expand Down
37 changes: 14 additions & 23 deletions src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import { login, getInfo } from 'api/user'
import { Toast } from 'vant'
import { getToken, setToken } from '@/utils/auth'
import { getToken, setToken, removeToken } from '@/utils/auth'

const LOGIN = 'LOGIN'// 获取用户信息
const SetUserData = 'SetUserData'// 获取用户信息
const LOGOUT = 'LOGOUT'// 退出登录、清除用户数据
const USER_DATA = 'userDate'// 用户数据

export default {
namespaced: true,
state: {
token: getToken() || '',
user: JSON.parse(localStorage.getItem('userDate')) || {}
user: JSON.parse(localStorage.getItem(USER_DATA) || null)
},
mutations: {

[LOGIN] (state, data) {
let userDate = data.data
state.token = userDate.token
// state.user = userDate
setToken(userDate.token)
// localStorage.setItem('token', userDate.token)
// localStorage.setItem('userDate', JSON.stringify(userDate))
},
[SetUserData] (state, data) {
let userDate = data.data
state.user = userDate
localStorage.setItem('userDate', JSON.stringify(userDate))

[SetUserData] (state, userData = {}) {
state.user = userData
localStorage.setItem(USER_DATA, JSON.stringify(userData))
},
[LOGOUT] (state) {
state.user = null
state.token = null
removeToken()
localStorage.removeItem(USER_DATA)
}

},
Expand Down Expand Up @@ -60,19 +65,6 @@ export default {
// eslint-disable-next-line
reject('Verification failed, please Login again.')
}

// const { roles, name, avatar, introduction } = data

// // roles must be a non-empty array
// if (!roles || roles.length <= 0) {
// // eslint-disable-next-line
// reject('getInfo: roles must be a non-null array!')
// }

// commit('SET_ROLES', roles)
// commit('SET_NAME', name)
// commit('SET_AVATAR', avatar)
// commit('SET_INTRODUCTION', introduction)
commit(SetUserData, data)
resolve(data)
}).catch(error => {
Expand All @@ -86,7 +78,6 @@ export default {
return state.token
},
user (state) {
console.log('state', state)
return state.user
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/utils/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ router.beforeEach(async (to, from, next) => {
next({ path: '/' })
// NProgress.done()
} else {
const hasGetUserInfo = store.getters.userData.name
console.log(hasGetUserInfo)
const hasGetUserInfo = store.getters.userData && store.getters.userData.name
console.log('hasGetUserInfo', hasGetUserInfo)
if (hasGetUserInfo) {
next()
} else {
Expand All @@ -38,7 +38,8 @@ router.beforeEach(async (to, from, next) => {
next()
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
// await store.dispatch('user/resetToken')
store.commit('user/LOGOUT')
Notify.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
// NProgress.done()
Expand Down

0 comments on commit 68e52f9

Please sign in to comment.