diff --git a/app/src/main/java/com/sw/inbound/GlobalData.kt b/app/src/main/java/com/sw/inbound/GlobalData.kt index d606692..305109f 100644 --- a/app/src/main/java/com/sw/inbound/GlobalData.kt +++ b/app/src/main/java/com/sw/inbound/GlobalData.kt @@ -56,8 +56,10 @@ object GlobalData { * 具体业务 BaseUrl */ // const val TEST_BASE_URL = "http://192.168.1.201:14801" - const val TEST_BASE_URL = "http://192.168.10.118:14801" - const val UAT_BASE_URL = "https://dev.yixiong-tech.com:8083" +// const val LOCAL_BASE_URL = "http://192.168.10.118:14801" + const val LOCAL_BASE_URL = "http://192.168.10.101:24801"; +// const val TEST_BASE_URL = "https://dev.yixiong-tech.com:8083" + const val TEST_BASE_URL = "https://dev.yixiong-tech.com:8081" const val PROD_BASE_URL = "https://api.dm.yixiong-tech.com:8443" var appBaseUrl: String = "" diff --git a/app/src/main/java/com/sw/inbound/activity/HomeActivity.kt b/app/src/main/java/com/sw/inbound/activity/HomeActivity.kt index 90ee64e..a422409 100644 --- a/app/src/main/java/com/sw/inbound/activity/HomeActivity.kt +++ b/app/src/main/java/com/sw/inbound/activity/HomeActivity.kt @@ -81,20 +81,20 @@ class HomeActivity : BaseActivity() { // startActivity() // } // -// // 时间点击:打开环境切换弹窗 -// binding.tvTime.setOnClickListener { v -> -// EnvSwitchDialog(context = this, onEnvChanged = { -// Loading.show(this) -// lifecycleScope.launch { + // 时间点击:打开环境切换弹窗 + binding.tvTime.setOnClickListener { v -> + EnvSwitchDialog(context = this, onEnvChanged = { + Loading.show(this) + lifecycleScope.launch { // ObjectBox.removeAll() // FoodModule.init(this@HomeActivity) -// v.postDelayed({ -// Loading.dismiss() -// toast("服务重置完成") -// }, 500) -// } -// }).show() -// } + v.postDelayed({ + Loading.dismiss() + toast("服务重置完成") + }, 500) + } + }).show() + } // // 采购单入库入口 // binding.ivOrderPurchase.setOnClickListener { diff --git a/app/src/main/java/com/sw/inbound/activity/InitActivity.kt b/app/src/main/java/com/sw/inbound/activity/InitActivity.kt index 4378e4e..52ebed5 100644 --- a/app/src/main/java/com/sw/inbound/activity/InitActivity.kt +++ b/app/src/main/java/com/sw/inbound/activity/InitActivity.kt @@ -1,7 +1,6 @@ package com.sw.inbound.activity import android.content.Intent -import android.net.Uri import android.os.Bundle import android.os.Handler import android.os.Looper @@ -152,7 +151,7 @@ class InitActivity : BaseActivity() { "f0bcabbb-0d58-3b14-8bed-9863b7ec61ef" ) if (list.contains(GlobalData.deviceId)) { - GlobalData.appBaseUrl = GlobalData.TEST_BASE_URL + GlobalData.appBaseUrl = GlobalData.LOCAL_BASE_URL } } val intent = Intent(this, HomeActivity::class.java) diff --git a/app/src/main/java/com/sw/inbound/di/InboundNetworkModule.kt b/app/src/main/java/com/sw/inbound/di/InboundNetworkModule.kt index f388199..df330c4 100644 --- a/app/src/main/java/com/sw/inbound/di/InboundNetworkModule.kt +++ b/app/src/main/java/com/sw/inbound/di/InboundNetworkModule.kt @@ -1,14 +1,19 @@ package com.sw.inbound.di import com.google.gson.GsonBuilder +import com.sw.inbound.GlobalData import com.sw.inbound.MyApp import com.sw.inbound.network.api.InboundApiService import com.sw.inbound.network.interceptor.InboundInterceptor +import com.sw.inbound.network.interceptor.RequestInterceptor import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull +import okhttp3.Interceptor import okhttp3.OkHttpClient +import okhttp3.Response import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory @@ -21,9 +26,23 @@ import javax.inject.Singleton @InstallIn(SingletonComponent::class) object InboundNetworkModule { - private const val INBOUND_BASE_URL = "http://192.168.10.101:24801/" +// private const val INBOUND_BASE_URL = "http://192.168.10.101:24801/" private const val TIME_OUT = 30L + /** 动态域名拦截器:将 Retrofit 请求的 baseUrl 实时替换为 [GlobalData.appBaseUrl] */ + class DynamicBaseUrlInterceptor : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + val newUrl = GlobalData.appBaseUrl.toHttpUrlOrNull() ?: return chain.proceed(request) + val url = request.url.newBuilder() + .scheme(newUrl.scheme) + .host(newUrl.host) + .port(newUrl.port) + .build() + return chain.proceed(request.newBuilder().url(url).build()) + } + } + @Provides @Singleton @Named("inbound") @@ -38,7 +57,9 @@ object InboundNetworkModule { level = if (MyApp.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE }) - .addInterceptor(InboundInterceptor()) + .addInterceptor(DynamicBaseUrlInterceptor()) +// .addInterceptor(InboundInterceptor()) + .addInterceptor(RequestInterceptor()) .build() } @@ -48,7 +69,7 @@ object InboundNetworkModule { fun provideInboundRetrofit(@Named("inbound") okHttpClient: OkHttpClient): Retrofit { val gson = GsonBuilder().setLenient().create() return Retrofit.Builder() - .baseUrl(INBOUND_BASE_URL) + .baseUrl(GlobalData.appBaseUrl) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .build() diff --git a/app/src/main/java/com/sw/inbound/dialog/EnvSwitchDialog.kt b/app/src/main/java/com/sw/inbound/dialog/EnvSwitchDialog.kt index 60e39ce..0ee0b98 100644 --- a/app/src/main/java/com/sw/inbound/dialog/EnvSwitchDialog.kt +++ b/app/src/main/java/com/sw/inbound/dialog/EnvSwitchDialog.kt @@ -40,8 +40,8 @@ class EnvSwitchDialog( override fun initView() { // 根据当前 appBaseUrl 预选对应 RadioButton,不匹配则不选 when (GlobalData.appBaseUrl) { - GlobalData.TEST_BASE_URL -> binding.rbTest.isChecked = true - GlobalData.UAT_BASE_URL -> binding.rbUat.isChecked = true + GlobalData.LOCAL_BASE_URL -> binding.rbTest.isChecked = true + GlobalData.TEST_BASE_URL -> binding.rbUat.isChecked = true GlobalData.PROD_BASE_URL -> binding.rbProd.isChecked = true } @@ -56,8 +56,8 @@ class EnvSwitchDialog( return@setOnClickListener } val newUrl = when (binding.rgEnv.checkedRadioButtonId) { - binding.rbTest.id -> GlobalData.TEST_BASE_URL - binding.rbUat.id -> GlobalData.UAT_BASE_URL + binding.rbTest.id -> GlobalData.LOCAL_BASE_URL + binding.rbUat.id -> GlobalData.TEST_BASE_URL binding.rbProd.id -> GlobalData.PROD_BASE_URL else -> return@setOnClickListener } diff --git a/app/src/main/java/com/sw/inbound/network/api/InboundApiService.kt b/app/src/main/java/com/sw/inbound/network/api/InboundApiService.kt index e87a669..0bdbc93 100644 --- a/app/src/main/java/com/sw/inbound/network/api/InboundApiService.kt +++ b/app/src/main/java/com/sw/inbound/network/api/InboundApiService.kt @@ -1,5 +1,6 @@ package com.sw.inbound.network.api +import com.sw.inbound.GlobalData import com.sw.inbound.model.response.IngredientByTrace import com.sw.inbound.model.response.InboundApiResponse import com.sw.inbound.model.response.SupplierOption @@ -8,6 +9,7 @@ import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST import retrofit2.http.Query +import retrofit2.http.Url /** * 入库秤设备端接口 @@ -17,34 +19,39 @@ import retrofit2.http.Query interface InboundApiService { /** 按溯源码查食材,用于毛菜/净菜入库时联动选择食材 */ - @GET("nutrition/neglect/inbound/ingredient-by-trace") + @GET suspend fun getIngredientByTrace( + @Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/inbound/ingredient-by-trace", @Query("traceCode") traceCode: String ): InboundApiResponse> /** 仓库区域下拉列表 */ - @GET("nutrition/neglect/inbound/zone-options") + @GET suspend fun getZoneOptions( + @Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/inbound/zone-options", @Query("warehouseId") warehouseId: Long? = null, @Query("keyword") keyword: String? = null ): InboundApiResponse> /** 供应商下拉列表,type: 1-内部,2-外部,不传返回全部 */ - @GET("nutrition/neglect/inbound/supplier-options") + @GET suspend fun getSupplierOptions( + @Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/inbound/supplier-options", @Query("type") type: Int? = null, @Query("keyword") keyword: String? = null ): InboundApiResponse> /** 毛菜入库提交,入参为 Map 以便动态组装字段 */ - @POST("nutrition/neglect/inbound/raw-inbound") + @POST suspend fun submitRawInbound( + @Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/inbound/raw-inbound", @Body param: Map ): InboundApiResponse /** 净菜入库提交,入参为 Map 以便动态组装字段 */ - @POST("nutrition/neglect/inbound/clean-inbound") + @POST suspend fun submitCleanInbound( + @Url url: String = "${GlobalData.appBaseUrl}/nutrition/neglect/inbound/clean-inbound", @Body param: Map ): InboundApiResponse } diff --git a/app/src/main/java/com/sw/inbound/repository/InboundRepository.kt b/app/src/main/java/com/sw/inbound/repository/InboundRepository.kt index 06d368c..5838b57 100644 --- a/app/src/main/java/com/sw/inbound/repository/InboundRepository.kt +++ b/app/src/main/java/com/sw/inbound/repository/InboundRepository.kt @@ -24,7 +24,7 @@ class InboundRepository @Inject constructor( /** 按溯源码查食材 */ suspend fun getIngredientByTrace(traceCode: String): InboundApiResponse> { - return safeCall { apiService.getIngredientByTrace(traceCode) } + return safeCall { apiService.getIngredientByTrace(traceCode = traceCode) } } /** 仓库区域下拉列表 */ @@ -32,7 +32,7 @@ class InboundRepository @Inject constructor( warehouseId: Long? = null, keyword: String? = null ): InboundApiResponse> { - return safeCall { apiService.getZoneOptions(warehouseId, keyword) } + return safeCall { apiService.getZoneOptions(warehouseId = warehouseId, keyword = keyword) } } /** 供应商下拉列表 */ @@ -40,17 +40,17 @@ class InboundRepository @Inject constructor( type: Int? = null, keyword: String? = null ): InboundApiResponse> { - return safeCall { apiService.getSupplierOptions(type, keyword) } + return safeCall { apiService.getSupplierOptions(type = type, keyword = keyword) } } /** 毛菜入库提交 */ suspend fun submitRawInbound(param: Map): InboundApiResponse { - return safeCall { apiService.submitRawInbound(param) } + return safeCall { apiService.submitRawInbound(param = param) } } /** 净菜入库提交 */ suspend fun submitCleanInbound(param: Map): InboundApiResponse { - return safeCall { apiService.submitCleanInbound(param) } + return safeCall { apiService.submitCleanInbound(param = param) } } private suspend fun safeCall(call: suspend () -> InboundApiResponse): InboundApiResponse { diff --git a/app/src/main/res/layout/dialog_env_switch.xml b/app/src/main/res/layout/dialog_env_switch.xml index 7f86372..74312ed 100644 --- a/app/src/main/res/layout/dialog_env_switch.xml +++ b/app/src/main/res/layout/dialog_env_switch.xml @@ -79,7 +79,7 @@ android:gravity="center" android:orientation="horizontal"> -