api_service.dart 5.7 KB

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