fix(auth): transfer WeChat id on merge; stop false phone prompts
CI / verify (pull_request) Has been cancelled
CI / verify (pull_request) Has been cancelled
Merge used to drop wxOpenId so OAuth created a new guest without phone every time. Now migrate WeChat identity, recover legacy merged openIds, return accountMerged and force page reload, and only soft-prompt when phone is truly unbound.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import Taro from '@tarojs/taro';
|
||||
import { ClientApp } from '@dukang/shared-types';
|
||||
import { goLogin } from './auth-nav';
|
||||
import { goLogin, forceReloadAfterAccountMerge } from './auth-nav';
|
||||
|
||||
function resolveApiBase(): string {
|
||||
const origin =
|
||||
@@ -111,7 +111,13 @@ export async function request<T = unknown>(path: string, options: ReqOptions = {
|
||||
const stillCurrent = !!token && getToken() === token;
|
||||
if (stillCurrent) {
|
||||
clearAuth();
|
||||
if (!isOnLoginPage()) redirectToLogin();
|
||||
const mergedMsg = body?.message || '';
|
||||
if (/账号已合并/.test(mergedMsg)) {
|
||||
// 合并后旧 JWT 失效:强制刷新,不引导「重新登录」
|
||||
forceReloadAfterAccountMerge();
|
||||
} else if (!isOnLoginPage()) {
|
||||
redirectToLogin();
|
||||
}
|
||||
}
|
||||
throw new Error(body?.message || '登录已过期,请重新登录');
|
||||
}
|
||||
@@ -131,6 +137,8 @@ export function toast(title: string, icon: 'success' | 'error' | 'none' = 'none'
|
||||
export type SessionPayload = {
|
||||
accessToken: string;
|
||||
refreshToken?: string;
|
||||
phoneVerified?: boolean;
|
||||
accountMerged?: boolean;
|
||||
};
|
||||
|
||||
export type UserProfile = {
|
||||
|
||||
@@ -70,3 +70,46 @@ export function finishLoginNavigate(returnTo?: string) {
|
||||
|
||||
Taro.reLaunch({ url: '/pages/home/index' });
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号合并后强制整页刷新,避免旧会话栈 / 旧用户缓存继续提示绑定手机号。
|
||||
* H5:按当前路径推算出落地 URL 后 location.replace;小程序:reLaunch。
|
||||
*/
|
||||
export function forceReloadAfterAccountMerge(returnTo?: string) {
|
||||
const raw = (returnTo || '').trim();
|
||||
let target = '';
|
||||
try {
|
||||
target = raw ? decodeURIComponent(raw) : '';
|
||||
} catch {
|
||||
target = raw;
|
||||
}
|
||||
const pathOnly = target.split('?')[0];
|
||||
const safePath =
|
||||
pathOnly && pathOnly.startsWith('/pages/') && !pathOnly.includes('/pages/login')
|
||||
? target
|
||||
: '/pages/home/index';
|
||||
const launchPath = safePath.split('?')[0];
|
||||
|
||||
if (process.env.TARO_ENV === 'h5' && typeof window !== 'undefined') {
|
||||
const { origin, pathname, search } = window.location;
|
||||
const marker = '/pages/';
|
||||
const idx = pathname.indexOf(marker);
|
||||
let href: string;
|
||||
if (idx >= 0) {
|
||||
href = `${origin}${pathname.slice(0, idx)}${safePath}`;
|
||||
} else if (window.location.hash.includes('/pages/')) {
|
||||
href = `${origin}${pathname}${search}#${safePath}`;
|
||||
} else {
|
||||
const base = pathname.replace(/\/$/, '') || '';
|
||||
href = `${origin}${base}${safePath.startsWith('/') ? safePath : `/${safePath}`}`;
|
||||
}
|
||||
window.location.replace(href);
|
||||
return;
|
||||
}
|
||||
|
||||
if (TAB_PAGES.has(launchPath)) {
|
||||
Taro.reLaunch({ url: launchPath });
|
||||
return;
|
||||
}
|
||||
Taro.reLaunch({ url: safePath.startsWith('/') ? safePath : `/${safePath}` });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user