From 278208b8573ecc4d18c2f4576a8e448c4f7e0a5a Mon Sep 17 00:00:00 2001 From: wuqifeng <540416539@qq.com> Date: Fri, 26 Apr 2024 16:48:45 +0800 Subject: [PATCH] feat:1、用户访问权限调整;2、添加游戏访问权限 --- lib/app/splash_page.dart | 4 ++-- lib/common/core/user_util.dart | 16 ++++++++++++++-- lib/common/request/dao/user_dao.dart | 6 ++++++ lib/common/request/token_interceptor.dart | 4 ++-- lib/generated/json/user_entity.g.dart | 16 +++++++++++++++- lib/models/user_entity.dart | 8 +++++++- lib/pages/home/home_page.dart | 16 ++++++++-------- lib/pages/moduleSelect/view.dart | 26 ++++++++++++++++++++++++-- lib/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart | 6 +++--- 9 files changed, 81 insertions(+), 21 deletions(-) diff --git a/lib/app/splash_page.dart b/lib/app/splash_page.dart index 3cf91be..ee5aa39 100644 --- a/lib/app/splash_page.dart +++ b/lib/app/splash_page.dart @@ -45,8 +45,8 @@ class _TransitionViewState extends State { Log.d('Splash读本地, userEntity: $userEntity'); int apartInMilliseconds = 0; // 调一下接口判断一下有效性再往下 - if (userEntity != null) { - String token = userEntity.token; + if (userEntity != null && userEntity.token != null) { + String token = userEntity.token!; DateTime startTime = DateTime.now(); try { userEntity = await UserDao.getUserInfo(); diff --git a/lib/common/core/user_util.dart b/lib/common/core/user_util.dart index 96ffc15..873765b 100644 --- a/lib/common/core/user_util.dart +++ b/lib/common/core/user_util.dart @@ -11,8 +11,6 @@ import 'package:wow_english/utils/sp_util.dart'; class UserUtil { static UserEntity? _userEntity; - static String get token => _userEntity?.token ?? ''; - static void saveUser(UserEntity? user) { if (user == null) { _clearUserData(); @@ -64,4 +62,18 @@ class UserUtil { }*/ Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false, arguments: {'showPasswordPage': showPasswordLoginPage}); } + + // 是否有游戏权限 + static bool hasGamePermission() { + return _userEntity?.valid ?? false; + } + + // 是否登录(token是否有效) + static bool isLogined() { + return getUserToken().isNotEmpty; + } + + static String getUserToken() { + return _userEntity?.token ?? ''; + } } diff --git a/lib/common/request/dao/user_dao.dart b/lib/common/request/dao/user_dao.dart index 6894f65..18bae66 100644 --- a/lib/common/request/dao/user_dao.dart +++ b/lib/common/request/dao/user_dao.dart @@ -16,6 +16,12 @@ class UserDao { ); if (data != null) { UserUtil.saveUser(data); + // 由于userInfo接口不会返回token,所以这里需要再次保存一下token + final token = data.token; + //登录成功后zip一下getUserInfo,因为进入首页需要的信息在userinfo里,保证进入首页数据是最新的 + data = await getUserInfo(); + data?.token = token; + UserUtil.saveUser(data); } return data; } diff --git a/lib/common/request/token_interceptor.dart b/lib/common/request/token_interceptor.dart index 95f24fa..5291ddf 100644 --- a/lib/common/request/token_interceptor.dart +++ b/lib/common/request/token_interceptor.dart @@ -5,8 +5,8 @@ class TokenInterceptor extends Interceptor { @override void onRequest(RequestOptions options, RequestInterceptorHandler handler) { // 判断token不为空插入, todo token的取法应该跟user在一起,这里取不到user - if (UserUtil.token.isNotEmpty) { - options.headers["Auth-token"] = UserUtil.token; + if (UserUtil.isLogined()) { + options.headers["Auth-token"] = UserUtil.getUserToken(); } options.headers["version"] = '1.0.0'; super.onRequest(options, handler); diff --git a/lib/generated/json/user_entity.g.dart b/lib/generated/json/user_entity.g.dart index 730c6dc..ef0c8db 100644 --- a/lib/generated/json/user_entity.g.dart +++ b/lib/generated/json/user_entity.g.dart @@ -45,6 +45,14 @@ UserEntity $UserEntityFromJson(Map json) { if (effectiveDate != null) { userEntity.effectiveDate = effectiveDate; } + final int? validDay = jsonConvert.convert(json['validDay']); + if (validDay != null) { + userEntity.validDay = validDay; + } + final bool? valid = jsonConvert.convert(json['valid']); + if (valid != null) { + userEntity.valid = valid; + } return userEntity; } @@ -60,6 +68,8 @@ Map $UserEntityToJson(UserEntity entity) { data['fillUserInfo'] = entity.fillUserInfo; data['nowCourseModuleId'] = entity.nowCourseModuleId; data['effectiveDate'] = entity.effectiveDate; + data['validDay'] = entity.validDay; + data['valid'] = entity.valid; return data; } @@ -75,6 +85,8 @@ extension UserEntityExtension on UserEntity { int? fillUserInfo, int? nowCourseModuleId, String? effectiveDate, + int? validDay, + bool? valid, }) { return UserEntity() ..id = id ?? this.id @@ -86,6 +98,8 @@ extension UserEntityExtension on UserEntity { ..phoneNum = phoneNum ?? this.phoneNum ..fillUserInfo = fillUserInfo ?? this.fillUserInfo ..nowCourseModuleId = nowCourseModuleId ?? this.nowCourseModuleId - ..effectiveDate = effectiveDate ?? this.effectiveDate; + ..effectiveDate = effectiveDate ?? this.effectiveDate + ..validDay = validDay ?? this.validDay + ..valid = valid ?? this.valid; } } \ No newline at end of file diff --git a/lib/models/user_entity.dart b/lib/models/user_entity.dart index 4c9d1c1..44a68c1 100644 --- a/lib/models/user_entity.dart +++ b/lib/models/user_entity.dart @@ -9,7 +9,7 @@ class UserEntity { late String name; /// 一定有也必须要有 - late String token; + String? token; //late int expireTime; @@ -29,6 +29,12 @@ class UserEntity { /// 有效时间,VIP,为null没有 String? effectiveDate; + /// 有效天数 + int? validDay; + + /// 游戏权限 + bool? valid; + UserEntity(); factory UserEntity.fromJson(Map json) => $UserEntityFromJson(json); diff --git a/lib/pages/home/home_page.dart b/lib/pages/home/home_page.dart index 7a1629a..281229d 100644 --- a/lib/pages/home/home_page.dart +++ b/lib/pages/home/home_page.dart @@ -33,20 +33,20 @@ class _HomePageView extends StatelessWidget { if (type == HeaderActionType.video) { pushNamed(AppRouteName.reAfter); } else if (type == HeaderActionType.phase) { - if(UserUtil.token.isEmpty) { - pushNamed(AppRouteName.login); - } else { + if (UserUtil.isLogined()) { pushNamed(AppRouteName.lesson); + } else { + pushNamed(AppRouteName.login); } } else if (type == HeaderActionType.listen) { pushNamed(AppRouteName.listen); } else if (type == HeaderActionType.shop) { pushNamed(AppRouteName.shop); } else if (type == HeaderActionType.user) { - if(UserUtil.token.isEmpty) { - pushNamed(AppRouteName.login); - } else { + if (UserUtil.isLogined()) { pushNamed(AppRouteName.user); + } else { + pushNamed(AppRouteName.login); } } else { @@ -155,7 +155,7 @@ class _HomePageView extends StatelessWidget { //彩蛋 return GestureDetector( onTap: () { - if(UserUtil.token.isEmpty) { + if (!UserUtil.isLogined()) { pushNamed(AppRouteName.login); return; } @@ -173,7 +173,7 @@ class _HomePageView extends StatelessWidget { } else { return GestureDetector( onTap: () { - if(UserUtil.token.isEmpty) { + if (!UserUtil.isLogined()) { pushNamed(AppRouteName.login); return; } diff --git a/lib/pages/moduleSelect/view.dart b/lib/pages/moduleSelect/view.dart index 463fbd5..eca3a98 100644 --- a/lib/pages/moduleSelect/view.dart +++ b/lib/pages/moduleSelect/view.dart @@ -4,6 +4,8 @@ import 'package:wow_english/common/extension/string_extension.dart'; import 'package:wow_english/pages/moduleSelect/state.dart'; import 'package:wow_english/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart'; +import '../../common/core/user_util.dart'; +import '../../common/dialogs/show_dialog.dart'; import 'bloc.dart'; import 'event.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -47,7 +49,11 @@ class _HomePageView extends StatelessWidget { Expanded( child: GestureDetector( onTap: () { - pushNamed(AppRouteName.home); + if (UserUtil.isLogined()) { + pushNamed(AppRouteName.home); + } else { + pushNamed(AppRouteName.login); + } }, child: Column( mainAxisAlignment: MainAxisAlignment.center, @@ -74,7 +80,23 @@ class _HomePageView extends StatelessWidget { Expanded( child: GestureDetector( onTap: () { - pushNamed(AppRouteName.games); + //如果没登录先登录 + if (UserUtil.isLogined()) { + if (UserUtil.hasGamePermission()) { + pushNamed(AppRouteName.games); + } else { + showTwoActionDialog( + '提示', '忽略', '去续费', + '您的课程已到期,请快快续费继续学习吧!', leftTap: () { + popPage(); + }, rightTap: () { + popPage(); + pushNamed(AppRouteName.shop); + }); + } + } else { + pushNamed(AppRouteName.login); + } }, child: Column( mainAxisAlignment: MainAxisAlignment.center, diff --git a/lib/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart b/lib/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart index 5560345..a54bfcb 100644 --- a/lib/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart +++ b/lib/pages/moduleSelect/widgets/BaseHomeHeaderWidget.dart @@ -96,9 +96,9 @@ class BaseHomeHeaderWidget extends StatelessWidget { } void onUserClick() { - if (UserUtil.token.isEmpty) { - pushNamed(AppRouteName.login); - } else { + if (UserUtil.isLogined()) { pushNamed(AppRouteName.user); + } else { + pushNamed(AppRouteName.login); } } -- libgit2 0.22.2