5dd7c07138
网络嗅探与自动恢复: - ConnectivityNotifier:在线时 30s 检测,离线时切换为 1min 嗅探 - 新增 networkRecoveryCountProvider:网络恢复时自增,各数据 provider 通过 ref.watch 实现自动重新加载 - inventory/product/stock_in/stock_out/partner provider 均已接入 FutureBuilder 类屏幕: - finance_screen + batch_tracking_screen 通过 ref.listen 监听恢复事件 修复问题 1:API 超时 10s/30s → 5s/15s,减少无网络时等待 修复问题 2:offline banner 文字由"写操作已禁用"改为实际行为描述 连通性感知: - app 启动时(_AppBootstrap)立即 forceCheck,路由跳转前状态已就绪 - 登录页:watch connectivityProvider,离线时显示警告 banner - 登录按钮:离线状态下直接提示,不等待超时 测试: - login_screen_test + login_flow_test 通过 ConnectivityNotifier(skipInit: true) 覆盖 provider,避免测试环境中 pending HTTP timer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
75 lines
2.2 KiB
Dart
75 lines
2.2 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:dio/dio.dart';
|
|
import '../core/api/api_client.dart';
|
|
import '../core/auth/auth_state.dart';
|
|
|
|
typedef AuthLoginFn = Future<AuthUser> Function({
|
|
required String shopCode,
|
|
required String username,
|
|
required String password,
|
|
});
|
|
|
|
class AuthException implements Exception {
|
|
final String message;
|
|
const AuthException(this.message);
|
|
@override
|
|
String toString() => message;
|
|
}
|
|
|
|
class AuthRepository {
|
|
@visibleForTesting
|
|
static AuthLoginFn? loginOverride;
|
|
|
|
/// 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 {
|
|
final override = loginOverride;
|
|
if (override != null) {
|
|
return override(
|
|
shopCode: shopCode,
|
|
username: username,
|
|
password: password,
|
|
);
|
|
}
|
|
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}';
|
|
}
|
|
}
|
|
}
|