Blame view

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