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 {
Response resp = await _appService.get( try {
'/api/v1/comm/phone/bind/check', Response resp = await _appService.get(
queryParameters: { '/api/v1/comm/phone/bind/check',
"userid": userid, queryParameters: {
}, "userid": userid,
); },
return resp.data; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -38,15 +44,19 @@ class PhoneAuthRepository { ...@@ -38,15 +44,19 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> bind(String userid, String phone, String verifyCode) async { Future<dynamic> bind(String userid, String phone, String verifyCode) async {
Response resp = await _appService.post( try {
'/api/v1/comm/phone/bind', Response resp = await _appService.post(
{ '/api/v1/comm/phone/bind',
"userid": userid, {
"phone": phone, "userid": userid,
"verifyCode": verifyCode, "phone": phone,
}, "verifyCode": verifyCode,
); },
return resp.data; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -59,14 +69,18 @@ class PhoneAuthRepository { ...@@ -59,14 +69,18 @@ class PhoneAuthRepository {
/// type=0 验证码登陆下发 /// type=0 验证码登陆下发
/// ///
Future<dynamic> verifyCode(String phone, int type) async { Future<dynamic> verifyCode(String phone, int type) async {
Response resp = await _appService.get( try {
'/api/v1/comm/phone/verifycode', Response resp = await _appService.get(
queryParameters: { '/api/v1/comm/phone/verifycode',
"phone": phone, queryParameters: {
"type": type, "phone": phone,
}, "type": type,
); },
return resp.data; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -77,14 +91,18 @@ class PhoneAuthRepository { ...@@ -77,14 +91,18 @@ class PhoneAuthRepository {
/// } /// }
/// ///
Future<dynamic> login(String phone, String verifyCode) async { Future<dynamic> login(String phone, String verifyCode) async {
Response resp = await _appService.post( try {
'/api/v1/comm/phone/loginv2', Response resp = await _appService.post(
{ '/api/v1/comm/phone/loginv2',
"phone": phone, {
"verifyCode": verifyCode, "phone": phone,
}, "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 {
Response resp = await _appService.post( try {
'/api/v1/comm/phone/unbind', Response resp = await _appService.post(
{ '/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 {
Response resp = await _appService.post( try {
'/api/v1/comm/phone/unbind', Response resp = await _appService.post(
{ '/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,14 +12,18 @@ class SubsRepository { ...@@ -10,14 +12,18 @@ class SubsRepository {
} }
Future<dynamic> userGroups(String type, String userid, String classCode) async { Future<dynamic> userGroups(String type, String userid, String classCode) async {
Response resp = await _appService.post( try {
'/api/v1/comm/subs/usergroups', Response resp = await _appService.post(
{ '/api/v1/comm/subs/usergroups',
"type": type, {
"userid": userid, "type": type,
"classCode": classCode, "userid": userid,
}, "classCode": classCode,
); },
return resp.data; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
} }
...@@ -18,28 +18,36 @@ class UserAuthRepository { ...@@ -18,28 +18,36 @@ 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 {
Response resp = await _appService.post( try {
'/api/v1/comm/user/update', Response resp = await _appService.post(
{ '/api/v1/comm/user/update',
"userid": userid, {
"name": name, "userid": userid,
"nickName": nickName, "name": name,
"avatar": avatar, "nickName": nickName,
}, "avatar": avatar,
); },
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 {
Response resp = await _appService.post( try {
'/api/v1/comm/user/applelogin', Response resp = await _appService.post(
{ '/api/v1/comm/user/applelogin',
"user": userid, {
"authorizationCode": authorizationCode, "user": userid,
"identityToken": identityToken, "authorizationCode": authorizationCode,
}, "identityToken": identityToken,
); },
return resp.statusCode == HttpStatus.ok ? resp.data : null; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -49,14 +57,18 @@ class UserAuthRepository { ...@@ -49,14 +57,18 @@ class UserAuthRepository {
/// "error": "", /// "error": "",
/// } /// }
Future<dynamic> exchangeId(String userid) async { Future<dynamic> exchangeId(String userid) async {
Response resp = await _appService.post( try {
'/api/v1/comm/user/exchangeid', Response resp = await _appService.post(
{ '/api/v1/comm/user/exchangeid',
"userId": userid, {
"type": "apple", "userId": userid,
}, "type": "apple",
); },
return resp.statusCode == HttpStatus.ok ? resp.data : null; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
/// ///
...@@ -65,14 +77,18 @@ class UserAuthRepository { ...@@ -65,14 +77,18 @@ 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 {
Response resp = await _appService.post( try {
'/api/v1/comm/user/newbinding', Response resp = await _appService.post(
{ '/api/v1/comm/user/newbinding',
"userId": userid, {
"bxeUserId": bxeUserId, "userId": userid,
"type": type, "bxeUserId": bxeUserId,
}, "type": type,
); },
return resp.statusCode == HttpStatus.ok ? resp.data : null; );
return resp.statusCode == HttpStatus.ok ? resp.data : null;
} on DioException {
return null;
}
} }
} }
...@@ -13,30 +13,40 @@ class WechatAuthRepository { ...@@ -13,30 +13,40 @@ class WechatAuthRepository {
} }
Future<dynamic> codeToSk(String code) async { Future<dynamic> codeToSk(String code) async {
Response resp = await _apiService.post( try {
'/login/applet/wkbxe/codeToSkByApp?version=1.0.0', Response resp = await _apiService.post(
{ '/login/applet/wkbxe/codeToSkByApp?version=1.0.0',
"appCode": "bxeapp", {
"params": {"code": code}, "appCode": "bxeapp",
}, "params": {"code": code},
); },
);
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 {
Response resp = await _apiService.post( try {
'/login/applet/wkbxe/getTicketByApp?version=1.0.0', Response resp = await _apiService.post(
{ '/login/applet/wkbxe/getTicketByApp?version=1.0.0',
"appCode": "bxeapp", {
"params": {}, "appCode": "bxeapp",
}, "params": {},
); },
);
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!