This commit is contained in:
2021-10-20 16:25:18 +08:00
parent a62e9a9dce
commit c951ee56f5
686 changed files with 75106 additions and 5870 deletions

View File

@@ -1,15 +1,25 @@
/*
* @Date: 2020-12-14 09:07:03
* @LastEditors: gtz
* @LastEditTime: 2021-01-29 14:24:20
* @FilePath: \basic-admin\src\store\getters.js
* @Description:
*/
const getters = {
sidebar: state => state.app.sidebar,
language: state => state.app.language,
size: state => state.app.size,
device: state => state.app.device,
choicepart: state => state.app.choicepart,
visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews,
token: state => state.user.token,
avatar: state => state.user.avatar,
name: state => state.user.name,
introduction: state => state.user.introduction,
username: state => state.user.username,
roles: state => state.user.roles,
permission_routes: state => state.permission.routes,
errorLogs: state => state.errorLog.logs
menus: state => state.user.menus,
permission_routes: state => state.permission.routes
}
export default getters

View File

@@ -1,4 +1,13 @@
/*
* @Author: your name
* @Date: 2021-01-27 10:07:42
* @LastEditTime: 2021-01-27 10:53:02
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \mt-bus-fe\src\store\modules\app.js
*/
import Cookies from 'js-cookie'
import { getLanguage } from '@/lang/index'
const state = {
sidebar: {
@@ -6,7 +15,9 @@ const state = {
withoutAnimation: false
},
device: 'desktop',
size: Cookies.get('size') || 'medium'
language: getLanguage(),
size: Cookies.get('size') || 'medium',
choicepart: Cookies.get('choicepart') || null
}
const mutations = {
@@ -27,9 +38,17 @@ const mutations = {
TOGGLE_DEVICE: (state, device) => {
state.device = device
},
SET_LANGUAGE: (state, language) => {
state.language = language
Cookies.set('language', language)
},
SET_SIZE: (state, size) => {
state.size = size
Cookies.set('size', size)
},
SET_CHOICEPART: (state, choicepart) => {
state.choicepart = choicepart
Cookies.set('choicepart', choicepart)
}
}
@@ -43,8 +62,14 @@ const actions = {
toggleDevice({ commit }, device) {
commit('TOGGLE_DEVICE', device)
},
setLanguage({ commit }, language) {
commit('SET_LANGUAGE', language)
},
setSize({ commit }, size) {
commit('SET_SIZE', size)
},
setChoicepart({ commit }, choicepart) {
commit('SET_CHOICEPART', choicepart)
}
}

View File

@@ -1,31 +1,36 @@
import { asyncRoutes, constantRoutes } from '@/router'
import { dynamicRoutes, constantRoutes } from '@/router'
import { getListItems } from '@/utils/tree'
/**
* Use meta.role to determine if the current user has permission
* @param roles
* @param route
*/
function hasPermission(roles, route) {
if (route.meta && route.meta.roles) {
return roles.some(role => route.meta.roles.includes(role))
} else {
function hasPermission(menuList, route) {
// if (route.meta && route.meta.roles) {
// return roles.some(role => route.meta.roles.includes(role))
// } else {
// return true
// }
if (route.hidden) {
return true
}
return menuList.some(m => route.path === m.h)
}
/**
* Filter asynchronous routing tables by recursion
* @param routes asyncRoutes
* @param roles
* @param menuList
*/
export function filterAsyncRoutes(routes, roles) {
export function filterAsyncRoutes(routes, menuList) {
const res = []
routes.forEach(route => {
const tmp = { ...route }
if (hasPermission(roles, tmp)) {
if (hasPermission(menuList, tmp)) {
if (tmp.children) {
tmp.children = filterAsyncRoutes(tmp.children, roles)
tmp.children = filterAsyncRoutes(tmp.children, menuList)
}
res.push(tmp)
}
@@ -47,13 +52,19 @@ const mutations = {
}
const actions = {
generateRoutes({ commit }, roles) {
generateRoutes({ commit }, menus) {
return new Promise(resolve => {
let accessedRoutes
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes || []
console.log(menus)
if (!menus) {
accessedRoutes = []
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
const menuList = []
getListItems(menus, node => menuList.push(node))
console.log(menuList)
console.log(dynamicRoutes)
accessedRoutes = filterAsyncRoutes(dynamicRoutes, menuList)
console.log(accessedRoutes)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)

View File

@@ -13,7 +13,6 @@ const state = {
const mutations = {
CHANGE_SETTING: (state, { key, value }) => {
// eslint-disable-next-line no-prototype-builtins
if (state.hasOwnProperty(key)) {
state[key] = value
}

View File

@@ -1,13 +1,16 @@
import { login, logout, getInfo } from '@/api/user'
import { login, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import Cookies from 'js-cookie'
const state = {
token: getToken(),
name: '',
avatar: '',
introduction: '',
roles: []
roles: [],
menus: [],
username: Cookies.get('username') || ''
}
const mutations = {
@@ -23,8 +26,14 @@ const mutations = {
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_MENUS: (state, menus) => {
state.menus = menus
},
SET_ROLES: (state, roles) => {
state.roles = roles
},
SET_USERNAME: (state, username) => {
state.username = username
}
}
@@ -33,11 +42,18 @@ const actions = {
login({ commit }, userInfo) {
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password }).then(response => {
const { data } = response
commit('SET_TOKEN', data.token)
setToken(data.token)
resolve()
login({ mobile: username.trim(), password: password }).then(res => {
console.log(res)
if (res.code === 0) {
const { token, name } = res.data
commit('SET_TOKEN', token)
commit('SET_USERNAME', name)
Cookies.set('username', name)
setToken(token)
resolve()
} else {
reject(res)
}
}).catch(error => {
reject(error)
})
@@ -47,23 +63,24 @@ const actions = {
// get user info
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
getUserInfo(state.token).then(response => {
const { data } = response
if (!data) {
reject('Verification failed, please Login again.')
}
const { roles, name, avatar, introduction } = data
const { roles, name, avatar, introduction, menus } = data
// roles must be a non-empty array
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!')
if (!menus || menus.length <= 0) {
reject('getInfo: menus must be a non-null array!')
}
commit('SET_ROLES', roles)
commit('SET_ROLES', roles || [])
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_MENUS', menus)
commit('SET_INTRODUCTION', introduction)
resolve(data)
}).catch(error => {
@@ -75,20 +92,30 @@ const actions = {
// user logout
logout({ commit, state, dispatch }) {
return new Promise((resolve, reject) => {
logout(state.token).then(() => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
resetRouter()
removeToken()
resetRouter()
dispatch('tagsView/delAllViews', null, { root: true })
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
commit('SET_MENUS', [])
commit('SET_USERNAME')
Cookies.set('username', '')
localStorage.clear()
resolve()
// logout(state.token).then(() => {
// commit('SET_TOKEN', '')
// commit('SET_ROLES', [])
// removeToken()
// resetRouter()
// reset visited views and cached views
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
dispatch('tagsView/delAllViews', null, { root: true })
// // reset visited views and cached views
// // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
// dispatch('tagsView/delAllViews', null, { root: true })
resolve()
}).catch(error => {
reject(error)
})
// resolve()
// }).catch(error => {
// reject(error)
// })
})
},
@@ -103,23 +130,28 @@ const actions = {
},
// dynamically modify permissions
async changeRoles({ commit, dispatch }, role) {
const token = role + '-token'
changeRoles({ commit, dispatch }, role) {
return new Promise(async resolve => {
const token = role + '-token'
commit('SET_TOKEN', token)
setToken(token)
commit('SET_TOKEN', token)
setToken(token)
const { roles } = await dispatch('getInfo')
const { roles } = await dispatch('getInfo')
resetRouter()
resetRouter()
// generate accessible routes map based on roles
const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
// dynamically add accessible routes
router.addRoutes(accessRoutes)
// generate accessible routes map based on roles
const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
// reset visited views and cached views
dispatch('tagsView/delAllViews', null, { root: true })
// dynamically add accessible routes
router.addRoutes(accessRoutes)
// reset visited views and cached views
dispatch('tagsView/delAllViews', null, { root: true })
resolve()
})
}
}