44 lines
961 B
Dart
44 lines
961 B
Dart
import 'package:dio/dio.dart';
|
|
import 'package:app/global/config.dart';
|
|
|
|
import 'interceptor.dart';
|
|
|
|
class Request {
|
|
static Dio _dio = Dio();
|
|
|
|
//返回单例
|
|
factory Request() {
|
|
return Request._instance();
|
|
}
|
|
|
|
//初始化
|
|
Request._instance() {
|
|
//创建基本配置
|
|
final BaseOptions options = BaseOptions(
|
|
baseUrl: Config.baseUrl(),
|
|
connectTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(seconds: 10),
|
|
);
|
|
|
|
_dio = Dio(options);
|
|
|
|
_dio.interceptors.add(InterceptorsWrapper(
|
|
onRequest: onRequest,
|
|
onResponse: onResponse,
|
|
onError: onError,
|
|
));
|
|
}
|
|
|
|
///get请求
|
|
Future<T> get<T>(String path, [Map<String, dynamic>? params]) async {
|
|
var res = await _dio.get(path, queryParameters: params);
|
|
return res.data;
|
|
}
|
|
|
|
///post请求
|
|
Future<T> post<T>(String path, Object? data) async {
|
|
var res = await _dio.post(path, data: data);
|
|
return res.data;
|
|
}
|
|
}
|