diff --git a/lib/app/splash_page.dart b/lib/app/splash_page.dart index 8f97c18..d5c8572 100644 --- a/lib/app/splash_page.dart +++ b/lib/app/splash_page.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:wow_english/common/core/user_util.dart'; @@ -32,6 +33,9 @@ class _TransitionViewState extends State { Future startTime() async { // 判断是否登录 UserEntity? userEntity = UserUtil.getUser(); + if (kDebugMode) { + print('userEntity: $userEntity'); + } Timer(const Duration(seconds: 2), () { if (userEntity != null && userEntity.token.isNotEmpty) { context.read().add(UserInfoChangeEvent(userEntity)); diff --git a/lib/common/extension/string_extension.dart b/lib/common/extension/string_extension.dart index 9d472c3..a84f5dd 100644 --- a/lib/common/extension/string_extension.dart +++ b/lib/common/extension/string_extension.dart @@ -9,3 +9,7 @@ extension AssetExtension on String { String get assetGif => '$assetImg.gif'; } + +extension StringExtension on String { + String get prefixColon => ':$this'; +} diff --git a/lib/common/request/api_response/api_response_entity.g.dart b/lib/common/request/api_response/api_response_entity.g.dart index d6295e6..48ff8fa 100644 --- a/lib/common/request/api_response/api_response_entity.g.dart +++ b/lib/common/request/api_response/api_response_entity.g.dart @@ -16,7 +16,7 @@ ApiResponse $ApiResponseFromJson(Map json) { String type = T.toString(); T? data; if (kDebugMode) { - print("ApiResponse type:$type"); + print("ApiResponse T-type:$type, data-type:${json['data'].runtimeType}"); } if (json['data'] != null) { data = jsonConvert.convert(json['data']); diff --git a/lib/common/request/exception.dart b/lib/common/request/exception.dart index 83ac278..855902c 100644 --- a/lib/common/request/exception.dart +++ b/lib/common/request/exception.dart @@ -5,82 +5,84 @@ import 'api_response/api_response_entity.dart'; class ApiException implements Exception { static const unknownException = "未知错误"; + static const customErrorCode = -1; final String? message; final int? code; String? stackInfo; ApiException([this.code, this.message]); - factory ApiException.fromDioError(DioException error) { - switch (error.type) { + factory ApiException.fromDioException(DioException exception) { + if (kDebugMode) { + print('fromDioException 异常: $exception'); + } + switch (exception.type) { case DioExceptionType.connectionTimeout: - return BadRequestException(-1, "连接超时"); + return BadRequestException(customErrorCode, "连接超时"); case DioExceptionType.sendTimeout: - return BadRequestException(-1, "请求超时"); + return BadRequestException(customErrorCode, "请求超时"); case DioExceptionType.receiveTimeout: - return BadRequestException(-1, "响应超时"); + return BadRequestException(customErrorCode, "响应超时"); case DioExceptionType.badCertificate: - return BadRequestException(-1, "证书错误"); + return BadRequestException(customErrorCode, "证书错误"); case DioExceptionType.badResponse: - return BadRequestException(-1, "返回错误"); + return BadRequestException(customErrorCode, "返回错误"); case DioExceptionType.cancel: - return BadRequestException(-1, "请求取消"); + return BadRequestException(customErrorCode, "请求取消"); case DioExceptionType.connectionError: - return BadRequestException(-1, "连接错误"); + return BadRequestException(customErrorCode, "连接错误"); case DioExceptionType.unknown: - try { - /// http错误码带业务错误信息 - ApiResponse apiResponse = ApiResponse.fromJson(error.response?.data); - if (apiResponse.code != null) { - return ApiException(apiResponse.code, apiResponse.msg); - } + int? errCode = exception.response?.statusCode; + switch (errCode) { + case 400: + return BadRequestException(errCode, "请求语法错误"); + case 401: + return UnauthorisedException(errCode, "没有权限"); + case 403: + return UnauthorisedException(errCode, "服务器拒绝执行"); + case 404: + return UnauthorisedException(errCode, "无法连接服务器"); + case 405: + // 会出发登出的code,这里强制换掉 + return UnauthorisedException(406, "请求方法被禁止"); + case 500: + return UnauthorisedException(errCode, "服务器内部错误"); + case 502: + return UnauthorisedException(errCode, "无效的请求"); + case 503: + return UnauthorisedException(errCode, "服务器异常"); + case 505: + return UnauthorisedException(errCode, "不支持HTTP协议请求"); + default: - int? errCode = error.response?.statusCode; - switch (errCode) { - case 400: - return BadRequestException(errCode, "请求语法错误"); - case 401: - return UnauthorisedException(errCode!, "没有权限"); - case 403: - return UnauthorisedException(errCode!, "服务器拒绝执行"); - case 404: - return UnauthorisedException(errCode!, "无法连接服务器"); - case 405: - return UnauthorisedException(errCode!, "请求方法被禁止"); - case 500: - return UnauthorisedException(errCode!, "服务器内部错误"); - case 502: - return UnauthorisedException(errCode!, "无效的请求"); - case 503: - return UnauthorisedException(errCode!, "服务器异常"); - case 505: - return UnauthorisedException(errCode!, "不支持HTTP协议请求"); - default: - return ApiException(errCode, error.response?.statusMessage ?? '未知错误'); - } - } on Exception catch (e) { - if (kDebugMode) { - return ApiException(-1, e.toString()); - } else { - return ApiException(-1, unknownException); - } + /// 试一下Response能不能解析出业务上的错误码,不能再继续走dio抛出的异常 + try { + ApiResponse apiResponse = ApiResponse.fromJson(exception.response?.data); + if (apiResponse.code != null) { + return ApiException(apiResponse.code, apiResponse.msg ?? "错误码:${apiResponse.code}"); + } + } catch (e) { + if (kDebugMode) { + print('DioExceptionType.unknown 错误: $e'); + } + } } - default: - return ApiException(-1, error.message); } + var errorCode = exception.response?.statusCode ?? customErrorCode; + var errorMsg = kDebugMode ? (exception.response?.statusMessage ?? exception.message) : unknownException; + return ApiException(errorCode, errorMsg); } factory ApiException.from(dynamic exception) { if (exception is DioException) { - return ApiException.fromDioError(exception); + return ApiException.fromDioException(exception); } if (exception is ApiException) { return exception; - } else { - var apiException = ApiException(-1, unknownException); - apiException.stackInfo = exception?.toString(); - return apiException; } + var apiException = ApiException(-1, unknownException); + apiException.stackInfo = exception?.toString(); + return apiException; } } @@ -91,5 +93,5 @@ class BadRequestException extends ApiException { /// 未认证异常 class UnauthorisedException extends ApiException { - UnauthorisedException([int code = -1, String message = '']) : super(code, message); + UnauthorisedException([int? code, String? message]) : super(code, message); } diff --git a/lib/common/request/request.dart b/lib/common/request/request.dart index a0337c4..760d86c 100644 --- a/lib/common/request/request.dart +++ b/lib/common/request/request.dart @@ -4,7 +4,7 @@ import 'exception_handler.dart'; Future request( Function() block, { - String loadingText = '加载中...', + String loadingText = '请稍候...', bool Function(ApiException)? onError, }) async { try { diff --git a/lib/common/request/request_client.dart b/lib/common/request/request_client.dart index dded184..5691d15 100644 --- a/lib/common/request/request_client.dart +++ b/lib/common/request/request_client.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; +import '../core/user_util.dart'; import 'api_response/api_response_entity.dart'; import 'api_response/raw_data.dart'; import 'config.dart'; @@ -40,6 +41,10 @@ class RequestClient { return _handleResponse(response, onResponse); } catch (e) { + if ((e as ApiException?)?.code == 405) { + UserUtil.logout(); + return null; + } var exception = ApiException.from(e); if (onError?.call(exception) != true) { throw exception; diff --git a/lib/models/user_entity.dart b/lib/models/user_entity.dart index b6aa797..5560c49 100644 --- a/lib/models/user_entity.dart +++ b/lib/models/user_entity.dart @@ -5,24 +5,25 @@ import 'package:wow_english/generated/json/user_entity.g.dart'; @JsonSerializable() class UserEntity { - late int id; - late String name; - late int? age; - /// 性别:0, 1 - late int? gender; - late String? avatarUrl; - late String phoneNum; - late String token; - late int expireTime; + late int id; + late String name; + int? age; - UserEntity(); + /// 性别:0, 1 + int? gender; + String? avatarUrl; + late String phoneNum; + late String token; + late int expireTime; - factory UserEntity.fromJson(Map json) => $UserEntityFromJson(json); + UserEntity(); - Map toJson() => $UserEntityToJson(this); + factory UserEntity.fromJson(Map json) => $UserEntityFromJson(json); - @override - String toString() { - return jsonEncode(this); - } + Map toJson() => $UserEntityToJson(this); + + @override + String toString() { + return jsonEncode(this); + } } diff --git a/lib/pages/login/loginpage/bloc/login_bloc.dart b/lib/pages/login/loginpage/bloc/login_bloc.dart index 4767cfa..5562e14 100644 --- a/lib/pages/login/loginpage/bloc/login_bloc.dart +++ b/lib/pages/login/loginpage/bloc/login_bloc.dart @@ -1,9 +1,12 @@ import 'package:common_utils/common_utils.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; +import 'package:wow_english/common/extension/string_extension.dart'; import 'package:wow_english/common/request/dao/user_dao.dart'; import 'package:wow_english/common/request/exception.dart'; +import 'package:wow_english/common/request/request.dart'; import 'package:wow_english/models/user_entity.dart'; import 'package:wow_english/utils/loading.dart'; @@ -70,15 +73,22 @@ class LoginBloc extends Bloc { var checkKey = _isSmsLoginType ? 'smsCode' : 'password'; var type = _isSmsLoginType ? 'sms_code' : 'pwd'; + try { + request(() => null); + await loading(() async { var user = await UserDao.login(phoneNumber, type, checkKey, checkNumber); - print('user=$user'); + if (kDebugMode) { + print('UserEntity?=$user'); + } emitter.call(LoginResultChangeState(user!)); }); } catch (e) { - print((e as ApiException).message); - EasyLoading.showToast('登陆失败'); + if (kDebugMode) { + print(e); + } + EasyLoading.showToast('登陆失败${(e as ApiException?)?.message?.prefixColon}'); } } @@ -95,9 +105,10 @@ class LoginBloc extends Bloc { }); emitter(SmsCodeRequestState()); } catch (e) { - if (e is ApiException) { - EasyLoading.showToast(e.message??'请检查网络连接'); - } + if (kDebugMode) { + print(e); + } + EasyLoading.showToast('获取验证码失败${(e as ApiException?)?.message?.prefixColon}'); } } diff --git a/lib/utils/loading.dart b/lib/utils/loading.dart index 76b1427..94d57d1 100644 --- a/lib/utils/loading.dart +++ b/lib/utils/loading.dart @@ -1,6 +1,6 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; -Future loading(Function block, {String loadingText = '加载中...'}) async { +Future loading(Function block, {String loadingText = '请稍后...'}) async { if (loadingText.isNotEmpty) { showLoading(loadingText); }