47 lines
1022 B
Dart
47 lines
1022 B
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../../config/env.dart';
|
|
import 'interceptor.dart';
|
|
|
|
class Request {
|
|
static final Request _instance = Request._internal();
|
|
static Dio _dio = Dio();
|
|
|
|
//返回单例
|
|
factory Request() {
|
|
return _instance;
|
|
}
|
|
|
|
//初始化
|
|
Request._internal() {
|
|
//创建基本配置
|
|
final BaseOptions options = BaseOptions(
|
|
baseUrl: Config.baseUrl(),
|
|
connectTimeout: const Duration(seconds: 30),
|
|
receiveTimeout: const Duration(seconds: 30),
|
|
);
|
|
|
|
_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<dynamic> post(String path, Object? data) async {
|
|
var res = await _dio.post(path, data: data);
|
|
return res.data;
|
|
}
|
|
}
|