1- import 'package:dio/dio.dart' show Dio, DioError, Options, InterceptorsWrapper, RequestOptions, Response;
1+ import 'package:dio/dio.dart'
2+ show
3+ Dio,
4+ DioError,
5+ Options,
6+ InterceptorsWrapper,
7+ RequestOptions,
8+ // LogInterceptor,
9+ Response;
10+ import 'package:efox_flutter/utils/localStorage.dart' show LocalStorage;
11+
12+ void _print (title, message) {
13+ if (message == null ) {
14+ print ('$title ' );
15+ } else {
16+ print ('=================$title =============' );
17+ print ('$message ' );
18+ print ('=================response end=============' );
19+ }
20+ ;
21+ }
222
323Dio getDio ([Options options]) {
424 if (options == null ) {
525 options = Options (
626 headers: {
727 'context-type' : 'application/json' ,
8- 'user-agent' : 'dio' ,
9- 'common-header' : 'xx'
1028 },
1129 );
1230 }
@@ -16,42 +34,47 @@ Dio getDio([Options options]) {
1634 dio.options.connectTimeout = 30 * 1000 ; //5s
1735 dio.options.receiveTimeout = 30 * 1000 ;
1836 dio.options.headers = options.headers;
19-
20- dio.interceptors.add (InterceptorsWrapper (
21- onRequest: (RequestOptions options){
22- // Do something before request is sent
23- return options; //continue
24- // If you want to resolve the request with some custom data,
25- // you can return a `Response` object or return `dio.resolve(data)`.
26- // If you want to reject the request with a error message,
27- // you can return a `DioError` object or return `dio.reject(errMsg)`
28- },
29- onResponse: (Response response) {
30- // Do something with response data
31- print ('onResponse --- $response ' );
32- return response; // continue
33- },
34- onError: (DioError e) {
35- // Do something with response error
36- print ('onError --- $e ' );
37- return e;//continue
38- }
39- ));
4037
4138 // Add request interceptor
39+ dio.interceptors
40+ .add (InterceptorsWrapper (onRequest: (RequestOptions options) async {
41+ String token = await LocalStorage .get ('githubRespLoginToken' ) ?? '' ;
42+ if (options.headers['Authorization' ] == null ) {
43+ options.headers['Authorization' ] = 'token $token ' ;
44+ }
45+ // Do something before request is sent
46+ return options; //continue
47+ // If you want to resolve the request with some custom data,
48+ // you can return a `Response` object or return `dio.resolve(data)`.
49+ // If you want to reject the request with a error message,
50+ // you can return a `DioError` object or return `dio.reject(errMsg)`
51+ }, onResponse: (Response response) {
52+ // Do something with response data
53+ _print ('http.dart response success' , response);
54+ return response; // continue
55+ }, onError: (DioError e) {
56+ // Do something with response error
57+ _print ('http.dart response fail' ,
58+ '${e .response .data } status: ${e .response .statusCode }' );
59+ // return e; //continue
60+ }));
61+ // dio.interceptors.add(LogInterceptor(responseBody: false)); //开启请求日志
62+
4263 return dio;
4364}
4465
45- Future <dynamic > get (url, [ data = const {}] ) async {
66+ Future <dynamic > get ({ url, data = const {}} ) async {
4667 try {
47- return await getDio ().get (url).then ((res) {
48- return res.data;
49- });
68+ return getDio ().get (url).then ((resp) => resp.data);
5069 } on DioError catch (e) {
51- return e.response;
70+ return { 'code' : e.response.statusCode, 'msg' : e.response.data} ;
5271 }
5372}
5473
55- Future post (url, [data = const {}, params = const {}, options]) async {
56- return getDio (options).post (url, data: data);
74+ Future post ({url, data = const {}, options}) async {
75+ try {
76+ return getDio (options).post (url, data: data).then ((resp) => resp.data);
77+ } on DioError catch (e) {
78+ return {'code' : e.response.statusCode, 'msg' : e.response.data};
79+ }
5780}
0 commit comments