fix: 保留小程序登录上下文

This commit is contained in:
2026-06-05 20:58:40 +08:00
parent 5a6d69bebe
commit 6a03575d68
7 changed files with 112 additions and 17 deletions

View File

@@ -31,6 +31,7 @@ import {
getCurrentAuthUser,
getPublicAuthUserById,
liftAuthRiskBlock,
isWechatMiniProgramWebViewRuntime,
loginWithPhoneCode,
logoutAllAuthSessions,
redeemRegistrationInviteCode,
@@ -80,6 +81,7 @@ function createWindowMock(overrides: Record<string, unknown> = {}) {
describe('authService', () => {
beforeEach(() => {
vi.unstubAllGlobals();
vi.clearAllMocks();
vi.stubGlobal('window', createWindowMock());
clearStoredAccessToken({ emit: false });
@@ -428,6 +430,26 @@ describe('authService', () => {
});
});
it('detects mini program user agent before the WeChat bridge is ready', () => {
vi.stubGlobal('navigator', {
userAgent:
'Mozilla/5.0 iPhone MicroMessenger/8.0.49 NetType/WIFI Language/zh_CN miniProgram',
});
vi.stubGlobal(
'window',
createWindowMock({
location: {
pathname: '/',
hash: '',
search: '',
assign: vi.fn(),
},
}),
);
expect(isWechatMiniProgramWebViewRuntime()).toBe(true);
});
it('waits for an existing WeChat JS SDK script before opening the native auth page', async () => {
const navigateTo = vi.fn((options: { url: string; success?: () => void }) => {
options.success?.();

View File

@@ -90,9 +90,15 @@ export function isWechatMiniProgramWebViewRuntime() {
}
const params = new URLSearchParams(window.location.search || '');
const userAgent =
typeof navigator === 'undefined' ? '' : navigator.userAgent || '';
const normalizedUserAgent = userAgent.toLowerCase();
return (
params.get('clientRuntime') === 'wechat_mini_program' ||
params.get('clientType') === 'mini_program' ||
(normalizedUserAgent.includes('micromessenger') &&
normalizedUserAgent.includes('miniprogram')) ||
Boolean(window.wx?.miniProgram?.postMessage)
);
}