diff --git a/src/router/index.ts b/src/router/index.ts index 1ecbc43e..fb09a885 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,5 +1,5 @@ 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 { useUserInfo } from '@/store/userInfo' 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) => { // TODO 切换路由取消请求 const token = getToken() - if (to.path === AccountCenterBindPath) { + console.log(to.path, noTokenPath.includes(to.path)) + if (noTokenPath.includes(to.path)) { next() } else if (token) { if (to.path === LoginPath) { diff --git a/src/utils/request.ts b/src/utils/request.ts index a74fb062..c7ffca50 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -13,7 +13,16 @@ interface AxiosResponseRewrite extends AxiosResponse { 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({ withCredentials: false, @@ -148,10 +157,10 @@ const errorHandler = (error: any) => { } else if (status === 401) { showNotification('Unauthorized', '用户未登录', '401') setTimeout(() => { - cleanToken() - router.replace({ - path: LoginPath - }) + // cleanToken() + // router.replace({ + // path: LoginPath + // }) }, 0) } else if (status === 404) { showNotification(error?.code, error?.response?.data?.message, '404') diff --git a/src/views/oauth/index.vue b/src/views/oauth/index.vue index 547796f1..4e431e68 100644 --- a/src/views/oauth/index.vue +++ b/src/views/oauth/index.vue @@ -74,7 +74,7 @@ import { TOKEN_KEY } from '@/utils/variable' import { config, code, getOAuth2, initApplication, authLogin } from '@/api/login' import { getMe_api } from '@/api/home' -import { getImage } from '@/utils/comm' +import { getImage, getToken } from '@/utils/comm' const spinning = ref(true) const isLogin = ref(false) @@ -106,11 +106,14 @@ const captcha = reactive<{base64?: string, key?: string }>({ const getApplication = async (clientId: string) => { const res = await initApplication(clientId) - if (res.success) { + if (res.success && res.result) { appName.value = res.result.name } } +/** + * 获取验证码配置 + */ const getCode = async () => { const resp = await config() if (resp.result?.enabled) { @@ -138,26 +141,33 @@ const changeAccount = () => { } const getLoginUser = async (data?: any) => { - const res = await getMe_api() - if (res.success) { - userName.value = res.result?.user.name - isLogin.value = true - getApplication(data.client_id || params.value.client_id) - if (data.internal === 'true' || internal.value === 'true') { // 是否走oauth2 - goOAuth2Fn(data) + console.log(getToken()) + if (getToken()) { // 未登录 + const res = await getMe_api() + if (res.success) { + userName.value = res.result?.user.name + isLogin.value = true + getApplication(data.client_id || params.value.client_id) + if (data.internal === 'true' || internal.value === 'true') { // 是否走oauth2 + goOAuth2Fn(data) + } + } else if (res.status === 401) { + setTimeout(() => { + spinning.value = false + }) + getCode() + getApplication(data.client_id || params.value.client_id) + } else { + setTimeout(() => { + spinning.value = false + }) } - } else if (res.status === 401) { - setTimeout(() => { - spinning.value = false - }) - getCode() - getApplication(data.client_id || params.value.client_id) } else { + getApplication(data.client_id || params.value.client_id) setTimeout(() => { spinning.value = false }) } - } const getQueryVariable = (variable: any) => { @@ -178,9 +188,9 @@ const doLogin = async () => { ...formModel }) if (res.success) { - getLoginUser() const token = res.result.token localStorage.setItem(TOKEN_KEY, token) + getLoginUser() goOAuth2Fn() } else { getCode() @@ -189,6 +199,7 @@ const doLogin = async () => { const initPage = async () => { let redirectUrl + // 获取url中的配置信息 const items = { client_id: getQueryVariable('client_id'), state: getQueryVariable('state'), @@ -204,7 +215,7 @@ const initPage = async () => { redirectUrl = items.redirect_uri console.log(origin, items.redirect_uri) } - + // 获取用户信息 getLoginUser({ ...items, internal: getQueryVariable('internal'),