feat(client): 登录模块对接后端 API
- auth_state: 增加 refreshToken 字段 + flutter_secure_storage 持久化 - api_client: 增加 401 自动 refresh + retry 拦截器 - auth_repository: 封装 POST /api/v1/auth/login 调用,错误信息本地化 - login_screen: 增加门店编号字段,调真实 API,行内错误展示 - main: 启动时 restore 持久化登录态,避免每次重启都要重新登录 - 修复 app_shell: hotelName → hotelNo - 修复 widget_test: 移除失效的占位测试 测试账号:门店编号 H001 / 用户名 admin / 密码 password123 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../core/auth/auth_state.dart';
|
||||
import '../../core/theme/app_theme.dart';
|
||||
import '../../repositories/auth_repository.dart';
|
||||
|
||||
class LoginScreen extends ConsumerStatefulWidget {
|
||||
const LoginScreen({super.key});
|
||||
@@ -13,13 +14,16 @@ class LoginScreen extends ConsumerStatefulWidget {
|
||||
|
||||
class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _hotelCodeCtrl = TextEditingController();
|
||||
final _usernameCtrl = TextEditingController();
|
||||
final _passwordCtrl = TextEditingController();
|
||||
bool _loading = false;
|
||||
bool _obscure = true;
|
||||
String? _errorMessage;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_hotelCodeCtrl.dispose();
|
||||
_usernameCtrl.dispose();
|
||||
_passwordCtrl.dispose();
|
||||
super.dispose();
|
||||
@@ -27,20 +31,26 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
|
||||
Future<void> _login() async {
|
||||
if (!_formKey.currentState!.validate()) return;
|
||||
setState(() => _loading = true);
|
||||
await Future.delayed(const Duration(milliseconds: 600));
|
||||
// Mock login for UI demo
|
||||
ref.read(authStateProvider.notifier).login(AuthUser(
|
||||
token: 'demo-token',
|
||||
username: _usernameCtrl.text,
|
||||
hotelName: '示范大酒店',
|
||||
hotelNo: 'H001',
|
||||
hotelId: 1,
|
||||
));
|
||||
if (mounted) {
|
||||
context.go('/stock-in');
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_errorMessage = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final user = await AuthRepository.login(
|
||||
hotelCode: _hotelCodeCtrl.text.trim(),
|
||||
username: _usernameCtrl.text.trim(),
|
||||
password: _passwordCtrl.text,
|
||||
);
|
||||
await ref.read(authStateProvider.notifier).login(user);
|
||||
if (mounted) context.go('/stock-in');
|
||||
} on AuthException catch (e) {
|
||||
setState(() => _errorMessage = e.message);
|
||||
} catch (e) {
|
||||
setState(() => _errorMessage = '登录失败:$e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -80,7 +90,7 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: AppTheme.primary.withOpacity(0.4),
|
||||
color: AppTheme.primary.withAlpha(102),
|
||||
blurRadius: 12,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
@@ -105,20 +115,37 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
style: TextStyle(
|
||||
fontSize: 13, color: AppTheme.textSecondary),
|
||||
),
|
||||
const SizedBox(height: 36),
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// Hotel code
|
||||
TextFormField(
|
||||
controller: _hotelCodeCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '门店编号',
|
||||
hintText: '请输入门店编号',
|
||||
prefixIcon: Icon(Icons.store_outlined, size: 20),
|
||||
),
|
||||
validator: (v) =>
|
||||
(v == null || v.isEmpty) ? '请输入门店编号' : null,
|
||||
textInputAction: TextInputAction.next,
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
|
||||
// Username
|
||||
TextFormField(
|
||||
controller: _usernameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '用户名',
|
||||
hintText: '请输入用户名',
|
||||
prefixIcon:
|
||||
Icon(Icons.person_outline, size: 20),
|
||||
prefixIcon: Icon(Icons.person_outline, size: 20),
|
||||
),
|
||||
validator: (v) =>
|
||||
(v == null || v.isEmpty) ? '请输入用户名' : null,
|
||||
textInputAction: TextInputAction.next,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const SizedBox(height: 14),
|
||||
|
||||
// Password
|
||||
TextFormField(
|
||||
controller: _passwordCtrl,
|
||||
obscureText: _obscure,
|
||||
@@ -141,7 +168,38 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
(v == null || v.isEmpty) ? '请输入密码' : null,
|
||||
onFieldSubmitted: (_) => _login(),
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
|
||||
// Inline error message
|
||||
if (_errorMessage != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.danger.withAlpha(15),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
border: Border.all(
|
||||
color: AppTheme.danger.withAlpha(80)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.error_outline,
|
||||
color: AppTheme.danger, size: 16),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_errorMessage!,
|
||||
style: const TextStyle(
|
||||
color: AppTheme.danger, fontSize: 13),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
const SizedBox(height: 24),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 46,
|
||||
@@ -164,18 +222,12 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
letterSpacing: 2)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextButton(
|
||||
onPressed: () {},
|
||||
child: const Text('忘记密码?'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// Bottom version info
|
||||
Positioned(
|
||||
bottom: 16,
|
||||
left: 0,
|
||||
@@ -184,7 +236,7 @@ class _LoginScreenState extends ConsumerState<LoginScreen> {
|
||||
child: Text(
|
||||
'© 2026 酒库管理系统 v1.0.0',
|
||||
style: TextStyle(
|
||||
color: Colors.white.withOpacity(0.4), fontSize: 12),
|
||||
color: Colors.white.withAlpha(102), fontSize: 12),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -198,13 +250,13 @@ class _BackgroundPainter extends CustomPainter {
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final paint = Paint()
|
||||
..color = const Color(0xFF1976D2).withOpacity(0.15)
|
||||
..color = const Color(0xFF1976D2).withAlpha(38)
|
||||
..style = PaintingStyle.fill;
|
||||
canvas.drawCircle(
|
||||
Offset(size.width * 0.1, size.height * 0.2), 180, paint);
|
||||
canvas.drawCircle(
|
||||
Offset(size.width * 0.9, size.height * 0.8), 220, paint);
|
||||
paint.color = const Color(0xFF0D47A1).withOpacity(0.1);
|
||||
paint.color = const Color(0xFF0D47A1).withAlpha(26);
|
||||
canvas.drawCircle(
|
||||
Offset(size.width * 0.8, size.height * 0.1), 140, paint);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user