fix(mp): 导航/请求健壮性 — reLaunch 去重 + 失败降级

- navigateToLogin 添加去重 + reLaunch 失败降级 redirectTo
- request.ts safeReLaunch 添加目标页检测 + 失败降级
- 退出登录 reLaunch 失败降级 redirectTo
- DoctorTabBar / 首页医生端跳转 reLaunch 失败降级
- 网络恢复时正确清理 toast 状态和定时器
This commit is contained in:
iven
2026-05-25 13:45:12 +08:00
parent 485b9bb926
commit 1a376a255d
5 changed files with 39 additions and 5 deletions

View File

@@ -30,6 +30,7 @@ const OFFLINE_MAX_MS = 30_000;
let offlineDetectedAt = 0;
let offlineSuppressMs = OFFLINE_SUPPRESS_MS;
let networkToastShown = false;
let networkToastTimer: ReturnType<typeof setTimeout> | null = null;
let consecutiveNetErrors = 0;
function isOffline(): boolean {
@@ -44,7 +45,8 @@ function markOffline(): void {
if (!networkToastShown) {
networkToastShown = true;
Taro.showToast({ title: '网络异常,请检查连接', icon: 'none', duration: 2000 });
setTimeout(() => { networkToastShown = false; }, offlineSuppressMs);
if (networkToastTimer) clearTimeout(networkToastTimer);
networkToastTimer = setTimeout(() => { networkToastShown = false; networkToastTimer = null; }, offlineSuppressMs);
}
}
@@ -52,6 +54,8 @@ function clearOffline(): void {
offlineDetectedAt = 0;
offlineSuppressMs = OFFLINE_SUPPRESS_MS;
consecutiveNetErrors = 0;
if (networkToastTimer) { clearTimeout(networkToastTimer); networkToastTimer = null; }
networkToastShown = false;
}
function safeGet(key: string): string {
@@ -157,9 +161,15 @@ async function doRefresh(): Promise<boolean> {
let reLaunchPromise: Promise<void> | null = null;
function safeReLaunch(url: string): void {
// 已在目标页,跳过(防止 DevTools reLaunch bug
const pages = Taro.getCurrentPages();
const currentPath = pages[pages.length - 1]?.path || '';
if (currentPath.includes('pages/login')) return;
if (reLaunchPromise) return;
reLaunchPromise = Taro.reLaunch({ url }).then(() => {}, (err) => {
console.warn('[request] reLaunch failed:', err);
// reLaunch 失败时降级为 redirectTo
Taro.redirectTo({ url }).catch(() => {});
}).then(() => {
setTimeout(() => { reLaunchPromise = null; }, 2000);
});