api_router.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:convert';
  2. import 'package:archive/archive.dart';
  3. import 'package:dio/dio.dart';
  4. import '../../../utils/crypto.dart';
  5. import '../../../utils/developer/ix_developer_tools.dart';
  6. import '../../../utils/log/logger.dart';
  7. import '../../constants/keys.dart';
  8. import '../../data/models/api_exception.dart';
  9. import '../../data/models/api_result.dart';
  10. import '../../data/sp/ix_sp.dart';
  11. import '../base/base_api.dart';
  12. import '../core/api_core_paths.dart';
  13. /// 核心API
  14. class ApiRouter extends BaseApi {
  15. static final _instance = ApiRouter._internal();
  16. factory ApiRouter() => _instance;
  17. ApiRouter._internal() {
  18. _initDio();
  19. }
  20. /// 初始化Dio
  21. void _initDio() {
  22. // 此处可以设置默认的DIO参数
  23. final options = BaseOptions();
  24. options.connectTimeout = const Duration(seconds: 15);
  25. options.receiveTimeout = const Duration(seconds: 15);
  26. options.responseType = ResponseType.bytes;
  27. options.headers['content-type'] = 'application/json';
  28. dio = Dio(options);
  29. dio.interceptors.add(SimpleApiMonitorInterceptor());
  30. // 添加拦截器
  31. _setupInterceptors();
  32. }
  33. // bool _isRefreshing = false;
  34. void _setupInterceptors() {
  35. dio.interceptors.add(
  36. InterceptorsWrapper(
  37. onRequest: (options, handler) async {
  38. // 在请求发送前添加token
  39. final user = IXSP.getUser();
  40. if (user != null) {
  41. options.headers['Authorization'] = user.accessToken;
  42. }
  43. return handler.next(options);
  44. },
  45. onError: (DioException error, handler) async {
  46. return handler.next(error);
  47. },
  48. ),
  49. );
  50. }
  51. @override
  52. Map<String, dynamic>? getDefaultHeader() {
  53. // 可以设置自定义Header
  54. final headers = {
  55. 'X-NL-Product-Code': 'nomo',
  56. 'X-NL-Content-Encoding': 'gzip',
  57. };
  58. return headers;
  59. }
  60. @override
  61. Map<String, dynamic>? getDefaultQuery() {
  62. // 可以设置自定义Query
  63. return null;
  64. }
  65. @override
  66. dynamic encrypt(dynamic input) {
  67. try {
  68. final data = jsonEncode(input);
  69. final bytes = utf8.encode(data);
  70. final gzipEncoder = GZipEncoder();
  71. final compressedBytes = gzipEncoder.encode(bytes);
  72. log('ApiLog', '>>: $data');
  73. return Crypto.encryptBytesUint8(compressedBytes, Keys.aesKey, Keys.aesIv);
  74. } catch (error) {
  75. throw ApiException("-2", "encrypt error: $error");
  76. }
  77. }
  78. @override
  79. dynamic decrypt(dynamic input) {
  80. try {
  81. final decryptedBytes = Crypto.decryptBytes(
  82. base64Encode(input),
  83. Keys.aesKey,
  84. Keys.aesIv,
  85. );
  86. // 使用GZip解压
  87. final gzipDecoder = GZipDecoder();
  88. final decodeBytes = gzipDecoder.decodeBytes(decryptedBytes);
  89. final data = utf8.decode(decodeBytes);
  90. log('ApiLog', '<<:$data');
  91. return jsonDecode(data);
  92. } catch (error) {
  93. throw ApiException("-2", "decrypt error: $error");
  94. }
  95. }
  96. /// 获取调度信息
  97. Future<ApiResult> getDispatchInfo(
  98. dynamic data, {
  99. CancelToken? cancelToken,
  100. }) async {
  101. return post(
  102. ApiCorePaths.getDispatchInfo,
  103. data: data,
  104. cancelToken: cancelToken,
  105. );
  106. }
  107. /// 连接成功
  108. Future<ApiResult> connected(dynamic data, {CancelToken? cancelToken}) async {
  109. return post(ApiCorePaths.connected, data: data, cancelToken: cancelToken);
  110. }
  111. }