account_logoff_cubit.dart
4.56 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
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:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AccountLogoffState extends Equatable {
final String phone;
final bool showSnackBar;
final String snackBarMsg;
final bool allowSend;
final int seconds;
final bool isLoading;
const AccountLogoffState({
this.phone = '',
this.showSnackBar = false,
this.snackBarMsg = '',
this.allowSend = true,
this.seconds = 0,
this.isLoading = false,
});
AccountLogoffState copyWith({
String? phone,
bool? showSnackBar,
String? snackBarMsg,
bool? allowSend,
int? seconds,
bool? isLoading,
}) {
return AccountLogoffState(
phone: phone ?? this.phone,
showSnackBar: showSnackBar ?? this.showSnackBar,
snackBarMsg: snackBarMsg ?? this.snackBarMsg,
allowSend: allowSend ?? this.allowSend,
seconds: seconds ?? this.seconds,
isLoading: isLoading ?? this.isLoading,
);
}
@override
List<Object?> get props => [
phone,
showSnackBar,
snackBarMsg,
allowSend,
seconds,
isLoading,
];
}
class AccountLogoffCubit extends Cubit<AccountLogoffState> {
late TextEditingController _codeController;
Timer? _timer;
int countdown = 60;
late final PhoneAuthRepository _phoneAuthRepository;
TextEditingController get codeController => _codeController;
AccountLogoffCubit(super.initialState) {
_codeController = TextEditingController();
_codeController.text = '';
_phoneAuthRepository = getIt.get<PhoneAuthRepository>();
}
/// 开始倒计时
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) {
if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(state.phone)) {
emit(state.copyWith(showSnackBar: true, snackBarMsg: '手机号码信息错误'));
emit(state.copyWith(showSnackBar: false));
return;
}
var result = await _phoneAuthRepository.verifyCode(state.phone, 1);
if (result['code'] != 0) {
emit(state.copyWith(showSnackBar: true, snackBarMsg: result['error']));
emit(state.copyWith(showSnackBar: false));
return;
}
emit(state.copyWith(allowSend: false, seconds: 60));
startCountdown();
}
}
/// 注销账户
Future<void> logoff() async {
// String verifyCode = _codeController.text;
if (!RegExp(r'^1[3-9][0-9]{9}$').hasMatch(state.phone)) {
emit(state.copyWith(showSnackBar: true, snackBarMsg: '手机号码信息错误'));
emit(state.copyWith(showSnackBar: false));
return;
}
// if (!RegExp(r'^\d{4}$').hasMatch(verifyCode)) {
// emit(state.copyWith(showSnackBar: true, snackBarMsg: '请输入正确的验证码'));
// emit(state.copyWith(showSnackBar: false));
// return;
// }
emit(state.copyWith(isLoading: true));
// var sharedPreferences = getIt.get<SharedPreferences>();
// var userCode = sharedPreferences.getString('auth_userCode') ?? '';
// var result = await _phoneAuthRepository.unbindWithVerifyCode(userCode, verifyCode);
var result = await _phoneAuthRepository.unbindPhone(state.phone);
emit(state.copyWith(isLoading: false));
if (result['code'] != 0) {
emit(state.copyWith(showSnackBar: true, snackBarMsg: result['error']));
emit(state.copyWith(showSnackBar: false));
return;
}
emit(state.copyWith(showSnackBar: true, snackBarMsg: '操作成功'));
emit(state.copyWith(showSnackBar: false));
await Future.delayed(Duration(seconds: 1));
var sharedPreferences = getIt.get<SharedPreferences>();
sharedPreferences.getKeys().forEach((key) {
if (key.startsWith('auth_')) {
sharedPreferences.remove(key);
}
});
router.go('/loginMain');
}
@override
Future<void> close() async {
_timer?.cancel();
try {
_codeController.dispose();
} catch (e) {
print(e);
}
await super.close();
}
}