swagger_generator_flutter/example/generated/basic_api_service.dart

71 lines
1.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 主 API 接口定义 - 集合所有 Tag 的 API
// 基于 Swagger API 文档:
// 由 xy_swagger_generator by max 生成
// Copyright (C) 2025 YuanXuan. All rights reserved.
import 'package:dio/dio.dart';
import 'package:learning_officer_oa/common/models/common/base_page_result.dart';
import 'package:learning_officer_oa/common/models/common/base_result.dart';
import 'api_error.dart';
import 'api_error_handler.dart';
/// 统一API客户端类
/// 聚合所有分模块的API接口提供统一的访问入口
class BasicApiService {
final Dio _dio;
BasicApiService(this._dio, {String? baseUrl});
/// 获取Dio实例
Dio get dio => _dio;
/// 设置认证token
void setAuthToken(String token) {
_dio.options.headers['Authorization'] = 'Bearer $token';
}
/// 清除认证token
void clearAuthToken() {
_dio.options.headers.remove('Authorization');
}
/// 设置基础URL
void setBaseUrl(String baseUrl) {
_dio.options.baseUrl = baseUrl;
}
/// 添加请求拦截器
void addRequestInterceptor(Interceptor interceptor) {
_dio.interceptors.add(interceptor);
}
/// 添加响应拦截器
void addResponseInterceptor(Interceptor interceptor) {
_dio.interceptors.add(interceptor);
}
/// 添加错误拦截器
void addErrorInterceptor(Interceptor interceptor) {
_dio.interceptors.add(interceptor);
}
/// 创建带错误处理的API调用
Future<T> callWithErrorHandling<T>(Future<T> Function() apiCall) async {
try {
return await apiCall();
} on DioException catch (e) {
final error = ApiErrorHandler.handleDioError(e);
throw error;
} catch (e) {
final error = ApiError(
type: ApiErrorType.unknown,
message: '未知错误: $e',
code: -1,
originalError: e,
);
throw error;
}
}
}