bdf80e577b
CI / verify (pull_request) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
131 lines
4.2 KiB
TypeScript
131 lines
4.2 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
|
|
const TAB_PAGES = new Set([
|
|
'/pages/home/index',
|
|
'/pages/stores/index',
|
|
'/pages/benefit/index',
|
|
'/pages/mine/index',
|
|
]);
|
|
|
|
let loginNavigationPending = false;
|
|
|
|
function isLoginPageActive(): boolean {
|
|
const pages = Taro.getCurrentPages();
|
|
const current = pages[pages.length - 1] as { route?: string } | undefined;
|
|
return !!current?.route?.includes('pages/login/');
|
|
}
|
|
|
|
function currentPagePath(): string {
|
|
const pages = Taro.getCurrentPages();
|
|
const cur = pages[pages.length - 1] as
|
|
| { route?: string; options?: Record<string, string | undefined> }
|
|
| undefined;
|
|
if (!cur?.route) return '';
|
|
const path = cur.route.startsWith('/') ? cur.route : `/${cur.route}`;
|
|
if (path.includes('/pages/login/')) return '';
|
|
const opts = cur.options ?? {};
|
|
const qs = Object.entries(opts)
|
|
.filter(([, v]) => v != null && v !== '')
|
|
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
|
|
.join('&');
|
|
return qs ? `${path}?${qs}` : path;
|
|
}
|
|
|
|
/** 跳转登录页;默认带回当前页作为 return */
|
|
export function goLogin(returnPath?: string, extras?: Record<string, string>) {
|
|
if (loginNavigationPending || isLoginPageActive()) return;
|
|
const returnTo = returnPath ?? currentPagePath();
|
|
const parts: string[] = [];
|
|
if (returnTo) parts.push(`return=${encodeURIComponent(returnTo)}`);
|
|
if (extras) {
|
|
for (const [key, value] of Object.entries(extras)) {
|
|
if (value) parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
}
|
|
}
|
|
const url = parts.length ? `/pages/login/index?${parts.join('&')}` : '/pages/login/index';
|
|
loginNavigationPending = true;
|
|
void Taro.navigateTo({ url })
|
|
.catch(() => Taro.redirectTo({ url }))
|
|
.finally(() => {
|
|
// 等路由栈稳定后再释放,拦截同一轮请求触发的重复登录跳转。
|
|
setTimeout(() => {
|
|
loginNavigationPending = false;
|
|
}, 500);
|
|
});
|
|
}
|
|
|
|
/** 登录成功后回到 return 页,或回退 / 首页 */
|
|
export function finishLoginNavigate(returnTo?: string) {
|
|
const raw = (returnTo || '').trim();
|
|
let target = '';
|
|
try {
|
|
target = raw ? decodeURIComponent(raw) : '';
|
|
} catch {
|
|
target = raw;
|
|
}
|
|
// 防止 return 仍指向登录页造成死循环
|
|
const pathOnly = target.split('?')[0];
|
|
if (!pathOnly || pathOnly.includes('/pages/login')) {
|
|
Taro.reLaunch({ url: '/pages/home/index' });
|
|
return;
|
|
}
|
|
|
|
if (TAB_PAGES.has(pathOnly)) {
|
|
Taro.switchTab({ url: pathOnly }).catch(() => {
|
|
Taro.reLaunch({ url: pathOnly });
|
|
});
|
|
return;
|
|
}
|
|
if (pathOnly.startsWith('/pages/')) {
|
|
Taro.redirectTo({ url: target }).catch(() => {
|
|
Taro.reLaunch({ url: pathOnly });
|
|
});
|
|
return;
|
|
}
|
|
|
|
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}` });
|
|
}
|