api_service.dart 5.6 KB

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