|
|
@ -11,6 +11,35 @@ class AuthInterceptor extends Interceptor { |
|
|
final GetStorage _storage = GetStorage(); |
|
|
final GetStorage _storage = GetStorage(); |
|
|
final String _signKey = '@214%67g15q4*67m17#4l67!'; // 替换为实际的密钥 |
|
|
final String _signKey = '@214%67g15q4*67m17#4l67!'; // 替换为实际的密钥 |
|
|
|
|
|
|
|
|
|
|
|
bool _shouldSuppressErrorSnackbar(DioException err) { |
|
|
|
|
|
final path = err.requestOptions.path; |
|
|
|
|
|
return path == '/api/home/user_getinfo'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool _shouldSuppressBusinessErrorSnackbar(RequestOptions options) { |
|
|
|
|
|
final path = options.path; |
|
|
|
|
|
return path == '/api/home/user_getinfo'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String _mapBadResponseMessage(DioException err) { |
|
|
|
|
|
final statusCode = err.response?.statusCode; |
|
|
|
|
|
final statusMessage = (err.response?.statusMessage ?? '').toLowerCase(); |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode == 401 || statusCode == 403) { |
|
|
|
|
|
return 'loginExpiredPleaseRelogin'.tr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode == 404 || statusMessage.contains('record not found')) { |
|
|
|
|
|
return 'requestedResourceNotFound'.tr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (statusCode != null && statusCode >= 500) { |
|
|
|
|
|
return 'serverTemporarilyUnavailable'.tr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 'unknownError'.tr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@override |
|
|
@override |
|
|
void onRequest(RequestOptions options, RequestInterceptorHandler handler) { |
|
|
void onRequest(RequestOptions options, RequestInterceptorHandler handler) { |
|
|
var jsonString = _storage.read<String>("logintoken"); |
|
|
var jsonString = _storage.read<String>("logintoken"); |
|
|
@ -37,9 +66,20 @@ class AuthInterceptor extends Interceptor { |
|
|
} else { |
|
|
} else { |
|
|
response.data = null; |
|
|
response.data = null; |
|
|
handler.next(response); |
|
|
handler.next(response); |
|
|
|
|
|
|
|
|
|
|
|
if (_shouldSuppressBusinessErrorSnackbar(response.requestOptions)) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final rawMessage = entity.message ?? ''; |
|
|
|
|
|
final lowerMessage = rawMessage.toLowerCase(); |
|
|
|
|
|
final displayMessage = lowerMessage.contains('record not found') |
|
|
|
|
|
? 'requestedResourceNotFound'.tr |
|
|
|
|
|
: (entity.message ?? 'unknownError'.tr); |
|
|
|
|
|
|
|
|
Get.snackbar( |
|
|
Get.snackbar( |
|
|
'error'.tr, // 错误 |
|
|
'error'.tr, // 错误 |
|
|
entity.message ?? 'unknownError'.tr, |
|
|
displayMessage, |
|
|
snackPosition: SnackPosition.TOP, |
|
|
snackPosition: SnackPosition.TOP, |
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
colorText: Colors.red, |
|
|
colorText: Colors.red, |
|
|
@ -93,24 +133,18 @@ class AuthInterceptor extends Interceptor { |
|
|
} |
|
|
} |
|
|
case DioExceptionType.badResponse: |
|
|
case DioExceptionType.badResponse: |
|
|
{ |
|
|
{ |
|
|
try { |
|
|
if (_shouldSuppressErrorSnackbar(err)) { |
|
|
String? errMsg = err.response?.statusMessage; |
|
|
handler.next(err); |
|
|
Get.snackbar( |
|
|
return; |
|
|
'error'.tr, // 错误 |
|
|
|
|
|
errMsg ?? 'unknownError'.tr, |
|
|
|
|
|
snackPosition: SnackPosition.TOP, |
|
|
|
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
|
|
colorText: Colors.red, |
|
|
|
|
|
); |
|
|
|
|
|
} on Exception catch (_) { |
|
|
|
|
|
Get.snackbar( |
|
|
|
|
|
'error'.tr, // 错误 |
|
|
|
|
|
'unknownError'.tr, |
|
|
|
|
|
snackPosition: SnackPosition.TOP, |
|
|
|
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
|
|
colorText: Colors.red, |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Get.snackbar( |
|
|
|
|
|
'error'.tr, // 错误 |
|
|
|
|
|
_mapBadResponseMessage(err), |
|
|
|
|
|
snackPosition: SnackPosition.TOP, |
|
|
|
|
|
backgroundColor: Colors.red.withOpacity(0.1), |
|
|
|
|
|
colorText: Colors.red, |
|
|
|
|
|
); |
|
|
} |
|
|
} |
|
|
default: |
|
|
default: |
|
|
{ |
|
|
{ |
|
|
|