account_phone_cubit.dart
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import 'dart:async';
import 'package:appframe/config/locator.dart';
import 'package:appframe/config/routes.dart';
import 'package:appframe/data/repositories/phone_auth_repository.dart';
import 'package:appframe/data/repositories/user_auth_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AccountPhoneState extends Equatable {
final String phone;
final bool allowBind;
final bool allowSend;
final int seconds;
final bool allowSubmit;
const AccountPhoneState({
this.phone = '',
this.allowBind = false,
this.allowSend = true,
this.seconds = 0,
this.allowSubmit = false,
});
AccountPhoneState copyWith({
String? phone,
bool? allowBind,
bool? allowSend,
int? seconds,
bool? allowSubmit,
}) {
return AccountPhoneState(
phone: phone ?? this.phone,
allowBind: allowBind ?? this.allowBind,
allowSend: allowSend ?? this.allowSend,
seconds: seconds ?? this.seconds,
allowSubmit: allowSubmit ?? this.allowSubmit,
);
}
@override
List<Object?> get props => [
phone,
allowBind,
allowSend,
seconds,
allowSubmit,
];
}
class AccountPhoneCubit extends Cubit<AccountPhoneState> {
late TextEditingController _phoneController;
late TextEditingController _codeController;
Timer? _timer;
int countdown = 60; // 倒计时秒数
late final PhoneAuthRepository _phoneAuthRepository;
late final UserAuthRepository _userAuthRepository;
TextEditingController get phoneController => _phoneController;
TextEditingController get codeController => _codeController;
AccountPhoneCubit(super.initialState) {
_phoneController = TextEditingController();
_codeController = TextEditingController();
_phoneController.text = '';
_codeController.text = '';
_phoneAuthRepository = getIt.get<PhoneAuthRepository>();
_userAuthRepository = getIt.get<UserAuthRepository>();
_phoneController.addListener(_updateAllowSubmit);
_codeController.addListener(_updateAllowSubmit);
}
void _updateAllowSubmit() {
final phoneOk = RegExp(r'^1[3-9][0-9]{9}$').hasMatch(_phoneController.text);
final codeOk = RegExp(r'^\d{4}$').hasMatch(_codeController.text);
emit(state.copyWith(allowSubmit: phoneOk && codeOk));
}
void change(){
emit(state.copyWith(allowBind: true));
}
/// 开始倒计时
void startCountdown() {
countdown = 60;
emit(state.copyWith(allowSend: false, seconds: countdown));
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
countdown--;
if (countdown <= 0) {
_timer?.cancel();
emit(state.copyWith(allowSend: true, seconds: 60));
} else {
emit(state.copyWith(seconds: countdown));
}
});
}
/// 发送验证码
Future<void> sendVerificationCode() async {
if (state.allowSend) {
// 验证手机号码
String phone = _phoneController.text;
if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(phone)) {
Fluttertoast.showToast(msg: '请输入正确的手机号码');
return;
}
// 发送验证码
var result = await _phoneAuthRepository.verifyCode(phone, 1);
if (result['code'] != 0) {
Fluttertoast.showToast(msg: result['error']);
return;
}
emit(state.copyWith(allowSend: false, seconds: 60));
// 开始倒计时
startCountdown();
}
}
Future<void> bind() async {
String phone = _phoneController.text;
String verifyCode = _codeController.text;
if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(phone)) {
Fluttertoast.showToast(msg: '请输入正确的手机号码');
return;
}
if (!RegExp(r'^\d{4}$').hasMatch(verifyCode)) {
Fluttertoast.showToast(msg: '请输入正确的验证码');
return;
}
var sharedPreferences = getIt.get<SharedPreferences>();
var userCode = sharedPreferences.getString('auth_userCode') ?? '';
var result = await _phoneAuthRepository.bind(userCode, phone, verifyCode);
if (result['code'] != 0) {
Fluttertoast.showToast(msg: result['error']);
return;
}
// 绑定成功,返回手机号码
router.pop({'phone': phone});
}
Future<void> unbindPhone() async {
var result = await _userAuthRepository.unbind(null, state.phone);
if(result==null) {
Fluttertoast.showToast(msg: '解绑失败');
return;
}
if (result['code'] != 0) {
Fluttertoast.showToast(msg: result['error']);
return;
}
// 解绑成功,路由出栈
router.pop({'phone': ''});
}
@override
Future<void> close() async {
try {
_phoneController.dispose();
} catch (e) {
print(e);
}
try {
_codeController.dispose();
} catch (e) {
print(e);
}
await super.close();
}
}