123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- import 'package:dart_json_mapper/dart_json_mapper.dart';
- import 'package:dio/dio.dart';
- import 'package:naiyouwl/app/common/LogHelper.dart';
- import 'package:naiyouwl/app/common/SharedPreferencesUtil.dart';
- import 'package:naiyouwl/app/data/model/LoginMode.dart';
- import 'package:naiyouwl/app/data/model/NodeMode.dart';
- import 'package:naiyouwl/app/data/model/UpdateVersion.dart';
- import '../data/model/SysConfig.dart';
- import '../data/model/UserMode.dart';
- import 'dio_client.dart';
- class ApiService {
- static final ApiService _instance = ApiService._internal();
- final DioClient _dioClient = DioClient();
- factory ApiService() => _instance;
- ApiService._internal();
- Future<void> init() async {
- String? lastSuccessfulUrl = await SharedPreferencesUtil().getString("last_successful_url");
- if (lastSuccessfulUrl != null && lastSuccessfulUrl.isNotEmpty) {
- //LogHelper().d("last_successful_url---- $lastSuccessfulUrl");
- _dioClient.updateBaseUrl(lastSuccessfulUrl);
- }
- }
- Future<SysConfig> fetchSysConfig(String path) async {
- final data = await _requestWrapper(() => _dioClient.get(path));
- final result = JsonMapper.deserialize<SysConfig>(data);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- //authUser
- Future<NodeMode> fetchAuthUser(String path) async {
- final data = await _requestWrapper(() => _dioClient.get(path));
- final result = JsonMapper.deserialize<NodeMode>(data);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<SysConfig> fetchLogout(String path) async {
- final data = await _requestWrapper(() => _dioClient.get(path));
- final result = JsonMapper.deserialize<SysConfig>(data);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<UpdateVersion> fetchUpdateVersion(String path, Map<String, dynamic> data) async {
- final ret = await _requestWrapper(() => _dioClient.get(path,queryParameters: data));
- final result = JsonMapper.deserialize<UpdateVersion>(ret);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<SysConfig> fetchUserSysConfig(String path, Map<String, dynamic> data) async {
- final ret = await _requestWrapper(() => _dioClient.get(path,queryParameters: data));
- final result = JsonMapper.deserialize<SysConfig>(ret);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<LoginMode> login(String path, {Map<String, dynamic>? data}) async {
- final retData =await _requestWrapper(() => _dioClient.post(path,data: data));
- final result = JsonMapper.deserialize<LoginMode>(retData);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<LoginMode> reg(String path, {Map<String, dynamic>? data}) async {
- final retData =await _requestWrapper(() => _dioClient.post(path,data: data));
- final result = JsonMapper.deserialize<LoginMode>(retData);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<User> userinfo(String path) async {
- final retData =await _requestWrapper(() => _dioClient.get(path));
- final result = JsonMapper.deserialize<User>(retData);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }
- Future<List<NodeMode>> getNode(String path) async {
- final retData = await _requestWrapper(() => _dioClient.get(path));
- if (retData is List) {
- // 遍历List并为每一项调用NodeMode.fromJson
- return (retData).map((item) {
- final result = JsonMapper.deserialize<NodeMode>(item);
- if (result != null) {
- return result;
- } else {
- throw Exception("Failed API response is NUll");
- }
- }).toList();
- } else {
- throw Exception("Expected a list but received ${retData.runtimeType}");
- }
- }
- Future<Map<String, dynamic>> fetchData(String path, {Map<String, dynamic>? queryParameters}) async {
- return await _dioClient.get(path, queryParameters: queryParameters);
- }
- Future<Map<String, dynamic>> createData(String path, {Map<String, dynamic>? data}) async {
- return await _dioClient.post(path, data: data);
- }
- Future<Map<String, dynamic>> updateData(String path, {Map<String, dynamic>? data}) async {
- return await _dioClient.put(path, data: data);
- }
- Future<void> downloadFile(String urlPath, String savePath) async {
- await _dioClient.download(urlPath, savePath);
- }
- Future<dynamic> _requestWrapper(Future<dynamic> Function() apiCall) async {
- try {
- await ApiService().init();
- final response = await apiCall();
- // 如果你有返回的HTTP状态码, 检查它们
- // if(response.statusCode < 200 || response.statusCode >= 300) {
- // throw Exception('API returned non-success status code: ${response.statusCode}');
- // }
- // 对于特定的请求, 检查返回值是否为null
- if (response == null) {
- throw Exception('API response is null');
- }
- return response;
- } catch (e) {
- // 日志错误
- print('API request error: $e');
- if (e is DioError && e.error is AppException) {
- final appException = e.error as AppException;
- print('API request failed with status code: ${appException.statusCode}');
- throw appException; // 抛出自定义的AppException
- } else {
- throw Exception('API request failed: $e');
- }
- }
- }
- }
|