封装核心思想
将请求地址隐藏起来,并对http请求数据加盐保密。
- 固定请求地址,
request URL
所有接口都是同一个 - 接口详解,
apiCode
区别各个接口,sign
对请求加盐保密,白名单定义token
是否需要传,params
包裹传输数据 - 调用方式支持
await
异步,避免回调地狱 - 请求使用拦截器
Interceptor
处理各个部分
请求结果效果图如下:
封装后,优雅调用
// 调用方法
Future _submit() async {
var params = Map<String, String>();
params["phone"] = '13812346666';
var res = await http.request(Api.GET_CODE, params);
if (res.data["code"] == 0) {
print('验证码发送成功');
} else {
print(res.data["msg"]);
}
}
封装完整文件
import 'dart:async' show Future;
import 'dart:collection' show HashMap;
import 'dart:convert';
import 'package:dio/dio.dart'
show Dio, Options, DioError, Response, DioErrorType;
import './result_data.dart';
import '../constants/constants.dart';
import './interceptors/token_interceptor.dart' show TokenInterceptors;
import './interceptors/header_interceptor.dart' show HeaderInterceptors;
import './interceptors/log_interceptor.dart' show LogsInterceptors;
import './interceptors/error_interceptor.dart' show ErrorInterceptors;
import 'interceptors/api_interceptor.dart' show ApiInterceptors;
// import './result_data.dart' show ResultData;
import './code.dart' show Code;
class HTTP {
static const CONTENT_TYPE_JSON = 'application/json';
static const CONTENT_TYPE_FROM = 'application/x-www-form-urlencoded';
static HTTP inst;
static HTTP getInstance() {
if (inst != null) {
return inst;
}
inst = new HTTP();
return inst;
}
Dio _dio = new Dio();
TokenInterceptors _tokenInterceptors = new TokenInterceptors();
HTTP() {
_dio.interceptors.add(new HeaderInterceptors());
// _dio.interceptors.add(_tokenInterceptors);
_dio.interceptors.add(new LogsInterceptors());
_dio.interceptors.add(new ErrorInterceptors(_dio));
_dio.interceptors.add(new ApiInterceptors());
}
Future<ResultData> request(
String apiCode,
data, {
Map<String, dynamic> headers,
Options options,
isNoTip = false,
}) async {
Map<String, dynamic> _headers = new HashMap();
if (headers != null) {
_headers.addAll(headers);
}
if (options != null) {
options.headers = _headers;
} else {
options = new Options(method: 'post');
options.headers = _headers;
}
Response response;
try {
data["apiCode"] = apiCode;
// 注意,所有data数据都要encode变为json字符串.
response = await _dio.request(Constants.BASE_URL,
data: json.encode(data), options: options);
} on DioError catch (error) {
return resultError(error, isNoTip);
}
if (response.data is DioError) {
return resultError(response.data, isNoTip);
}
return response.data;
}
clearAuthorization() {
_tokenInterceptors.clearAuthorization();
}
getAuthorization() async {
return _tokenInterceptors.getAuthorization();
}
resultError(DioError error, bool isNoTip) {
Response errorResponse;
if (error.response != null) {
errorResponse = error.response;
} else {
errorResponse = new Response(statusCode: 666);
}
if (error.type == DioErrorType.CONNECT_TIMEOUT ||
error.type == DioErrorType.RECEIVE_TIMEOUT) {
errorResponse.statusCode = Code.NETWORK_TIMEOUT;
}
return new ResultData(
Code.errorHandleFunction(
errorResponse.statusCode,
error.message,
isNoTip,
),
false,
errorResponse.statusCode,
);
}
}
final HTTP http = HTTP.getInstance();
--------------如果文章对你有用,请右上角点赞/收藏,谢谢-------------