Commit aa4e28be2a38f2d373a27a01a40e04e4612bc92f
1 parent
2eb67dd4
removed: cache_bloc.dart
用户相关操作请使用UserUtil
Showing
12 changed files
with
46 additions
and
86 deletions
lib/app/app.dart
... | ... | @@ -4,7 +4,6 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; |
4 | 4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
5 | 5 | import 'package:responsive_framework/breakpoint.dart'; |
6 | 6 | import 'package:responsive_framework/responsive_breakpoints.dart'; |
7 | -import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
8 | 7 | import 'package:wow_english/common/widgets/hide_keyboard_widget.dart'; |
9 | 8 | import 'package:wow_english/pages/tab/blocs/tab_bloc.dart'; |
10 | 9 | import 'package:wow_english/route/route.dart'; |
... | ... | @@ -19,7 +18,6 @@ class App extends StatelessWidget { |
19 | 18 | builder: (_, __) => MultiBlocProvider( |
20 | 19 | providers: [ |
21 | 20 | BlocProvider<TabBloc>(create: (_) => TabBloc()), |
22 | - BlocProvider<CacheBloc>(create: (_) => CacheBloc()) | |
23 | 21 | ], |
24 | 22 | child: HideKeyboard( |
25 | 23 | child: MaterialApp( | ... | ... |
lib/app/splash_page.dart
... | ... | @@ -2,15 +2,12 @@ import 'dart:async'; |
2 | 2 | |
3 | 3 | import 'package:flutter/foundation.dart'; |
4 | 4 | import 'package:flutter/material.dart'; |
5 | -import 'package:flutter_bloc/flutter_bloc.dart'; | |
6 | 5 | import 'package:wow_english/common/core/user_util.dart'; |
7 | 6 | import 'package:wow_english/common/extension/string_extension.dart'; |
8 | 7 | import 'package:wow_english/models/user_entity.dart'; |
9 | 8 | import 'package:wow_english/route/route.dart'; |
10 | 9 | import 'package:wow_english/utils/sp_util.dart'; |
11 | 10 | |
12 | -import '../common/blocs/cachebloc/cache_bloc.dart'; | |
13 | - | |
14 | 11 | class SplashPage extends StatelessWidget { |
15 | 12 | const SplashPage({super.key}); |
16 | 13 | |
... | ... | @@ -34,11 +31,11 @@ class _TransitionViewState extends State<TransitionView> { |
34 | 31 | // 判断是否登录 |
35 | 32 | UserEntity? userEntity = UserUtil.getUser(); |
36 | 33 | if (kDebugMode) { |
37 | - print('userEntity: $userEntity'); | |
34 | + print('Splash读本地userEntity: $userEntity'); | |
38 | 35 | } |
39 | 36 | Timer(const Duration(seconds: 2), () { |
40 | - if (userEntity != null && userEntity.token.isNotEmpty) { | |
41 | - context.read<CacheBloc>().add(UserInfoChangeEvent(userEntity)); | |
37 | + if (userEntity != null) { | |
38 | + // todo 调一下接口判断一下有效性再往下 | |
42 | 39 | Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.home, (route) => false); |
43 | 40 | } else { |
44 | 41 | Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false); | ... | ... |
lib/common/blocs/cachebloc/cache_bloc.dart
1 | -import 'package:flutter/cupertino.dart'; | |
2 | 1 | import 'package:flutter_bloc/flutter_bloc.dart'; |
3 | -import 'package:wow_english/models/user_entity.dart'; | |
4 | 2 | |
5 | 3 | part 'cache_event.dart'; |
6 | 4 | part 'cache_state.dart'; |
7 | 5 | |
8 | 6 | class CacheBloc extends Bloc<CacheEvent, CacheState> { |
9 | - UserEntity? _userEntity; | |
10 | - | |
11 | - UserEntity? get userEntity => _userEntity; | |
12 | - | |
13 | - CacheBloc() : super(CacheInitial()) { | |
14 | - on<UserInfoChangeEvent>(_userInfoChange); | |
15 | - on<UserInfoClearEvent>(_userInfoClear); | |
16 | - } | |
17 | - | |
18 | - void _userInfoChange(UserInfoChangeEvent event,Emitter<CacheState> emitter) async { | |
19 | - _userEntity = event.userEntity; | |
20 | - emitter(UserInfoChangeState()); | |
21 | - } | |
22 | - | |
23 | - void _userInfoClear(UserInfoClearEvent event,Emitter<CacheState> emitter) async { | |
24 | - _userEntity = null; | |
25 | - emitter(UserInfoClearState()); | |
26 | - } | |
7 | + CacheBloc() : super(CacheInitial()); | |
27 | 8 | } | ... | ... |
lib/common/blocs/cachebloc/cache_event.dart
1 | 1 | part of 'cache_bloc.dart'; |
2 | 2 | |
3 | -@immutable | |
4 | -abstract class CacheEvent {} | |
5 | - | |
6 | -class UserInfoChangeEvent extends CacheEvent{ | |
7 | - UserEntity? userEntity; | |
8 | - UserInfoChangeEvent(this.userEntity); | |
9 | -} | |
10 | - | |
11 | -class UserInfoClearEvent extends CacheEvent {} | |
3 | +sealed class CacheEvent {} | ... | ... |
lib/common/blocs/cachebloc/cache_state.dart
lib/common/core/user_util.dart
1 | 1 | import 'dart:convert'; |
2 | 2 | |
3 | +import 'package:flutter/foundation.dart'; | |
3 | 4 | import 'package:flutter/material.dart'; |
4 | 5 | import 'package:wow_english/common/core/sp_const.dart'; |
5 | 6 | import 'package:wow_english/models/user_entity.dart'; |
... | ... | @@ -7,44 +8,49 @@ import 'package:wow_english/route/route.dart'; |
7 | 8 | import 'package:wow_english/utils/sp_util.dart'; |
8 | 9 | |
9 | 10 | class UserUtil { |
10 | - static String token = ''; | |
11 | + static UserEntity? _userEntity; | |
12 | + | |
13 | + static String get token => _userEntity?.token ?? ''; | |
11 | 14 | |
12 | 15 | static void saveUser(UserEntity user) { |
13 | - token = user.token; | |
14 | - saveUserJson(user.toString()); | |
16 | + _userEntity = user; | |
17 | + _saveUserJson(user.toString()); | |
15 | 18 | } |
16 | 19 | |
17 | 20 | static UserEntity? getUser() { |
18 | - String? userJson = getUserJson(); | |
19 | - if (userJson != null && userJson.isNotEmpty) { | |
20 | - var userEntity = UserEntity.fromJson(json.decode(userJson)); | |
21 | - // todo 并且在有效期,计算一下, "expireTime": 2592000 倒计时需要手动转换,后面再优化 | |
22 | - if (userEntity.token.isNotEmpty) { | |
23 | - token = userEntity.token; | |
24 | - return userEntity; | |
25 | - } else { | |
26 | - // token为空或失效的,清除sp | |
27 | - clearUserSp(); | |
21 | + if (_userEntity == null) { | |
22 | + String? userJson = _getUserJson(); | |
23 | + if (userJson != null && userJson.isNotEmpty) { | |
24 | + try { | |
25 | + var userEntity = UserEntity.fromJson(json.decode(userJson)); | |
26 | + // todo 并且在有效期,计算一下, "expireTime": 2592000 倒计时需要手动转换,后面再优化 | |
27 | + _userEntity = userEntity; | |
28 | + } catch (e) { | |
29 | + if (kDebugMode) { | |
30 | + print(e); | |
31 | + } | |
32 | + _clearUserData(); | |
33 | + } | |
28 | 34 | } |
29 | 35 | } |
30 | - return null; | |
36 | + return _userEntity; | |
31 | 37 | } |
32 | 38 | |
33 | - static void saveUserJson(String userJson) { | |
39 | + static void _saveUserJson(String userJson) { | |
34 | 40 | SpUtil.getInstance().setData(SpConst.prefsKeyUserInfo, userJson); |
35 | 41 | } |
36 | 42 | |
37 | - static String? getUserJson() { | |
43 | + static String? _getUserJson() { | |
38 | 44 | return SpUtil.getInstance().get<String>(SpConst.prefsKeyUserInfo); |
39 | 45 | } |
40 | 46 | |
41 | - static void clearUserSp() { | |
42 | - token = ''; | |
47 | + static void _clearUserData() { | |
48 | + _userEntity = null; | |
43 | 49 | SpUtil.getInstance().remove(SpConst.prefsKeyUserInfo); |
44 | 50 | } |
45 | 51 | |
46 | 52 | static void logout() { |
47 | - clearUserSp(); | |
53 | + _clearUserData(); | |
48 | 54 | Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false); |
49 | 55 | } |
50 | 56 | } | ... | ... |
lib/common/request/dao/user_dao.dart
lib/pages/home/widgets/home_tab_header_widget.dart
1 | 1 | import 'package:flutter/material.dart'; |
2 | -import 'package:flutter_bloc/flutter_bloc.dart'; | |
3 | 2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
4 | -import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
3 | +import 'package:wow_english/common/core/user_util.dart'; | |
5 | 4 | import 'package:wow_english/common/extension/string_extension.dart'; |
5 | +import 'package:wow_english/utils/image_util.dart'; | |
6 | 6 | |
7 | 7 | enum HeaderActionType { |
8 | 8 | //视频跟读 |
... | ... | @@ -44,10 +44,7 @@ class HomeTabHeaderWidget extends StatelessWidget { |
44 | 44 | ), |
45 | 45 | child: CircleAvatar( |
46 | 46 | radius: 21, |
47 | - backgroundImage: NetworkImage( | |
48 | - context.read<CacheBloc>().userEntity?.avatarUrl ?? | |
49 | - 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2Faa1c2213-820a-4223-8757-5f8cee318a28%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1688713226&t=192b18a613683bcdc5bd76f65c9ff032', | |
50 | - ), | |
47 | + backgroundImage: ImageUtil.getImageProviderOnDefault(UserUtil.getUser()?.avatarUrl), | |
51 | 48 | ), |
52 | 49 | ), |
53 | 50 | ), |
... | ... | @@ -66,7 +63,7 @@ class HomeTabHeaderWidget extends StatelessWidget { |
66 | 63 | border: Border.all(width: 1.0, color: const Color(0xFF140C10), style: BorderStyle.solid), |
67 | 64 | ), |
68 | 65 | child: Text( |
69 | - context.read<CacheBloc>().userEntity?.name ?? '', | |
66 | + UserUtil.getUser()?.name ?? '姓名丢了', | |
70 | 67 | style: TextStyle(color: const Color(0xFF333333), fontSize: 16.sp), |
71 | 68 | ), |
72 | 69 | ), | ... | ... |
lib/pages/login/loginpage/bloc/login_bloc.dart
... | ... | @@ -5,7 +5,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; |
5 | 5 | import 'package:wow_english/common/extension/string_extension.dart'; |
6 | 6 | import 'package:wow_english/common/request/dao/user_dao.dart'; |
7 | 7 | import 'package:wow_english/common/request/exception.dart'; |
8 | -import 'package:wow_english/models/user_entity.dart'; | |
9 | 8 | import 'package:wow_english/utils/loading.dart'; |
10 | 9 | import 'package:wow_english/utils/toast_util.dart'; |
11 | 10 | |
... | ... | @@ -76,9 +75,9 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> { |
76 | 75 | await loading(() async { |
77 | 76 | var user = await UserDao.login(phoneNumber, type, checkKey, checkNumber); |
78 | 77 | if (kDebugMode) { |
79 | - print('UserEntity?=$user'); | |
78 | + print('登陆 UserEntity=$user'); | |
80 | 79 | } |
81 | - emitter.call(LoginResultChangeState(user!)); | |
80 | + emitter.call(LoginResultChangeState()); | |
82 | 81 | }); |
83 | 82 | } catch (e) { |
84 | 83 | if (kDebugMode) { | ... | ... |
lib/pages/login/loginpage/bloc/login_state.dart
... | ... | @@ -21,8 +21,4 @@ class AgreementTypeChangeState extends LoginState {} |
21 | 21 | class SmsCodeRequestState extends LoginState {} |
22 | 22 | |
23 | 23 | ///登陆请求结果 |
24 | -class LoginResultChangeState extends LoginState { | |
25 | - final UserEntity userEntity; | |
26 | - | |
27 | - LoginResultChangeState(this.userEntity); | |
28 | -} | |
24 | +class LoginResultChangeState extends LoginState {} | ... | ... |
lib/pages/login/loginpage/login_page.dart
... | ... | @@ -2,7 +2,6 @@ import 'package:flutter/gestures.dart'; |
2 | 2 | import 'package:flutter/material.dart'; |
3 | 3 | import 'package:flutter_bloc/flutter_bloc.dart'; |
4 | 4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
5 | -import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
6 | 5 | import 'package:wow_english/common/core/app_consts.dart'; |
7 | 6 | import 'package:wow_english/common/extension/string_extension.dart'; |
8 | 7 | import 'package:wow_english/common/widgets/textfield_customer_widget.dart'; |
... | ... | @@ -29,9 +28,9 @@ class _LoginPageView extends StatelessWidget { |
29 | 28 | return BlocListener<LoginBloc, LoginState>( |
30 | 29 | listener: (context, state) { |
31 | 30 | if (state is LoginResultChangeState) { |
32 | - context.read<CacheBloc>().add(UserInfoChangeEvent(state.userEntity)); | |
33 | - Navigator.of(context).pushNamed(AppRouteName.home); | |
34 | - // Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.home, (route) => false); | |
31 | + // 调试用 | |
32 | + // Navigator.of(context).pushNamed(AppRouteName.home); | |
33 | + Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.home, (route) => false); | |
35 | 34 | } |
36 | 35 | }, |
37 | 36 | child: _buildLoginViewWidget(), |
... | ... | @@ -117,7 +116,8 @@ class _LoginPageView extends StatelessWidget { |
117 | 116 | recognizer: TapGestureRecognizer() |
118 | 117 | ..onTap = () { |
119 | 118 | Navigator.of(context).pushNamed(AppRouteName.webView, arguments: { |
120 | - 'urlStr': 'https://ishowedu-public-images.ishowedu.com/%E5%8F%A3%E8%AF%AD%E6%98%9F%E7%90%83%E5%84%BF%E7%AB%A5%E9%9A%90%E7%A7%81%E4%BF%9D%E6%8A%A4%E6%94%BF%E7%AD%96_iShowLD%E4%BF%AE%E8%AE%A2_20210913%281%29.html', | |
119 | + 'urlStr': | |
120 | + 'https://ishowedu-public-images.ishowedu.com/%E5%8F%A3%E8%AF%AD%E6%98%9F%E7%90%83%E5%84%BF%E7%AB%A5%E9%9A%90%E7%A7%81%E4%BF%9D%E6%8A%A4%E6%94%BF%E7%AD%96_iShowLD%E4%BF%AE%E8%AE%A2_20210913%281%29.html', | |
121 | 121 | 'webViewTitle': '儿童隐私协议' |
122 | 122 | }); |
123 | 123 | }) | ... | ... |
lib/pages/user/user_page.dart
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:flutter_bloc/flutter_bloc.dart'; |
3 | 3 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
4 | -import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
5 | 4 | import 'package:wow_english/common/core/app_consts.dart'; |
6 | 5 | import 'package:wow_english/common/core/assets_const.dart'; |
6 | +import 'package:wow_english/common/core/user_util.dart'; | |
7 | 7 | import 'package:wow_english/common/widgets/we_app_bar.dart'; |
8 | 8 | import 'package:wow_english/models/user_entity.dart'; |
9 | 9 | import 'package:wow_english/pages/login/setpwd/set_pwd_page.dart'; |
... | ... | @@ -36,8 +36,7 @@ class _UserView extends StatelessWidget { |
36 | 36 | return current != previous; |
37 | 37 | },*/ |
38 | 38 | builder: (context, state) { |
39 | - final cacheBloc = BlocProvider.of<CacheBloc>(context); | |
40 | - UserEntity user = cacheBloc.userEntity!; | |
39 | + UserEntity user = UserUtil.getUser()!; | |
41 | 40 | final userBloc = BlocProvider.of<UserBloc>(context); |
42 | 41 | |
43 | 42 | // 常规按钮的字体样式 | ... | ... |