Commit 23384a42aae14b48a65a828407d44d69c454fe78

Authored by liangchengyou
1 parent 22b7d4da

feat:登陆请求

lib/login/loginpage/bloc/login_bloc.dart
1 1 import 'package:flutter/cupertino.dart';
2 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 6 part 'login_event.dart';
5 7 part 'login_state.dart';
... ... @@ -40,7 +42,19 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
40 42  
41 43 ///请求登陆
42 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 18 BasicConfigurationManager._internal(){
19 19 DevelopEvent developType = DevelopEvent.dev;
20 20 if(developType == DevelopEvent.dev) {
21   - baseUrl = 'http://wow-app.dev.kouyuxingqiu.com';
  21 + baseUrl = 'http://wow-app.dev.kouyuxingqiu.com/';
22 22 } else {
23   - baseUrl = 'http://wow-app.dev.kouyuxingqiu.com';
  23 + baseUrl = 'http://wow-app.dev.kouyuxingqiu.com/';
24 24 }
25 25 sessionId = '';
26 26 }
... ...
lib/network/network_manager.dart
... ... @@ -5,6 +5,8 @@ import &#39;package:flutter/foundation.dart&#39;;
5 5 import 'package:flutter_easyloading/flutter_easyloading.dart';
6 6 import 'package:wow_english/network/basic_configuration.dart';
7 7  
  8 +import '../modes/response_model.dart';
  9 +
8 10 enum HttpMethod {
9 11 get,
10 12 put,
... ... @@ -35,7 +37,7 @@ class DioUtil {
35 37 Future<void> requestData<T>(
36 38 String path, {
37 39 data,
38   - HttpMethod method = HttpMethod.get,
  40 + HttpMethod method = HttpMethod.post,
39 41 Map<String, dynamic>? queryParameters,
40 42 ProgressCallback? onSendProgress,
41 43 ProgressCallback? onReceiveProgress,
... ... @@ -46,7 +48,7 @@ class DioUtil {
46 48 Map<String, dynamic> headers = <String, dynamic>{};
47 49  
48 50 if (method == HttpMethod.post) {
49   - headers['content-type'] = 'application/x-www-form-urlencoded';
  51 + headers['content-type'] = 'application/json';
50 52 }
51 53 Response<dynamic> response;
52 54 response = await _dio.request(
... ... @@ -56,9 +58,16 @@ class DioUtil {
56 58 options: Options(method: method.name,headers: headers),
57 59 onSendProgress: onSendProgress,
58 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 71 } else {
63 72 errorCallBack('请求失败');
64 73 }
... ...