feat(app): 班级码验证前后端联调 — AuthBloc接入API + 错误计数锁定UI
This commit is contained in:
@@ -31,6 +31,19 @@ class _ClassCodeJoinPageState extends State<ClassCodeJoinPage> {
|
||||
(_) => FocusNode(),
|
||||
);
|
||||
|
||||
int _failedAttempts = 0;
|
||||
DateTime? _lockoutEndTime;
|
||||
|
||||
/// 当前是否被锁定
|
||||
bool get _isCurrentlyLocked {
|
||||
if (_lockoutEndTime == null) return false;
|
||||
if (DateTime.now().isAfter(_lockoutEndTime!)) {
|
||||
_lockoutEndTime = null;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -58,6 +71,14 @@ class _ClassCodeJoinPageState extends State<ClassCodeJoinPage> {
|
||||
bool get _isComplete =>
|
||||
_controllers.every((c) => c.text.isNotEmpty);
|
||||
|
||||
/// 清空所有输入框
|
||||
void _clearInputs() {
|
||||
for (final c in _controllers) {
|
||||
c.clear();
|
||||
}
|
||||
_focusNodes[0].requestFocus();
|
||||
}
|
||||
|
||||
void _onChanged(int index, String value) {
|
||||
if (value.isEmpty && index > 0) {
|
||||
// 退格清空 → 跳到前一位
|
||||
@@ -74,13 +95,48 @@ class _ClassCodeJoinPageState extends State<ClassCodeJoinPage> {
|
||||
}
|
||||
|
||||
void _submit() {
|
||||
if (!_isComplete) return;
|
||||
if (!_isComplete || _isCurrentlyLocked) return;
|
||||
context.read<AuthBloc>().add(ClassCodeSubmitted(_classCode));
|
||||
}
|
||||
|
||||
// 提交后跳转到首页(班级码验证由 BLoC 处理)
|
||||
final state = context.read<AuthBloc>().state;
|
||||
if (state is Authenticated && !state.needsClassCode) {
|
||||
/// 处理 AuthBloc 状态变化
|
||||
void _handleAuthState(BuildContext context, AuthState state) {
|
||||
if (state is! Authenticated) return;
|
||||
|
||||
// 成功加入班级
|
||||
if (!state.needsClassCode) {
|
||||
context.go('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
// 班级码验证错误
|
||||
final error = state.classCodeError;
|
||||
if (error != null && error.isNotEmpty) {
|
||||
_failedAttempts++;
|
||||
|
||||
// 检查是否为锁定错误
|
||||
if (error.contains('30 分钟') || error.contains('尝试次数过多')) {
|
||||
setState(() {
|
||||
_lockoutEndTime = DateTime.now().add(
|
||||
Duration(minutes: DesignTokens.classCodeLockoutMinutes),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// 清空输入
|
||||
_clearInputs();
|
||||
|
||||
// 显示错误提示
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).clearSnackBars();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(error),
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
duration: const Duration(seconds: 3),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,75 +144,136 @@ class _ClassCodeJoinPageState extends State<ClassCodeJoinPage> {
|
||||
Widget build(BuildContext context) {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: DesignTokens.spacing24,
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Spacer(flex: 2),
|
||||
return BlocListener<AuthBloc, AuthState>(
|
||||
listener: _handleAuthState,
|
||||
child: Scaffold(
|
||||
body: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: DesignTokens.spacing24,
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Spacer(flex: 2),
|
||||
|
||||
// 标题
|
||||
Icon(
|
||||
Icons.groups_rounded,
|
||||
size: 64,
|
||||
color: colorScheme.primary,
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing24),
|
||||
Text(
|
||||
'加入班级',
|
||||
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing8),
|
||||
Text(
|
||||
'输入老师提供的 6 位班级码',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing48),
|
||||
|
||||
// 6 位输入框
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: List.generate(
|
||||
DesignTokens.classCodeLength,
|
||||
(index) => _buildCodeInput(context, index, colorScheme),
|
||||
// 标题
|
||||
Icon(
|
||||
Icons.groups_rounded,
|
||||
size: 64,
|
||||
color: colorScheme.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing24),
|
||||
const SizedBox(height: DesignTokens.spacing24),
|
||||
Text(
|
||||
'加入班级',
|
||||
style: Theme.of(context).textTheme.headlineMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing8),
|
||||
Text(
|
||||
'输入老师提供的 6 位班级码',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: DesignTokens.spacing48),
|
||||
|
||||
// 跳过按钮
|
||||
TextButton(
|
||||
onPressed: () => context.go('/home'),
|
||||
child: Text(
|
||||
'稍后再加入',
|
||||
style: TextStyle(
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.5),
|
||||
// 锁定状态 or 输入框
|
||||
if (_isCurrentlyLocked)
|
||||
_buildLockoutView(context, colorScheme)
|
||||
else
|
||||
_buildCodeInputs(context, colorScheme),
|
||||
|
||||
const SizedBox(height: DesignTokens.spacing24),
|
||||
|
||||
// 跳过按钮
|
||||
TextButton(
|
||||
onPressed: () => context.go('/home'),
|
||||
child: Text(
|
||||
'稍后再加入',
|
||||
style: TextStyle(
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const Spacer(flex: 3),
|
||||
],
|
||||
// 剩余尝试次数提示
|
||||
if (!_isCurrentlyLocked && _failedAttempts > 0)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(
|
||||
'剩余 ${DesignTokens.classCodeMaxAttempts - _failedAttempts} 次尝试机会',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: _failedAttempts >= 4
|
||||
? colorScheme.error
|
||||
: colorScheme.onSurface.withValues(alpha: 0.5),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const Spacer(flex: 3),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 锁定视图 — 超过最大尝试次数后显示
|
||||
Widget _buildLockoutView(BuildContext context, ColorScheme colorScheme) {
|
||||
return Column(
|
||||
children: [
|
||||
Icon(Icons.lock_outline, size: 48, color: colorScheme.error),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'尝试次数过多',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'请等待 ${DesignTokens.classCodeLockoutMinutes} 分钟后再试',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.6),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 6 位班级码输入框
|
||||
Widget _buildCodeInputs(BuildContext context, ColorScheme colorScheme) {
|
||||
return BlocBuilder<AuthBloc, AuthState>(
|
||||
builder: (context, state) {
|
||||
final isLoading = state is Authenticated && state.isLoading;
|
||||
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: List.generate(
|
||||
DesignTokens.classCodeLength,
|
||||
(index) => _buildCodeInput(context, index, colorScheme, isLoading),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 单个班级码输入框
|
||||
Widget _buildCodeInput(BuildContext context, int index, ColorScheme colorScheme) {
|
||||
Widget _buildCodeInput(
|
||||
BuildContext context,
|
||||
int index,
|
||||
ColorScheme colorScheme,
|
||||
bool isLoading,
|
||||
) {
|
||||
return SizedBox(
|
||||
width: 48,
|
||||
height: 56,
|
||||
child: TextField(
|
||||
controller: _controllers[index],
|
||||
focusNode: _focusNodes[index],
|
||||
enabled: !isLoading,
|
||||
textAlign: TextAlign.center,
|
||||
textAlignVertical: TextAlignVertical.center,
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
|
||||
Reference in New Issue
Block a user