api_service.dart 4.8 KB

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