|
|
@@ -1,4 +1,4 @@
|
|
|
-import { useEffect, useState } from 'react';
|
|
|
+import { useEffect, useRef, useState } from 'react';
|
|
|
import { Spin, message } from 'antd';
|
|
|
import { history } from 'umi';
|
|
|
import { request } from 'umi';
|
|
|
@@ -11,10 +11,23 @@ import { DEFAULT_ENTER_PAGE } from '@/config/settings';
|
|
|
*/
|
|
|
const SSOCallback = () => {
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
+ const processedRef = useRef(false);
|
|
|
|
|
|
useEffect(() => {
|
|
|
const exchangeCode = async () => {
|
|
|
- const params = new URLSearchParams(window.location.search);
|
|
|
+ // 防止重复执行
|
|
|
+ if (processedRef.current) return;
|
|
|
+ processedRef.current = true;
|
|
|
+
|
|
|
+ // 从完整 URL 中提取 code(兼容 hash 路由模式)
|
|
|
+ // URL 可能是 /auth/callback?code=xxx#/login 或 /auth/callback#/auth/callback?code=xxx
|
|
|
+ const href = window.location.href;
|
|
|
+ const hashIndex = href.indexOf('#');
|
|
|
+ const urlBeforeHash = hashIndex > -1 ? href.substring(0, hashIndex) : href;
|
|
|
+ const queryIndex = urlBeforeHash.indexOf('?');
|
|
|
+ const queryString = queryIndex > -1 ? urlBeforeHash.substring(queryIndex) : '';
|
|
|
+
|
|
|
+ const params = new URLSearchParams(queryString);
|
|
|
const code = params.get('code');
|
|
|
const error = params.get('error');
|
|
|
|
|
|
@@ -49,8 +62,8 @@ const SSOCallback = () => {
|
|
|
|
|
|
message.success('登录成功');
|
|
|
|
|
|
- // 跳转首页
|
|
|
- history.push(DEFAULT_ENTER_PAGE.user);
|
|
|
+ // 跳转首页 - 使用 replace 避免 back 回到 callback
|
|
|
+ history.replace(DEFAULT_ENTER_PAGE.user);
|
|
|
} else {
|
|
|
message.error(result.message || '登录失败');
|
|
|
history.push(DEFAULT_ENTER_PAGE.login);
|