Browse Source

切换国家切换网络引擎

liukai 7 months ago
parent
commit
30b3dfef74

+ 9 - 1
packages/cpt_auth/lib/modules/login/login_controller.dart

@@ -5,11 +5,14 @@ import 'package:domain/entity/response/hotel_info_entity.dart';
 import 'package:domain/repository/auth_repository.dart';
 import 'package:get/get.dart';
 import 'package:plugin_basic/constants/app_constant.dart';
+import 'package:plugin_basic/service/app_config_service.dart';
 import 'package:plugin_basic/service/user_service.dart';
 import 'package:plugin_platform/engine/sp/sp_util.dart';
 import 'package:plugin_platform/engine/toast/toast_engine.dart';
 import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
+import 'package:plugin_platform/http/http_provider.dart';
 import 'package:router/componentRouter/component_router_service.dart';
+import 'package:shared/utils/event_bus.dart';
 import 'package:shared/utils/log_utils.dart';
 import 'package:shared/utils/util.dart';
 
@@ -78,7 +81,12 @@ class LoginController extends GetxController with DioCancelableMixin {
     UserService.to.setToken(token);
 
     //保存是否是管理员登录
-    SPUtil.putInt(AppConstant.storageIsAdmin, state.loginOptionPosition);
+    if (ConfigService.to.curSelectCountry.value == 1) {
+      //如果是新加坡用户,强制是管理员登录
+      SPUtil.putInt(AppConstant.storageIsAdmin, 1);
+    }else{
+      SPUtil.putInt(AppConstant.storageIsAdmin, state.loginOptionPosition);
+    }
 
     //去首页
     MainPage.startWithPopAll();

+ 22 - 14
packages/cpt_auth/lib/modules/login/login_page.dart

@@ -8,6 +8,7 @@ import 'package:get/get.dart';
 import 'package:plugin_basic/base/base_stateful_page.dart';
 import 'package:plugin_basic/base/base_state.dart';
 import 'package:plugin_basic/base/mixin_state_lifecycle.dart';
+import 'package:plugin_basic/service/app_config_service.dart';
 import 'package:plugin_basic/utils/ext_get_nav.dart';
 import 'package:router/path/router_path.dart';
 import 'package:shared/utils/device_utils.dart';
@@ -97,13 +98,15 @@ class _MainPageState extends BaseState<LoginPage, LoginController> with StateLif
                     mainAxisSize: MainAxisSize.min,
                     crossAxisAlignment: CrossAxisAlignment.center,
                     children: [
-                      MyTextView(
-                        "Vietnam".tr,
-                        textColor: ColorConstants.white,
-                        isFontMedium: true,
-                        marginRight: 5,
-                        fontSize: 15,
-                      ),
+                      Obx(() {
+                        return MyTextView(
+                          ConfigService.to.curSelectCountry == 0 ? "Vietnam".tr : "Singapore".tr,
+                          textColor: ColorConstants.white,
+                          isFontMedium: true,
+                          marginRight: 5,
+                          fontSize: 15,
+                        );
+                      }),
                       MyAssetImage(
                         Assets.baseServiceTriangleDropDown,
                         width: 8,
@@ -207,12 +210,17 @@ class _MainPageState extends BaseState<LoginPage, LoginController> with StateLif
                                 ),
 
                                 //选择签到功能还是全功能
-                                CustomRadioCheck(
-                                  options: state.loginOption,
-                                  onOptionSelected: (index, text) {
-                                    state.loginOptionPosition = index;
-                                  },
-                                ).marginSymmetric(vertical: 20),
+                                Obx(() {
+                                  return Visibility(
+                                    visible: ConfigService.to.curSelectCountry == 0,
+                                    child: CustomRadioCheck(
+                                      options: state.loginOption,
+                                      onOptionSelected: (index, text) {
+                                        state.loginOptionPosition = index;
+                                      },
+                                    ).marginOnly(top: 20),
+                                  );
+                                }),
 
                                 //登录按钮
                                 MyButton(
@@ -228,7 +236,7 @@ class _MainPageState extends BaseState<LoginPage, LoginController> with StateLif
                                   radius: 22.5,
                                   backgroundColor: hexToColor("#FFBB1B"),
                                   fontWeight: FontWeight.w500,
-                                ),
+                                ).marginOnly(top: 20),
 
                                 //注册入口
                                 Visibility(

+ 5 - 3
packages/cpt_auth/lib/modules/main/main_state.dart

@@ -10,11 +10,13 @@ class MainState {
   final List<HomeModule> modules = [
     HomeModule(key: 'labReq', moduleName: 'Labour Request'.tr, moduleIconPath: Assets.mainHomeLabourRequest, iconWidth: 50, iconHeight: 40.3),
     HomeModule(key: 'jobList', moduleName: 'Job List'.tr, moduleIconPath: Assets.mainHomeJobList, iconWidth: 45, iconHeight: 45),
-    HomeModule(key: 'sign', moduleName: 'Sign in Sign out'.tr, moduleIconPath: Assets.mainHomeSignInOut, iconWidth: 44.5, iconHeight: 44.5),
-    HomeModule(key: 'devices', moduleName: 'Devices'.tr, moduleIconPath: Assets.mainHomeDevices, iconWidth: 45.5, iconHeight: 45.5),
+    HomeModule(key: 'sign', moduleName: 'Sign in Sign out'.tr, moduleIconPath: Assets.mainHomeSignInOut, iconWidth: 44.5, iconHeight: 44.5),  //越南的手动签到
+    HomeModule(key: 'reviseList', moduleName: 'Revise List'.tr, moduleIconPath: Assets.mainHomeSignInOut, iconWidth: 44.5, iconHeight: 44.5),  //新加坡的修改列表
+    HomeModule(key: 'device', moduleName: 'Devices'.tr, moduleIconPath: Assets.mainHomeDevices, iconWidth: 45.5, iconHeight: 45.5),
     HomeModule(key: 'reqReview', moduleName: 'Labour Request Review'.tr, moduleIconPath: Assets.mainHomeLabourRequestReview, iconWidth: 50.5, iconHeight: 43),
     HomeModule(key: 'attReview', moduleName: 'Attendance Review'.tr, moduleIconPath: Assets.mainHomeAttendanceReview, iconWidth: 47.5, iconHeight: 46),
-    HomeModule(key: 'template', moduleName: 'Default Job Title'.tr, moduleIconPath: Assets.mainHomeJobTemplate, iconWidth: 48.5, iconHeight: 46.5),
+    HomeModule(key: 'template', moduleName: 'Default Job Title'.tr, moduleIconPath: Assets.mainHomeJobTemplate, iconWidth: 48.5, iconHeight: 46.5), //越南的模版
+    HomeModule(key: 'jobTitle', moduleName: 'Default Job Title'.tr, moduleIconPath: Assets.mainHomeJobTemplate, iconWidth: 48.5, iconHeight: 46.5), //新加坡的模板
     HomeModule(key: 'report', moduleName: 'Report'.tr, moduleIconPath: Assets.mainHomeReport, iconWidth: 50.5, iconHeight: 45.5),
   ];
 }

+ 23 - 2
packages/cpt_auth/lib/modules/select_country/select_country_controller.dart

@@ -1,7 +1,28 @@
+import 'package:domain/constants/api_constants.dart';
 import 'package:get/get.dart';
+import 'package:plugin_basic/constants/app_constant.dart';
+import 'package:plugin_basic/service/app_config_service.dart';
+import 'package:plugin_basic/service/http_provider_injection.dart';
+import 'package:plugin_platform/engine/sp/sp_util.dart';
 
-import 'select_country_state.dart';
 
 class SelectCountryController extends GetxController {
-  final SelectCountryState state = SelectCountryState();
+
+  //设置下一步
+  void setupNext() {
+    int country = ConfigService.to.curSelectCountry.value;
+    SPUtil.putInt(AppConstant.storageSelectedCountry, country);
+
+    String baseUrl;
+    if (country == 1){
+      //新加坡
+      baseUrl = ApiConstants.sgBaseUrl;
+    }else {
+      baseUrl = ApiConstants.baseUrl;
+    }
+
+    HttpProviderInjection.switchBaseUrl(baseUrl);
+
+    Get.back();
+  }
 }

+ 8 - 9
packages/cpt_auth/lib/modules/select_country/select_country_page.dart

@@ -1,4 +1,3 @@
-import 'package:cpt_auth/modules/select_country/select_country_state.dart';
 import 'package:cs_resources/constants/color_constants.dart';
 import 'package:cs_resources/generated/assets.dart';
 import 'package:flutter/cupertino.dart';
@@ -6,6 +5,7 @@ import 'package:flutter/material.dart';
 import 'package:flutter/widgets.dart';
 import 'package:get/get.dart';
 import 'package:plugin_basic/base/base_stateless_page.dart';
+import 'package:plugin_basic/service/app_config_service.dart';
 import 'package:plugin_basic/utils/ext_get_nav.dart';
 import 'package:router/path/router_path.dart';
 import 'package:shared/utils/screen_util.dart';
@@ -24,11 +24,8 @@ class SelectCountryPage extends BaseStatelessPage<SelectCountryController> {
     return Get.start(RouterPath.AUTH_SELECT_COUNTRY);
   }
 
-  late SelectCountryState state;
-
   @override
   void initState() {
-    state = controller.state;
   }
 
   @override
@@ -116,14 +113,14 @@ class SelectCountryPage extends BaseStatelessPage<SelectCountryController> {
                             ).expanded(),
                             Obx(() {
                               return Visibility(
-                                visible: state.curSelectCountry.value == 1,
+                                visible: ConfigService.to.curSelectCountry.value == 1,
                                 child: MyAssetImage(Assets.cptAuthCheckedIcon, width: 22, height: 22),
                               );
                             }),
                           ],
                         ),
                       ).onTap(() {
-                        state.curSelectCountry.value = 1;
+                        ConfigService.to.curSelectCountry.value = 1;
                       }),
 
                       //越南的选项
@@ -147,14 +144,14 @@ class SelectCountryPage extends BaseStatelessPage<SelectCountryController> {
                             ).expanded(),
                             Obx(() {
                               return Visibility(
-                                visible: state.curSelectCountry.value == 0,
+                                visible: ConfigService.to.curSelectCountry.value == 0,
                                 child: MyAssetImage(Assets.cptAuthCheckedIcon, width: 22, height: 22),
                               );
                             }),
                           ],
                         ),
                       ).onTap(() {
-                        state.curSelectCountry.value = 0;
+                        ConfigService.to.curSelectCountry.value = 0;
                       }),
 
                       //Next按钮
@@ -163,7 +160,9 @@ class SelectCountryPage extends BaseStatelessPage<SelectCountryController> {
                           MyButton(
                             type: ClickType.throttle,
                             milliseconds: 500,
-                            onPressed: () {},
+                            onPressed: () {
+                              controller.setupNext();
+                            },
                             text: "Next".tr,
                             textColor: ColorConstants.white,
                             fontSize: 18,

+ 0 - 7
packages/cpt_auth/lib/modules/select_country/select_country_state.dart

@@ -1,7 +0,0 @@
-import 'package:plugin_basic/basic_export.dart';
-
-class SelectCountryState {
-
-  RxInt curSelectCountry = 0.obs;
-
-}

+ 3 - 16
packages/cs_initializer/lib/global_services_injection.dart

@@ -5,6 +5,7 @@ import 'package:domain/repository/labour_repository.dart';
 import 'package:plugin_basic/basic_export.dart';
 import 'package:plugin_basic/dio_interceptors/interceptor_auth_dio.dart';
 import 'package:plugin_basic/dio_interceptors/interceptor_status_code_dio.dart';
+import 'package:plugin_basic/service/http_provider_injection.dart';
 import 'package:plugin_basic/service/user_service.dart';
 import 'package:plugin_platform/http/http_provider.dart';
 import 'package:shared/utils/log_utils.dart';
@@ -22,13 +23,8 @@ class GlobalServicesInjection {
   static void init({void Function()? additionalDependencies}) {
     Log.d('----ConfigServicesInjection------start-----');
 
-    //全局单例的 HttpProvider 用于发起网络请求
-    Get.put(
-        HttpProvider(
-          ApiConstants.baseUrl,
-          interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()], //需要加上与App关联的一些拦截处理
-        ),
-        permanent: true);
+    //全局单例的 HttpProvider 用于发起网络请求 (根据不同的选择国家注入不同的域名)
+    HttpProviderInjection.putHttpProviderByCountry();
 
     //全局单例的用户数据仓库
     Get.put(AuthRepository(httpProvider: Get.find()));
@@ -48,13 +44,4 @@ class GlobalServicesInjection {
     Log.d('----ConfigServicesInjection------end-----');
   }
 
-  // 切换应用的BaseUrl的逻辑
-  static void switchBaseUrl(String baseUrl) {
-    Get.replace(
-      HttpProvider(
-        baseUrl, // 使用新的 baseUrl
-        interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()],
-      ),
-    );
-  }
 }

+ 0 - 1
packages/cs_plugin_basic/lib/constants/app_constant.dart

@@ -25,5 +25,4 @@ class AppConstant {
   static const eventLabourRequestRefresh = 'event_labour_request_refresh'; //用工请求的列表刷新
   static const eventAppliedListRefresh = 'event_applied_list_refresh'; //已申请的列表刷新
 
-
 }

+ 10 - 0
packages/cs_plugin_basic/lib/service/app_config_service.dart

@@ -6,10 +6,13 @@ import 'package:flutter/material.dart';
 import 'package:get/get.dart';
 import 'package:package_info_plus/package_info_plus.dart';
 import 'package:plugin_basic/service/config_services_injection.dart';
+import 'package:plugin_platform/engine/sp/sp_util.dart';
 import 'package:shared/utils/device_utils.dart';
 import 'package:shared/utils/log_utils.dart';
 import 'package:shared/utils/screen_util.dart';
 
+import '../constants/app_constant.dart';
+
 
 /*
  * 获取并保存当前应用的信息。
@@ -18,6 +21,9 @@ import 'package:shared/utils/screen_util.dart';
 class ConfigService extends GetxService {
   static ConfigService get to => Get.find();
 
+  //选择的国家
+  RxInt curSelectCountry = 0.obs;
+
   // 设备信息
   /// android 设备信息
   AndroidDeviceInfo? androidDeviceInfo;
@@ -91,6 +97,10 @@ class ConfigService extends GetxService {
   void onReady() {
     super.onReady();
 
+    //赋值选中的国家
+    int country = SPUtil.getInt(AppConstant.storageSelectedCountry, defValue: 0) ?? 0;
+    curSelectCountry.value = country;
+
     //打印应用与设备的信息
     getDeviceInfo();
     getAppInfos();

+ 38 - 0
packages/cs_plugin_basic/lib/service/http_provider_injection.dart

@@ -0,0 +1,38 @@
+import 'package:domain/constants/api_constants.dart';
+import 'package:plugin_basic/constants/app_constant.dart';
+import 'package:plugin_platform/engine/sp/sp_util.dart';
+import 'package:plugin_platform/http/http_provider.dart';
+
+import '../basic_export.dart';
+import '../dio_interceptors/interceptor_auth_dio.dart';
+import '../dio_interceptors/interceptor_status_code_dio.dart';
+
+class HttpProviderInjection {
+  // 设置默认的
+  static void putHttpProviderByCountry() {
+    //根据选中的国家赋值Api
+    String baseUrl;
+    int country = SPUtil.getInt(AppConstant.storageSelectedCountry, defValue: 0) ?? 0;
+    if (country == 1) {
+      baseUrl = ApiConstants.sgBaseUrl;
+    } else {
+      baseUrl = ApiConstants.baseUrl;
+    }
+
+    //注入 HttpProvider
+    Get.put(
+        HttpProvider(
+          baseUrl,
+          interceptors: [StatusCodeDioInterceptors(), AuthDioInterceptors()], //需要加上与App关联的一些拦截处理
+        ),
+        permanent: true);
+  }
+
+  // 切换应用的BaseUrl的逻辑
+  static void switchBaseUrl(String baseUrl) {
+    //找到单例对象 HttpProvider,切换域名
+    HttpProvider httpProvider = Get.find();
+    httpProvider.switchBaseUrl(baseUrl);
+
+  }
+}

+ 13 - 2
packages/cs_plugin_platform/lib/engine/network/network_engine.dart

@@ -16,9 +16,15 @@ import '../../http/http_provider.dart';
  */
 class NetworkEngine {
   late Dio dio;
+  late String baseUrl;
+  List<Interceptor>? interceptors;
 
-  NetworkEngine(String baseUrl, List<Interceptor>? interceptors) {
+  NetworkEngine(this.baseUrl, this.interceptors) {
     /// 网络配置
+    _initializeDio();
+  }
+
+  void _initializeDio() {
     final options = BaseOptions(
         baseUrl: baseUrl,
         connectTimeout: const Duration(seconds: 30),
@@ -49,11 +55,16 @@ class NetworkEngine {
     }
 
     if (interceptors != null) {
-      interceptors.forEach((interceptor) {
+      interceptors!.forEach((interceptor) {
         dio.interceptors.add(interceptor);
       });
     }
+  }
 
+  //切换BaseUrl
+  void switchBaseUrlAndDio(String url) {
+    this.baseUrl = url;
+    _initializeDio();
   }
 
   /// 网络请求 Post 请求

+ 11 - 3
packages/cs_plugin_platform/lib/http/http_provider.dart

@@ -27,6 +27,11 @@ class HttpProvider {
     networkEngine = NetworkEngine(baseUrl, interceptors);
   }
 
+  //切换BaseUrl
+  void switchBaseUrl(String url){
+    networkEngine.switchBaseUrlAndDio(url);
+  }
+
   /// 封装网络请求入口
   Future<HttpResult> requestNetResult(
     String url, {
@@ -183,9 +188,7 @@ class HttpProvider {
         // 如果其他的Http网络请求的Code的处理
         Log.d("网络请求错误,data:${e.response?.data}");
         return HttpResult(isSuccess: false, errorMsg: "错误码:${e.response?.statusCode} 错误信息:${e.response?.statusMessage}");
-      } else if (e.type == DioExceptionType.connectionTimeout ||
-          e.type == DioExceptionType.sendTimeout ||
-          e.type == DioExceptionType.receiveTimeout) {
+      } else if (e.type == DioExceptionType.connectionTimeout || e.type == DioExceptionType.sendTimeout || e.type == DioExceptionType.receiveTimeout) {
         return HttpResult(isSuccess: false, errorMsg: "网络连接超时,请稍后再试");
       } else if (e.type == DioExceptionType.cancel) {
         return HttpResult(isSuccess: false, errorMsg: "网络请求已取消");
@@ -264,4 +267,9 @@ class HttpProvider {
       );
     }
   }
+
+  @override
+  String toString() {
+    return "networkEngine 的 baseUrl:${networkEngine.baseUrl}";
+  }
 }

+ 3 - 0
packages/cs_resources/lib/local/language/en_US.dart

@@ -141,6 +141,9 @@ const Map<String, String> en_US = {
   'Staff Name/ID/Phone': 'Staff Name/ID/Phone',
   'Select Country': 'Select Country',
   'Browse jobs available in your selected country.': 'Browse jobs available in your selected country.',
+  'Next': 'Next',
+  'Singapore': 'Singapore',
+  'Revise List': 'Revise List',
 
   //插件的国际化
   'Pull to refresh': 'Pull to refresh',

+ 3 - 0
packages/cs_resources/lib/local/language/vi_VN.dart

@@ -141,6 +141,9 @@ const Map<String, String> vi_VN = {
   'Staff Name/ID/Phone': 'Tên nhân viên/ID/Số điện thoại',
   'Select Country': 'Chọn quốc gia',
   'Browse jobs available in your selected country.': 'Duyệt các công việc có sẵn ở quốc gia đã chọn',
+  'Next': 'Tiếp theo',
+  'Singapore': 'Singapore',
+  'Revise List': 'Danh sách sửa đổi',
 
   //插件的国际化
   "Pull to refresh": "Kéo để làm mới",

+ 3 - 0
packages/cs_resources/lib/local/language/zh_CN.dart

@@ -141,6 +141,9 @@ const Map<String, String> zh_CN = {
   'Staff Name/ID/Phone': '员工姓名/ID/电话号码',
   'Select Country': '选择国家',
   'Browse jobs available in your selected country.': '浏览所选国家/地区的可用工作',
+  'Next': '下一步',
+  'Singapore': '新加坡',
+  'Revise List': '修改列表',
 
   //插件的国际化
   'Pull to refresh': '下拉刷新',