三方登录

This commit is contained in:
liaoboping 2025-09-28 10:40:20 +08:00
parent 1b45cb6927
commit 51a6fcbfb9
4 changed files with 49 additions and 19 deletions

View File

@ -11,7 +11,7 @@ import openThirdLogin from './utils/openThirdLogin'
NProgress.configure({ showSpinner: false }) // NProgress Configuration NProgress.configure({ showSpinner: false }) // NProgress Configuration
const allowList = ['thirdLogin', 'login', 'register', 'registerResult'] // no redirect allowList const allowList = ['login', 'Welcome'] // no redirect allowList
const loginRoutePath = '/user/login' const loginRoutePath = '/user/login'
const defaultRoutePath = '/user/welcome' const defaultRoutePath = '/user/welcome'
@ -58,11 +58,11 @@ router.beforeEach((to, from, next) => {
}) })
// 失败时,获取用户信息失败时,调用登出,来清空历史保留信息 // 失败时,获取用户信息失败时,调用登出,来清空历史保留信息
store.dispatch('Logout').then(() => { store.dispatch('Logout').then(() => {
// if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
// openThirdLogin() openThirdLogin()
// } else { } else {
next({ path: loginRoutePath, query: { redirect: to.fullPath } }) next({ path: loginRoutePath, query: { redirect: to.fullPath } })
// } }
}) })
}) })
} else { } else {
@ -74,11 +74,11 @@ router.beforeEach((to, from, next) => {
// 在免登录名单,直接进入 // 在免登录名单,直接进入
next() next()
} else { } else {
// if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
// openThirdLogin() openThirdLogin()
// } else { } else {
next({ path: loginRoutePath, query: { redirect: to.fullPath } }) next({ path: loginRoutePath, query: { redirect: to.fullPath } })
// } }
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
} }
} }

View File

@ -1,5 +1,5 @@
export default () => { export default () => {
const redirectUri = window.location.origin + '/user/thirdLogin' const redirectUri = window.location.origin + '/user/welcome'
window.location.href = window.location.href =
window._CONFIG.thirdLoginUrl + window._CONFIG.thirdLoginUrl +
'?response_type=code&active_type=user&state=' + '?response_type=code&active_type=user&state=' +

View File

@ -26,7 +26,10 @@ const errorHandler = (error) => {
description: data.message, description: data.message,
}) })
} }
if (error.response.status === 401 && !(data.result && data.result.isLogin)) { if (
(error.response.status === 500 && error.response.data.message === '未找到授权信息') ||
(error.response.status === 401 && !(data.result && data.result.isLogin))
) {
notification.error({ notification.error({
message: '温馨提示', message: '温馨提示',
description: '登录过期,请重新登录', description: '登录过期,请重新登录',
@ -35,13 +38,19 @@ const errorHandler = (error) => {
if (token) { if (token) {
store.dispatch('Logout').then(() => { store.dispatch('Logout').then(() => {
setTimeout(() => { setTimeout(() => {
// if (process.env.NODE_ENV === 'production') { if (process.env.NODE_ENV === 'production') {
// openThirdLogin() openThirdLogin()
// } else { } else {
window.location.reload() window.location.reload()
// } }
}, 1500) }, 1500)
}) })
} else {
if (process.env.NODE_ENV === 'production') {
openThirdLogin()
} else {
window.location.reload()
}
} }
} }
} }

View File

@ -23,6 +23,7 @@
</template> </template>
<script> <script>
import { getAction } from '@/api/manage'
import { mapState } from 'vuex' import { mapState } from 'vuex'
export default { export default {
@ -81,7 +82,27 @@ export default {
userInfo: (state) => state.user.info, userInfo: (state) => state.user.info,
}), }),
}, },
created() { async created() {
const searchParams = new URLSearchParams(window.location.search)
const searchCode = searchParams.get('code')
const localCode = window.localStorage.getItem('code')
if (searchCode) {
window.localStorage.setItem('code', searchCode)
window.location.href = window.location.origin + window.location.pathname
} else if (localCode) {
try {
const tokenRes = await getAction('/oauth2/thirdLogin', { code: localCode })
storage.set(ACCESS_TOKEN, tokenRes.data.token, 7 * 24 * 60 * 60 * 1000)
this.$store.commit('SET_TOKEN', tokenRes.data.token)
await this.$store.dispatch('GetInfo')
} catch (error) {
console.log(error)
} finally {
window.localStorage.removeItem('code')
}
} else {
this.$store.dispatch('GetInfo')
}
window.addEventListener('beforeunload', (e) => { window.addEventListener('beforeunload', (e) => {
this.openChildren.forEach((childWindow) => { this.openChildren.forEach((childWindow) => {
childWindow && childWindow.close() childWindow && childWindow.close()