fix(@vben/web-antd): 修复企业微信绑定回调 type 参数丢失问题

redirect_uri 中的查询参数会被企业微信 OAuth 截断,
改用 sessionStorage 暂存社交平台类型,同时增加绑定
错误处理和类型匹配的健壮性。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
lzh
2026-03-23 17:19:03 +08:00
parent 429e52007f
commit 91eec65734

View File

@@ -44,7 +44,7 @@ const allBindList = computed<SocialBindItem[]>(() => {
.filter((social) => !HIDDEN_SOCIAL_TYPES.has(social.type))
.map((social) => {
const socialUser = bindList.value.find(
(item) => item.type === social.type,
(item) => Number(item.type) === social.type,
);
return {
...social,
@@ -77,7 +77,9 @@ async function onBind(bind: SocialBindItem) {
return;
}
try {
const redirectUri = `${location.origin}/profile?${encodeURIComponent(`type=${type}`)}`;
// 将 type 存入 sessionStorage避免放入 redirect_uri 导致参数被截断
sessionStorage.setItem('socialBindType', String(type));
const redirectUri = `${location.origin}/profile`;
window.location.href = await socialAuthRedirect(type, redirectUri);
} catch (error) {
console.error('社交绑定处理失败:', error);
@@ -86,17 +88,30 @@ async function onBind(bind: SocialBindItem) {
/** 监听路由变化,处理社交绑定回调 */
async function bindSocial() {
const type = Number(getUrlValue('type'));
const code = route.query.code as string;
const state = route.query.state as string;
if (!code) {
return;
}
await socialBind({ type, code, state });
message.success('绑定成功');
emit('update:activeName', 'userSocial');
await loadBindList();
window.history.replaceState({}, '', location.pathname);
// 优先从 sessionStorage 获取 type兼容从 URL 参数获取
const storedType = sessionStorage.getItem('socialBindType');
const type = storedType ? Number(storedType) : Number(getUrlValue('type'));
sessionStorage.removeItem('socialBindType');
if (!type) {
message.error('绑定失败:缺少社交平台类型');
return;
}
try {
await socialBind({ type, code, state });
message.success('绑定成功');
emit('update:activeName', 'userSocial');
await loadBindList();
} catch (error) {
console.error('社交绑定失败:', error);
message.error('绑定失败,请重试');
} finally {
window.history.replaceState({}, '', location.pathname);
}
}
/** 初始化 */