Commit 99efc5739928718d1f975c739d9ab04cae2d1590
1 parent
993c1a04
兼容非json response header接口
Showing
3 changed files
with
25 additions
and
10 deletions
lib/common/core/user_util.dart
| ... | ... | @@ -45,6 +45,6 @@ class UserUtil { |
| 45 | 45 | |
| 46 | 46 | static void logout() { |
| 47 | 47 | clearUserSp(); |
| 48 | - Navigator.of(AppRouter.context).push(AppRouteName.login as Route<Object?>); | |
| 48 | + Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false); | |
| 49 | 49 | } |
| 50 | 50 | } | ... | ... |
lib/common/request/request_client.dart
| 1 | 1 | import 'dart:convert'; |
| 2 | 2 | |
| 3 | 3 | import 'package:dio/dio.dart'; |
| 4 | +import 'package:flutter/foundation.dart'; | |
| 4 | 5 | import 'package:pretty_dio_logger/pretty_dio_logger.dart'; |
| 5 | 6 | |
| 6 | 7 | import '../core/user_util.dart'; |
| ... | ... | @@ -18,7 +19,10 @@ class RequestClient { |
| 18 | 19 | RequestClient() { |
| 19 | 20 | _dio = Dio(BaseOptions(baseUrl: RequestConfig.baseUrl, connectTimeout: RequestConfig.connectTimeout)); |
| 20 | 21 | _dio.interceptors.add(TokenInterceptor()); |
| 21 | - _dio.interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true)); | |
| 22 | + if (kDebugMode) { | |
| 23 | + _dio.interceptors | |
| 24 | + .add(PrettyDioLogger(requestHeader: true, requestBody: true, responseHeader: true, maxWidth: 120)); | |
| 25 | + } | |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | Future<T?> request<T>( |
| ... | ... | @@ -38,10 +42,11 @@ class RequestClient { |
| 38 | 42 | data = _convertRequestData(data); |
| 39 | 43 | |
| 40 | 44 | Response response = await _dio.request(url, queryParameters: queryParameters, data: data, options: options); |
| 41 | - | |
| 45 | + print("response.body type=${response.data.runtimeType}"); | |
| 42 | 46 | return _handleResponse<T>(response, onResponse); |
| 43 | 47 | } catch (e) { |
| 44 | - if ((e as ApiException?)?.code == 405) { | |
| 48 | + print("e type=${e.runtimeType}"); | |
| 49 | + if (e is ApiException && e.code == 405) { | |
| 45 | 50 | UserUtil.logout(); |
| 46 | 51 | return null; |
| 47 | 52 | } |
| ... | ... | @@ -143,12 +148,24 @@ class RequestClient { |
| 143 | 148 | raw.value = response.data; |
| 144 | 149 | return raw as T; |
| 145 | 150 | } else { |
| 146 | - ApiResponse<T> apiResponse = ApiResponse<T>.fromJson(response.data); | |
| 151 | + print('response.data.runtimeType=${response.data.runtimeType}'); | |
| 152 | + | |
| 153 | + // 应对response的header中content-type: [text/html;charset=utf-8]的情况 | |
| 154 | + // 实际上都是json格式返回 | |
| 155 | + Map<String, dynamic> json; | |
| 156 | + if (response.data is String) { | |
| 157 | + json = jsonDecode(response.data); | |
| 158 | + } else { | |
| 159 | + json = response.data; | |
| 160 | + } | |
| 161 | + //ApiResponse<T> apiResponse = ApiResponse<T>.fromJson(json.decode(response.data)); | |
| 162 | + ApiResponse<T> apiResponse = ApiResponse.fromJson(json); | |
| 147 | 163 | onResponse?.call(apiResponse); |
| 148 | 164 | return _handleBusinessResponse<T>(apiResponse); |
| 149 | 165 | } |
| 150 | 166 | } else { |
| 151 | - var exception = ApiException(response.statusCode, ApiException.unknownException); | |
| 167 | + ApiException exception = ApiException(response.statusCode, ApiException.unknownException); | |
| 168 | + print("_handleResponse exception type=${exception.runtimeType}"); | |
| 152 | 169 | throw exception; |
| 153 | 170 | } |
| 154 | 171 | } |
| ... | ... | @@ -158,7 +175,8 @@ class RequestClient { |
| 158 | 175 | if (response.code == RequestConfig.successCode) { |
| 159 | 176 | return response.data; |
| 160 | 177 | } else { |
| 161 | - var exception = ApiException(response.code, response.msg); | |
| 178 | + ApiException exception = ApiException(response.code, response.msg); | |
| 179 | + print("_handleBusinessResponse exception type=${exception.runtimeType}"); | |
| 162 | 180 | throw exception; |
| 163 | 181 | } |
| 164 | 182 | } | ... | ... |
lib/pages/login/loginpage/bloc/login_bloc.dart
| ... | ... | @@ -6,7 +6,6 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; |
| 6 | 6 | import 'package:wow_english/common/extension/string_extension.dart'; |
| 7 | 7 | import 'package:wow_english/common/request/dao/user_dao.dart'; |
| 8 | 8 | import 'package:wow_english/common/request/exception.dart'; |
| 9 | -import 'package:wow_english/common/request/request.dart'; | |
| 10 | 9 | import 'package:wow_english/models/user_entity.dart'; |
| 11 | 10 | import 'package:wow_english/utils/loading.dart'; |
| 12 | 11 | |
| ... | ... | @@ -75,8 +74,6 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> { |
| 75 | 74 | |
| 76 | 75 | |
| 77 | 76 | try { |
| 78 | - request(() => null); | |
| 79 | - | |
| 80 | 77 | await loading(() async { |
| 81 | 78 | var user = await UserDao.login(phoneNumber, type, checkKey, checkNumber); |
| 82 | 79 | if (kDebugMode) { | ... | ... |