完成路由中间件处理
This commit is contained in:
parent
ac67682918
commit
998f61dea7
|
@ -12,12 +12,13 @@ class UserService extends GetxService {
|
||||||
// 是否登录
|
// 是否登录
|
||||||
var _isLogin = false.obs;
|
var _isLogin = false.obs;
|
||||||
|
|
||||||
// 令牌 token
|
|
||||||
String token = "";
|
String token = "";
|
||||||
|
|
||||||
// 用户 profile
|
// 用户 profile
|
||||||
var _profile = UserLoginResponse().obs;
|
var _profile = UserLoginResponse().obs;
|
||||||
|
|
||||||
|
bool get isLogin => _isLogin.value; // 令牌 token
|
||||||
|
UserLoginResponse get profile => _profile.value;
|
||||||
bool get hasToken => token.isNotEmpty;
|
bool get hasToken => token.isNotEmpty;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -27,6 +28,7 @@ class UserService extends GetxService {
|
||||||
var profileOffline = StorageService.to.getString(StorageUserProfileKey);
|
var profileOffline = StorageService.to.getString(StorageUserProfileKey);
|
||||||
if (profileOffline.isNotEmpty) {
|
if (profileOffline.isNotEmpty) {
|
||||||
_profile(UserLoginResponse.fromJson(jsonDecode(profileOffline)));
|
_profile(UserLoginResponse.fromJson(jsonDecode(profileOffline)));
|
||||||
|
_isLogin.value = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,7 @@ class MyApp extends StatelessWidget {
|
||||||
title: 'News',
|
title: 'News',
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
theme: AppTheme.light,
|
theme: AppTheme.light,
|
||||||
// initialRoute: AppRoutes.Initial,
|
initialRoute: AppRoutes.Initial,
|
||||||
initialRoute: AppRoutes.Application,
|
|
||||||
getPages: AppPages.pages,
|
getPages: AppPages.pages,
|
||||||
builder: EasyLoading.init(),
|
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() {
|
handleNavSignUp() {
|
||||||
// TODO 注册页面
|
|
||||||
Get.toNamed(AppRoutes.Signup);
|
Get.toNamed(AppRoutes.Signup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,8 +47,9 @@ class SignInController extends GetxController {
|
||||||
);
|
);
|
||||||
|
|
||||||
UserLoginResponse response = await repository.login(request);
|
UserLoginResponse response = await repository.login(request);
|
||||||
|
// 设置令牌和用户信息
|
||||||
UserService.to.saveProfile(response);
|
UserService.to.saveProfile(response);
|
||||||
|
UserService.to.setToken(response.accessToken!);
|
||||||
|
|
||||||
Get.offAndToNamed(AppRoutes.Application);
|
Get.offAndToNamed(AppRoutes.Application);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import 'package:get/get.dart';
|
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_binding.dart';
|
||||||
import 'package:news_getx/modules/application/application_page.dart';
|
import 'package:news_getx/modules/application/application_page.dart';
|
||||||
import 'package:news_getx/modules/category/category_binding.dart';
|
import 'package:news_getx/modules/category/category_binding.dart';
|
||||||
|
@ -22,6 +24,7 @@ abstract class AppPages {
|
||||||
name: AppRoutes.Initial,
|
name: AppRoutes.Initial,
|
||||||
page: () => WelcomePage(),
|
page: () => WelcomePage(),
|
||||||
binding: WelcomeBinding(),
|
binding: WelcomeBinding(),
|
||||||
|
middlewares: [RouteWelcomeMiddleware()],
|
||||||
),
|
),
|
||||||
GetPage(
|
GetPage(
|
||||||
name: AppRoutes.Signin,
|
name: AppRoutes.Signin,
|
||||||
|
@ -43,13 +46,10 @@ abstract class AppPages {
|
||||||
name: AppRoutes.Application,
|
name: AppRoutes.Application,
|
||||||
page: () => ApplicationPage(),
|
page: () => ApplicationPage(),
|
||||||
binding: ApplicationBinding(),
|
binding: ApplicationBinding(),
|
||||||
bindings: [MainBinding(), CategoryBinding()]
|
bindings: [MainBinding(), CategoryBinding()],
|
||||||
),
|
middlewares: [
|
||||||
// 分类页
|
RouteAuthMiddleware(),
|
||||||
GetPage(
|
],
|
||||||
name: AppRoutes.Category,
|
|
||||||
page: () => CategoryPage(),
|
|
||||||
binding: CategoryBinding(),
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue