api_service.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:dart_json_mapper/dart_json_mapper.dart';
  2. import 'package:dio/dio.dart';
  3. import 'package:naiyouwl/app/data/model/LoginMode.dart';
  4. import 'package:naiyouwl/app/data/model/NodeMode.dart';
  5. import '../data/model/SysConfig.dart';
  6. import '../data/model/UserMode.dart';
  7. import 'dio_client.dart';
  8. class ApiService {
  9. static final ApiService _instance = ApiService._internal();
  10. final DioClient _dioClient = DioClient();
  11. factory ApiService() => _instance;
  12. ApiService._internal();
  13. Future<SysConfig> fetchSysConfig(String path) async {
  14. final data = await _requestWrapper(() => _dioClient.get(path));
  15. final result = JsonMapper.deserialize<SysConfig>(data);
  16. if (result != null) {
  17. return result;
  18. } else {
  19. throw Exception("Failed API response is NUll");
  20. }
  21. }
  22. Future<LoginMode> login(String path, {Map<String, dynamic>? data}) async {
  23. final retData =await _requestWrapper(() => _dioClient.post(path,data: data));
  24. final result = JsonMapper.deserialize<LoginMode>(retData);
  25. if (result != null) {
  26. return result;
  27. } else {
  28. throw Exception("Failed API response is NUll");
  29. }
  30. }
  31. Future<User> userinfo(String path) async {
  32. final retData =await _requestWrapper(() => _dioClient.get(path));
  33. final result = JsonMapper.deserialize<User>(retData);
  34. if (result != null) {
  35. return result;
  36. } else {
  37. throw Exception("Failed API response is NUll");
  38. }
  39. }
  40. Future<List<NodeMode>> getNode(String path) async {
  41. final retData = await _requestWrapper(() => _dioClient.get(path));
  42. if (retData is List) {
  43. // 遍历List并为每一项调用NodeMode.fromJson
  44. return (retData).map((item) {
  45. final result = JsonMapper.deserialize<NodeMode>(item);
  46. if (result != null) {
  47. return result;
  48. } else {
  49. throw Exception("Failed API response is NUll");
  50. }
  51. }).toList();
  52. } else {
  53. throw Exception("Expected a list but received ${retData.runtimeType}");
  54. }
  55. }
  56. Future<Map<String, dynamic>> fetchData(String path, {Map<String, dynamic>? queryParameters}) async {
  57. return await _dioClient.get(path, queryParameters: queryParameters);
  58. }
  59. Future<Map<String, dynamic>> createData(String path, {Map<String, dynamic>? data}) async {
  60. return await _dioClient.post(path, data: data);
  61. }
  62. Future<Map<String, dynamic>> updateData(String path, {Map<String, dynamic>? data}) async {
  63. return await _dioClient.put(path, data: data);
  64. }
  65. Future<void> downloadFile(String urlPath, String savePath) async {
  66. await _dioClient.download(urlPath, savePath);
  67. }
  68. Future<dynamic> _requestWrapper(Future<dynamic> Function() apiCall) async {
  69. try {
  70. final response = await apiCall();
  71. // 如果你有返回的HTTP状态码, 检查它们
  72. // if(response.statusCode < 200 || response.statusCode >= 300) {
  73. // throw Exception('API returned non-success status code: ${response.statusCode}');
  74. // }
  75. // 对于特定的请求, 检查返回值是否为null
  76. if (response == null) {
  77. throw Exception('API response is null');
  78. }
  79. return response;
  80. } catch (e) {
  81. // 日志错误
  82. print('API request error: $e');
  83. if (e is DioError && e.error is AppException) {
  84. throw e.error; // 抛出自定义的AppException
  85. } else {
  86. throw Exception('API request failed: $e');
  87. }
  88. }
  89. }
  90. }