From 74d7efec1fed52dc1541cfe6195ce98c8fdb02b0 Mon Sep 17 00:00:00 2001 From: iven Date: Fri, 24 Apr 2026 12:20:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(miniprogram):=20=E6=B7=BB=E5=8A=A0=E5=85=A8?= =?UTF-8?q?=E5=B1=80=20ErrorBoundary=EF=BC=8C=E4=BF=AE=E5=A4=8D=20tryRefre?= =?UTF-8?q?shToken=20=E9=9D=99=E9=BB=98=E5=90=9E=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/miniprogram/src/app.tsx | 3 +- .../src/components/ErrorBoundary/index.tsx | 38 +++++++++++++++++++ apps/miniprogram/src/services/request.ts | 4 +- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 apps/miniprogram/src/components/ErrorBoundary/index.tsx diff --git a/apps/miniprogram/src/app.tsx b/apps/miniprogram/src/app.tsx index e0a0931..35b8829 100644 --- a/apps/miniprogram/src/app.tsx +++ b/apps/miniprogram/src/app.tsx @@ -1,8 +1,9 @@ import { PropsWithChildren } from 'react'; +import ErrorBoundary from './components/ErrorBoundary'; import './app.scss'; function App({ children }: PropsWithChildren>) { - return children; + return {children}; } export default App; diff --git a/apps/miniprogram/src/components/ErrorBoundary/index.tsx b/apps/miniprogram/src/components/ErrorBoundary/index.tsx new file mode 100644 index 0000000..a0003d5 --- /dev/null +++ b/apps/miniprogram/src/components/ErrorBoundary/index.tsx @@ -0,0 +1,38 @@ +import React, { Component } from 'react'; +import { View, Text } from '@tarojs/components'; + +interface Props { + children: React.ReactNode; +} + +interface State { + hasError: boolean; +} + +export default class ErrorBoundary extends Component { + constructor(props: Props) { + super(props); + this.state = { hasError: false }; + } + + static getDerivedStateFromError(): State { + return { hasError: true }; + } + + componentDidCatch(error: Error, info: React.ErrorInfo) { + console.error('[ErrorBoundary]', error, info.componentStack); + } + + render() { + if (this.state.hasError) { + return ( + + 😵 + 页面出了点问题 + 请返回重试 + + ); + } + return this.props.children; + } +} diff --git a/apps/miniprogram/src/services/request.ts b/apps/miniprogram/src/services/request.ts index 367c6c8..a5a5cfe 100644 --- a/apps/miniprogram/src/services/request.ts +++ b/apps/miniprogram/src/services/request.ts @@ -33,8 +33,8 @@ async function tryRefreshToken(): Promise { Taro.setStorageSync('refresh_token', res.data.data.refresh_token); return true; } - } catch { - // ignore + } catch (err) { + console.error('[tryRefreshToken] token 刷新失败:', err); } Taro.removeStorageSync('access_token'); Taro.removeStorageSync('refresh_token');