fix: 优化oauth页面

This commit is contained in:
xieyonghong 2023-04-27 16:25:08 +08:00
parent 816cadbc3b
commit ce4d63be55
3 changed files with 48 additions and 26 deletions

View File

@ -1,5 +1,5 @@
import { createRouter, createWebHashHistory } from 'vue-router' import { createRouter, createWebHashHistory } from 'vue-router'
import menus, { AccountCenterBindPath, InitHomePath, LoginPath } from './menu' import menus, { AccountCenterBindPath, InitHomePath, LoginPath, OauthPath } from './menu'
import { cleanToken, getToken } from '@/utils/comm' import { cleanToken, getToken } from '@/utils/comm'
import { useUserInfo } from '@/store/userInfo' import { useUserInfo } from '@/store/userInfo'
import { useSystem } from '@/store/system' import { useSystem } from '@/store/system'
@ -14,12 +14,14 @@ const router = createRouter({
} }
}) })
const filterPath = [ InitHomePath, AccountCenterBindPath ] const filterPath = [ InitHomePath ]
const noTokenPath = [ AccountCenterBindPath, OauthPath ]
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
// TODO 切换路由取消请求 // TODO 切换路由取消请求
const token = getToken() const token = getToken()
if (to.path === AccountCenterBindPath) { console.log(to.path, noTokenPath.includes(to.path))
if (noTokenPath.includes(to.path)) {
next() next()
} else if (token) { } else if (token) {
if (to.path === LoginPath) { if (to.path === LoginPath) {

View File

@ -13,7 +13,16 @@ interface AxiosResponseRewrite<T = any[]> extends AxiosResponse<T, any> {
export const SUCCESS_CODE = 200 // 成功代码 export const SUCCESS_CODE = 200 // 成功代码
const filterApiUrl = ['/system/version', '/system/config/front', '/authorize/captcha/config', '/application/sso/_all', '/authorize/captcha/image', '/application/sso/bind-code', '/authorize/login'] const filterApiUrl = [
'/system/version',
'/system/config/front',
'/authorize/captcha/config',
'/application/sso/_all',
'/authorize/captcha/image',
'/application/sso/bind-code',
'/authorize/login',
'/application/'
]
export const request = axios.create({ export const request = axios.create({
withCredentials: false, withCredentials: false,
@ -148,10 +157,10 @@ const errorHandler = (error: any) => {
} else if (status === 401) { } else if (status === 401) {
showNotification('Unauthorized', '用户未登录', '401') showNotification('Unauthorized', '用户未登录', '401')
setTimeout(() => { setTimeout(() => {
cleanToken() // cleanToken()
router.replace({ // router.replace({
path: LoginPath // path: LoginPath
}) // })
}, 0) }, 0)
} else if (status === 404) { } else if (status === 404) {
showNotification(error?.code, error?.response?.data?.message, '404') showNotification(error?.code, error?.response?.data?.message, '404')

View File

@ -74,7 +74,7 @@
import { TOKEN_KEY } from '@/utils/variable' import { TOKEN_KEY } from '@/utils/variable'
import { config, code, getOAuth2, initApplication, authLogin } from '@/api/login' import { config, code, getOAuth2, initApplication, authLogin } from '@/api/login'
import { getMe_api } from '@/api/home' import { getMe_api } from '@/api/home'
import { getImage } from '@/utils/comm' import { getImage, getToken } from '@/utils/comm'
const spinning = ref(true) const spinning = ref(true)
const isLogin = ref(false) const isLogin = ref(false)
@ -106,11 +106,14 @@ const captcha = reactive<{base64?: string, key?: string }>({
const getApplication = async (clientId: string) => { const getApplication = async (clientId: string) => {
const res = await initApplication(clientId) const res = await initApplication(clientId)
if (res.success) { if (res.success && res.result) {
appName.value = res.result.name appName.value = res.result.name
} }
} }
/**
* 获取验证码配置
*/
const getCode = async () => { const getCode = async () => {
const resp = await config() const resp = await config()
if (resp.result?.enabled) { if (resp.result?.enabled) {
@ -138,6 +141,8 @@ const changeAccount = () => {
} }
const getLoginUser = async (data?: any) => { const getLoginUser = async (data?: any) => {
console.log(getToken())
if (getToken()) { //
const res = await getMe_api() const res = await getMe_api()
if (res.success) { if (res.success) {
userName.value = res.result?.user.name userName.value = res.result?.user.name
@ -157,7 +162,12 @@ const getLoginUser = async (data?: any) => {
spinning.value = false spinning.value = false
}) })
} }
} else {
getApplication(data.client_id || params.value.client_id)
setTimeout(() => {
spinning.value = false
})
}
} }
const getQueryVariable = (variable: any) => { const getQueryVariable = (variable: any) => {
@ -178,9 +188,9 @@ const doLogin = async () => {
...formModel ...formModel
}) })
if (res.success) { if (res.success) {
getLoginUser()
const token = res.result.token const token = res.result.token
localStorage.setItem(TOKEN_KEY, token) localStorage.setItem(TOKEN_KEY, token)
getLoginUser()
goOAuth2Fn() goOAuth2Fn()
} else { } else {
getCode() getCode()
@ -189,6 +199,7 @@ const doLogin = async () => {
const initPage = async () => { const initPage = async () => {
let redirectUrl let redirectUrl
// url
const items = { const items = {
client_id: getQueryVariable('client_id'), client_id: getQueryVariable('client_id'),
state: getQueryVariable('state'), state: getQueryVariable('state'),
@ -204,7 +215,7 @@ const initPage = async () => {
redirectUrl = items.redirect_uri redirectUrl = items.redirect_uri
console.log(origin, items.redirect_uri) console.log(origin, items.redirect_uri)
} }
//
getLoginUser({ getLoginUser({
...items, ...items,
internal: getQueryVariable('internal'), internal: getQueryVariable('internal'),