config_inject.dart 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'dart:io';
  2. import 'package:dartin/dartin.dart';
  3. import 'package:dio/dio.dart';
  4. import 'package:dio_http_cache/dio_http_cache.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:package_info/package_info.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:sport/config.dart';
  9. import 'package:sport/provider/game_info_model.dart';
  10. import 'package:sport/provider/login_info_model.dart';
  11. import 'package:sport/router/navigator_util.dart';
  12. import 'package:sport/services/api/login_api.dart';
  13. import 'package:sport/services/api/resp.dart';
  14. import 'package:sport/services/api/rest_client.dart';
  15. import 'package:sport/utils/toast.dart';
  16. mixin ConfigInject<T extends StatefulWidget> on State<T> {
  17. int _os;
  18. String _version;
  19. void initState() {
  20. super.initState();
  21. final config = Config.config;
  22. startDartIn([
  23. Module([
  24. single<DioCacheManager>(({params}) {
  25. return DioCacheManager(CacheConfig());
  26. }),
  27. single<Dio>(({params}) {
  28. BaseOptions options = new BaseOptions(
  29. baseUrl: config.baseUrl,
  30. connectTimeout: 10000,
  31. receiveTimeout: 10000,
  32. );
  33. // 两点就是直接返回自身对象 类似链式调用
  34. return Dio(options)
  35. ..interceptors.add(inject<DioCacheManager>().interceptor)
  36. ..interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
  37. // if(!options.headers.containsKey("Content-Type"))
  38. // options.headers["Content-Type"] = "application/json";
  39. if (_version == null) {
  40. _version = (await PackageInfo.fromPlatform()).version;
  41. }
  42. if (_os == null) {
  43. _os = Platform.isAndroid ? 1 : Platform.isIOS ? 2 : 3;
  44. }
  45. SharedPreferences prefs = await SharedPreferences.getInstance();
  46. String token = prefs.getString("token");
  47. return options.merge(headers: {"token": token}, queryParameters: options.queryParameters..addAll({"ver": "$_version", "os": "$_os"}));
  48. }, onResponse: (Response response) async {
  49. if(response.request?.path?.contains("checkUpdate") == true){
  50. return response;
  51. }
  52. if (response.data is Map<String, dynamic>) {
  53. Map<String, dynamic> resp = response.data;
  54. if (resp['code'] == -5) {
  55. ToastUtil.show("${resp['msg']}");
  56. NavigatorUtil.goLogin(context);
  57. } else if (resp['code'] != 0) {
  58. String msg = resp['msg'];
  59. if (msg != null) ToastUtil.show("${resp['msg']}");
  60. }
  61. }
  62. return response;
  63. }))
  64. ..interceptors.add(LogInterceptor(responseBody: true, responseHeader: false, requestBody: true));
  65. }),
  66. // 注入实例
  67. single<RestClient>(({params}) {
  68. final dio = inject<Dio>();
  69. return RestClient(dio);
  70. }),
  71. single<LoginApi>(({params}) {
  72. final dio = inject<Dio>();
  73. return LoginApi(dio);
  74. }),
  75. // single<GameApi>(({params}) {
  76. // final dio = inject<Dio>();
  77. // return GameApi(dio);
  78. // }),
  79. ]),
  80. Module([
  81. factory<LoginInfoModel>(({params}) => LoginInfoModel()),
  82. factory<GameInfoModel>(({params}) => GameInfoModel()),
  83. ])
  84. ]);
  85. }
  86. }