Blame view

lib/pages/login/loginpage/bloc/login_bloc.dart 4.58 KB
fcc3a982   liangchengyou   feat:全局缓存bloc
1
2
  import 'dart:js';
  
062f0df2   liangchengyou   feat:登录模块代码提交
3
  import 'package:flutter/cupertino.dart';
2a29701f   liangchengyou   feat:提交代码
4
  import 'package:flutter_bloc/flutter_bloc.dart';
056970d8   Key   feat: api
5
6
  import 'package:flutter_easyloading/flutter_easyloading.dart';
  
4b358e22   liangchengyou   feat:调整文件结构
7
8
9
10
11
  import '../../../../common/request/api_response/api_response_entity.dart';
  import '../../../../common/request/apis.dart';
  import '../../../../common/request/request.dart';
  import '../../../../common/request/request_client.dart';
  import '../../../../models/user_entity.dart';
2a29701f   liangchengyou   feat:提交代码
12
13
14
15
  
  part 'login_event.dart';
  part 'login_state.dart';
  
062f0df2   liangchengyou   feat:登录模块代码提交
16
17
18
  enum LoginType {
    ///密码登陆
    pwd,
056970d8   Key   feat: api
19
  
062f0df2   liangchengyou   feat:登录模块代码提交
20
21
22
23
    ///验证码登陆
    sms,
  }
  
2a29701f   liangchengyou   feat:提交代码
24
  class LoginBloc extends Bloc<LoginEvent, LoginState> {
062f0df2   liangchengyou   feat:登录模块代码提交
25
    bool _canLogin = false;
056970d8   Key   feat: api
26
  
062f0df2   liangchengyou   feat:登录模块代码提交
27
28
    ///是否可以发送验证码
    bool _canSendSms = false;
056970d8   Key   feat: api
29
  
062f0df2   liangchengyou   feat:登录模块代码提交
30
31
    ///是否阅读协议
    bool _agreement = false;
056970d8   Key   feat: api
32
  
062f0df2   liangchengyou   feat:登录模块代码提交
33
    ///登陆方式
056970d8   Key   feat: api
34
35
    //LoginType _loginType = LoginType.sms;
    bool _isSmsLoginType = true;
062f0df2   liangchengyou   feat:登录模块代码提交
36
  
062f0df2   liangchengyou   feat:登录模块代码提交
37
38
    final TextEditingController phoneNumController = TextEditingController();
    final TextEditingController checkNumController = TextEditingController();
2a29701f   liangchengyou   feat:提交代码
39
  
062f0df2   liangchengyou   feat:登录模块代码提交
40
    bool get canLogin => _canLogin;
056970d8   Key   feat: api
41
  
062f0df2   liangchengyou   feat:登录模块代码提交
42
    bool get agreement => _agreement;
056970d8   Key   feat: api
43
44
45
46
47
  
    //LoginType get loginType => _loginType;
  
    bool get isSmsLoginType => _isSmsLoginType;
  
062f0df2   liangchengyou   feat:登录模块代码提交
48
    bool get canSendSms => _canSendSms;
2a29701f   liangchengyou   feat:提交代码
49
50
51
  
    LoginBloc() : super(LoginInitial()) {
      on<RequestLoginEvent>(_requestLoginApi);
062f0df2   liangchengyou   feat:登录模块代码提交
52
53
54
      on<ChangeLoginTypeEvent>(_changeLoginType);
      on<PhoneNumChangeEvent>(_changePhoneNumber);
      on<AgreementChangeEvent>(_agreementTypeChange);
062f0df2   liangchengyou   feat:登录模块代码提交
55
      on<CheckFieldChangeEvent>(_checkFiledChange);
7912b3f7   liangchengyou   feat:更新代码
56
      on<RequestSmsCodeEvent>(_requestSmsCodeApi);
2a29701f   liangchengyou   feat:提交代码
57
58
    }
  
062f0df2   liangchengyou   feat:登录模块代码提交
59
    ///请求登陆
2a29701f   liangchengyou   feat:提交代码
60
    void _requestLoginApi(RequestLoginEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
      var phoneNumber = phoneNumController.text;
      if (phoneNumber.isEmpty) {
        EasyLoading.showToast('号码不能为空');
        return;
      }
  
      var checkNumber = checkNumController.text;
      if (checkNumber.isEmpty) {
        var text = _isSmsLoginType ? '密码' : '验证码';
        EasyLoading.showToast('$text不能为空');
        return;
      }
      var checkKey = _isSmsLoginType ? 'smsCode' : 'password';
      var type = _isSmsLoginType ? 'sms_code' : 'pwd';
  
      request(() async {
        var params = {'phoneNum': phoneNumber, 'type': type, checkKey: checkNumber};
        await requestClient.post(
          Apis.login,
          data: params,
          onResponse: (ApiResponse<UserEntity> response) {
            print('response=$response');
            // todo 持久化用户对象
            // todo 写入全局对象
            //emitter.call(LoginResultChangeState());
          },
          onError: (e) {
            EasyLoading.showToast('登陆失败:${e.message}');
            return true;
23384a42   liangchengyou   feat:登陆请求
90
          },
056970d8   Key   feat: api
91
92
        );
      });
062f0df2   liangchengyou   feat:登录模块代码提交
93
94
    }
  
7912b3f7   liangchengyou   feat:更新代码
95
96
97
98
99
    ///请求验证码
    void _requestSmsCodeApi(RequestSmsCodeEvent event, Emitter<LoginState> emitter) async {
  
    }
  
062f0df2   liangchengyou   feat:登录模块代码提交
100
101
    ///切换登陆方式
    void _changeLoginType(ChangeLoginTypeEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
102
103
      if (_isSmsLoginType) {
        _isSmsLoginType = false;
062f0df2   liangchengyou   feat:登录模块代码提交
104
      } else {
056970d8   Key   feat: api
105
        _isSmsLoginType = true;
062f0df2   liangchengyou   feat:登录模块代码提交
106
107
108
109
110
111
112
113
114
115
      }
      checkNumController.clear();
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
      emitter(LoginTypeChangeState());
    }
  
    ///手机号输入
    void _changePhoneNumber(PhoneNumChangeEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
116
      if (phoneNumController.text.isNotEmpty) {
062f0df2   liangchengyou   feat:登录模块代码提交
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        if (!_canSendSms) {
          _canSendSms = true;
          emitter(SmsSendTypeChangeState());
        }
        if (_loginStateChange()) {
          emitter(LoginEventChangeState());
        }
      } else {
        if (_canSendSms) {
          _canSendSms = false;
          emitter(SmsSendTypeChangeState());
        }
        if (_loginStateChange()) {
          emitter(LoginEventChangeState());
        }
      }
    }
  
    ///验证码/密码输入变化
056970d8   Key   feat: api
136
    void _checkFiledChange(CheckFieldChangeEvent event, Emitter<LoginState> emitter) async {
062f0df2   liangchengyou   feat:登录模块代码提交
137
138
139
140
141
142
143
144
145
146
147
148
149
150
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
    ///是否阅读协议
    void _agreementTypeChange(AgreementChangeEvent event, Emitter<LoginState> emitter) async {
      _agreement = !_agreement;
      emitter(AgreementTypeChangeState());
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
bfb40cd0   liangchengyou   feat:忘记密码获取验证码
151
    ///登陆状态判断
062f0df2   liangchengyou   feat:登录模块代码提交
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    bool _loginStateChange() {
      if (_agreement) {
        if (phoneNumController.text.isNotEmpty && checkNumController.text.isNotEmpty) {
          if (!_canLogin) {
            _canLogin = true;
            return true;
          }
        } else {
          if (_canLogin) {
            _canLogin = false;
            return true;
          }
        }
      } else {
        if (_canLogin) {
          _canLogin = false;
          return true;
        }
      }
      return false;
2a29701f   liangchengyou   feat:提交代码
172
173
    }
  }