单点登录bug修复

This commit is contained in:
wuhongjian 2022-07-08 19:02:20 +08:00
parent 678367a6bf
commit a839c90a1e
4 changed files with 273 additions and 290 deletions

View File

@ -28,10 +28,10 @@
<!-- 站点配置 --> <!-- 站点配置 -->
<script> <script>
window.SITE_CONFIG = {}; window.SITE_CONFIG = {};
// window.SITE_CONFIG['backUrl'] = 'http://15.72.183.90:8001'; window.SITE_CONFIG['backUrl'] = 'http://15.72.183.90:8001';
// window.SITE_CONFIG['previewUrl'] = 'http://15.72.183.90:7008/'; window.SITE_CONFIG['previewUrl'] = 'http://15.72.183.90:7008/';
// window.SITE_CONFIG['frontUrl'] = 'http://15.72.183.90:7008/document/#/devModelFile/'; window.SITE_CONFIG['frontUrl'] = 'http://15.72.183.90:7008/document/#/devModelFile/';
// window.SITE_CONFIG['apiURL'] = 'http://15.72.183.90:8000/renren-admin'; window.SITE_CONFIG['apiURL'] = 'http://15.72.183.90:8000/renren-admin';
window.SITE_CONFIG['POI_URL'] = 'http://15.72.178.129:8090/iserver/services/addressmatch-qingdaoPOI181015/restjsr/v1/address'; window.SITE_CONFIG['POI_URL'] = 'http://15.72.178.129:8090/iserver/services/addressmatch-qingdaoPOI181015/restjsr/v1/address';
// 包头 // 包头
// window.SITE_CONFIG['backUrl'] = 'http://10.110.205.1:8001'; // window.SITE_CONFIG['backUrl'] = 'http://10.110.205.1:8001';
@ -45,10 +45,10 @@
// window.SITE_CONFIG['frontUrl'] = 'http://10.134.135.9:9796/document/#/devModelFile/'; // window.SITE_CONFIG['frontUrl'] = 'http://10.134.135.9:9796/document/#/devModelFile/';
// window.SITE_CONFIG['apiURL'] = 'http://10.134.135.9:8888/renren-admin'; // window.SITE_CONFIG['apiURL'] = 'http://10.134.135.9:8888/renren-admin';
// 开发 // 开发
window.SITE_CONFIG['backUrl'] = 'http://15.2.21.238:9797'; // window.SITE_CONFIG['backUrl'] = 'http://15.2.21.238:9797';
window.SITE_CONFIG['previewUrl'] = 'http://15.2.21.238:9796/'; // window.SITE_CONFIG['previewUrl'] = 'http://15.2.21.238:9796/';
window.SITE_CONFIG['frontUrl'] = 'http://15.2.21.238:9796/document/#/devModelFile/'; // window.SITE_CONFIG['frontUrl'] = 'http://15.2.21.238:9796/document/#/devModelFile/';
window.SITE_CONFIG['apiURL'] = 'http://15.2.21.238:8888/renren-admin'; // window.SITE_CONFIG['apiURL'] = 'http://15.2.21.238:8888/renren-admin';
// window.SITE_CONFIG['POI_URL'] = 'http://15.72.178.129:8090/iserver/services/addressmatch-qingdaoPOI181015/restjsr/v1/address'; // window.SITE_CONFIG['POI_URL'] = 'http://15.72.178.129:8090/iserver/services/addressmatch-qingdaoPOI181015/restjsr/v1/address';
// 穿透版本 // 穿透版本
// window.SITE_CONFIG['backUrl'] = 'http://124.222.94.39:9797'; // window.SITE_CONFIG['backUrl'] = 'http://124.222.94.39:9797';

View File

@ -2,175 +2,182 @@
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 登录获取用户信息退出登录清除accessToken逻辑不建议修改 * @description 登录获取用户信息退出登录清除accessToken逻辑不建议修改
*/ */
import { getUserInfo, login, logout } from '@/api/user' import { getUserInfo, login, logout } from '@/api/user'
import { import {
getAccessToken, getAccessToken,
removeAccessToken, removeAccessToken,
setAccessToken, setAccessToken,
} from '@/utils/accessToken' } from '@/utils/accessToken'
import { title, tokenName } from '@/config' import { title, tokenName } from '@/config'
import { message, notification } from 'ant-design-vue' import { message, notification } from 'ant-design-vue'
const state = () => ({ const state = () => ({
accessToken: getAccessToken(), accessToken: getAccessToken(),
username: '', username: '',
userId: '', userId: '',
avatar: '', avatar: '',
role: 0, // role: 0, //
}) })
const getters = { const getters = {
accessToken: (state) => state.accessToken, accessToken: (state) => state.accessToken,
username: (state) => state.username, username: (state) => state.username,
avatar: (state) => state.avatar, avatar: (state) => state.avatar,
role: (state) => state.role, role: (state) => state.role,
userId: (state) => state.userId, userId: (state) => state.userId,
} }
const mutations = { const mutations = {
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 设置accessToken * @description 设置accessToken
* @param {*} state * @param {*} state
* @param {*} accessToken * @param {*} accessToken
*/ */
setAccessToken(state, accessToken) { setAccessToken(state, accessToken) {
state.accessToken = accessToken state.accessToken = accessToken
setAccessToken(accessToken) setAccessToken(accessToken)
}, },
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 设置用户名 * @description 设置用户名
* @param {*} state * @param {*} state
* @param {*} username * @param {*} username
*/ */
setUsername(state, username) { setUsername(state, username) {
state.username = username state.username = username
}, },
// //
setRole(state, role) { setRole(state, role) {
state.role = role state.role = role
}, },
// userId // userId
setUserId(state, id) { setUserId(state, id) {
state.userId = id state.userId = id
}, },
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 设置头像 * @description 设置头像
* @param {*} state * @param {*} state
* @param {*} avatar * @param {*} avatar
*/ */
setAvatar(state, avatar) { setAvatar(state, avatar) {
state.avatar = avatar state.avatar = avatar
}, },
} }
const actions = { const actions = {
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 登录拦截放行时设置虚拟角色 * @description 登录拦截放行时设置虚拟角色
* @param {*} { commit, dispatch } * @param {*} { commit, dispatch }
*/ */
setVirtualRoles({ commit, dispatch }) { setVirtualRoles({ commit, dispatch }) {
dispatch('acl/setFull', true, { dispatch('acl/setFull', true, {
root: true, root: true,
}) })
commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif') commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
commit('setUsername', 'admin(未开启登录拦截)') commit('setUsername', 'admin(未开启登录拦截)')
}, },
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 登录 * @description 登录
* @param {*} { commit } * @param {*} { commit }
* @param {*} userInfo * @param {*} userInfo
*/ */
async login({ commit }, userInfo) { async login({ commit }, userInfo) {
const { data } = await login(userInfo) const { data } = await login(userInfo)
// debugger // debugger
const accessToken = data.data[tokenName] const accessToken = data.data[tokenName]
if (accessToken) { if (accessToken) {
commit('setAccessToken', accessToken) commit('setAccessToken', accessToken)
const hour = new Date().getHours() const hour = new Date().getHours()
const thisTime = const thisTime =
hour < 8 hour < 8
? '早上好' ? '早上好'
: hour <= 11 : hour <= 11
? '上午好' ? '上午好'
: hour <= 13 : hour <= 13
? '中午好' ? '中午好'
: hour < 18 : hour < 18
? '下午好' ? '下午好'
: '晚上好' : '晚上好'
notification.open({ notification.open({
message: `欢迎登录${title}`, message: `欢迎登录${title}`,
description: `${thisTime}`, description: `${thisTime}`,
}) })
} else { } else {
message.error(`登录接口异常,未正确返回${tokenName}...`) message.error(`登录接口异常,未正确返回${tokenName}...`)
} }
}, },
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 获取用户信息接口 这个接口非常非常重要如果没有明确底层前逻辑禁止修改此方法错误的修改可能造成整个框架无法正常使用 * @description 获取用户信息接口 这个接口非常非常重要如果没有明确底层前逻辑禁止修改此方法错误的修改可能造成整个框架无法正常使用
* @param {*} { commit, dispatch, state } * @param {*} { commit, dispatch, state }
* @returns * @returns
*/ */
getUserInfo({ commit }) { async getUserInfo({ commit }) {
return new Promise((resolve, reject)=>{ const { data } = await getUserInfo()
getUserInfo().then( (res) =>{ if (!data) {
console.log('res', res) message.error(`验证失败,请重新登录...`)
const data = res.data return false
if (!data) { }
message.error(`验证失败,请重新登录...`) // debugger
reject() commit('setUsername', data.data.username)
} commit('setRole', data.data.roleIdList.length)
commit('setUsername', data.data.username) commit('setUserId', data.data.id)
commit('setRole', data.data.roleIdList.length) // TODO
commit('setUserId', data.data.id) // let { username, avatar, roles, ability } = data
setTimeout(()=>{ // if (username && roles && Array.isArray(roles)) {
resolve() // dispatch('acl/setRole', roles, {
},500) // root: true,
// })
// if (ability && ability.length > 0)
// dispatch('acl/setAbility', ability, {
// root: true,
// })
// commit('setUsername', username)
// commit('setAvatar', avatar)
// } else {
// message.error('')
// }
},
}) /**
}) * @author chuzhixin 1204505056@qq.com
}, * @description 退出登录
* @param {*} { dispatch }
/** */
* @author chuzhixin 1204505056@qq.com async logout({ dispatch }) {
* @description 退出登录 await logout(state.accessToken)
* @param {*} { dispatch } await dispatch('resetAll')
*/ },
async logout({ dispatch }) { /**
await logout(state.accessToken) * @author chuzhixin 1204505056@qq.com
await dispatch('resetAll') * @description 重置accessTokenrolesabilityrouter等
}, * @param {*} { commit, dispatch }
/** */
* @author chuzhixin 1204505056@qq.com async resetAll({ dispatch }) {
* @description 重置accessTokenrolesabilityrouter等 await dispatch('setAccessToken', '')
* @param {*} { commit, dispatch } await dispatch('acl/setFull', false, {
*/ root: true,
async resetAll({ dispatch }) { })
await dispatch('setAccessToken', '') await dispatch('acl/setRole', [], {
await dispatch('acl/setFull', false, { root: true,
root: true, })
}) await dispatch('acl/setAbility', [], {
await dispatch('acl/setRole', [], { root: true,
root: true, })
}) removeAccessToken()
await dispatch('acl/setAbility', [], { },
root: true, /**
}) * @author chuzhixin 1204505056@qq.com
removeAccessToken() * @description 设置token
}, */
/** setAccessToken({ commit }, accessToken) {
* @author chuzhixin 1204505056@qq.com commit('setAccessToken', accessToken)
* @description 设置token },
*/ }
setAccessToken({ commit }, accessToken) { export default {
commit('setAccessToken', accessToken) state,
}, getters,
} mutations,
export default { actions,
state, }
getters,
mutations,
actions,
}

View File

@ -12,7 +12,7 @@ import qs from 'qs'
import router from '@/router' import router from '@/router'
// import { isArray } from '@/utils/validate' // import { isArray } from '@/utils/validate'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import { setAccessToken } from '@/utils/accessToken' import { getAccessToken, setAccessToken } from '@/utils/accessToken'
let loadingInstance let loadingInstance
@ -22,15 +22,24 @@ let loadingInstance
* @param {*} code * @param {*} code
* @param {*} msg * @param {*} msg
*/ */
const handleCode = (code, msg, redirect) => { const handleCode = (code, msg, res) => {
debugger
switch (code) { switch (code) {
case 401: case 401:
// message.error(msg || '') // message.error(msg || '')
store.dispatch('user/resetAll').catch(() => {}) store.dispatch('user/resetAll').catch(() => {})
// // location.reload() if (res.token) {
if (redirect) { console.log('存在token信息', res.token)
window.location.href = redirect setAccessToken(res.token)
const token = getAccessToken()
console.log('验证token信息', token)
} }
// // location.reload()
// if (redirect) {
// window.location.href = res.redirect
// }
break break
case 403: case 403:
router.push({ path: '/401' }).catch(() => {}) router.push({ path: '/401' }).catch(() => {})
@ -87,10 +96,21 @@ instance.interceptors.request.use(
*/ */
instance.interceptors.response.use( instance.interceptors.response.use(
(response) => { (response) => {
// console.log('', response) console.log('接口返回', response)
// console.log('headers', response.headers) console.log('接口返回headers', response.headers)
// console.log('REDIRECT', response.headers.redirect) console.log('接口返回REDIRECT', response.headers.redirect)
response['Access-Control-Expose-Headers'] = 'redirect'
const { code, message } = response.data
if (response.headers.token) {
setAccessToken(response.headers.token)
} else {
if (response.headers.redirect) {
// window.location.href = response.headers.redirect
window.location.replace(response.headers.redirect)
return
// location.reload()
}
}
if (response.headers.redirect === '/#/login') { if (response.headers.redirect === '/#/login') {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g) var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
if (keys) { if (keys) {
@ -109,29 +129,12 @@ instance.interceptors.response.use(
new Date(0).toUTCString() // .kevis.com new Date(0).toUTCString() // .kevis.com
} }
} }
// console.log('') console.log('已清除')
setTimeout(() => { setTimeout(() => {
location.reload() location.reload()
}, 1000) }, 1000)
} }
debugger; handleCode(code, message, response.headers)
response['Access-Control-Expose-Headers'] = 'redirect'
const { code, message } = response.data
if (response.headers.token) {
setAccessToken(response.headers.token)
debugger
// setAccessToken(response.headers.token)
// store.dispatch('user/getUserInfo')
} else {
if (response.headers.redirect) {
// window.location.href = response.headers.redirect
window.location.replace(response.headers.redirect)
return
// location.reload()
}
}
handleCode(code, message, response.headers.redirect)
if (loadingInstance) loadingInstance.close() if (loadingInstance) loadingInstance.close()
// const { data, config } = response // const { data, config } = response
// const { code, msg } = data // const { code, msg } = data
@ -151,38 +154,15 @@ instance.interceptors.response.use(
// } // }
}, },
(error) => { (error) => {
// console.log('error', error) console.log('接口error', error)
if (loadingInstance) loadingInstance.close() if (loadingInstance) loadingInstance.close()
const { response, message } = error const { response, message } = error
if (error.response && error.response.data) { if (error.response && error.response.data) {
// console.log('', response) console.log('接口返回', response)
// console.log('headers', response.headers) console.log('接口返回headers', response.headers)
// console.log('REDIRECT1111', response.headers.redirect) console.log('接口返回REDIRECT', response.headers.redirect)
if (response.headers.redirect === '/#/login') {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
if (keys) {
for (var i = keys.length; i--; ) {
document.cookie =
keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() //,m.kevis.com
document.cookie =
keys[i] +
'=0;path=/;domain=' +
document.domain +
';expires=' +
new Date(0).toUTCString() // .m.kevis.com
document.cookie =
keys[i] +
'=0;path=/;domain=kevis.com;expires=' +
new Date(0).toUTCString() // .kevis.com
}
}
// console.log('')
setTimeout(() => {
location.reload()
}, 1000)
}
if (response.headers.redirect) { if (response.headers.redirect) {
window.location.href = response.headers.redirect window.location.replace(response.headers.redirect)
return Promise.reject(error) return Promise.reject(error)
} }
const { status, data } = response const { status, data } = response

View File

@ -2,67 +2,63 @@
* @Author: hisense.wuhongjian * @Author: hisense.wuhongjian
* @Date: 2022-04-01 17:23:11 * @Date: 2022-04-01 17:23:11
* @LastEditors: hisense.wuhongjian * @LastEditors: hisense.wuhongjian
* @LastEditTime: 2022-07-07 19:58:32 * @LastEditTime: 2022-07-08 18:55:28
* @Description: 告诉大家这是什么 * @Description: 告诉大家这是什么
*/ */
/** /**
* @author chuzhixin 1204505056@qq.com * @author chuzhixin 1204505056@qq.com
* @description 路由守卫目前两种模式all模式与intelligence模式 * @description 路由守卫目前两种模式all模式与intelligence模式
*/ */
import router from '@/router' import router from '@/router'
import store from '@/store' import store from '@/store'
import getPageTitle from '@/utils/pageTitle' import getPageTitle from '@/utils/pageTitle'
import { import { getUserInfo } from '@/api/user'
// authentication, import {
loginInterception, // authentication,
// recordRoute, loginInterception,
routesWhiteList, // recordRoute,
} from '@/config' routesWhiteList,
import { setAccessToken, getAccessToken } from '@/utils/accessToken' } from '@/config'
router.beforeEach(async (to, from, next) => { import { setAccessToken, getAccessToken } from '@/utils/accessToken'
// router.beforeEach(async (to, from, next) => {
// debugger // debugger
// const SSOTOKEN = to.query.SSOToken // const SSOTOKEN = to.query.SSOToken
// if (SSOTOKEN) { // if (SSOTOKEN) {
// setAccessToken(SSOTOKEN) // setAccessToken(SSOTOKEN)
// } // }
const token = getAccessToken() const token = getAccessToken()
// console.log('token', token) console.log('token', token)
let hasToken = token || store.getters['user/accessToken'] let hasToken = token || store.getters['user/accessToken']
// debugger // debugger
// if (!loginInterception) hasToken = true
if (!loginInterception) hasToken = true console.log('hasToken存在巨大问题', hasToken)
// console.log('hasToken', hasToken) if (hasToken) {
debugger setAccessToken(hasToken)
if (hasToken) { await store.dispatch('user/getUserInfo')
setAccessToken(token) next()
await store.dispatch('user/getUserInfo') } else {
debugger let accessRoutes = []
next() accessRoutes = await store.dispatch('routes/setRoutes')
} else { accessRoutes.forEach((item) => {
let accessRoutes = [] router.addRoute(item)
accessRoutes = await store.dispatch('routes/setRoutes') })
accessRoutes.forEach((item) => { if (routesWhiteList.indexOf(to.path) !== -1) {
router.addRoute(item) next()
}) } else {
if (routesWhiteList.indexOf(to.path) !== -1) { getUserInfo().then(res=>{
next() console.log(res)
} else { router.replace('/home')
store.dispatch('user/getUserInfo').then((res)=>{
console.log('马到成功!')
next()
}) })
// if (recordRoute) // await store.dispatch('user/getUserInfo')
// next({ path: '/login', query: { redirect: to.path }, replace: true })
// else next({ path: '/login', replace: true })
// debugger
// setTimeout(()=>{
// next() // next()
// }, 500) // if (recordRoute)
// window.open('http://www.baidu.com', '_self') // next({ path: '/login', query: { redirect: to.path }, replace: true })
} // else next({ path: '/login', replace: true })
} // next()
}) // window.open('http://www.baidu.com', '_self')
router.afterEach((to) => { }
document.title = getPageTitle(to.meta.title) }
}) })
router.afterEach((to) => {
document.title = getPageTitle(to.meta.title)
})