diff --git a/src/api/login/index.ts b/src/api/login/index.ts index 7d7d407b48854861c785e8471dacec2914be228f..d735737d2abdb70b502c607a75386581023bf320 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 79f4d40273eb330c588c09c3aae7e0ea97e98620..00d09c0e46d442be55b4e02848c9d9f69e665759 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 91e182721876d3c25a6d5bd4d2a5069efcc2d858..ed88042dd0b2fac35d207064b9b19d61d5e6b6e2 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 3c4e1d1a97039ee3a8c973d4ffcdcd35a8f76dbf..c530e9401b1fe34ffc0d5c49e9d1cb14246c41cb 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 } ] // 获取验证码 @@ -316,10 +318,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 8d2535471cbeb64ebd63f6e6c739d53f63b7717a..32acf29fc4882e11327bd395f7e40e13f85e9c56 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