123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import type { Method } from "alova";
- import router from "@/router";
- // Custom error class for API errors
- export class ApiError extends Error {
- code: number;
- data?: any;
- constructor(message: string, code: number, data?: any) {
- super(message);
- this.name = "ApiError";
- this.code = code;
- this.data = data;
- }
- }
- // Define a type for the expected API response structure
- interface ApiResponse {
- code?: number;
- message?: string;
- result?: any;
- success?: boolean;
- total?: number;
- more?: boolean;
- }
- // Handle successful responses
- export async function handleAlovaResponse(
- response:
- | UniApp.RequestSuccessCallbackResult
- | UniApp.UploadFileSuccessCallbackResult
- | UniApp.DownloadSuccessData,
- ) {
- return new Promise((resolve, reject) => {
- const globalToast = useGlobalToast();
- // Extract status code and data from UniApp response
- const { statusCode, data } =
- response as UniNamespace.RequestSuccessCallbackResult;
- // 处理401/403错误(如果不是在handleAlovaResponse中处理的)
- if (
- typeof data === "object" &&
- data !== null &&
- "code" in data &&
- (data.code === 401 || data.code === 403)
- ) {
- // 如果是未授权错误,清除用户信息并跳转到登录页
- uni.showToast({
- title: "登录已过期,请重新登录!",
- duration: 2000,
- icon: "none",
- });
- const timer = setTimeout(() => {
- clearTimeout(timer);
- router.replaceAll({ name: "login" });
- }, 500);
- throw new ApiError("登录已过期,请重新登录!", statusCode, data);
- }
- // Handle HTTP error status codes
- if (
- typeof data === "object" &&
- data !== null &&
- "code" in data &&
- data.code >= 400
- ) {
- uni.showToast({
- title: (data as any).message,
- duration: 2000,
- icon: "none",
- });
- reject(`${(data as any).message}`);
- }
- // The data is already parsed by UniApp adapter
- const json = data as ApiResponse;
- // Log response in development
- if (import.meta.env.MODE === "development") {
- console.log("[Alova Response]", json);
- }
- // Return data for successful responses
- resolve(json.result);
- });
- }
- // Handle request errors
- export function handleAlovaError(error: any, method: Method) {
- const globalToast = useGlobalToast();
- // Log error in development
- if (import.meta.env.MODE === "development") {
- console.error("[Alova Error]", error, method);
- }
- if (error.name === "Error") {
- uni.showToast({
- title: "服务器异常,请稍后再试",
- icon: "none",
- });
- }
- // 处理401/403错误(如果不是在handleAlovaResponse中处理的)
- if (error instanceof ApiError && (error.code === 401 || error.code === 403)) {
- // 如果是未授权错误,清除用户信息并跳转到登录页
- globalToast.error({ msg: "登录已过期,请重新登录!", duration: 500 });
- const timer = setTimeout(() => {
- clearTimeout(timer);
- router.replaceAll({ name: "login" });
- }, 500);
- throw new ApiError("登录已过期,请重新登录!", error.code, error.data);
- }
- // Handle different types of errors
- if (error.name === "NetworkError") {
- globalToast.error("网络错误,请检查您的网络连接");
- } else if (error.name === "TimeoutError") {
- globalToast.error("请求超时,请重试");
- } else if (error instanceof ApiError) {
- globalToast.error(error.message || "请求失败");
- } else {
- globalToast.error("发生意外错误");
- }
- throw error;
- }
|