api_service.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 'package:naiyouwl/app/data/model/UpdateVersion.dart';
  6. import '../data/model/SysConfig.dart';
  7. import '../data/model/UserMode.dart';
  8. import 'dio_client.dart';
  9. class ApiService {
  10. static final ApiService _instance = ApiService._internal();
  11. final DioClient _dioClient = DioClient();
  12. factory ApiService() => _instance;
  13. ApiService._internal();
  14. Future<SysConfig> fetchSysConfig(String path) async {
  15. final data = await _requestWrapper(() => _dioClient.get(path));
  16. final result = JsonMapper.deserialize<SysConfig>(data);
  17. if (result != null) {
  18. return result;
  19. } else {
  20. throw Exception("Failed API response is NUll");
  21. }
  22. }
  23. //authUser
  24. Future<NodeMode> fetchAuthUser(String path) async {
  25. final data = await _requestWrapper(() => _dioClient.get(path));
  26. final result = JsonMapper.deserialize<NodeMode>(data);
  27. if (result != null) {
  28. return result;
  29. } else {
  30. throw Exception("Failed API response is NUll");
  31. }
  32. }
  33. Future<SysConfig> fetchLogout(String path) async {
  34. final data = await _requestWrapper(() => _dioClient.get(path));
  35. final result = JsonMapper.deserialize<SysConfig>(data);
  36. if (result != null) {
  37. return result;
  38. } else {
  39. throw Exception("Failed API response is NUll");
  40. }
  41. }
  42. Future<UpdateVersion> fetchUpdateVersion(String path, Map<String, dynamic> data) async {
  43. final ret = await _requestWrapper(() => _dioClient.get(path,queryParameters: data));
  44. final result = JsonMapper.deserialize<UpdateVersion>(ret);
  45. if (result != null) {
  46. return result;
  47. } else {
  48. throw Exception("Failed API response is NUll");
  49. }
  50. }
  51. Future<SysConfig> fetchUserSysConfig(String path, Map<String, dynamic> data) async {
  52. final ret = await _requestWrapper(() => _dioClient.get(path,queryParameters: data));
  53. final result = JsonMapper.deserialize<SysConfig>(ret);
  54. if (result != null) {
  55. return result;
  56. } else {
  57. throw Exception("Failed API response is NUll");
  58. }
  59. }
  60. Future<LoginMode> login(String path, {Map<String, dynamic>? data}) async {
  61. final retData =await _requestWrapper(() => _dioClient.post(path,data: data));
  62. final result = JsonMapper.deserialize<LoginMode>(retData);
  63. if (result != null) {
  64. return result;
  65. } else {
  66. throw Exception("Failed API response is NUll");
  67. }
  68. }
  69. Future<LoginMode> reg(String path, {Map<String, dynamic>? data}) async {
  70. final retData =await _requestWrapper(() => _dioClient.post(path,data: data));
  71. final result = JsonMapper.deserialize<LoginMode>(retData);
  72. if (result != null) {
  73. return result;
  74. } else {
  75. throw Exception("Failed API response is NUll");
  76. }
  77. }
  78. Future<User> userinfo(String path) async {
  79. final retData =await _requestWrapper(() => _dioClient.get(path));
  80. final result = JsonMapper.deserialize<User>(retData);
  81. if (result != null) {
  82. return result;
  83. } else {
  84. throw Exception("Failed API response is NUll");
  85. }
  86. }
  87. Future<List<NodeMode>> getNode(String path) async {
  88. final retData = await _requestWrapper(() => _dioClient.get(path));
  89. if (retData is List) {
  90. // 遍历List并为每一项调用NodeMode.fromJson
  91. return (retData).map((item) {
  92. final result = JsonMapper.deserialize<NodeMode>(item);
  93. if (result != null) {
  94. return result;
  95. } else {
  96. throw Exception("Failed API response is NUll");
  97. }
  98. }).toList();
  99. } else {
  100. throw Exception("Expected a list but received ${retData.runtimeType}");
  101. }
  102. }
  103. Future<Map<String, dynamic>> fetchData(String path, {Map<String, dynamic>? queryParameters}) async {
  104. return await _dioClient.get(path, queryParameters: queryParameters);
  105. }
  106. Future<Map<String, dynamic>> createData(String path, {Map<String, dynamic>? data}) async {
  107. return await _dioClient.post(path, data: data);
  108. }
  109. Future<Map<String, dynamic>> updateData(String path, {Map<String, dynamic>? data}) async {
  110. return await _dioClient.put(path, data: data);
  111. }
  112. Future<void> downloadFile(String urlPath, String savePath) async {
  113. await _dioClient.download(urlPath, savePath);
  114. }
  115. Future<dynamic> _requestWrapper(Future<dynamic> Function() apiCall) async {
  116. try {
  117. final response = await apiCall();
  118. // 如果你有返回的HTTP状态码, 检查它们
  119. // if(response.statusCode < 200 || response.statusCode >= 300) {
  120. // throw Exception('API returned non-success status code: ${response.statusCode}');
  121. // }
  122. // 对于特定的请求, 检查返回值是否为null
  123. if (response == null) {
  124. throw Exception('API response is null');
  125. }
  126. return response;
  127. } catch (e) {
  128. // 日志错误
  129. print('API request error: $e');
  130. if (e is DioError && e.error is AppException) {
  131. final appException = e.error as AppException;
  132. print('API request failed with status code: ${appException.statusCode}');
  133. throw appException; // 抛出自定义的AppException
  134. } else {
  135. throw Exception('API request failed: $e');
  136. }
  137. }
  138. }
  139. }