86 lines
2.4 KiB
Vue
86 lines
2.4 KiB
Vue
<script setup lang="ts">
|
|
import { onMounted, reactive } from 'vue';
|
|
import { useRouter } from 'vue-router';
|
|
import useI18n from '@/hooks/useI18n';
|
|
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
|
|
import {
|
|
delAccessToken,
|
|
delRefreshToken,
|
|
setAccessToken,
|
|
setRefreshToken,
|
|
} from '@/plugins/auth-token';
|
|
import { loginOAuth2Token } from '@/api/auth';
|
|
const router = useRouter();
|
|
const { t } = useI18n();
|
|
|
|
const info = reactive({
|
|
status: 'warning' as 'error' | 'warning' | 'success',
|
|
title: t('views.login.authorizedNotfound'),
|
|
subTitle: '',
|
|
});
|
|
|
|
function fnToLogin() {
|
|
router.replace({ name: 'Login' }).finally(() => {
|
|
delAccessToken();
|
|
delRefreshToken();
|
|
location.replace(location.origin);
|
|
});
|
|
}
|
|
|
|
onMounted(() => {
|
|
// 从URL参数中获取code和state
|
|
const params = new URLSearchParams(location.search);
|
|
const code = params.get('code');
|
|
const state = params.get('state');
|
|
if (code && state) {
|
|
loginOAuth2Token(code, state).then(res => {
|
|
if (res.code === RESULT_CODE_SUCCESS) {
|
|
info.status = 'success';
|
|
info.title = t('views.login.authorizedSuccess');
|
|
info.subTitle = t('views.login.redirectHome', { i: 3 });
|
|
setAccessToken(res.data.accessToken, res.data.refreshExpiresIn);
|
|
setRefreshToken(res.data.refreshToken, res.data.refreshExpiresIn);
|
|
// 3秒后跳转主页
|
|
let i = 2;
|
|
const timer = setInterval(() => {
|
|
info.subTitle = t('views.login.redirectHome', { i });
|
|
i--;
|
|
if (i <= -1) {
|
|
clearInterval(timer);
|
|
/**登录后跳转主页 */
|
|
router.replace({ name: 'Index' }).finally(() => {
|
|
location.replace(location.origin);
|
|
});
|
|
}
|
|
}, 1000);
|
|
} else {
|
|
info.status = 'error';
|
|
info.title = t('views.login.authorizedFailed');
|
|
info.subTitle = res.msg;
|
|
}
|
|
});
|
|
} else {
|
|
// info.subTitle = "未获取到code和state参数";
|
|
info.subTitle = "code and state parameters not obtained";
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<a-result
|
|
:status="info.status"
|
|
:title="info.title"
|
|
v-model:subTitle="info.subTitle"
|
|
>
|
|
<template #extra>
|
|
<a-button
|
|
type="default"
|
|
v-if="info.status !== 'success'"
|
|
@click="fnToLogin"
|
|
>
|
|
{{ t('views.login.backBtnLogin') }}
|
|
</a-button>
|
|
</template>
|
|
</a-result>
|
|
</template>
|