123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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<RequestOptions> _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<String, dynamic> 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>(
- 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<String, dynamic>) {
- Map<String, dynamic> 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>(RestClient(getIt<Dio>(), baseUrl: config.baseUrl), signalsReady: true);
- getIt.registerSingleton<LoginApi>(LoginApi(getIt<Dio>()), signalsReady: true);
- getIt.registerSingleton<Bluetooth>(Bluetooth());
- getIt.registerSingleton<GameModel>(GameModel());
- getIt.registerSingleton<MessageModel>(MessageModel());
- getIt.registerSingleton<LoginInfoModel>(LoginInfoModel());
- getIt.registerFactory(() => GameInfoModel());
- }
- }
|