From 0e556314c63ce6647c8e09208d60922590144922 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期三, 30 九月 2026 15:24:31 +0800
Subject: [PATCH] fix(auth): 修复生产 hash 路由下社交登录/绑定回跳不跳转

---
 src/views/_core/profile/modules/user-social.vue  |    4 +++-
 src/views/_core/authentication/login.vue         |    6 +++++-
 src/packages/@core/base/shared/src/utils/util.ts |    6 +++++-
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/packages/@core/base/shared/src/utils/util.ts b/src/packages/@core/base/shared/src/utils/util.ts
index 97d88b0..bb4f84e 100644
--- a/src/packages/@core/base/shared/src/utils/util.ts
+++ b/src/packages/@core/base/shared/src/utils/util.ts
@@ -67,7 +67,11 @@
   urlStr: string = location.href,
 ): string {
   if (!urlStr || !key) return '';
-  const url = new URL(decodeURIComponent(urlStr));
+  // hash 璺敱锛堢敓浜ф瀯寤� VITE_ROUTER_HISTORY=hash锛変笅锛�? 鍚庨潰鐨勫弬鏁拌惤鍦� URL 鐨� fragment 閲岋紝
+  // URL.searchParams 璇讳笉鍒帮紝鍙傛暟浼氳璇垽鎴�"涓㈠け"銆傝繖閲屽厛鎽樻帀 # 鍐嶈В鏋愶紱
+  // history 妯″紡涓嬪湴鍧�閲屾病鏈� #锛岃蛋鍘熼�昏緫锛岃涓轰笉鍙樸��
+  const normalized = urlStr.includes('#') ? urlStr.replace('#', '') : urlStr;
+  const url = new URL(decodeURIComponent(normalized));
   return url.searchParams.get(key) ?? '';
 }
 
diff --git a/src/views/_core/authentication/login.vue b/src/views/_core/authentication/login.vue
index 3e2356d..f78f508 100644
--- a/src/views/_core/authentication/login.vue
+++ b/src/views/_core/authentication/login.vue
@@ -101,7 +101,11 @@
     wxcpLoading.value = true;
     // 娉ㄦ剰锛歵ype 闇�瑕佸厛 encode 涓�娆★紝閰嶅悎 social-login 椤甸潰鐨� getUrlValue() 浣跨敤锛�
     // 鍚﹀垯浼佷笟寰俊鍥炶烦鍚� type 鍙傛暟浼氳鍚炴帀锛屽鑷存棤娉曡瘑鍒ぞ浜ょ被鍨�
-    const redirectUri = `${location.origin}/auth/social-login?${encodeURIComponent(
+    // 鍥炶皟鍦板潃蹇呴』甯� hash 鍓嶇紑锛氱敓浜х敤 hash 璺敱锛圴ITE_ROUTER_HISTORY=hash锛夛紝
+    // 灏戜簡 # 浼佷笟寰俊鍥炶烦鍚� vue-router 浼氳В鏋愬埌 /锛岃惤鍒扮櫥褰曢〉鑰屼笉鏄� social-login 椤甸潰
+    const hashPrefix =
+      import.meta.env.VITE_ROUTER_HISTORY === 'hash' ? '/#' : '';
+    const redirectUri = `${location.origin}${hashPrefix}/auth/social-login?${encodeURIComponent(
       `type=${WXCP_SOCIAL_TYPE}`,
     )}`;
     window.location.href = await socialAuthRedirect(
diff --git a/src/views/_core/profile/modules/user-social.vue b/src/views/_core/profile/modules/user-social.vue
index d0925ba..4c96f99 100644
--- a/src/views/_core/profile/modules/user-social.vue
+++ b/src/views/_core/profile/modules/user-social.vue
@@ -122,7 +122,9 @@
   try {
     // 璁$畻 redirectUri
     // tricky: type 闇�瑕佸厛 encode 涓�娆★紝鍚﹀垯閽夐拤鍥炶皟浼氫涪澶便�傞厤鍚� getUrlValue() 浣跨敤
-    const redirectUri = `${location.origin}/profile?${encodeURIComponent(`type=${type}`)}`;
+    // hash 璺敱锛堢敓浜ф瀯寤猴級涓嬪繀椤诲甫 # 鍓嶇紑锛屽惁鍒欏洖璺冲悗 vue-router 瑙f瀽鍒� /锛岃繘涓嶄簡缁戝畾娴佺▼
+    const hashPrefix = import.meta.env.VITE_ROUTER_HISTORY === 'hash' ? '/#' : '';
+    const redirectUri = `${location.origin}${hashPrefix}/profile?${encodeURIComponent(`type=${type}`)}`;
 
     // 杩涜璺宠浆
     window.location.href = await socialAuthRedirect(type, redirectUri);

--
Gitblit v1.9.3