feat(auth): 添加启动认证功能

- 在本地化文件中添加认证相关的多语言支持
- 实现密码验证逻辑和锁定机制
- 创建登录页面UI组件
- 集成路由保护,未认证用户自动重定向到登录页
- 支持密码错误次数限制和倒计时锁定功能
This commit is contained in:
Developer
2026-06-12 15:30:25 +08:00
parent 3d849bd468
commit 57badf213a
5 changed files with 350 additions and 3 deletions

View File

@@ -231,6 +231,20 @@ class AppLocalizations {
String get usbUsageStep3 => _localizedValues[locale.languageCode]?['usbUsageStep3'] ?? '检测成功后点击"导入程序"加载程序列表';
String get usbPathInvalid => _localizedValues[locale.languageCode]?['usbPathInvalid'] ?? 'U盘路径无效';
// ---- 启动认证 ----
String get authTitle => _localizedValues[locale.languageCode]?['authTitle'] ?? '身份验证';
String get authSubtitle => _localizedValues[locale.languageCode]?['authSubtitle'] ?? '请输入操作员密码以继续使用';
String get enterPassword => _localizedValues[locale.languageCode]?['enterPassword'] ?? '请输入密码';
String get passwordError => _localizedValues[locale.languageCode]?['passwordError'] ?? '密码错误';
String lockCountdown(int seconds) =>
locale.languageCode == 'en'
? 'Please wait $seconds seconds to retry'
: '请等待 $seconds 秒后重试';
String remainingAttempts(int count) =>
locale.languageCode == 'en'
? '$count attempt${count > 1 ? 's' : ''} remaining'
: '剩余 $count 次尝试机会';
// ---- 通用 ----
String get back => _localizedValues[locale.languageCode]?['back'] ?? '返回';
String get totalProgress => _localizedValues[locale.languageCode]?['totalProgress'] ?? '总进度';
@@ -427,6 +441,10 @@ class AppLocalizations {
'usbUsageStep2': '插入 U盘后点击"重新检测"',
'usbUsageStep3': '检测成功后点击"导入程序"加载程序列表',
'usbPathInvalid': 'U盘路径无效',
'authTitle': '身份验证',
'authSubtitle': '请输入操作员密码以继续使用',
'enterPassword': '请输入密码',
'passwordError': '密码错误',
'back': '返回',
'totalProgress': '总进度',
'appTitle': '污水毒品快检一体机',
@@ -607,6 +625,10 @@ class AppLocalizations {
'usbUsageStep2': 'Insert USB then click "Re-detect"',
'usbUsageStep3': 'After detection, click "Import Programs" to load',
'usbPathInvalid': 'USB path invalid',
'authTitle': 'Authentication',
'authSubtitle': 'Enter operator password to continue',
'enterPassword': 'Enter password',
'passwordError': 'Password incorrect',
'back': 'Back',
'totalProgress': 'Total Progress',
'appTitle': 'Wastewater Drug Detection System',

View File

@@ -1,22 +1,53 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../features/auth/pages/login_page.dart';
import '../../features/home/pages/home_page.dart';
import '../../features/programs/pages/programs_page.dart';
import '../../features/program_detail/pages/program_detail_page.dart';
import '../../features/settings/pages/settings_page.dart';
import '../../features/home/pages/complete_page.dart';
import '../../features/auth/providers/auth_provider.dart';
final _authRefreshStream = AuthRefreshStream();
/// 应用路由配置
final goRouterProvider = Provider<GoRouter>((ref) {
final authState = ref.watch(authProvider);
// 监听认证状态变化,触发 GoRouter 重新评估 redirect
ref.listen(authProvider, (prev, next) {
_authRefreshStream.notify();
});
return GoRouter(
initialLocation: '/',
redirect: (context, state) {
final isAuthenticated = authState.status == AuthStatus.authenticated;
final isLoginPage = state.uri.path == '/login';
if (!isAuthenticated && !isLoginPage) {
return '/login';
}
if (isAuthenticated && isLoginPage) {
return '/';
}
return null;
},
refreshListenable: _authRefreshStream,
routes: [
GoRoute(
path: '/login',
name: 'login',
builder: (context, state) => const LoginPage(),
),
GoRoute(
path: '/',
name: 'home',
builder: (context, state) {
// 支持 ?tab=N 查询参数,用于从其他页面跳回首页并切换到指定 tab
final tabParam = state.uri.queryParameters['tab'];
final initialTab = int.tryParse(tabParam ?? '') ?? 0;
return HomePage(initialTab: initialTab);
@@ -47,4 +78,9 @@ final goRouterProvider = Provider<GoRouter>((ref) {
),
],
);
});
});
/// 桥接 Riverpod 到 GoRouter 的 refreshListenable
class AuthRefreshStream extends ChangeNotifier {
void notify() => notifyListeners();
}