mirror of
https://github.com/rustfs/console.git
synced 2026-11-01 13:11:59 +08:00
- 修复 lib/api-client.ts 中在类方法中直接调用 composables 的问题 - 添加 ApiErrorHandler 错误处理器,通过依赖注入方式处理 401 错误 - 创建路由工具函数 utils/routes.ts,统一管理路由路径 - 修复 composables/useAuth.ts 中的硬编码路径,使用工具函数 - 修复 components/copy-input.vue 的生命周期问题,在 onMounted 中创建 ClipboardJS 实例 - 修复 plugins/03.s3.ts 中的错误处理,使用 navigateTo 替代直接操作 window.location - 修复 pages/config.vue 中的硬编码路径 - 添加 composables/useFormValidation.ts、useAsyncOperation.ts、useErrorHandler.ts 工具函数(供后续使用) - 添加 types/api.ts 类型定义
71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
/**
|
|
* API 错误处理器
|
|
* 处理 API 请求中的错误,特别是认证错误
|
|
*/
|
|
import type { ApiErrorHandlerCallback, IApiErrorHandler } from '~/types/api';
|
|
|
|
export class ApiErrorHandler implements IApiErrorHandler {
|
|
private onUnauthorized?: ApiErrorHandlerCallback;
|
|
private onForbidden?: ApiErrorHandlerCallback;
|
|
private onServerError?: ApiErrorHandlerCallback;
|
|
|
|
constructor(callbacks?: {
|
|
onUnauthorized?: ApiErrorHandlerCallback;
|
|
onForbidden?: ApiErrorHandlerCallback;
|
|
onServerError?: ApiErrorHandlerCallback;
|
|
}) {
|
|
this.onUnauthorized = callbacks?.onUnauthorized;
|
|
this.onForbidden = callbacks?.onForbidden;
|
|
this.onServerError = callbacks?.onServerError;
|
|
}
|
|
|
|
/**
|
|
* 处理 401 未授权错误
|
|
*/
|
|
async handle401(): Promise<void> {
|
|
if (this.onUnauthorized) {
|
|
await this.onUnauthorized();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理 403 禁止访问错误
|
|
*/
|
|
async handle403(): Promise<void> {
|
|
if (this.onForbidden) {
|
|
await this.onForbidden();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理 5xx 服务器错误
|
|
*/
|
|
async handleServerError(): Promise<void> {
|
|
if (this.onServerError) {
|
|
await this.onServerError();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据状态码处理错误
|
|
* @param status HTTP 状态码
|
|
*/
|
|
async handleByStatus(status: number): Promise<void> {
|
|
switch (status) {
|
|
case 401:
|
|
await this.handle401();
|
|
break;
|
|
case 403:
|
|
await this.handle403();
|
|
break;
|
|
case 500:
|
|
case 502:
|
|
case 503:
|
|
case 504:
|
|
await this.handleServerError();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|