401增加节流
This commit is contained in:
22
ln_jq_app/lib/common/AuthGuard.dart
Normal file
22
ln_jq_app/lib/common/AuthGuard.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:getx_scaffold/common/index.dart';
|
||||
import 'package:ln_jq_app/pages/login/view.dart';
|
||||
import 'package:ln_jq_app/storage_service.dart';
|
||||
|
||||
class AuthGuard {
|
||||
static bool _handling401 = false;
|
||||
|
||||
static Future<void> handle401(String? message) async {
|
||||
if (_handling401) return;
|
||||
_handling401 = true;
|
||||
|
||||
try {
|
||||
await StorageService.to.clearLoginInfo();
|
||||
Get.offAll(() => const LoginPage());
|
||||
} finally {
|
||||
// 防止意外卡死,可视情况是否延迟重置
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
_handling401 = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||||
import 'package:get_storage/get_storage.dart';
|
||||
import 'package:getx_scaffold/getx_scaffold.dart';
|
||||
import 'package:ln_jq_app/common/AuthGuard.dart';
|
||||
import 'package:ln_jq_app/common/model/base_model.dart';
|
||||
import 'package:ln_jq_app/common/token_interceptor.dart';
|
||||
import 'package:ln_jq_app/storage_service.dart';
|
||||
@@ -20,7 +21,7 @@ void main() async {
|
||||
logTag: '小羚羚',
|
||||
supportedLocales: [const Locale('zh', 'CN')],
|
||||
);
|
||||
|
||||
|
||||
// 保持原生闪屏页,直到 WelcomeController 调用 remove()
|
||||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||||
|
||||
@@ -73,8 +74,7 @@ void initHttpSet() {
|
||||
if (baseModel.code == 0 || baseModel.code == 200) {
|
||||
return null;
|
||||
} else if (baseModel.code == 401) {
|
||||
await StorageService.to.clearLoginInfo();
|
||||
Get.offAll(() => const LoginPage());
|
||||
await AuthGuard.handle401(baseModel.message);
|
||||
return baseModel.message;
|
||||
} else {
|
||||
return (baseModel.error.toString()).isEmpty
|
||||
|
||||
Reference in New Issue
Block a user