From f99ba302c69b26577681d3a27d4e6d531c7cf591 Mon Sep 17 00:00:00 2001 From: "YOMO.LEE" <75362129@qq.com> Date: Wed, 30 Jul 2025 18:04:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=BE=AE=E8=BD=AF?= =?UTF-8?q?=E7=A4=BE=E4=BA=A4=E7=99=BB=E5=BD=95=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A4=BE=E4=BA=A4=E7=99=BB=E5=BD=95=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E5=92=8C=E5=9B=9E=E8=B0=83=E5=9C=B0=E5=9D=80=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/login/index.ts | 26 +++++++++++++++++-- src/api/system/user/socialUser.ts | 7 +++++- src/utils/constants.ts | 6 +++++ src/views/Login/components/LoginForm.vue | 21 ++++++++++++---- src/views/Profile/components/UserSocial.vue | 28 +++++++++++++++++++-- 5 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/api/login/index.ts b/src/api/login/index.ts index 7d7d407b4..d735737d2 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -53,10 +53,27 @@ export const smsLogin = (data: SmsLoginVO) => { // 社交快捷登录,使用 code 授权码 export function socialLogin(type: string, code: string, state: string) { + const message = useMessage() + + // 如果 type 为空,尝试从 sessionStorage 获取(主要用于微软平台) + let finalType = type + if (!finalType) { + const storedType = sessionStorage.getItem('social_bind_type') + // 只有微软平台需要从 sessionStorage 获取 type,其他平台不需要 + if (storedType && storedType === '40') { + finalType = storedType + // 使用后清除,避免影响后续操作 + sessionStorage.removeItem('social_bind_type') + } else { + message.error('无法获取社交平台类型信息') + return + } + } + return request.post({ url: '/system/auth/social-login', data: { - type, + type: finalType, code, state } @@ -66,7 +83,12 @@ export function socialLogin(type: string, code: string, state: string) { // 社交授权的跳转 export const socialAuthRedirect = (type: number, redirectUri: string) => { return request.get({ - url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri + url: + '/system/auth/social-auth-redirect?type=' + + type + + '&redirectUri=' + + redirectUri + + '&callbackType=login' }) } // 获取验证图片以及 token diff --git a/src/api/system/user/socialUser.ts b/src/api/system/user/socialUser.ts index 79f4d4027..00d09c0e4 100644 --- a/src/api/system/user/socialUser.ts +++ b/src/api/system/user/socialUser.ts @@ -26,6 +26,11 @@ export const socialUnbind = (type, openid) => { // 社交授权的跳转 export const socialAuthRedirect = (type, redirectUri) => { return request.get({ - url: '/system/auth/social-auth-redirect?type=' + type + '&redirectUri=' + redirectUri + url: + '/system/auth/social-auth-redirect?type=' + + type + + '&redirectUri=' + + redirectUri + + '&callbackType=bind' }) } diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 91e182721..ed88042dd 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -61,6 +61,12 @@ export const SystemUserSocialTypeEnum = { type: 30, source: 'wechat_enterprise', img: 'https://s1.ax1x.com/2022/05/22/OzMrzn.png' + }, + MICROSOFT: { + title: '微软', + type: 40, + source: 'microsoft', + img: 'https://upload.wikimedia.org/wikipedia/commons/4/44/Microsoft_logo.svg' // 可换成你喜欢的微软logo } } diff --git a/src/views/Login/components/LoginForm.vue b/src/views/Login/components/LoginForm.vue index fa6612362..68fa42da1 100644 --- a/src/views/Login/components/LoginForm.vue +++ b/src/views/Login/components/LoginForm.vue @@ -203,7 +203,9 @@ const socialList = [ { icon: 'ant-design:wechat-filled', type: 30 }, { icon: 'ant-design:dingtalk-circle-filled', type: 20 }, { icon: 'ant-design:github-filled', type: 0 }, - { icon: 'ant-design:alipay-circle-filled', type: 0 } + { icon: 'ant-design:alipay-circle-filled', type: 0 }, + // 新增微软三方登录按钮,type请与后端约定(如40) + { icon: 'ant-design:windows-filled', type: 40 } ] // 获取验证码 @@ -314,10 +316,19 @@ const doSocialLogin = async (type: number) => { // 计算 redirectUri // 注意: type、redirect 需要先 encode 一次,否则钉钉回调会丢失。 // 配合 social-login.vue#getUrlValue() 使用 - const redirectUri = - location.origin + - '/social-login?' + - encodeURIComponent(`type=${type}&redirect=${redirect.value || '/'}`) + // 对于微软平台,不在回调地址中添加查询参数 + let redirectUri: string + if (type === 40) { + // 微软平台使用纯回调地址,但将 type 存储到 sessionStorage + sessionStorage.setItem('social_bind_type', type.toString()) + redirectUri = location.origin + '/social-login' + } else { + // 其他平台可以在回调地址中添加 type 参数 + redirectUri = + location.origin + + '/social-login?' + + encodeURIComponent(`type=${type}&redirect=${redirect.value || '/'}`) + } // 进行跳转 window.location.href = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri)) diff --git a/src/views/Profile/components/UserSocial.vue b/src/views/Profile/components/UserSocial.vue index b7f955be8..5af3e751a 100644 --- a/src/views/Profile/components/UserSocial.vue +++ b/src/views/Profile/components/UserSocial.vue @@ -63,7 +63,22 @@ const bindSocial = () => { if (!code) { return } - socialBind(type, code, state).then(() => { + + // 如果 type 为空,尝试从 sessionStorage 获取(主要用于微软平台) + let finalType = type + if (!finalType) { + const storedType = sessionStorage.getItem('social_bind_type') + if (storedType) { + finalType = storedType + // 使用后清除,避免影响后续操作 + sessionStorage.removeItem('social_bind_type') + } else { + message.error('无法获取社交平台类型信息') + return + } + } + + socialBind(finalType, code, state).then(() => { message.success('绑定成功') emit('update:activeName', 'userSocial') }) @@ -77,7 +92,16 @@ function getUrlValue(key: string): string { const bind = (row) => { // 双层 encode 解决钉钉回调 type 参数丢失的问题 - const redirectUri = location.origin + '/user/profile?' + encodeURIComponent(`type=${row.type}`) + let redirectUri: string + // 对于微软平台,不在回调地址中添加查询参数 + if (row.type === 40) { + // 微软平台使用纯回调地址,但将 type 存储到 sessionStorage + sessionStorage.setItem('social_bind_type', row.type.toString()) + redirectUri = location.origin + '/user/profile' + } else { + // 其他平台可以在回调地址中添加 type 参数 + redirectUri = location.origin + '/user/profile?' + encodeURIComponent(`type=${row.type}`) + } // 进行跳转 socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => { window.location.href = res