http_provider_injection.dart 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'package:domain/constants/api_constants.dart';
  2. import 'package:plugin_basic/constants/app_constant.dart';
  3. import 'package:plugin_platform/engine/sp/sp_util.dart';
  4. import 'package:plugin_platform/http/http_provider.dart';
  5. import '../basic_export.dart';
  6. import '../dio_interceptors/interceptor_auth_dio.dart';
  7. import '../dio_interceptors/interceptor_status_code_dio.dart';
  8. class HttpProviderInjection {
  9. // 设置默认的
  10. static void putHttpProviderByCountry() {
  11. //根据选中的国家赋值Api
  12. String baseUrl;
  13. int country = SPUtil.getInt(AppConstant.storageSelectedCountry, defValue: 0) ?? 0;
  14. if (country == 1) {
  15. baseUrl = ApiConstants.sgBaseUrl;
  16. } else {
  17. baseUrl = ApiConstants.baseUrl;
  18. }
  19. //注入 HttpProvider
  20. Get.put(
  21. HttpProvider(
  22. baseUrl,
  23. interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()], //需要加上与App关联的一些拦截处理
  24. ),
  25. permanent: true);
  26. }
  27. // 切换应用的BaseUrl的逻辑
  28. static void switchBaseUrl(String baseUrl) {
  29. //找到单例对象 HttpProvider,切换域名
  30. HttpProvider httpProvider = Get.find();
  31. httpProvider.switchBaseUrl(baseUrl);
  32. }
  33. }