From e55bbdf3e9c2e15793aadb445e46b0db70c336d2 Mon Sep 17 00:00:00 2001 From: Key Date: Mon, 10 Jul 2023 05:10:41 +0800 Subject: [PATCH] fixed: aliyun oss private->public read fixed: login refresh user info --- lib/app/splash_page.dart | 28 ++++++++++++++++++---------- lib/common/core/user_util.dart | 7 +++++++ lib/common/request/basic_config.dart | 1 + lib/common/request/request_client.dart | 2 ++ lib/models/user_entity.dart | 25 +++++++++++++++++++++++++ lib/route/route.dart | 48 ++++++++++++++++++++++++++---------------------- lib/utils/aliyun_oss_util.dart | 2 +- 7 files changed, 80 insertions(+), 33 deletions(-) diff --git a/lib/app/splash_page.dart b/lib/app/splash_page.dart index 4ff4e28..96d7725 100644 --- a/lib/app/splash_page.dart +++ b/lib/app/splash_page.dart @@ -9,6 +9,7 @@ import 'package:limiting_direction_csx/limiting_direction_csx.dart'; import 'package:wow_english/common/core/user_util.dart'; import 'package:wow_english/common/extension/string_extension.dart'; import 'package:wow_english/common/request/config.dart'; +import 'package:wow_english/common/request/dao/user_dao.dart'; import 'package:wow_english/models/user_entity.dart'; import 'package:wow_english/route/route.dart'; import 'package:wow_english/utils/log_util.dart'; @@ -38,23 +39,30 @@ class _TransitionViewState extends State { UserEntity? userEntity = UserUtil.getUser(); Log.d('当前模式:kDebugMode=$kDebugMode, kProfileMode=$kProfileMode, kReleaseMode=$kReleaseMode'); Log.d('当前API地址:${RequestConfig.baseUrl}'); - if (kDebugMode) { - Log.d('Splash读本地userEntity: $userEntity'); - } + //BuildContext context = Navigator.of(context).context; + //var currentPageName = ModalRoute.of(Navigator.of(AppRouter.context))?.settings.name; + //Log.d('Splash读当前页面:$currentPageName'); + Log.d('Splash读本地, userEntity: $userEntity'); int apartInMilliseconds = 0; // 调一下接口判断一下有效性再往下 if (userEntity != null) { + String token = userEntity.token; DateTime startTime = DateTime.now(); - // try { - // userEntity = await UserDao.getUserInfo(); - // UserUtil.saveUser(userEntity); - // } catch (e) { - // e.logE(); - // } + try { + userEntity = await UserDao.getUserInfo(); + Log.d('Splash在线更新, userEntity: $userEntity'); + if (userEntity != null) { + userEntity.token = token; + Log.d('Splash重设token, userEntity: $userEntity'); + UserUtil.saveUser(userEntity); + } + } catch (e) { + e.logE(); + } apartInMilliseconds = DateTime.now().difference(startTime).inMilliseconds; } - Log.d('Splash getUserInfo 耗时:${apartInMilliseconds}ms'); int duration = max(2000 - apartInMilliseconds, 0); + Log.d('Splash getUserInfo 耗时:${apartInMilliseconds}ms, 最终duration=$duration'); Timer(Duration(milliseconds: duration), () { /*if (userEntity != null) { pushNamedAndRemoveUntil(AppRouteName.home, (route) => false); diff --git a/lib/common/core/user_util.dart b/lib/common/core/user_util.dart index 0f097a6..eb206d7 100644 --- a/lib/common/core/user_util.dart +++ b/lib/common/core/user_util.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:wow_english/common/core/sp_const.dart'; import 'package:wow_english/models/user_entity.dart'; import 'package:wow_english/route/route.dart'; @@ -55,6 +56,12 @@ class UserUtil { static void logout() { _clearUserData(); + // 判断下不在Splash页就跳转 + /*var currentPageName = ModalRoute.of(AppRouter.context)?.settings.name; + Log.d('当前页面:$currentPageName'); + if (currentPageName != AppRouteName.splash) { + Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false); + }*/ Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(AppRouteName.login, (route) => false); } } diff --git a/lib/common/request/basic_config.dart b/lib/common/request/basic_config.dart index f86b8f9..c925b4f 100644 --- a/lib/common/request/basic_config.dart +++ b/lib/common/request/basic_config.dart @@ -1,3 +1,4 @@ class BasicConfig { bool isTestDev = true; + // bool isTestDev = false; } diff --git a/lib/common/request/request_client.dart b/lib/common/request/request_client.dart index bf67733..69d9805 100644 --- a/lib/common/request/request_client.dart +++ b/lib/common/request/request_client.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:pretty_dio_logger/pretty_dio_logger.dart'; +import 'package:wow_english/utils/toast_util.dart'; import '../core/user_util.dart'; import 'api_response/api_response_entity.dart'; @@ -49,6 +50,7 @@ class RequestClient { } catch (e) { print("e type=${e.runtimeType}"); if (e is ApiException && e.code == 405) { + showToast('登录已失效,请重新登录!', duration: const Duration(seconds: 3)); UserUtil.logout(); return null; } diff --git a/lib/models/user_entity.dart b/lib/models/user_entity.dart index 674ea0b..4c9d1c1 100644 --- a/lib/models/user_entity.dart +++ b/lib/models/user_entity.dart @@ -56,4 +56,29 @@ class UserEntity { ? '男' : '女'; } + + UserEntity copyWith({ + int? id, + String? name, + String? token, + int? age, + int? gender, + String? avatarUrl, + String? phoneNum, + int? fillUserInfo, + int? nowCourseModuleId, + String? effectiveDate, + }) { + return UserEntity() + ..id = id ?? this.id + ..name = name ?? this.name + ..token = token ?? this.token + ..age = age ?? this.age + ..gender = gender ?? this.gender + ..avatarUrl = avatarUrl ?? this.avatarUrl + ..phoneNum = phoneNum ?? this.phoneNum + ..fillUserInfo = fillUserInfo ?? this.fillUserInfo + ..nowCourseModuleId = nowCourseModuleId ?? this.nowCourseModuleId + ..effectiveDate = effectiveDate ?? this.effectiveDate; + } } diff --git a/lib/route/route.dart b/lib/route/route.dart index 935e0f8..7b76a90 100644 --- a/lib/route/route.dart +++ b/lib/route/route.dart @@ -43,6 +43,7 @@ class AppRouteName { /// 用户详细信息页 static const String userInformation = 'userInformation'; + /// 修改用户头像 static const String userAvatar = 'userAvatar'; @@ -50,12 +51,13 @@ class AppRouteName { //static const String userModifyInformation = 'userModifyInformation'; ///看视频 static const String lookVideo = 'lookVideo'; + ///绘本 static const String reading = 'reading'; + ///视频跟读详情 static const String readAfterContent = 'readAfterContent'; - static const String tab = '/'; } @@ -82,8 +84,8 @@ class AppRouter { } return CupertinoPageRoute( builder: (_) => HomePage( - moduleId: moduleId, - )); + moduleId: moduleId, + )); case AppRouteName.fogPwd: return CupertinoPageRoute(builder: (_) => const ForgetPasswordHomePage()); case AppRouteName.lesson: @@ -105,11 +107,14 @@ class AppRouter { case AppRouteName.userAvatar: var pageType = 0; if (settings.arguments != null) { - final content = (settings.arguments as Map)['pageType'] as String??'0'; + final content = (settings.arguments as Map)['pageType'] as String ?? '0'; pageType = int.parse(content); } - return CupertinoPageRoute(builder: (_) => ModifyUserAvatarPage(pageType: pageType,)); - /*case AppRouteName.userModifyInformation: + return CupertinoPageRoute( + builder: (_) => ModifyUserAvatarPage( + pageType: pageType, + )); + /*case AppRouteName.userModifyInformation: return CupertinoPageRoute(builder: (_) { ModifyUserInformationType argument = ModifyUserInformationType.name; if (settings.arguments != null) { @@ -120,18 +125,18 @@ class AppRouter { case AppRouteName.topicPic: var courseLessonId = ''; if (settings.arguments != null) { - courseLessonId = (settings.arguments as Map)['courseLessonId'] as String??''; + courseLessonId = (settings.arguments as Map)['courseLessonId'] as String ?? ''; } - return CupertinoPageRoute(builder: (_) => TopicPicturePage(courseLessonId: courseLessonId)); + return CupertinoPageRoute(builder: (_) => TopicPicturePage(courseLessonId: courseLessonId)); case AppRouteName.lookVideo: final videoUrl = (settings.arguments as Map)['videoUrl'] as String; final title = (settings.arguments as Map)['title'] as String?; return CupertinoPageRoute( builder: (_) => LookVideoPage( - videoUrl: videoUrl, - typeTitle: title, - )); - /*case AppRouteName.setPwd: + videoUrl: videoUrl, + typeTitle: title, + )); + /*case AppRouteName.setPwd: case AppRouteName.setPwd: phoneNum: phoneNum, smsCode: smsCode, @@ -142,16 +147,15 @@ class AppRouter { final webViewTitle = (settings.arguments as Map)['webViewTitle'] as String; return CupertinoPageRoute( builder: (_) => WowWebViewPage( - urlStr: urlStr, - webViewTitle: webViewTitle, - )); + urlStr: urlStr, + webViewTitle: webViewTitle, + )); case AppRouteName.readAfterContent: var videoFollowReadId = ''; if (settings.arguments != null) { - videoFollowReadId = (settings.arguments as Map)['videoFollowReadId'] as String??''; + videoFollowReadId = (settings.arguments as Map)['videoFollowReadId'] as String ?? ''; } - return CupertinoPageRoute( - builder: (_) => RepeatAfterContentPage(videoFollowReadId:videoFollowReadId)); + return CupertinoPageRoute(builder: (_) => RepeatAfterContentPage(videoFollowReadId: videoFollowReadId)); case AppRouteName.tab: return PageRouteBuilder( opaque: false, @@ -162,7 +166,7 @@ class AppRouter { case AppRouteName.reading: var courseLessonId = ''; if (settings.arguments != null) { - courseLessonId = (settings.arguments as Map)['courseLessonId'] as String??''; + courseLessonId = (settings.arguments as Map)['courseLessonId'] as String ?? ''; } return CupertinoPageRoute(builder: (_) => ReadingPage(courseLessonId: courseLessonId)); default: @@ -173,11 +177,11 @@ class AppRouter { } void pushNamed(String routeName, {Object? arguments}) { - Navigator.of(AppRouter.context).pushNamed(routeName,arguments: arguments); + Navigator.of(AppRouter.context).pushNamed(routeName, arguments: arguments); } -void pushNamedAndRemoveUntil(String routeName,RoutePredicate predicate, {Object? arguments}) { - Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(routeName, predicate,arguments: arguments); +void pushNamedAndRemoveUntil(String routeName, RoutePredicate predicate, {Object? arguments}) { + Navigator.of(AppRouter.context).pushNamedAndRemoveUntil(routeName, predicate, arguments: arguments); } void popPage() { diff --git a/lib/utils/aliyun_oss_util.dart b/lib/utils/aliyun_oss_util.dart index 9ba3a70..6082d77 100644 --- a/lib/utils/aliyun_oss_util.dart +++ b/lib/utils/aliyun_oss_util.dart @@ -37,7 +37,7 @@ class AliyunOssUtil { onReceiveProgress: (count, total) { Log.d("receive: count = $count, and total = $total"); }, - aclModel: AclMode.private, + aclModel: AclMode.publicRead, callback: Callback( callbackUrl: stsEntity.callbackParam.callbackUrl, callbackBody: stsEntity.callbackParam.callbackBody, -- libgit2 0.22.2