fix(mp): setTimeout 无清理修复 — useSafeTimeout hook + 10 页面接入

新增 useSafeTimeout hook,页面隐藏时自动清理所有定时器。
10 个页面接入:daily-monitoring、exchange、family-add、
health/input、prescription detail/create、dialysis detail/create、
appointment detail/create。所有 fire-and-forget setTimeout 替换为
safeSetTimeout,避免页面切走后定时器回调在错误上下文执行。
This commit is contained in:
iven
2026-05-15 00:38:23 +08:00
parent 74bffb4878
commit fed1759985
11 changed files with 57 additions and 14 deletions

View File

@@ -10,6 +10,7 @@ import type { PointsProduct } from '../../../services/points';
import { usePointsStore } from '../../../stores/points';
import Loading from '../../../components/Loading';
import { useElderClass } from '../../../hooks/useElderClass';
import { useSafeTimeout } from '@/hooks/useSafeTimeout';
import './index.scss';
const TYPE_INITIAL: Record<string, string> = {
@@ -37,6 +38,7 @@ export default function ExchangeConfirm() {
const refreshPoints = usePointsStore((s) => s.refresh);
const [loading, setLoading] = useState(true);
const [submitting, setSubmitting] = useState(false);
const { safeSetTimeout } = useSafeTimeout();
useThrottledDidShow(() => {
Taro.setNavigationBarTitle({ title: '确认兑换' });
@@ -48,7 +50,7 @@ export default function ExchangeConfirm() {
const productId = instance.router?.params?.product_id;
if (!productId) {
Taro.showToast({ title: '参数错误', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
safeSetTimeout(() => Taro.navigateBack(), 1500);
return;
}
@@ -61,13 +63,13 @@ export default function ExchangeConfirm() {
const found = productRes.data.find((p) => p.id === productId);
if (!found) {
Taro.showToast({ title: '商品不存在', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
safeSetTimeout(() => Taro.navigateBack(), 1500);
return;
}
setProduct(found);
} catch {
Taro.showToast({ title: '加载失败', icon: 'none' });
setTimeout(() => Taro.navigateBack(), 1500);
safeSetTimeout(() => Taro.navigateBack(), 1500);
} finally {
setLoading(false);
}
@@ -96,7 +98,7 @@ export default function ExchangeConfirm() {
const order = await exchangeProduct(product.id);
Taro.showToast({ title: '兑换成功', icon: 'success', duration: 2000 });
setTimeout(() => {
safeSetTimeout(() => {
Taro.showModal({
title: '兑换成功',
content: `核销码: ${order.qr_code}\n请凭此码到前台核销`,