Commit 8de2f9cb by tanghuan

网络请求异常捕获处理

1 parent bd25deab
...@@ -98,6 +98,10 @@ class LoginPhoneCubit extends Cubit<LoginPhoneState> { ...@@ -98,6 +98,10 @@ class LoginPhoneCubit extends Cubit<LoginPhoneState> {
// 发送验证码 // 发送验证码
var result = await _phoneAuthRepository.verifyCode(phone, 1); var result = await _phoneAuthRepository.verifyCode(phone, 1);
if (result == null) {
Fluttertoast.showToast(msg: '发送请求失败', gravity: ToastGravity.TOP, backgroundColor: Colors.red);
return;
}
if (result['code'] != 0) { if (result['code'] != 0) {
Fluttertoast.showToast(msg: result['error'], gravity: ToastGravity.TOP, backgroundColor: Colors.red); Fluttertoast.showToast(msg: result['error'], gravity: ToastGravity.TOP, backgroundColor: Colors.red);
return; return;
......
import 'dart:io';
import 'package:appframe/config/locator.dart'; import 'package:appframe/config/locator.dart';
import 'package:appframe/services/api_service.dart'; import 'package:appframe/services/api_service.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
...@@ -22,13 +24,17 @@ class PhoneAuthRepository { ...@@ -22,13 +24,17 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> bindCheck(String userid) async { Future<dynamic> bindCheck(String userid) async {
try {
Response resp = await _appService.get( Response resp = await _appService.get(
'/api/v1/comm/phone/bind/check', '/api/v1/comm/phone/bind/check',
queryParameters: { queryParameters: {
"userid": userid, "userid": userid,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -38,6 +44,7 @@ class PhoneAuthRepository { ...@@ -38,6 +44,7 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> bind(String userid, String phone, String verifyCode) async { Future<dynamic> bind(String userid, String phone, String verifyCode) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/phone/bind', '/api/v1/comm/phone/bind',
{ {
...@@ -46,7 +53,10 @@ class PhoneAuthRepository { ...@@ -46,7 +53,10 @@ class PhoneAuthRepository {
"verifyCode": verifyCode, "verifyCode": verifyCode,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -59,6 +69,7 @@ class PhoneAuthRepository { ...@@ -59,6 +69,7 @@ class PhoneAuthRepository {
/// type=0 验证码登陆下发 /// type=0 验证码登陆下发
/// ///
Future<dynamic> verifyCode(String phone, int type) async { Future<dynamic> verifyCode(String phone, int type) async {
try {
Response resp = await _appService.get( Response resp = await _appService.get(
'/api/v1/comm/phone/verifycode', '/api/v1/comm/phone/verifycode',
queryParameters: { queryParameters: {
...@@ -66,7 +77,10 @@ class PhoneAuthRepository { ...@@ -66,7 +77,10 @@ class PhoneAuthRepository {
"type": type, "type": type,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -77,6 +91,7 @@ class PhoneAuthRepository { ...@@ -77,6 +91,7 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> login(String phone, String verifyCode) async { Future<dynamic> login(String phone, String verifyCode) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/phone/loginv2', '/api/v1/comm/phone/loginv2',
{ {
...@@ -84,7 +99,10 @@ class PhoneAuthRepository { ...@@ -84,7 +99,10 @@ class PhoneAuthRepository {
"verifyCode": verifyCode, "verifyCode": verifyCode,
}, },
); );
return resp.statusCode == 200 ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -94,22 +112,30 @@ class PhoneAuthRepository { ...@@ -94,22 +112,30 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> unbind(String userid) async { Future<dynamic> unbind(String userid) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/phone/unbind', '/api/v1/comm/phone/unbind',
{ {
"userid": userid, "userid": userid,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
Future<dynamic> unbindPhone(String phone) async { Future<dynamic> unbindPhone(String phone) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/phone/unbind', '/api/v1/comm/phone/unbind',
{ {
"phone": phone, "phone": phone,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
} }
import 'dart:io';
import 'package:appframe/config/locator.dart'; import 'package:appframe/config/locator.dart';
import 'package:appframe/services/api_service.dart'; import 'package:appframe/services/api_service.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
...@@ -10,6 +12,7 @@ class SubsRepository { ...@@ -10,6 +12,7 @@ class SubsRepository {
} }
Future<dynamic> userGroups(String type, String userid, String classCode) async { Future<dynamic> userGroups(String type, String userid, String classCode) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/subs/usergroups', '/api/v1/comm/subs/usergroups',
{ {
...@@ -18,6 +21,9 @@ class SubsRepository { ...@@ -18,6 +21,9 @@ class SubsRepository {
"classCode": classCode, "classCode": classCode,
}, },
); );
return resp.data; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
} }
...@@ -18,6 +18,7 @@ class UserAuthRepository { ...@@ -18,6 +18,7 @@ class UserAuthRepository {
/// } /// }
/// ///
Future<dynamic> updateUser(String userid, String name, String nickName, String avatar) async { Future<dynamic> updateUser(String userid, String name, String nickName, String avatar) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/user/update', '/api/v1/comm/user/update',
{ {
...@@ -28,9 +29,13 @@ class UserAuthRepository { ...@@ -28,9 +29,13 @@ class UserAuthRepository {
}, },
); );
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
Future<dynamic> appleLogin(String userid, String authorizationCode, String identityToken) async { Future<dynamic> appleLogin(String userid, String authorizationCode, String identityToken) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/user/applelogin', '/api/v1/comm/user/applelogin',
{ {
...@@ -40,6 +45,9 @@ class UserAuthRepository { ...@@ -40,6 +45,9 @@ class UserAuthRepository {
}, },
); );
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -49,6 +57,7 @@ class UserAuthRepository { ...@@ -49,6 +57,7 @@ class UserAuthRepository {
/// "error": "", /// "error": "",
/// } /// }
Future<dynamic> exchangeId(String userid) async { Future<dynamic> exchangeId(String userid) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/user/exchangeid', '/api/v1/comm/user/exchangeid',
{ {
...@@ -57,6 +66,9 @@ class UserAuthRepository { ...@@ -57,6 +66,9 @@ class UserAuthRepository {
}, },
); );
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -65,6 +77,7 @@ class UserAuthRepository { ...@@ -65,6 +77,7 @@ class UserAuthRepository {
/// "code": 0, /// "code": 0,
/// } /// }
Future<dynamic> newBinding(String userid, String bxeUserId, String type) async { Future<dynamic> newBinding(String userid, String bxeUserId, String type) async {
try {
Response resp = await _appService.post( Response resp = await _appService.post(
'/api/v1/comm/user/newbinding', '/api/v1/comm/user/newbinding',
{ {
...@@ -74,5 +87,8 @@ class UserAuthRepository { ...@@ -74,5 +87,8 @@ class UserAuthRepository {
}, },
); );
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
} }
...@@ -13,6 +13,7 @@ class WechatAuthRepository { ...@@ -13,6 +13,7 @@ class WechatAuthRepository {
} }
Future<dynamic> codeToSk(String code) async { Future<dynamic> codeToSk(String code) async {
try {
Response resp = await _apiService.post( Response resp = await _apiService.post(
'/login/applet/wkbxe/codeToSkByApp?version=1.0.0', '/login/applet/wkbxe/codeToSkByApp?version=1.0.0',
{ {
...@@ -24,9 +25,14 @@ class WechatAuthRepository { ...@@ -24,9 +25,14 @@ class WechatAuthRepository {
debugPrint('登录结果: $resp'); debugPrint('登录结果: $resp');
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException catch (e) {
debugPrint('codeToSk网络异常: $e');
return null;
}
} }
Future<dynamic> getTicket() async { Future<dynamic> getTicket() async {
try {
Response resp = await _apiService.post( Response resp = await _apiService.post(
'/login/applet/wkbxe/getTicketByApp?version=1.0.0', '/login/applet/wkbxe/getTicketByApp?version=1.0.0',
{ {
...@@ -38,5 +44,9 @@ class WechatAuthRepository { ...@@ -38,5 +44,9 @@ class WechatAuthRepository {
debugPrint('获取ticket: $resp'); debugPrint('获取ticket: $resp');
return resp.statusCode == HttpStatus.ok ? resp.data : null; return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException catch (e) {
debugPrint('getTicket网络异常: $e');
return null;
}
} }
} }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!