account_cubit.dart
3.46 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
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_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AccountState extends Equatable {
final bool loaded;
final String name;
final String phone;
final String nickname;
final String imgIcon;
const AccountState({
this.loaded = false,
this.name = '',
this.phone = '',
this.nickname = '',
this.imgIcon = '',
});
AccountState copyWith({
bool? loaded,
String? name,
String? phone,
String? nickname,
String? imgIcon,
}) {
return AccountState(
loaded: loaded ?? this.loaded,
name: name ?? this.name,
phone: phone ?? this.phone,
nickname: nickname ?? this.nickname,
imgIcon: imgIcon ?? this.imgIcon,
);
}
@override
List<Object?> get props => [
loaded,
name,
phone,
nickname,
imgIcon,
];
}
class AccountCubit extends Cubit<AccountState> {
late final PhoneAuthRepository _phoneAuthRepository;
AccountCubit(super.initialState) {
_phoneAuthRepository = getIt.get<PhoneAuthRepository>();
init();
}
Future<void> init() async {
var sharedPreferences = getIt.get<SharedPreferences>();
var userCode = sharedPreferences.getString('auth_userCode') ?? '';
try {
var result = await _phoneAuthRepository.bindCheck(userCode);
var code = result['code'];
var data = result['data'];
if (code != 0) {
return;
}
emit(
state.copyWith(
loaded: true,
name: data['name'],
phone: data['phone'],
nickname: data['nickname'],
imgIcon: data['imgIcon'],
),
);
} catch (e) {
print(e);
}
}
Future<void> goSetUserInfo() async {
dynamic result = await router.push(
'/account/user',
extra: {
'name': state.name,
'nickname': state.nickname,
'avatar': state.imgIcon,
},
);
if (result != null && result.isNotEmpty) {
Map<String, dynamic> resultMap = Map<String, dynamic>.from(result);
emit(state.copyWith(imgIcon: resultMap['avatar'], name: resultMap['name'], nickname: resultMap['nickname']));
}
}
Future<void> goBind() async {
String? result = await router.push(
'/account/phone',
extra: {
'phone': state.phone,
},
);
if (result != null && result.isNotEmpty) {
emit(state.copyWith(phone: result));
}
}
void goLogoff() {
router.push(
'/account/logoff',
extra: {
'phone': state.phone,
},
);
}
Future<void> unbind() async {
// var sharedPreferences = getIt.get<SharedPreferences>();
// var userCode = sharedPreferences.getString('auth_userCode') ?? '';
// _phoneAuthRepository.unbind(userCode);
// 当前只会成功,不会失败
// 解绑成功,跳转登录界面
// router.go('/loginMain');
// 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');
}
}