Commit 23384a42aae14b48a65a828407d44d69c454fe78
1 parent
22b7d4da
feat:登陆请求
Showing
3 changed files
with
31 additions
and
8 deletions
lib/login/loginpage/bloc/login_bloc.dart
| 1 | import 'package:flutter/cupertino.dart'; | 1 | import 'package:flutter/cupertino.dart'; |
| 2 | import 'package:flutter_bloc/flutter_bloc.dart'; | 2 | import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:wow_english/network/api.dart'; | ||
| 4 | +import 'package:wow_english/network/network_manager.dart'; | ||
| 3 | 5 | ||
| 4 | part 'login_event.dart'; | 6 | part 'login_event.dart'; |
| 5 | part 'login_state.dart'; | 7 | part 'login_state.dart'; |
| @@ -40,7 +42,19 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> { | @@ -40,7 +42,19 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> { | ||
| 40 | 42 | ||
| 41 | ///请求登陆 | 43 | ///请求登陆 |
| 42 | void _requestLoginApi(RequestLoginEvent event, Emitter<LoginState> emitter) async { | 44 | void _requestLoginApi(RequestLoginEvent event, Emitter<LoginState> emitter) async { |
| 43 | - emitter(LoginResultChangeState()); | 45 | + DioUtil().requestData( |
| 46 | + Api.login, | ||
| 47 | + data: { | ||
| 48 | + 'phoneNum':'17730280759', | ||
| 49 | + 'type':_loginType.toString(), | ||
| 50 | + 'password':'asd123456'}, | ||
| 51 | + successCallBack: (data){ | ||
| 52 | + | ||
| 53 | + }, | ||
| 54 | + errorCallBack: (error){ | ||
| 55 | + | ||
| 56 | + }); | ||
| 57 | + // emitter(LoginResultChangeState()); | ||
| 44 | } | 58 | } |
| 45 | 59 | ||
| 46 | ///切换登陆方式 | 60 | ///切换登陆方式 |
lib/network/basic_configuration.dart
| @@ -18,9 +18,9 @@ class BasicConfigurationManager { | @@ -18,9 +18,9 @@ class BasicConfigurationManager { | ||
| 18 | BasicConfigurationManager._internal(){ | 18 | BasicConfigurationManager._internal(){ |
| 19 | DevelopEvent developType = DevelopEvent.dev; | 19 | DevelopEvent developType = DevelopEvent.dev; |
| 20 | if(developType == DevelopEvent.dev) { | 20 | if(developType == DevelopEvent.dev) { |
| 21 | - baseUrl = 'http://wow-app.dev.kouyuxingqiu.com'; | 21 | + baseUrl = 'http://wow-app.dev.kouyuxingqiu.com/'; |
| 22 | } else { | 22 | } else { |
| 23 | - baseUrl = 'http://wow-app.dev.kouyuxingqiu.com'; | 23 | + baseUrl = 'http://wow-app.dev.kouyuxingqiu.com/'; |
| 24 | } | 24 | } |
| 25 | sessionId = ''; | 25 | sessionId = ''; |
| 26 | } | 26 | } |
lib/network/network_manager.dart
| @@ -5,6 +5,8 @@ import 'package:flutter/foundation.dart'; | @@ -5,6 +5,8 @@ import 'package:flutter/foundation.dart'; | ||
| 5 | import 'package:flutter_easyloading/flutter_easyloading.dart'; | 5 | import 'package:flutter_easyloading/flutter_easyloading.dart'; |
| 6 | import 'package:wow_english/network/basic_configuration.dart'; | 6 | import 'package:wow_english/network/basic_configuration.dart'; |
| 7 | 7 | ||
| 8 | +import '../modes/response_model.dart'; | ||
| 9 | + | ||
| 8 | enum HttpMethod { | 10 | enum HttpMethod { |
| 9 | get, | 11 | get, |
| 10 | put, | 12 | put, |
| @@ -35,7 +37,7 @@ class DioUtil { | @@ -35,7 +37,7 @@ class DioUtil { | ||
| 35 | Future<void> requestData<T>( | 37 | Future<void> requestData<T>( |
| 36 | String path, { | 38 | String path, { |
| 37 | data, | 39 | data, |
| 38 | - HttpMethod method = HttpMethod.get, | 40 | + HttpMethod method = HttpMethod.post, |
| 39 | Map<String, dynamic>? queryParameters, | 41 | Map<String, dynamic>? queryParameters, |
| 40 | ProgressCallback? onSendProgress, | 42 | ProgressCallback? onSendProgress, |
| 41 | ProgressCallback? onReceiveProgress, | 43 | ProgressCallback? onReceiveProgress, |
| @@ -46,7 +48,7 @@ class DioUtil { | @@ -46,7 +48,7 @@ class DioUtil { | ||
| 46 | Map<String, dynamic> headers = <String, dynamic>{}; | 48 | Map<String, dynamic> headers = <String, dynamic>{}; |
| 47 | 49 | ||
| 48 | if (method == HttpMethod.post) { | 50 | if (method == HttpMethod.post) { |
| 49 | - headers['content-type'] = 'application/x-www-form-urlencoded'; | 51 | + headers['content-type'] = 'application/json'; |
| 50 | } | 52 | } |
| 51 | Response<dynamic> response; | 53 | Response<dynamic> response; |
| 52 | response = await _dio.request( | 54 | response = await _dio.request( |
| @@ -56,9 +58,16 @@ class DioUtil { | @@ -56,9 +58,16 @@ class DioUtil { | ||
| 56 | options: Options(method: method.name,headers: headers), | 58 | options: Options(method: method.name,headers: headers), |
| 57 | onSendProgress: onSendProgress, | 59 | onSendProgress: onSendProgress, |
| 58 | onReceiveProgress: onReceiveProgress); | 60 | onReceiveProgress: onReceiveProgress); |
| 59 | - if (response.statusCode == HttpStatus.ok || | ||
| 60 | - response.statusCode == HttpStatus.created) { | ||
| 61 | - successCallBack(response.data); | 61 | + if (response.statusCode == HttpStatus.ok || response.statusCode == HttpStatus.created) { |
| 62 | + if (kDebugMode) { | ||
| 63 | + print(response.data); | ||
| 64 | + } | ||
| 65 | + final ResponseModel model = ResponseModel.fromJson(response.data); | ||
| 66 | + if (model.code != 200) { | ||
| 67 | + errorCallBack(model.msg); | ||
| 68 | + } else { | ||
| 69 | + successCallBack(response.data); | ||
| 70 | + } | ||
| 62 | } else { | 71 | } else { |
| 63 | errorCallBack('请求失败'); | 72 | errorCallBack('请求失败'); |
| 64 | } | 73 | } |