onResponse method
The callback will be executed on success.
If you want to continue the response, call handler.next
.
If you want to complete the response with some custom data directly,
you can resolve a Response object with handler.resolve
and other
response interceptor(s) will not be executed.
If you want to complete the response with an error message,
you can reject a DioError object with handler.reject
.
Implementation
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
// 请求 url
var requestUrl = response.requestOptions.uri.toString();
requestUrl = Uri.decodeComponent(requestUrl);
// 响应处理
if (response.data is Map) {
Map data = response.data;
RespData respData = mapToRespData(data);
if (respData.code == null && respData.message.isEmpty) {
XLog.json(response.data, 'Quick请求url:$requestUrl');
return handler.resolve(response);
}
if (respData.success) {
if (!requestUrl.contains('hideLog') && showLog) {
XLog.json(response.data, '请求url:$requestUrl');
}
if (respData.data != null) response.data = respData.data;
return handler.resolve(response);
} else {
// 需要登录, 用接口内部 code ,确定未登录
if (respData.code.toString() == respData.unLoginCode.toString()) {
respData.onUnLogin?.call();
}
if (!requestUrl.contains('hideMsg') &&
U.isNotEmpty(respData.message) &&
respData.message.length < 50) {
//显示请求错误的信息
doShowErrorMsg(respData.message);
}
XLog.e(response.data, '请求url:$requestUrl');
return handler.reject(DioError(
requestOptions: response.requestOptions,
error: respData.message,
));
}
} else {
XLog.e(response.data, '请求的结果不是jsonMap');
return handler.resolve(response);
}
}