import 'dart:io'; import 'package:crypto/crypto.dart' as crypto; import 'package:dio/adapter.dart'; import 'package:dio/dio.dart'; import 'package:dio_cache_interceptor/dio_cache_interceptor.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:package_info/package_info.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/application.dart'; import 'package:sport/config.dart'; import 'package:sport/provider/bluetooth.dart'; import 'package:sport/provider/game_info_model.dart'; import 'package:sport/provider/game_model.dart'; import 'package:sport/provider/login_info_model.dart'; import 'package:sport/provider/message_model.dart'; import 'package:sport/router/navigator_util.dart'; import 'package:sport/services/api/login_api.dart'; import 'package:sport/services/api/rest_client.dart'; import 'package:sport/utils/toast.dart'; class ConfigInject { int? os; String? version; Future _merge(RequestOptions options) async { if (version == null) { PackageInfo info = await PackageInfo.fromPlatform(); version = "${info.version}+${info.buildNumber}"; } if (os == null) { os = Platform.isAndroid ? 1 : Platform.isIOS ? 2 : 3; } SharedPreferences prefs = await SharedPreferences.getInstance(); String token = prefs.getString("token") ?? ""; Map headers = {Headers.acceptHeader: "gzip"}; if (options.path.startsWith("/user/")) { headers.addAll(await Application.getDeviceInfoMap()); } headers["token"] = "$token"; var t = "${DateTime.now().millisecondsSinceEpoch}"; headers["t"] = t; headers["sign"] = "${signMd5("ouj.com$t$token")}"; return options.copyWith(headers: headers, queryParameters: options.queryParameters..addAll({"ver": "$version", "os": "$os", "sv": shoeVersion})); } String signMd5(String data) { return crypto.md5.convert(data.codeUnits).toString(); } void inject(Config config) { isDebugShoe = config.debug; GetIt getIt = GetIt.instance; getIt.registerSingleton( Dio(BaseOptions( baseUrl: config.baseUrl, connectTimeout: 10000, sendTimeout: 30000, receiveTimeout: 30000, // requestEncoder: (String request, RequestOptions options){ // return GZipCodec().encoder() // } )) ..interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options, RequestInterceptorHandler handler) { _merge(options).then((d) { handler.next(d); }).catchError((error, stackTrace) { handler.next(options); // handler.reject(error, true); }); }, onResponse: (Response response, ResponseInterceptorHandler handler) { if (response.requestOptions.path.contains("checkUpdate") == true) { handler.next(response); return; } if (response.data is Map) { Map resp = response.data; if (resp['code'] == -5 || resp['code'] == -14) { SharedPreferences.getInstance().then((value) { if (Application.context != null && value.getString("token")?.isNotEmpty == true) { ToastUtil.showBottom("${resp['msg']}"); NavigatorUtil.goLogin(Application.context!); } value.clear(); }); handler.resolve(response); return; } else if (resp['code'] != 0) { String? msg = resp['msg']; if (msg != null) ToastUtil.show("${resp['msg']}"); } } handler.next(response); }, onError: (DioError e, ErrorInterceptorHandler handler) { print("network error $e"); if (e.error is SocketException) { if (appLifecycleState == AppLifecycleState.resumed) ToastUtil.show("当前网络不可用"); } handler.next(e); })) ..interceptors.add(DioCacheInterceptor(options: CacheOptions(store: MemCacheStore()))) ..interceptors.add(LogInterceptor(request: config.debug, responseBody: config.debug, responseHeader: config.debug, requestBody: config.debug, requestHeader: config.debug)) ..httpClientAdapter = (DefaultHttpClientAdapter() ..onHttpClientCreate = (client) { // client.findProxy = (uri) { // return 'PROXY 172.16.14.128:8080'; // }; client.badCertificateCallback = (X509Certificate cert, String host, int port) { return true; }; return client; }), signalsReady: true); getIt.registerSingleton(RestClient(getIt(), baseUrl: config.baseUrl), signalsReady: true); getIt.registerSingleton(LoginApi(getIt()), signalsReady: true); getIt.registerSingleton(Bluetooth()); getIt.registerSingleton(GameModel()); getIt.registerSingleton(MessageModel()); getIt.registerSingleton(LoginInfoModel()); getIt.registerFactory(() => GameInfoModel()); } }