安虹睿
1 year ago
12 changed files with 439 additions and 100 deletions
@ -0,0 +1,140 @@ |
|||||
|
// 第一版token路由
|
||||
|
import router from './router' |
||||
|
import store from './store' |
||||
|
import { Message } from 'element-ui' |
||||
|
import NProgress from 'nprogress' // progress bar
|
||||
|
import 'nprogress/nprogress.css' // progress bar style
|
||||
|
import { getToken } from '@/utils/auth' // get token from cookie
|
||||
|
import getPageTitle from '@/utils/get-page-title' |
||||
|
|
||||
|
NProgress.configure({ |
||||
|
showSpinner: false |
||||
|
}) // NProgress Configuration
|
||||
|
|
||||
|
const whiteList = ['/login', '/auth-redirect', '/testForm'] // no redirect whitelist
|
||||
|
|
||||
|
// 跳转登录页
|
||||
|
const pageToLogin = (to, from, next) => { |
||||
|
if (whiteList.indexOf(to.path) !== -1) { |
||||
|
// in the free login whitelist, go directly
|
||||
|
next() |
||||
|
NProgress.done() |
||||
|
} else { |
||||
|
// other pages that do not have permission to access are redirected to the login page.
|
||||
|
next(`/login?redirect=${to.path}`) |
||||
|
NProgress.done() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 首次进入页面加载数据处理
|
||||
|
const toPageInit = async (to, from, next) => { |
||||
|
let isFresh = false |
||||
|
|
||||
|
// 获取表头转义
|
||||
|
if(!store.getters.columZHList){ |
||||
|
await store.dispatch('definition/getColumZHList') |
||||
|
} |
||||
|
|
||||
|
// 获取菜单数据
|
||||
|
if(!store.getters.permission_routes || store.getters.permission_routes.length <= 0){ |
||||
|
isFresh = true |
||||
|
await store.dispatch('permission/getApiColumnsNames') |
||||
|
} |
||||
|
|
||||
|
// 获取枚举数据
|
||||
|
if(!store.getters.enumList || store.getters.enumList.length <= 0){ |
||||
|
await store.dispatch('definition/getDefinitionConfig') |
||||
|
} |
||||
|
|
||||
|
// 获取用户信息
|
||||
|
let _username = to.query.loginName || localStorage.getItem("isLoginName") || localStorage.getItem("loginName") |
||||
|
if(!store.getters.currentUserInfo || store.getters.currentUserInfo.length <= 0){ |
||||
|
await store.dispatch('user/getUserInfo',_username) |
||||
|
.then(()=>{ |
||||
|
toNextHandle(to, from, next,isFresh) |
||||
|
}) |
||||
|
.catch(()=>{ |
||||
|
toNextHandle(to, from, next,isFresh) |
||||
|
}) |
||||
|
}else{ |
||||
|
toNextHandle(to, from, next,isFresh) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 跳转处理
|
||||
|
const toNextHandle = (to, from, next,isFresh) => { |
||||
|
if(isFresh){ |
||||
|
next({...to,replace:true}) |
||||
|
}else{ |
||||
|
next() |
||||
|
} |
||||
|
NProgress.done() |
||||
|
} |
||||
|
|
||||
|
router.beforeEach(async (to, from, next) => { |
||||
|
|
||||
|
// start progress bar
|
||||
|
NProgress.start() |
||||
|
// set page title
|
||||
|
document.title = getPageTitle() |
||||
|
|
||||
|
const hasToken = getToken() |
||||
|
// 判断是否自动登录
|
||||
|
let isAutoLogin = JSON.parse(localStorage.getItem('isAutoLogin')) |
||||
|
// 如果路径中有isAutoLogin参数则优先,否则读取配置文件中的isAutoLogin参数
|
||||
|
let _url_isAutoLogin = to.query.isAutoLogin |
||||
|
if( _url_isAutoLogin== 'false' || _url_isAutoLogin == 'true'){ |
||||
|
isAutoLogin = JSON.parse(_url_isAutoLogin) |
||||
|
} |
||||
|
|
||||
|
if(hasToken){ |
||||
|
if (to.path === '/login') { |
||||
|
console.log('有token,是登录页,将跳转至/') |
||||
|
// if is logged in, redirect to the home page
|
||||
|
next({ |
||||
|
path: '/' |
||||
|
}) |
||||
|
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
||||
|
} else { |
||||
|
console.log('有token,不是登录页,toPageInit') |
||||
|
await toPageInit(to, from, next) |
||||
|
} |
||||
|
}else{ |
||||
|
if (to.path === '/login') { |
||||
|
console.log('没有token,是登录页,pageToLogin') |
||||
|
pageToLogin(to, from, next) |
||||
|
} else { |
||||
|
if(isAutoLogin){ |
||||
|
console.log('没有token,不是登录页,是自动登录,自动登录是否成功都将继续toPageInit') |
||||
|
let _username = to.query.loginName || localStorage.getItem("isLoginName") || localStorage.getItem("loginName") |
||||
|
let _password = to.query.loginPass || localStorage.getItem("client_secret") |
||||
|
// todo-fast:password确定
|
||||
|
await store.dispatch('user/login', {password:_password,username:_username}) |
||||
|
.then(()=>{ |
||||
|
toPageInit(to, from, next) |
||||
|
}) |
||||
|
.catch(()=>{ |
||||
|
toPageInit(to, from, next) |
||||
|
}) |
||||
|
}else{ |
||||
|
console.log('没有token,不是登录页,不是自动登录,pageToLogin') |
||||
|
pageToLogin(to, from, next) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}) |
||||
|
|
||||
|
router.afterEach(() => { |
||||
|
// element-ui 使用 Tooltip 提示时候,更换页面,提示依旧存在偶现bug处理。
|
||||
|
const mytooltipList = document.querySelectorAll('.el-tooltip__popper') // 获取页面所有tooltip的dom集合
|
||||
|
setTimeout(() => { |
||||
|
mytooltipList.forEach((value) => { |
||||
|
value.style.display = 'none' |
||||
|
if(value.parentElement){ |
||||
|
value.parentElement.removeChild(value) |
||||
|
} |
||||
|
}) |
||||
|
}, 1000) |
||||
|
// finish progress bar
|
||||
|
NProgress.done() |
||||
|
}) |
@ -0,0 +1,168 @@ |
|||||
|
import { |
||||
|
token, |
||||
|
logout, |
||||
|
getUsersByUserName |
||||
|
} from '@/api/wms-auth' |
||||
|
import { |
||||
|
getToken, |
||||
|
setToken, |
||||
|
removeToken, |
||||
|
} from '@/utils/auth' |
||||
|
import router, { |
||||
|
resetRouter |
||||
|
} from '@/router' |
||||
|
import qs from 'qs' |
||||
|
import store from '@/store' |
||||
|
|
||||
|
const state = { |
||||
|
token: getToken(), |
||||
|
introduction: '', |
||||
|
roles: [], |
||||
|
userInfo:null |
||||
|
} |
||||
|
|
||||
|
const mutations = { |
||||
|
SET_TOKEN: (state, token) => { |
||||
|
state.token = token |
||||
|
}, |
||||
|
SET_INTRODUCTION: (state, introduction) => { |
||||
|
state.introduction = introduction |
||||
|
}, |
||||
|
// 设置权限(v-permission)
|
||||
|
SET_ROLES: (state, roles) => { |
||||
|
let _roles = [ |
||||
|
'IncomingDataHistory.filter', |
||||
|
'IncomingData.export', |
||||
|
'MesProductL7PartsNote.filter', |
||||
|
'MesProductL7PartsNote.export' |
||||
|
] |
||||
|
state.roles = _roles |
||||
|
}, |
||||
|
SET_USERINFO: (state, data) => { |
||||
|
state.userInfo = data |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const actions = { |
||||
|
// user login
|
||||
|
login({ |
||||
|
commit, |
||||
|
dispatch |
||||
|
}, userInfo) { |
||||
|
const { |
||||
|
password, |
||||
|
username, |
||||
|
company, |
||||
|
warehouseCode |
||||
|
} = userInfo |
||||
|
let loginData = { |
||||
|
userNameOrEmailAddress: username, |
||||
|
password: password, |
||||
|
rememberMe: true |
||||
|
} |
||||
|
let parseData = JSON.parse(JSON.stringify(userInfo)) |
||||
|
delete parseData.warehouseCode |
||||
|
delete parseData.company |
||||
|
parseData.client_secret = window.SITE_CONFIG['client_secret'] |
||||
|
let tokenData = qs.stringify(parseData) |
||||
|
return new Promise((resolve, reject) => { |
||||
|
// login(loginData).then(response => {
|
||||
|
let params = qs.stringify({ |
||||
|
password: password, |
||||
|
username: username, |
||||
|
client_id:'Z_App', |
||||
|
grant_type:'password', |
||||
|
scopes:'Z' |
||||
|
}); |
||||
|
token(params).then(response => { |
||||
|
const { |
||||
|
access_token, |
||||
|
token_type |
||||
|
} = response |
||||
|
if (access_token == null) { |
||||
|
reject() |
||||
|
} else { |
||||
|
commit('SET_TOKEN', access_token) |
||||
|
setToken(token_type + ' ' + access_token) |
||||
|
store.dispatch('user/getUserInfo',username).then(()=>{ |
||||
|
resolve() |
||||
|
}).catch(()=>{ |
||||
|
reject() |
||||
|
}) |
||||
|
} |
||||
|
}).catch(error => { |
||||
|
reject(error) |
||||
|
}) |
||||
|
// }).catch(error => {
|
||||
|
// reject(error)
|
||||
|
// })
|
||||
|
}) |
||||
|
}, |
||||
|
|
||||
|
// user logout
|
||||
|
logout({ |
||||
|
commit, |
||||
|
dispatch |
||||
|
}) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
// logout().then(() => {
|
||||
|
commit('SET_TOKEN', null) |
||||
|
commit('SET_USERINFO', null) |
||||
|
removeToken() |
||||
|
store.dispatch('permission/clearRouters') |
||||
|
// resetRouter()
|
||||
|
// reset visited views and cached views
|
||||
|
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
|
||||
|
dispatch('tagsView/delAllViews', null, { |
||||
|
root: true |
||||
|
}) |
||||
|
resolve() |
||||
|
// 防止路由叠加
|
||||
|
// location.reload()
|
||||
|
// }).catch(error => {
|
||||
|
// reject(error)
|
||||
|
// })
|
||||
|
}) |
||||
|
}, |
||||
|
|
||||
|
// remove token
|
||||
|
resetToken({ |
||||
|
commit |
||||
|
}) { |
||||
|
return new Promise(resolve => { |
||||
|
commit('SET_TOKEN', '') |
||||
|
commit('SET_ROLES', []) |
||||
|
removeToken() |
||||
|
resolve() |
||||
|
}) |
||||
|
}, |
||||
|
|
||||
|
// get userInfo
|
||||
|
getUserInfo({ |
||||
|
commit |
||||
|
},username) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
getUsersByUserName(username).then(res=>{ |
||||
|
if(res && JSON.stringify(res).length > 0){ |
||||
|
localStorage.setItem('isLoginName',username) |
||||
|
// commit('SET_ROLES', res)
|
||||
|
commit('SET_USERINFO', res) |
||||
|
resolve(res) |
||||
|
}else{ |
||||
|
reject('获取用户信息失败,请重试') |
||||
|
} |
||||
|
}) |
||||
|
.catch(err => { |
||||
|
reject(err) |
||||
|
console.log('获取用户信息失败,请重试') |
||||
|
}) |
||||
|
}) |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
export default { |
||||
|
namespaced: true, |
||||
|
state, |
||||
|
mutations, |
||||
|
actions |
||||
|
} |
Loading…
Reference in new issue