2023-06-20 04:12:49 +00:00
|
|
|
<template>
|
2023-07-02 16:48:57 +08:00
|
|
|
<div v-loading="loading" class="social-callback"></div>
|
2023-06-20 04:12:49 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2023-07-01 16:24:48 +08:00
|
|
|
import { login, callback } from '@/api/login';
|
2023-08-14 12:56:31 +08:00
|
|
|
import { setToken, getToken } from '@/utils/auth';
|
2023-07-01 16:24:48 +08:00
|
|
|
import { LoginData } from '@/api/types';
|
2023-06-20 04:12:49 +00:00
|
|
|
|
|
|
|
const route = useRoute();
|
2023-07-01 02:26:58 +08:00
|
|
|
const loading = ref(true);
|
2023-06-20 04:12:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 接收Route传递的参数
|
|
|
|
* @param {Object} route.query.
|
|
|
|
*/
|
2023-07-01 16:24:48 +08:00
|
|
|
const code = route.query.code as string;
|
|
|
|
const state = route.query.state as string;
|
2023-06-20 04:12:49 +00:00
|
|
|
const source = route.query.source as string;
|
2024-06-18 18:44:17 +08:00
|
|
|
const tenantId = route.query.tenantId as string ? route.query.tenantId as string : '000000';
|
2023-07-01 16:24:48 +08:00
|
|
|
|
|
|
|
const processResponse = async (res: any) => {
|
|
|
|
if (res.code !== 200) {
|
|
|
|
throw new Error(res.msg);
|
|
|
|
}
|
2024-06-18 18:44:17 +08:00
|
|
|
if (res.data !== null && res.data.access_token !== null) {
|
2023-07-12 13:52:59 +08:00
|
|
|
setToken(res.data.access_token);
|
|
|
|
}
|
2023-07-01 16:24:48 +08:00
|
|
|
ElMessage.success(res.msg);
|
2023-07-13 16:19:29 +08:00
|
|
|
setTimeout(() => {
|
2024-06-18 18:44:17 +08:00
|
|
|
if (res.data !== null && res.data.domain !== null) {
|
|
|
|
location.href = res.data.domain + import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
|
|
|
|
} else {
|
|
|
|
location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
|
|
|
|
}
|
2023-07-13 16:19:29 +08:00
|
|
|
}, 2000);
|
2023-07-01 16:24:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleError = (error: any) => {
|
|
|
|
ElMessage.error(error.message);
|
2023-07-13 16:19:29 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
|
|
|
|
}, 2000);
|
2023-07-01 16:24:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const callbackByCode = async (data: LoginData) => {
|
|
|
|
try {
|
|
|
|
const res = await callback(data);
|
|
|
|
await processResponse(res);
|
2023-06-20 04:12:49 +00:00
|
|
|
loading.value = false;
|
2023-07-01 16:24:48 +08:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const loginByCode = async (data: LoginData) => {
|
|
|
|
try {
|
|
|
|
const res = await login(data);
|
|
|
|
await processResponse(res);
|
2023-06-20 04:12:49 +00:00
|
|
|
loading.value = false;
|
2023-07-01 16:24:48 +08:00
|
|
|
} catch (error) {
|
|
|
|
handleError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const init = async () => {
|
|
|
|
const data: LoginData = {
|
|
|
|
socialCode: code,
|
|
|
|
socialState: state,
|
|
|
|
tenantId: tenantId,
|
|
|
|
source: source,
|
|
|
|
clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
|
|
|
grantType: 'social'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!getToken()) {
|
|
|
|
await loginByCode(data);
|
|
|
|
} else {
|
|
|
|
await callbackByCode(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
init();
|
2023-06-20 04:12:49 +00:00
|
|
|
});
|
2023-07-01 16:24:48 +08:00
|
|
|
});
|
2023-06-20 04:12:49 +00:00
|
|
|
</script>
|