Blame view

lib/pages/login/loginpage/bloc/login_bloc.dart 4.94 KB
39e06486   liangchengyou   feat:获取验证码逻辑处理
1
  import 'package:common_utils/common_utils.dart';
062f0df2   liangchengyou   feat:登录模块代码提交
2
  import 'package:flutter/cupertino.dart';
1892df31   Key   优化接口调用
3
  import 'package:flutter/foundation.dart';
2a29701f   liangchengyou   feat:提交代码
4
  import 'package:flutter_bloc/flutter_bloc.dart';
1892df31   Key   优化接口调用
5
  import 'package:wow_english/common/extension/string_extension.dart';
05f9b20a   Key   fixed: api调用方式,未完善
6
  import 'package:wow_english/common/request/dao/user_dao.dart';
2b55d503   liangchengyou   feat:添加插件
7
  import 'package:wow_english/common/request/exception.dart';
05f9b20a   Key   fixed: api调用方式,未完善
8
9
  import 'package:wow_english/models/user_entity.dart';
  import 'package:wow_english/utils/loading.dart';
c61b3c1a   Key   feat: toast_util....
10
  import 'package:wow_english/utils/toast_util.dart';
2a29701f   liangchengyou   feat:提交代码
11
12
13
14
  
  part 'login_event.dart';
  part 'login_state.dart';
  
062f0df2   liangchengyou   feat:登录模块代码提交
15
16
17
  enum LoginType {
    ///密码登陆
    pwd,
056970d8   Key   feat: api
18
  
062f0df2   liangchengyou   feat:登录模块代码提交
19
20
21
22
    ///验证码登陆
    sms,
  }
  
2a29701f   liangchengyou   feat:提交代码
23
  class LoginBloc extends Bloc<LoginEvent, LoginState> {
062f0df2   liangchengyou   feat:登录模块代码提交
24
    bool _canLogin = false;
056970d8   Key   feat: api
25
  
062f0df2   liangchengyou   feat:登录模块代码提交
26
27
    ///是否可以发送验证码
    bool _canSendSms = false;
056970d8   Key   feat: api
28
  
062f0df2   liangchengyou   feat:登录模块代码提交
29
30
    ///是否阅读协议
    bool _agreement = false;
056970d8   Key   feat: api
31
  
062f0df2   liangchengyou   feat:登录模块代码提交
32
    ///登陆方式
056970d8   Key   feat: api
33
34
    //LoginType _loginType = LoginType.sms;
    bool _isSmsLoginType = true;
062f0df2   liangchengyou   feat:登录模块代码提交
35
  
062f0df2   liangchengyou   feat:登录模块代码提交
36
37
    final TextEditingController phoneNumController = TextEditingController();
    final TextEditingController checkNumController = TextEditingController();
2a29701f   liangchengyou   feat:提交代码
38
  
062f0df2   liangchengyou   feat:登录模块代码提交
39
    bool get canLogin => _canLogin;
056970d8   Key   feat: api
40
  
062f0df2   liangchengyou   feat:登录模块代码提交
41
    bool get agreement => _agreement;
056970d8   Key   feat: api
42
43
44
45
46
  
    //LoginType get loginType => _loginType;
  
    bool get isSmsLoginType => _isSmsLoginType;
  
062f0df2   liangchengyou   feat:登录模块代码提交
47
    bool get canSendSms => _canSendSms;
2a29701f   liangchengyou   feat:提交代码
48
49
50
  
    LoginBloc() : super(LoginInitial()) {
      on<RequestLoginEvent>(_requestLoginApi);
062f0df2   liangchengyou   feat:登录模块代码提交
51
52
53
      on<ChangeLoginTypeEvent>(_changeLoginType);
      on<PhoneNumChangeEvent>(_changePhoneNumber);
      on<AgreementChangeEvent>(_agreementTypeChange);
062f0df2   liangchengyou   feat:登录模块代码提交
54
      on<CheckFieldChangeEvent>(_checkFiledChange);
7912b3f7   liangchengyou   feat:更新代码
55
      on<RequestSmsCodeEvent>(_requestSmsCodeApi);
2a29701f   liangchengyou   feat:提交代码
56
57
    }
  
062f0df2   liangchengyou   feat:登录模块代码提交
58
    ///请求登陆
2a29701f   liangchengyou   feat:提交代码
59
    void _requestLoginApi(RequestLoginEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
60
61
      var phoneNumber = phoneNumController.text;
      if (phoneNumber.isEmpty) {
c61b3c1a   Key   feat: toast_util....
62
        showToast('号码不能为空');
056970d8   Key   feat: api
63
64
65
66
67
68
        return;
      }
  
      var checkNumber = checkNumController.text;
      if (checkNumber.isEmpty) {
        var text = _isSmsLoginType ? '密码' : '验证码';
c61b3c1a   Key   feat: toast_util....
69
        showToast('$text不能为空');
056970d8   Key   feat: api
70
71
72
73
74
        return;
      }
      var checkKey = _isSmsLoginType ? 'smsCode' : 'password';
      var type = _isSmsLoginType ? 'sms_code' : 'pwd';
  
05f9b20a   Key   fixed: api调用方式,未完善
75
      try {
05f9b20a   Key   fixed: api调用方式,未完善
76
77
        await loading(() async {
          var user = await UserDao.login(phoneNumber, type, checkKey, checkNumber);
1892df31   Key   优化接口调用
78
79
80
          if (kDebugMode) {
            print('UserEntity?=$user');
          }
94342c3f   Key   feat: user util
81
          emitter.call(LoginResultChangeState(user!));
05f9b20a   Key   fixed: api调用方式,未完善
82
83
        });
      } catch (e) {
1892df31   Key   优化接口调用
84
85
86
        if (kDebugMode) {
          print(e);
        }
c61b3c1a   Key   feat: toast_util....
87
        showToast('登陆失败${(e as ApiException?)?.message?.prefixColon}');
05f9b20a   Key   fixed: api调用方式,未完善
88
      }
062f0df2   liangchengyou   feat:登录模块代码提交
89
90
    }
  
7912b3f7   liangchengyou   feat:更新代码
91
    ///请求验证码
39e06486   liangchengyou   feat:获取验证码逻辑处理
92
93
94
    void _requestSmsCodeApi(RequestSmsCodeEvent event, Emitter<LoginState> emitter) async {
      final phoneNumber = phoneNumController.text;
      if (!RegexUtil.isMobileExact(phoneNumber)) {
c61b3c1a   Key   feat: toast_util....
95
        showToast('请检查手机号');
39e06486   liangchengyou   feat:获取验证码逻辑处理
96
97
98
99
100
101
102
103
        return;
      }
      try {
        await loading(() async {
          await UserDao.sendCode(phoneNumber);
        });
        emitter(SmsCodeRequestState());
      } catch (e) {
1892df31   Key   优化接口调用
104
105
106
        if (kDebugMode) {
          print(e);
        }
c61b3c1a   Key   feat: toast_util....
107
        showToast('获取验证码失败${(e as ApiException?)?.message?.prefixColon}');
39e06486   liangchengyou   feat:获取验证码逻辑处理
108
109
      }
    }
7912b3f7   liangchengyou   feat:更新代码
110
  
062f0df2   liangchengyou   feat:登录模块代码提交
111
112
    ///切换登陆方式
    void _changeLoginType(ChangeLoginTypeEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
113
114
      if (_isSmsLoginType) {
        _isSmsLoginType = false;
062f0df2   liangchengyou   feat:登录模块代码提交
115
      } else {
056970d8   Key   feat: api
116
        _isSmsLoginType = true;
062f0df2   liangchengyou   feat:登录模块代码提交
117
118
119
120
121
122
123
124
125
126
      }
      checkNumController.clear();
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
      emitter(LoginTypeChangeState());
    }
  
    ///手机号输入
    void _changePhoneNumber(PhoneNumChangeEvent event, Emitter<LoginState> emitter) async {
056970d8   Key   feat: api
127
      if (phoneNumController.text.isNotEmpty) {
062f0df2   liangchengyou   feat:登录模块代码提交
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
        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
147
    void _checkFiledChange(CheckFieldChangeEvent event, Emitter<LoginState> emitter) async {
062f0df2   liangchengyou   feat:登录模块代码提交
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
    ///是否阅读协议
    void _agreementTypeChange(AgreementChangeEvent event, Emitter<LoginState> emitter) async {
      _agreement = !_agreement;
      emitter(AgreementTypeChangeState());
      if (_loginStateChange()) {
        emitter(LoginEventChangeState());
      }
    }
  
bfb40cd0   liangchengyou   feat:忘记密码获取验证码
162
    ///登陆状态判断
062f0df2   liangchengyou   feat:登录模块代码提交
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    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:提交代码
183
184
    }
  }