feat: 集成错误处理与网络状态管理

- 在 Providers 组件中引入 ErrorProvider 以处理全局错误状态
- 更新 PlayerAppShell 组件的注释,说明网络状态横幅的用途
- 在 lotteryHttp 中添加对 500、502、503 错误的处理,更新全局错误状态
- 导出 useNetworkStatus 和 useIsOffline 钩子以支持网络状态管理
This commit is contained in:
2026-05-13 15:14:02 +08:00
parent 1e7a06dc86
commit c8f8f90515
12 changed files with 629 additions and 10 deletions

72
src/stores/error-store.ts Normal file
View File

@@ -0,0 +1,72 @@
"use client";
import { create } from "zustand";
// 颜色常量
export const ERROR_COLORS = {
success: "#52c41a",
error: "#ff4d4f",
warning: "#faad14",
neutral: "#d9d9d9",
} as const;
type ErrorType = "network" | "server" | null;
interface ErrorState {
// 网络断开状态
isOffline: boolean;
setIsOffline: (value: boolean) => void;
// 服务器错误状态 (500)
isServerError: boolean;
serverErrorMessage: string;
setServerError: (isError: boolean, message?: string) => void;
clearServerError: () => void;
// 当前主要错误类型优先级server > network
currentErrorType: ErrorType;
}
export const useErrorStore = create<ErrorState>((set, get) => ({
// 初始状态
isOffline: typeof window !== "undefined" ? !navigator.onLine : false,
isServerError: false,
serverErrorMessage: "服务器暂时不可用,请稍后重试",
currentErrorType: null,
// Actions
setIsOffline: (value: boolean) => {
set({ isOffline: value });
// 更新当前错误类型
const { isServerError } = get();
if (isServerError) {
set({ currentErrorType: "server" });
} else if (value) {
set({ currentErrorType: "network" });
} else {
set({ currentErrorType: null });
}
},
setServerError: (isError: boolean, message?: string) => {
set({
isServerError: isError,
serverErrorMessage: message || "服务器暂时不可用,请稍后重试",
});
// 更新当前错误类型 - 服务器错误优先级更高
if (isError) {
set({ currentErrorType: "server" });
} else {
const { isOffline } = get();
set({ currentErrorType: isOffline ? "network" : null });
}
},
clearServerError: () => {
const { isOffline } = get();
set({
isServerError: false,
currentErrorType: isOffline ? "network" : null,
});
},
}));