feat: rewrite ApiService for registration API, remove ScanRecord model
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,21 +0,0 @@
|
|||||||
enum SendStatus { pending, success, failed }
|
|
||||||
|
|
||||||
class ScanRecord {
|
|
||||||
final String barcode;
|
|
||||||
final String codeType;
|
|
||||||
final DateTime timestamp;
|
|
||||||
SendStatus status;
|
|
||||||
|
|
||||||
ScanRecord({
|
|
||||||
required this.barcode,
|
|
||||||
required this.codeType,
|
|
||||||
required this.timestamp,
|
|
||||||
this.status = SendStatus.pending,
|
|
||||||
});
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'barcode': barcode,
|
|
||||||
'code_type': codeType,
|
|
||||||
'timestamp': timestamp.toUtc().toIso8601String(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,29 +1,78 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
|
/// Result of a registration API call.
|
||||||
|
class RegistrationResult {
|
||||||
|
final bool success;
|
||||||
|
final bool isDuplicate;
|
||||||
|
final String? errorMessage;
|
||||||
|
final Map<String, dynamic>? duplicateInfo;
|
||||||
|
|
||||||
|
RegistrationResult({
|
||||||
|
required this.success,
|
||||||
|
this.isDuplicate = false,
|
||||||
|
this.errorMessage,
|
||||||
|
this.duplicateInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory RegistrationResult.ok() =>
|
||||||
|
RegistrationResult(success: true);
|
||||||
|
|
||||||
|
factory RegistrationResult.duplicate(Map<String, dynamic> info) =>
|
||||||
|
RegistrationResult(success: false, isDuplicate: true, duplicateInfo: info);
|
||||||
|
|
||||||
|
factory RegistrationResult.error(String message) =>
|
||||||
|
RegistrationResult(success: false, errorMessage: message);
|
||||||
|
}
|
||||||
|
|
||||||
class ApiService {
|
class ApiService {
|
||||||
final http.Client _client;
|
final http.Client _client;
|
||||||
|
final Duration timeout;
|
||||||
|
|
||||||
ApiService({http.Client? client}) : _client = client ?? http.Client();
|
ApiService({http.Client? client, this.timeout = const Duration(seconds: 5)})
|
||||||
|
: _client = client ?? http.Client();
|
||||||
|
|
||||||
Future<bool> sendScanData(
|
/// Submit a shelf registration (上架登记).
|
||||||
String url, {
|
Future<RegistrationResult> registerLocation({
|
||||||
required String barcode,
|
required String baseUrl,
|
||||||
required String codeType,
|
required String zongpaiNo,
|
||||||
|
required String locationCode,
|
||||||
}) async {
|
}) async {
|
||||||
|
final uri = Uri.parse('$baseUrl/CargoTrace/location');
|
||||||
try {
|
try {
|
||||||
final response = await _client
|
final response = await _client
|
||||||
.post(
|
.post(
|
||||||
Uri.parse(url),
|
uri,
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: jsonEncode({
|
body: jsonEncode({
|
||||||
'barcode': barcode,
|
'zongpai_no': zongpaiNo,
|
||||||
'code_type': codeType,
|
'location_code': locationCode,
|
||||||
'timestamp': DateTime.now().toUtc().toIso8601String(),
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 5));
|
.timeout(timeout);
|
||||||
return response.statusCode >= 200 && response.statusCode < 300;
|
|
||||||
|
switch (response.statusCode) {
|
||||||
|
case 200:
|
||||||
|
return RegistrationResult.ok();
|
||||||
|
case 409:
|
||||||
|
final body = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
return RegistrationResult.duplicate(body);
|
||||||
|
default:
|
||||||
|
final body = jsonDecode(response.body) as Map<String, dynamic>;
|
||||||
|
final msg = body['error'] ?? body['message'] ?? 'Unknown error (${response.statusCode})';
|
||||||
|
return RegistrationResult.error(msg.toString());
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return RegistrationResult.error('网络异常,请检查网络连接');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test connectivity by making a HEAD request to the base URL.
|
||||||
|
Future<bool> testConnection(String baseUrl) async {
|
||||||
|
try {
|
||||||
|
final uri = Uri.parse(baseUrl);
|
||||||
|
final response = await _client.head(uri).timeout(timeout);
|
||||||
|
return response.statusCode < 500;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user