Blame view

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