refactor(social): 支付宝登录/绑定统一走 SocialApi

去掉 AlipayAuthApi 与 renderSocialAuth 特判,回调页仅归一 auth_code/code。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
DaxPay Dev
2026-07-09 17:08:33 +08:00
parent 9fc4bde2a9
commit b6927ffe1e
5 changed files with 11 additions and 70 deletions

View File

@@ -173,53 +173,3 @@ export interface SocialEnabledPlatform {
/** 平台编码(weChat/weCom/qq/github/gitee/feishu/dingTalk/douyin/alipay) */
source: string;
}
/**
* 支付宝授权登录专用 API
*
* 支付宝非标准 OAuth2(走 alipay.system.oauth.token), 不复用 JustAuth 的 SocialApi.render/exchangeLogin,
* 使用独立的 render/exchange 端点。配置走平台级 PlatformAlipayAuthConfigApi。
*/
export const AlipayAuthApi = {
/**
* 生成支付宝授权地址(前端拿到后 location.href 跳转)
* @param client 终端编码(admin/merchant)
* @param mode 授权场景: BIND(已登录绑定) | LOGIN(未登录登录), 不传按登录态判断
*/
render(client: string = 'admin', mode?: string): Promise<Result<string>> {
return defHttp.get({ url: '/social/alipay/render', params: { client, mode } });
},
/**
* 支付宝授权码兑换(LOGIN/BIND 由 mode 决定)
* @param authCode 支付宝回调回传的 auth_code
* @param state render 阶段生成的 state
* @param client 终端编码
* @param mode 授权场景
*/
exchange(
authCode: string,
state: string,
client: string = 'admin',
mode?: string,
): Promise<Result<SocialExchangeResult>> {
return defHttp.post({
url: '/social/alipay/exchange',
params: { authCode, state, client, mode },
});
},
};
/**
* 统一生成三方授权地址
* 支付宝走专用端点, 其余走 JustAuth 标准 render
*/
export function renderSocialAuth(
source: string,
client: string = 'admin',
mode?: string,
): Promise<Result<string>> {
return source === 'alipay'
? AlipayAuthApi.render(client, mode)
: SocialApi.render(source, client, mode);
}

View File

@@ -3,7 +3,7 @@
import { $t } from '@vben/locales';
import { renderSocialAuth, SocialApi } from '#/api/iam/social.api';
import { SocialApi } from '#/api/iam/social.api';
import { SocialLogo } from '#/components/social';
defineOptions({ name: 'AuthThirdPartyPanel' });
@@ -35,8 +35,7 @@
}
/**
* 点击三方图标, 获取授权地址并跳转
* 支付宝走专用端点(非标准 OAuth2), 其余走 JustAuth 标准 render
* 点击三方图标, 获取授权地址并跳转(含支付宝, 统一走 SocialApi.render)
*/
async function handleSocialLogin(source: string) {
// 仅登录场景校验协议勾选,提示由父表单红字显示(绑定场景不校验)
@@ -44,7 +43,7 @@
const ok = await props.ensureAgreement();
if (!ok) return;
}
const { data: url } = await renderSocialAuth(source, 'admin', props.mode);
const { data: url } = await SocialApi.render(source, 'admin', props.mode);
if (url) {
// 跳转到第三方授权页, 授权后由后端回调处理并重定向回前端 /oauth-callback
window.location.href = url;

View File

@@ -5,7 +5,7 @@
import { $t } from '@vben/locales';
import { useAccessStore } from '@vben/stores';
import { AlipayAuthApi, SocialApi } from '#/api/iam/social.api';
import { SocialApi } from '#/api/iam/social.api';
import { SocialLogo } from '#/components/social';
import { useMessage } from '#/hooks/useMessage';
import { HOME_PATH } from '#/router/routes';
@@ -42,8 +42,7 @@
/**
* 处理社交登录回调(仅登录场景)
* 标准平台: URL 含 code+state → SocialApi.exchangeLogin
* 支付宝: URL 含 auth_code+state → AlipayAuthApi.exchange(兼容 code)
* 支付宝回传 auth_code, 标准 OAuth 回传 code; 归一后统一走 SocialApi.exchangeLogin
*/
onMounted(async () => {
const { code, auth_code: authCode, state } = route.query;
@@ -55,10 +54,7 @@
return;
}
try {
const res =
source.value === 'alipay'
? await AlipayAuthApi.exchange(oauthCode, state as string, 'admin', 'LOGIN')
: await SocialApi.exchangeLogin(oauthCode, state as string, source.value, 'admin');
const res = await SocialApi.exchangeLogin(oauthCode, state as string, source.value, 'admin');
await handleResult(res.data?.token, res.data?.error);
} catch {
message.error($t('_core.authentication.oauthProcessFailed'));

View File

@@ -4,7 +4,7 @@
import { $t } from '@vben/locales';
import { AlipayAuthApi, SocialApi } from '#/api/iam/social.api';
import { SocialApi } from '#/api/iam/social.api';
defineOptions({ name: 'SocialBindCallback' });
@@ -33,7 +33,7 @@
/**
* 处理绑定回调: 用授权码+state 调后端兑换, 成功后通知父窗口并关闭弹窗
* 支付宝回传 auth_code, 标准 OAuth 回传 code
* 支付宝回传 auth_code, 标准 OAuth 回传 code; 归一后统一走 SocialApi.exchangeBind
*/
onMounted(async () => {
const { code, auth_code: authCode, state } = route.query;
@@ -44,10 +44,7 @@
return;
}
try {
const res =
source.value === 'alipay'
? await AlipayAuthApi.exchange(oauthCode, state as string, 'admin', 'BIND')
: await SocialApi.exchangeBind(oauthCode, state as string, source.value, 'admin');
const res = await SocialApi.exchangeBind(oauthCode, state as string, source.value, 'admin');
if (res.data?.result === 'bind_success') {
success.value = true;
loading.value = false;

View File

@@ -5,7 +5,7 @@
import { $t } from '@vben/locales';
import { renderSocialAuth, SocialApi } from '#/api/iam/social.api';
import { SocialApi } from '#/api/iam/social.api';
import { SocialLogo } from '#/components/social';
import { useMessage } from '#/hooks/useMessage';
@@ -47,10 +47,9 @@
/**
* 绑定第三方账号(打开弹窗进行授权, 授权后弹窗自动关闭并通知刷新)
* 支付宝走专用端点, 与登录页 renderSocialAuth 一致
*/
async function handleBind(source: string) {
const { data: url } = await renderSocialAuth(source, 'admin', 'BIND');
const { data: url } = await SocialApi.render(source, 'admin', 'BIND');
if (url) {
window.open(url, 'social-bind', 'width=600,height=700');
}