http_provider_injection.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. //新加坡
  16. baseUrl = ApiConstants.sgBaseUrl;
  17. } else if (country == 2) {
  18. //韩国
  19. baseUrl = ApiConstants.koreaBaseUrl;
  20. } else {
  21. //默认越南
  22. baseUrl = ApiConstants.baseUrl;
  23. }
  24. //注入 HttpProvider
  25. Get.put(
  26. HttpProvider(
  27. baseUrl,
  28. interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()], //需要加上与App关联的一些拦截处理
  29. ),
  30. permanent: true);
  31. }
  32. // 切换应用的BaseUrl的逻辑
  33. static void switchBaseUrl(String baseUrl) {
  34. //找到单例对象 HttpProvider,切换域名
  35. HttpProvider httpProvider = Get.find();
  36. httpProvider.switchBaseUrl(baseUrl);
  37. }
  38. }