fix(@vben/web-antd): 企业微信扫码登录适配 hash 路由模式
hash 路由下 OAuth 回调 code/state 在 URL query 中,Vue Router 读不到。登录流程同绑定流程一样通过 sessionStorage 中转参数, 路由守卫检测后重定向到 social-login 页面完成登录。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -50,22 +50,29 @@ function setupCommonGuard(router: Router) {
|
||||
*/
|
||||
function setupAccessGuard(router: Router) {
|
||||
// 一次性检查:hash 路由模式下,OAuth 回调的 code/state 在 URL query(?code=xxx)中,
|
||||
// Vue Router 读不到,需要转存到 sessionStorage 供个人中心绑定页面使用
|
||||
const pendingBind = sessionStorage.getItem('socialBindAction');
|
||||
if (pendingBind === 'bind') {
|
||||
const url = new URL(window.location.href);
|
||||
const code = url.searchParams.get('code');
|
||||
const state = url.searchParams.get('state');
|
||||
if (code) {
|
||||
sessionStorage.setItem('socialBindCode', code);
|
||||
sessionStorage.setItem('socialBindState', state || '');
|
||||
// Vue Router 读不到,需要转存到 sessionStorage
|
||||
const url = new URL(window.location.href);
|
||||
const oauthCode = url.searchParams.get('code');
|
||||
if (oauthCode) {
|
||||
const oauthState = url.searchParams.get('state') || '';
|
||||
const pendingBind = sessionStorage.getItem('socialBindAction');
|
||||
const pendingLogin = sessionStorage.getItem('socialLoginAction');
|
||||
if (pendingBind === 'bind') {
|
||||
// 绑定回调:转存参数供个人中心使用
|
||||
sessionStorage.setItem('socialBindCode', oauthCode);
|
||||
sessionStorage.setItem('socialBindState', oauthState);
|
||||
sessionStorage.removeItem('socialBindAction');
|
||||
// 清理 URL 中的 OAuth 参数
|
||||
url.searchParams.delete('code');
|
||||
url.searchParams.delete('state');
|
||||
url.searchParams.delete('appid');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
} else if (pendingLogin === 'login') {
|
||||
// 登录回调:转存参数供 social-login 页面使用
|
||||
sessionStorage.setItem('socialLoginCode', oauthCode);
|
||||
sessionStorage.setItem('socialLoginState', oauthState);
|
||||
sessionStorage.removeItem('socialLoginAction');
|
||||
}
|
||||
// 清理 URL 中的 OAuth 参数
|
||||
url.searchParams.delete('code');
|
||||
url.searchParams.delete('state');
|
||||
url.searchParams.delete('appid');
|
||||
window.history.replaceState({}, '', url.toString());
|
||||
}
|
||||
|
||||
router.beforeEach(async (to, from) => {
|
||||
@@ -74,7 +81,15 @@ function setupAccessGuard(router: Router) {
|
||||
const authStore = useAuthStore();
|
||||
const dictStore = useDictStore();
|
||||
|
||||
// 社交绑定回调:检测到待处理的绑定参数,重定向到个人中心处理
|
||||
// 社交登录回调:重定向到 social-login 页面处理
|
||||
if (
|
||||
sessionStorage.getItem('socialLoginCode') &&
|
||||
to.path !== '/auth/social-login'
|
||||
) {
|
||||
return '/auth/social-login';
|
||||
}
|
||||
|
||||
// 社交绑定回调:重定向到个人中心处理
|
||||
if (
|
||||
sessionStorage.getItem('socialBindCode') &&
|
||||
accessStore.accessToken &&
|
||||
|
||||
Reference in New Issue
Block a user