Files
jiu/client/lib/repositories/auth_repository.dart
T
wangjia d717fb3735 feat(client): 全模块 API 对接 + 修复登陆跳转 + 菜单 UI 优化
API 对接:
- 入库/出库/库存/财务/往来单位/基础数据全部对接后端 REST API
- 新增 repositories、providers、models 层,统一分层架构
- auth 从 flutter_secure_storage 迁移到 shared_preferences

登陆跳转修复:
- 将 _RouterNotifier 提取为独立 Riverpod provider,appRouterProvider
  使用 ref.read 避免依赖链导致 router 重建后跳回 /login
- redirect 函数新增 initialized 守卫,防止 auth 未恢复时误重定向
- 添加调试日志(Router/Auth/ApiClient)定位 401 触发的 logout 链路

退出菜单 UI:
- 去掉 ListTile,改用 Row + 自定义 padding,文字左对齐
- MouseRegion + AnimatedContainer 实现 hover 高亮(普通项蓝底/退出红底)
- 菜单圆角 6px,elevation 8,分割线高度 1px

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-07 22:20:28 +08:00

57 lines
1.8 KiB
Dart

import 'package:dio/dio.dart';
import '../core/api/api_client.dart';
import '../core/auth/auth_state.dart';
class AuthException implements Exception {
final String message;
const AuthException(this.message);
@override
String toString() => message;
}
class AuthRepository {
/// POST /api/v1/auth/login
/// Request: { shop_code, username, password }
/// Response: { data: { access_token, refresh_token, expires_in, user: { id, username, real_name, role } } }
static Future<AuthUser> login({
required String shopCode,
required String username,
required String password,
}) async {
try {
final resp = await PublicApiClient.post('/auth/login', data: {
'shop_code': shopCode,
'username': username,
'password': password,
});
final data = resp.data['data'] as Map<String, dynamic>;
final user = data['user'] as Map<String, dynamic>;
return AuthUser(
accessToken: data['access_token'] as String,
refreshToken: data['refresh_token'] as String,
username: user['username'] as String,
realName: user['real_name'] as String? ?? username,
shopNo: shopCode,
shopId: (data['shop_id'] as num).toInt(),
);
} on DioException catch (e) {
final msg = e.response?.data?['error'] as String?;
throw AuthException(msg ?? _networkError(e));
}
}
static String _networkError(DioException e) {
switch (e.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.receiveTimeout:
return '连接超时,请检查网络';
case DioExceptionType.connectionError:
return '无法连接到服务器(localhost:8080),请先启动后端';
default:
return '网络错误:${e.message}';
}
}
}