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);
  }
}
