-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b546188
commit 5324a5b
Showing
9 changed files
with
130 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* @Author: mulingyuer | ||
* @Date: 2025-01-16 15:34:55 | ||
* @LastEditTime: 2025-01-16 17:53:37 | ||
* @LastEditors: mulingyuer | ||
* @Description: 扩展axios类型 | ||
* @FilePath: \element-admin-template\src\request\axios.d.ts | ||
* 怎么可能会有bug!!! | ||
*/ | ||
import "axios"; // 必须确保模块扩展的上下文是在 Axios 模块内 | ||
|
||
declare module "axios" { | ||
interface AxiosRequestConfig { | ||
/** 是否允许失败重试 */ | ||
enableRetry?: boolean; | ||
/** 是否显示错误消息弹窗 */ | ||
showErrorMessage?: boolean; | ||
/** 取消请求是否显示错误消息,注意:`showErrorMessage` 优先级大于该设置。 | ||
* 这个配置你得在cancel的时候设置,例: | ||
* ```typescript | ||
* const CancelToken = axios.CancelToken; | ||
* const source = CancelToken.source(); | ||
* source.cancel("取消的原因", { showCancelErrorMessage: false }); | ||
* ``` | ||
*/ | ||
showCancelErrorMessage?: boolean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* @Author: mulingyuer | ||
* @Date: 2025-01-16 15:48:18 | ||
* @LastEditTime: 2025-01-16 17:44:46 | ||
* @LastEditors: mulingyuer | ||
* @Description: 请求辅助函数 | ||
* @FilePath: \element-admin-template\src\request\helper.ts | ||
* 怎么可能会有bug!!! | ||
*/ | ||
import { AxiosError } from "axios"; | ||
import axios from "axios"; | ||
import { isNetworkOrIdempotentRequestError } from "axios-retry"; | ||
|
||
/** 失败重试显示错误消息弹窗 */ | ||
export function showMaxRetryErrorMessage(error: AxiosError) { | ||
if (shouldShowErrorMessage(error)) { | ||
showErrorMessage(getErrorMessage(error)); | ||
} | ||
} | ||
|
||
/** 显示请求错误消息弹窗 */ | ||
export function showRequestErrorMessage(error: any) { | ||
if (isRetryError(error)) return; | ||
if (shouldShowErrorMessage(error)) { | ||
showErrorMessage(getErrorMessage(error)); | ||
} | ||
} | ||
|
||
/** 显示错误消息 */ | ||
export function showErrorMessage(message: string) { | ||
ElNotification({ | ||
type: "error", | ||
title: "请求失败", | ||
message: message ?? "请求失败" | ||
}); | ||
} | ||
|
||
/** 根据axios的config判断是否显示错误消息 */ | ||
function shouldShowErrorMessage(error: any) { | ||
console.log("🚀 ~ shouldShowErrorMessage ~ error:", error); | ||
if (!error.config) return true; | ||
const config = (error as AxiosError)?.config; | ||
const showErrorMessage = config?.showErrorMessage ?? true; | ||
const showCancelErrorMessage = config?.showCancelErrorMessage ?? true; | ||
// 取消请求 | ||
if (axios.isCancel(error)) { | ||
return showErrorMessage && showCancelErrorMessage; | ||
} | ||
return showErrorMessage; | ||
} | ||
|
||
/** 是否是失败重试错误 */ | ||
function isRetryError(error: any) { | ||
if (error instanceof AxiosError) { | ||
const isRetry = error.config?.enableRetry ?? true; | ||
return isRetry && isNetworkOrIdempotentRequestError(error); | ||
} | ||
return true; | ||
} | ||
|
||
/** 获取错误消息 */ | ||
function getErrorMessage(error: any): string { | ||
if (axios.isCancel(error)) return error.message ?? "请求被取消"; | ||
if (error instanceof AxiosError) return error.response?.data?.message ?? error.message; | ||
if (error instanceof Error) return error.message; | ||
|
||
return "未知错误"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
/* | ||
* @Author: mulingyuer | ||
* @Date: 2024-09-25 16:18:08 | ||
* @LastEditTime: 2024-09-25 16:54:57 | ||
* @LastEditTime: 2025-01-16 15:44:35 | ||
* @LastEditors: mulingyuer | ||
* @Description: 请求封装 | ||
* @FilePath: \spirit-app-microservice-admin\src\request\index.ts | ||
* @FilePath: \element-admin-template\src\request\index.ts | ||
* 怎么可能会有bug!!! | ||
*/ | ||
import { instance, setBaseUrl } from "./core"; | ||
import type { RequestConfig } from "./types"; | ||
export type * from "./types"; | ||
import { instance } from "./core"; | ||
import type { AxiosRequestConfig } from "axios"; | ||
|
||
/** 请求函数 */ | ||
export function request<T>(config: RequestConfig): Promise<T> { | ||
export function request<T>(config: AxiosRequestConfig): Promise<T> { | ||
return instance.request(config); | ||
} | ||
|
||
export { setBaseUrl }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters