完成路由中间件处理
This commit is contained in:
parent
ac67682918
commit
998f61dea7
|
@ -12,12 +12,13 @@ class UserService extends GetxService {
|
|||
// 是否登录
|
||||
var _isLogin = false.obs;
|
||||
|
||||
// 令牌 token
|
||||
String token = "";
|
||||
|
||||
// 用户 profile
|
||||
var _profile = UserLoginResponse().obs;
|
||||
|
||||
bool get isLogin => _isLogin.value; // 令牌 token
|
||||
UserLoginResponse get profile => _profile.value;
|
||||
bool get hasToken => token.isNotEmpty;
|
||||
|
||||
@override
|
||||
|
@ -27,6 +28,7 @@ class UserService extends GetxService {
|
|||
var profileOffline = StorageService.to.getString(StorageUserProfileKey);
|
||||
if (profileOffline.isNotEmpty) {
|
||||
_profile(UserLoginResponse.fromJson(jsonDecode(profileOffline)));
|
||||
_isLogin.value = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -34,8 +34,7 @@ class MyApp extends StatelessWidget {
|
|||
title: 'News',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.light,
|
||||
// initialRoute: AppRoutes.Initial,
|
||||
initialRoute: AppRoutes.Application,
|
||||
initialRoute: AppRoutes.Initial,
|
||||
getPages: AppPages.pages,
|
||||
builder: EasyLoading.init(),
|
||||
),
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:news_getx/data/services/user.dart';
|
||||
import 'package:news_getx/routes/app_pages.dart';
|
||||
|
||||
/// 检查是否登录
|
||||
class RouteAuthMiddleware extends GetMiddleware {
|
||||
// priority 数字小优先级高
|
||||
@override
|
||||
int? priority = 0;
|
||||
|
||||
RouteAuthMiddleware({this.priority});
|
||||
|
||||
@override
|
||||
RouteSettings? redirect(String? route) {
|
||||
// 如果已登录或者访问的页面是无需登录的则不处理
|
||||
if (UserService.to.isLogin ||
|
||||
route == AppRoutes.Signin ||
|
||||
route == AppRoutes.Signup ||
|
||||
route == AppRoutes.Initial) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future.delayed(
|
||||
Duration(seconds: 1),
|
||||
() => Get.snackbar("提示", "登录过期,请重新登录"),
|
||||
);
|
||||
return RouteSettings(name: AppRoutes.Signin);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:news_getx/data/services/config.dart';
|
||||
import 'package:news_getx/data/services/user.dart';
|
||||
import 'package:news_getx/routes/app_pages.dart';
|
||||
|
||||
|
||||
/// 第一次欢迎页面
|
||||
class RouteWelcomeMiddleware extends GetMiddleware {
|
||||
// priority 数字小优先级高
|
||||
@override
|
||||
int? priority = 0;
|
||||
|
||||
RouteWelcomeMiddleware({this.priority});
|
||||
|
||||
@override
|
||||
RouteSettings? redirect(String? route) {
|
||||
if (ConfigService.to.isFirstOpen == true) {
|
||||
return null;
|
||||
} else if (UserService.to.isLogin == true) {
|
||||
return RouteSettings(name: AppRoutes.Application);
|
||||
} else {
|
||||
return RouteSettings(name: AppRoutes.Signin);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,7 +21,6 @@ class SignInController extends GetxController {
|
|||
|
||||
// 跳转 注册界面
|
||||
handleNavSignUp() {
|
||||
// TODO 注册页面
|
||||
Get.toNamed(AppRoutes.Signup);
|
||||
}
|
||||
|
||||
|
@ -48,8 +47,9 @@ class SignInController extends GetxController {
|
|||
);
|
||||
|
||||
UserLoginResponse response = await repository.login(request);
|
||||
|
||||
// 设置令牌和用户信息
|
||||
UserService.to.saveProfile(response);
|
||||
UserService.to.setToken(response.accessToken!);
|
||||
|
||||
Get.offAndToNamed(AppRoutes.Application);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import 'package:get/get.dart';
|
||||
import 'package:news_getx/middlewares/router_auth.dart';
|
||||
import 'package:news_getx/middlewares/router_welcome.dart';
|
||||
import 'package:news_getx/modules/application/application_binding.dart';
|
||||
import 'package:news_getx/modules/application/application_page.dart';
|
||||
import 'package:news_getx/modules/category/category_binding.dart';
|
||||
|
@ -22,6 +24,7 @@ abstract class AppPages {
|
|||
name: AppRoutes.Initial,
|
||||
page: () => WelcomePage(),
|
||||
binding: WelcomeBinding(),
|
||||
middlewares: [RouteWelcomeMiddleware()],
|
||||
),
|
||||
GetPage(
|
||||
name: AppRoutes.Signin,
|
||||
|
@ -43,13 +46,10 @@ abstract class AppPages {
|
|||
name: AppRoutes.Application,
|
||||
page: () => ApplicationPage(),
|
||||
binding: ApplicationBinding(),
|
||||
bindings: [MainBinding(), CategoryBinding()]
|
||||
),
|
||||
// 分类页
|
||||
GetPage(
|
||||
name: AppRoutes.Category,
|
||||
page: () => CategoryPage(),
|
||||
binding: CategoryBinding(),
|
||||
bindings: [MainBinding(), CategoryBinding()],
|
||||
middlewares: [
|
||||
RouteAuthMiddleware(),
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue