|
|
@@ -1,89 +1,66 @@
|
|
|
-<template lang="">
|
|
|
- <div id="verify-root">
|
|
|
- <div class="verify" style="border-radius: 8px; box-shadow: var(--el-box-shadow-lighter);">
|
|
|
- <el-row >
|
|
|
- <el-col >
|
|
|
- <el-text type="success" sytle="font-size: 22px;color: rgb(36, 36, 36); " tag="b">扫码成功</el-text>
|
|
|
- </el-col>
|
|
|
- </el-row>
|
|
|
- <el-row>
|
|
|
- <el-col>
|
|
|
- <el-text>正在跳转到</el-text>
|
|
|
- </el-col>
|
|
|
- </el-row>
|
|
|
- <el-row>
|
|
|
- <el-col>
|
|
|
- <!-- 使用 v-if 控制链接的显示,只有在登录成功后才显示 -->
|
|
|
- <el-link v-if="isLoggedIn" type="primary" @click="navigateToAccount">我的主页</el-link>
|
|
|
- <!-- 添加加载状态的文本 -->
|
|
|
- <el-text v-else>{{info}}...</el-text>
|
|
|
- </el-col>
|
|
|
- </el-row>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
-</template>
|
|
|
-
|
|
|
-<script setup>
|
|
|
-import { useRouter } from 'vue-router';
|
|
|
-import { ref, onMounted, nextTick } from 'vue';
|
|
|
-import axios from 'axios'; // 确保已经安装了axios
|
|
|
-import eventBus from 'vue3-eventbus'
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <!-- 可以在这里添加一个加载指示器或其他UI元素 -->
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
|
|
|
+<script setup>
|
|
|
+import { useRouter } from 'vue-router';
|
|
|
+import { onMounted, ref } from 'vue';
|
|
|
+import axios from 'axios';
|
|
|
+
|
|
|
+const apiUrls = import.meta.env.VITE_API_URLS.split(',');
|
|
|
+let currentIndex = ref(0); // 使用Vue的ref来确保响应性
|
|
|
+
|
|
|
const router = useRouter();
|
|
|
-
|
|
|
-const isLoggedIn = ref(false);
|
|
|
-const info = ref('正在验证登录信息...')
|
|
|
-async function tryLoginWithUrls(apiUrls, code, state, index = 0) {
|
|
|
- if (index >= apiUrls.length) {
|
|
|
- // 所有URL都尝试过了,抛出错误或处理失败
|
|
|
- throw new Error('所有服务器登录尝试均失败');
|
|
|
- }
|
|
|
|
|
|
- const apiUrl = apiUrls[index];
|
|
|
- try {
|
|
|
- const response = await axios.post(`${apiUrl}/login`, { code, state });
|
|
|
- if (response.data.message === 'success') {
|
|
|
- isLoggedIn.value = true;
|
|
|
- await nextTick();
|
|
|
- await nextTick();
|
|
|
- const userData = response.data; // 提取用户数据
|
|
|
- navigateToAccount(userData);
|
|
|
- } else {
|
|
|
- // 可以选择在这里再次尝试,或者直接失败
|
|
|
- throw new Error('登录失败: 无效的响应数据');
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- console.error(`尝试登录服务器${apiUrl}失败:`, error);
|
|
|
- // 尝试下一个URL
|
|
|
- return tryLoginWithUrls(apiUrls, code, state, index + 1);
|
|
|
- }
|
|
|
-}
|
|
|
+function getCurrentApiUrl() {
|
|
|
+ const url = apiUrls[currentIndex.value]; // 使用.value来访问ref的值
|
|
|
+
|
|
|
+ // 如果当前索引已到达列表末尾,则重置为0
|
|
|
+ currentIndex.value = (currentIndex.value + 1) % apiUrls.length;
|
|
|
+
|
|
|
+ return url;
|
|
|
+}
|
|
|
+
|
|
|
onMounted(async () => {
|
|
|
- try {
|
|
|
- const queryString = window.location.search;
|
|
|
- const urlParams = new URLSearchParams(queryString);
|
|
|
- const code = urlParams.get('code');
|
|
|
- const state = urlParams.get('state');
|
|
|
+ // 从URL中获取code和scopes
|
|
|
+ const code = router.currentRoute.value.query.code;
|
|
|
+ const scopes = router.currentRoute.value.query.scopes;
|
|
|
|
|
|
- const apiUrls = import.meta.env.VITE_API_URLS.split(',');
|
|
|
- await tryLoginWithUrls(apiUrls, code, state);
|
|
|
- } catch (error) {
|
|
|
- info.value = '登录失败:' + error.message;
|
|
|
- console.error('最终登录失败:', error);
|
|
|
- }
|
|
|
-});
|
|
|
+ if (code && scopes) {
|
|
|
+ try {
|
|
|
+ let retries = 3; // 设置重试次数
|
|
|
+ let response;
|
|
|
+
|
|
|
+ while (retries--) {
|
|
|
+ try {
|
|
|
+ const apiUrl = getCurrentApiUrl();
|
|
|
+ const loginUrl = apiUrl + '/login';
|
|
|
+ response = await axios.post(loginUrl, { code, scopes });
|
|
|
+
|
|
|
+ // 如果请求成功,则跳出循环
|
|
|
+ break;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Request failed, retrying with next URL...', error);
|
|
|
+
|
|
|
+ // 如果重试次数用完,抛出错误
|
|
|
+ if (retries === 0) {
|
|
|
+ throw new Error('All API requests failed');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ localStorage.setItem('token', response.data.token);
|
|
|
+ // 重定向到用户主页
|
|
|
+ router.push('/account');
|
|
|
|
|
|
-// 跳转函数
|
|
|
-const navigateToAccount = async (userData) => {
|
|
|
- console.log("userData:",userData)
|
|
|
- eventBus.emit('login-user-data', userData.data)
|
|
|
- await router.push({
|
|
|
- name: 'Account', // 假设你在路由配置中给 account.vue 设置了这个名字
|
|
|
- // params: {
|
|
|
- // userData: userData, // 将用户数据作为参数传递
|
|
|
- // },
|
|
|
- });
|
|
|
-};
|
|
|
+ } catch (error) {
|
|
|
+ console.error('An error occurred:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+});
|
|
|
</script>
|
|
|
|
|
|
<style lang="css" scoped>
|