Skip to content

Commit

Permalink
统一用户getinfo的时候返回路由表,过滤路由表
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed Sep 29, 2018
1 parent 18f2fac commit 7ef75a1
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 111 deletions.
19 changes: 7 additions & 12 deletions src/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ router.beforeEach((to, from, next) => {
next({
path: '/'
})
} else {
if (store.getters.roles.length === 0) {
} else {
if (store.getters.addRouters.length === 0) {
store.dispatch('GetInfo').then(res => {
console.log(res.data);
const roles = res.data.data.Roles.split(',')
store.dispatch('GenerateRoutes', {
roles
}).then(() => {
router.addRoutes(store.getters.addRouters)
next({ ...to
})
})

router.addRoutes(store.getters.addRouters)
next({ ...to})
})
} else {
}
else {
next()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default new Router({
routes: constantRouterMap
})

export const asyncRouterMap = [
const asyncRouterMap = [


{
Expand Down
6 changes: 2 additions & 4 deletions src/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const getters = {
cachedViews: state => state.tagsView.cachedViews,
realname:state=>state.user.realname,
name: state => state.user.name,
roles: state => state.user.roles,
permission_routers: state => state.permission.routers,
addRouters: state => state.permission.addRouters,
AllRouters:state=>state.permission.AllRouters
permission_routers: state => state.user.routers,
addRouters: state => state.user.addRouters
}
export default getters
4 changes: 1 addition & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import user from './modules/user'
import permission from './modules/permission'
import tagsView from './modules/tagsView'

import getters from './getters'
Expand All @@ -13,8 +12,7 @@ const store = new Vuex.Store({
modules: {
app,
user,
tagsView,
permission
tagsView
},
getters
})
Expand Down
68 changes: 0 additions & 68 deletions src/store/modules/permission.js

This file was deleted.

70 changes: 58 additions & 12 deletions src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,60 @@ import {
import {
ChangePassword
} from '@/api/system/users'
import { constantRouterMap } from '@/router/index'

import 'nprogress/nprogress.css' // Progress 进度条样式
import Layout from '@/views/layout/Layout'

const _import = require('@/router/_import_' + process.env.NODE_ENV)

function filterAsyncRouter(asyncRouterMap) { //遍历后台传来的路由字符串,转换为组件对象
const accessedRouters = asyncRouterMap.filter(route => {
if (route.component) {
if (route.component === 'Layout') {//Layout组件特殊处理
route.component = Layout
} else {
route.component = _import(route.component)
}
}
if (route.children && route.children.length) {
route.children = filterAsyncRouter(route.children)
}
return true
})

return accessedRouters
}



const user = {
state: {
token: getToken(),
name: '', //用户昵称名
realname:'',//用户登录名
roles: []
realname: '', //用户登录名
routers: constantRouterMap,
addRouters: []

},

mutations: {

SET_ROUTERS: (state, routers) => {
state.addRouters = routers.push({path:"*",redirect:'/404',hidden:true})
state.routers = constantRouterMap.concat(routers)
},



SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
},

SET_ROLES: (state, roles) => {
state.roles = roles
},

SET_REALNAME: (state, realname) => {
state.realname = realname
}
Expand All @@ -47,8 +82,8 @@ const user = {
if (data.code == 200) {
console.log(data.data);
setToken(data.data)


commit('SET_TOKEN', data.data)
resolve(data)

Expand All @@ -75,28 +110,39 @@ const user = {

const data = response.data.data
console.log(data);
commit('SET_ROLES', data.Roles)
commit('SET_NAME', data.RealName)
commit('SET_REALNAME', data.UserName)


commit('SET_ROUTERS', filterAsyncRouter(data.Routers))


resolve(response)

resolve(data)
}).catch(error => {
reject(error)
})
})
},


// 前端 登出
// 登出
FedLogOut({
commit
}) {
return new Promise(resolve => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
logout().then(res => {
if (res.data.code === 200) {
removeToken()
resolve()
} else
reject()
}).catch(err => {
reject(err)
})

removeToken()
resolve()
})
},

Expand Down
16 changes: 8 additions & 8 deletions src/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from 'element-ui'
import store from '../store'
import {
getToken
getToken,setToken
} from '@/utils/auth'

// 创建axios实例
Expand Down Expand Up @@ -33,21 +33,21 @@ service.interceptors.request.use(config => {
service.interceptors.response.use(
response => {
const res = response.data
// 400:帐号信息与token不匹配 需要重新拉取token
if (res.code === 401) {
// 401 403 :帐号信息与token不匹配 需要重新拉取token
if (res.code === 401||res.code === 403) {
MessageBox.alert('帐号信息发生变化,请重新登录', {
confirmButtonText: '重新登录',
showCancelButton: false,
type: 'warning'
}).then(() => {
store.dispatch('FedLogOut').then(() => {
setToken("")
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
return Promise.reject(error)

} else {
if (res.code !== 200) { //增删改操作的状态返回
}
else {
if (res.code !== 200) { //后台报错信息显示

Message({
message: res.message,
Expand All @@ -58,7 +58,7 @@ service.interceptors.response.use(

}

else if(res.data==null)
if(res.message!==""&&res.message!=null&&res.message!=="SUCCESS") //统一显示后台返回结果

{
Message({
Expand Down
5 changes: 2 additions & 3 deletions src/views/system/role.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ import {
UpdateRole
} from "@/api/system/role";
import { mapGetters } from "vuex";
import { asyncRouterMap } from "@/router/index";
import store from '@/store'
export default {
data() {
return {
Expand Down Expand Up @@ -111,7 +110,7 @@ export default {
created() {
this.fetchData(this.listQuery);
let arr = asyncRouterMap;
let arr = store.getters.AddRouters;
for (let i = 0; i < arr.length; i++) {
if (arr[i].hidden) {
Expand Down

0 comments on commit 7ef75a1

Please sign in to comment.